threads / discuss / 6925

git-push hook to update remote working copy safely

Subject: git-push hook to update remote working copy safely

## tl;dr

7 messages between Feb 23, 2007 and Feb 23, 2007.

replies: 6people: 3as markdown or json

Sam Watkins· Feb 23, 2007, 08:51 UTC · lore
peace,

I'm looking for a command that will update the remote working copy after a "git push", without damaging any changes that may have been made to the working copy. I haven't used git barely at all, hoping this is an okay place to ask about this. I can put the command into the push hook myself, I just don't know what command does that!

This is one of the last "pieces" needed for a hopefully useful application I'm working on with a friend, which uses git for storage and file transfer.

Sam
Junio C Hamano· Feb 23, 2007, 11:00 UTC · re: Sam Watkins · lore

Re: git-push hook to update remote working copy safely

"Sam Watkins" <swatkins@fastmail.fm> writes:
> I'm looking for a command that will update the remote working copy after
> a "git push", without damaging any changes that may have been made to
> the working copy.

Define "without damaging". If there are changes to paths that are modified by the pushed commit since the current HEAD, what should your "update the remote working copy" procedure would do?

 * Abort the push and refuse to update branch head?
 * Run file-level merge and leave the conflicted results on the
   working tree, in the $GIT_DIR/index and leave the pushed
   commit object name in $GIT_DIR/MERGE_HEAD?

I think the latter is just crazy, as you would then have to think about cases where you already have $GIT_DIR/MERGE_HEAD when you attempt to push.

Why in the first place does checking out need to perform a possibly conflicting update? Unless it is _also_ modified by something other than push (i.e. some human user modifies it in the editor, iow active development happening in the repository), you can assume that when a push tries to update the working tree, the working tree is clean (i.e. the index matches HEAD and the working tree files match the index). And in such a case, you can set receive.denyNonFastForwards, and push will not even call the update hook unless it is fast-forward. And checking out a fast-forward in a clean working tree should always succeed without conflicts. So I am puzzled what you are really trying to achieve here.

The only thing you need to protect against is simultaneous push, and I think that can be solved by holding a lock file while your update hook runs a checkout.

