Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git clone different repository in git hooks creating trouble

We have a specific requirement in which we have to push all files which arrive for being committed to a certain branch.

We are planning to achieve this via git hooks specifically commit-msg hook.

While doing so what we do is we clone branch to temporary location (/tmp/) and then in git commit-msg hook, attempt to commit arrived files to certain branch.

But what happens now is we see all files as deleted in /tmp/.

Crude commit-msg script is as under:-

#!/bin/bash
#
#!/usr/bin/env bash

#git config credential.helper store
REPOSRC="https://<USER>:<PASS>@<REPO_URL>"
LOCALREPO="<LOCAL_REPO_DIR>"

echo "Pulling code to temporarry location";

cd /tmp && git clone "${REPOSRC}" || (cd "${LOCALREPO}"; git pull;)

#here when I navigate to /tmp/<LOCALREPO> all files are listed as DELETED

git diff --cached --name-status | while read st file; do

    echo "file == $file and status == $st";

                    if [ "$st" == "A" ]; then
                        cd "${LOCALREPO}" && git add "$file" && git commit "$file" -m "$COMMIT_MSG" && git push origin "$branch"
                    else
                        cd "${LOCALREPO}" && git commit "$file" -m "$COMMIT_MSG" && git push origin "$branch"
                    fi
done

What can be the root cause for this?

EDIT:

GIT_INDEX_FILE shows path of index file from which commit was initiated and not /tmp/ path. Is there any way to change this variable? Also index file prints something like next-index-32419.lock. Regards

like image 294
Jatin Dhoot Avatar asked Mar 13 '18 06:03

Jatin Dhoot


People also ask

How do I clone a git repository to another repository?

On GitHub.com, navigate to the main page of the repository. Above the list of files, click Code. Click Open with GitHub Desktop to clone and open the repository with GitHub Desktop. Follow the prompts in GitHub Desktop to complete the clone.

Do git hooks get cloned?

Hooks are local to any given Git repository, and they are not copied over to the new repository when you run git clone . And, since hooks are local, they can be altered by anybody with access to the repository. This has an important impact when configuring hooks for a team of developers.

Should you commit git hooks?

Understanding Git Hooks They let you customize Git's internal behavior and trigger customizable actions at key points in the development life cycle. Major benefits of using Git hooks include encouraging a commit policy, automating development workflow, and implementing continuous integration.

Are git hooks pushed to repo?

It's important to note that Git hooks aren't committed to a Git repository themselves. They're local, untracked files. When you write an important hook that you want to keep around, copy it into a directory managed by Git! Git hooks are an important aspect of Git that is too often forgotten for being hidden away.


1 Answers

Whenever a hook changes folder, it is important to check the value of:

  • GIT_DIR (which should reference the right .git repo folder in your case)
  • GIT_WORK_TREE (which should not reference the expected folder)

That is why, for any Git command, you would need to replace git with:

git --work-tree=$(pwd) ...

(or /path/to/your/working/tree)

like image 81
VonC Avatar answered Sep 25 '22 16:09

VonC