Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

ghc - How to execute a command right after a fetch or pull command in git?

I cloned the GHC (Glasgow Haskell Compiler) repository. In order to build the compiler, you need several libraries, all of them are available as git repositories too. In order to ease ones live, the GHC hackers included a script sync-all that, when executed, updates all the dependent repositories.

Now to my question: How can I make git execute ./sync-all pull after I did a git pull automatically? I heard something about using hooks, but I don't really know, what I have to do.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you need to execute a script after a git pull on the client side (your machine), then a there is no reliable solution.

The post-merge client-side hook (mentioned here) won't be triggered by every pull (for instance, not in a pull rebase, not in case of fast-forward merge)

So an alias wrapping both commands remains a possible workaround.


Original answer (2011)

If you need that on the server side, where you would checkout the bare repo and pull from a working tree (on the server) after each push to a bare repo (on the server):

A post-receive or post-update hook is generally used for this kind of task, executed after each push.
The difference between the post-receive and post-update hooks is in the arguments: a post-receive will get both the old and the new values of all the refs in addition to their names.

The missing piece is: where that hook executes itself?

The blog post "Missing git hooks documentation" from (a great) SO contributor Mark Longair sheds some light on this:

the current working directory will be the git directory.

  • So, if this is a bare repository called “/src/git/test.git/”, that will be the current working directory – if this is a non-bare repository and the top level of the working tree is “/home/mark/test/” then the current working directory will be “/home/mark/test/.git/

Chris Johnsen details in his SO answer: "If –-git-dir or GIT_DIR are specified but none of -–work-tree, GIT_WORK_TREE and core.worktree is specified, the current working directory is regarded as the top directory of your working tree."

In other words, your working tree will also be the current directory (the .git directory), which almost certainly isn’t what you want.

Make sure your hook/post-receive script, once created and made executable, sets the GIT_WORK_TREE at the right path, in order for your ./sync-all pull to be executed in the right directory (i.e. not "xxx.git" one).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...