Within an "update" git hook, can I cd to a clone, `git fetch`, then `git checkout` the new commit?

StackOverflow https://stackoverflow.com/questions/23071892

  •  03-07-2023
  •  | 
  •  

Question

In an "update" git hook, I'm trying to cd to a clone of the repo, run git fetch, and git checkout the new commit. But I don't think that commit exists yet. Is there any way to accomplish this? I tried doing git archive [sha] but it takes too long, considering how often this hook will run in our company.

Should I checkout the base reference of the update, then apply a generated patch or something?

Était-ce utile?

La solution

I ended up with something like this:

git diff [OLD-COMMIT] [NEW-COMMIT] > /tmp/update.patch
cd /path/to/the/existing/clone
git fetch origin
git reset --hard [OLD-COMMIT]
git apply /tmp/update.patch
# Run tests

Works great.

Autres conseils

The only thing I can think of for your use case is to have 2 git repos. A workflow would go something like this.

git commit
git push ServerA
# post-receive checks out commit and runs unit tests
# ERROR: message returned
# fix your code
git commit
git push ServerA
# post-receive checks out commit and runs unit tests
# SUCCESS: pushes to ServerB

Since you want to reference a commit (which requires post-receive) and be able to reject a commit (which requires pre-receive), don't know if there is any better way. I will say that normal workflow is to run some tests locally, and then push to the repo to run complete tests. If it fails, push a fixed commit, and then when you merge everything into your develop/master branch you can rebase all the extra commits.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top