So it might be just the matter of something like this totally untested script:

	#!/bin/sh
        # Assumes that the repository has its own working tree and
	# $GIT_DIR is "/path/to/repository/.git".  update hook
        # is always called with `pwd` the same as $GIT_DIR.
        GIT_DIR=`pwd`
	cd .. ;# to the top of working tree
        BRANCH="$1"
        OLDREV="$2"
        NEWREV="$3"
	# Do not bother with non branch push.
        case "$BRANCH" in refs/heads/*) ;; *) exit 0 ;; esac
	# Make sure it is a fast-forward, unless totally new.
	if test "0000000000000000000000000000000000000000" = "$OLDREV"
	then
		MB=$(git merge-base "$OLDREV" "$NEWREV") &&
	        test "$OLDREV" = "$NEWREV" || exit 1
	fi
	# Pushing into a non-checked-out branch -- no need to
        # do anything.
        HEAD_BRANCH=`git symbolic-ref HEAD`
        test "z$HEAD_BRANCH" = "z$BRANCH" || exit 0
	# Could we have a lock please?
	lock="$GIT_DIR/push-update.lock"
        lockfile "$lock"
        trap 'rm -f "$lock"' 0
        # Make sure the index, working tree and HEAD all match.
	HEAD=`git rev-parse --verify HEAD` &&
        test "z$HEAD" = "z$OLDREV" &&
        git update-index --refresh &&
        test -z "`git diff-files --name-only`" &&
        test -z "`git diff-index --cached --name-only $HEAD`" ||
	exit 1
	# Update the working tree -- we do not do git-checkout
        # because updating the ref in 'update' hook is a big
        # No-no.  It would screw up the lockless update in
        # receive-pack that happens after update hook returns.
        git read-tree -m -u "$OLDREV" "$NEWREV"
Sam Watkins· Feb 23, 2007, 16:35 UTC · re: Junio C Hamano · lore

Re: git-push hook to update remote working copy safely

On Fri, 23 Feb 2007 03:00:09 -0800, "Junio C Hamano" <junkio@cox.net> said:

Show 10 quoted lines
> "Sam Watkins" <swatkins@fastmail.fm> writes:
> 
> > I'm looking for a command that will update the remote working copy after
> > a "git push", without damaging any changes that may have been made to
> > the working copy.
> 
> Define "without damaging".  If there are changes to paths that
> are modified by the pushed commit since the current HEAD, what
> should your "update the remote working copy" procedure would
> do?
hi Junio,

it should merge the changes as if I'd run git-pull on the remote box, and handle conflicts in the same way.

If possible I want to get exactly the effect of having run on box B "git-pull A", by running a command like "git-push B" on box A. I think maybe that's not possible without commiting all changes to the working copy on B first, but for our app it's fine to do that it's meant to do that automatically and frequently anyway. so I will just do that then run checkout -f from the hook.

I don't understand git very well yet but that will work I think. I thought "push" would be symmetrical to "pull" more or less, maybe it is symmetrical to "fetch" ?

thank-you for your help, I am reading some more of the git manual because I understand only some of your reply and very little about git yet!

The app "arcs" we are writing, it is peer to peer, not necessarily using a central repository, it commits pulls and pushes all changes made to working copies automatically or when asked to. currently I'm implementing push as a remote pull because of this problem, but that can't connect back through firewalls and suchlike.

Sam
Johannes Schindelin· Feb 23, 2007, 16:40 UTC · re: Sam Watkins · lore

Re: git-push hook to update remote working copy safely

Hi,
On Sat, 24 Feb 2007, Sam Watkins wrote:
> it should merge the changes as if I'd run git-pull on the remote box, 
> and handle conflicts in the same way.

But that leaves conflicts in the working directory! You _have_ to resolve them (or reset) before proceding.

> I don't understand git very well yet but that will work I think.  I 
> thought "push" would be symmetrical to "pull" more or less, maybe it is 
> symmetrical to "fetch" ?
Yes, that was the idea of the naming.
Show 5 quoted lines
> The app "arcs" we are writing, it is peer to peer, not necessarily using 
> a central repository, it commits pulls and pushes all changes made to 
> working copies automatically or when asked to.  currently I'm 
> implementing push as a remote pull because of this problem, but that 
> can't connect back through firewalls and suchlike.

Don't you have any user interaction? I.e. if the remote working directory is only ever changed by your hook, you can use what Junio sent. It is even overkill for that purpose.

Hth, Dscho

Sam Watkins· Feb 23, 2007, 17:04 UTC · re: Johannes Schindelin · lore

Re: git-push hook to update remote working copy safely

On Fri, 23 Feb 2007 17:40:02 +0100 (CET), "Johannes Schindelin" <Johannes.Schindelin@gmx.de> said:

Show 9 quoted lines
> Hi,
>
> On Sat, 24 Feb 2007, Sam Watkins wrote:
>
> > it should merge the changes as if I'd run git-pull on the remote
> > box, and handle conflicts in the same way.
>
> But that leaves conflicts in the working directory! You _have_ to
> resolve them (or reset) before proceding.
hi Johannes,

I think it's ok for our application if the push+hook leaves conflicts in the working directory, I just want to replicate the effect of running git-pull remotely without actually having to go through ssh contortions to achieve that. It might not be such a good idea to do that in the normal usage of git, but we are using it in not a normal way, and that is what I am trying to do, replicate the effect of running git-pull remotely using git-push plus (unknown attempt-to-merge command or whatever in the hook).

> Don't you have any user interaction? I.e. if the remote working
> directory is only ever changed by your hook, you can use what Junio
> sent. It is even overkill for that purpose.

no, the remote working directory can be edited just like the local working directory. there's user interaction in the form of editing on both ends. otherwise, the committing/syncing process is intended to be non-interactive.

If we were using a central non-editable repository, we could use "checkout -f" in the hook we did try that before.

thanks for your help :)
Sam
Johannes Schindelin· Feb 23, 2007, 18:13 UTC · re: Sam Watkins · lore

Re: git-push hook to update remote working copy safely

Hi,
On Sat, 24 Feb 2007, Sam Watkins wrote:
Show 15 quoted lines
> On Fri, 23 Feb 2007 17:40:02 +0100 (CET), "Johannes Schindelin"
> <Johannes.Schindelin@gmx.de> said:
> >
> > On Sat, 24 Feb 2007, Sam Watkins wrote:
> >
> > > it should merge the changes as if I'd run git-pull on the remote
> > > box, and handle conflicts in the same way.
> >
> > But that leaves conflicts in the working directory! You _have_ to
> > resolve them (or reset) before proceding.
> 
> I think it's ok for our application if the push+hook leaves conflicts in 
> the working directory, I just want to replicate the effect of running 
> git-pull remotely without actually having to go through ssh contortions 
> to achieve that.

I don't really see what you want to do there (and you don't want to tell me:-)). Nevertheless, it seems that Junio's script would help you. You cannot run a pull sanely when the working directory is dirty, and therefore the hook does not allow that.

BTW the easiest way to do what you apparently want is to push to a "remotely tracking" branch, i.e. a branch in the remotes/ hierarchy, and _then_ make an update hook which says "git merge remotes/<uploadbranch>".

Ciao, Dscho

Junio C Hamano· Feb 23, 2007, 18:32 UTC · re: Johannes Schindelin · lore

Re: git-push hook to update remote working copy safely

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> Don't you have any user interaction? I.e. if the remote working directory 
> is only ever changed by your hook, you can use what Junio sent. It is even 
> overkill for that purpose.

And it even has a bug (perhaps more, but I know of at least one).

The comparison after running merge-base should compare the merge base and the old commit object name.

← back to recent threads