# A simple script to do the reverse of git-push

4 messages from 2005-08-08 to 2005-08-11. Participants: Johannes Schindelin, Junio C Hamano, Petr Baudis.
Thread: https://gitlist.dev/t/1456

## Johannes Schindelin, 2005-08-08 21:25

Subject: A simple script to do the reverse of git-push
Message-ID: <Pine.LNX.4.63.0508082318400.2911@wgmdd8.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0508082318400.2911%40wgmdd8.biozentrum.uni-wuerzburg.de

```
Hi list,

I mentioned in another mail that I needed the opposite of git-push, namely 
getting all heads, and if they are strict parents of the local refs, just 
update them.

Well, Junio pointed out that it's easy using the available tools, and he 
was right. The result is attached (and tested...).

This script makes it easy to work with a central repository, multiple 
heads and users, in a CVS style: to start your day, you call the attached 
script, and after you have committed something, you can do a push on the 
repository. Sometimes, the push fails, then you have to pull that branch 
in order to merge it before trying a push again.

BTW, if you are lazy, like me, you just pull from Junio once in a while 
and do a "make test". Turns out there is a missing dependency though:

peek-remote.o: cache.h

which in my case lead to a git-peek-remote program which was unable to 
peek any ref.

Ciao,
Dscho


#!/bin/sh

. git-sh-setup-script || die "Not a git archive"

. git-parse-remote "$@"
repo="$_remote_repo"

git-ls-remote-script --heads "$repo" | while read sha1 refname; do
	shortname=$(basename $refname)
	if [ -e $GIT_DIR/$refname ]; then
		if [ $sha1 = $(cat $GIT_DIR/$refname) ]; then
			echo $shortname up to date. 1>&2
			continue
		fi
	fi

	git-cat-file commit $sha1 >/dev/null 2>&1 ||
	git-fetch-script $repo $shortname

	if [ ! -e $GIT_DIR/$refname ]; then
		echo Got new head $shortname 1>&2
		echo $sha1 > $GIT_DIR/$refname
	else
		origsha1=$(cat $GIT_DIR/$refname)
		if [ -z "$(git-rev-list $origsha1 ^$sha1)" ]; then
			echo Updated head $shortname 1>&2
			echo $sha1 > $GIT_DIR/$refname
		else
			saveref=$GIT_DIR/FETCH_HEAD_$shortname
			echo Error: $shortname not a child of remote head 1>&2
			echo "	Saved remote head to $saveref" 1>&2
			echo $sha1 > $saveref
		fi
	fi
done


```

## Junio C Hamano, 2005-08-08 22:42

Subject: Re: A simple script to do the reverse of git-push
Message-ID: <7vslxk125f.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vslxk125f.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <Pine.LNX.4.63.0508082318400.2911@wgmdd8.biozentrum.uni-wuerzburg.de>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> BTW, if you are lazy, like me, you just pull from Junio once in a while 
> and do a "make test". Turns out there is a missing dependency though:
>
> peek-remote.o: cache.h
>
> which in my case lead to a git-peek-remote program which was unable to 
> peek any ref.

You are right.  Thanks for noticing.

    $ (make clean ; make ) >/dev/null 2>/dev/null
    $ touch cache.h
    $ make 2>&1 | grep peek-remote
    cc -g -O2 -Wall  '-DSHA1_HEADER=<openssl/sha.h>' -o git-peek-remote peek-remote.o libgit.a -lz -lcrypto

I think recent "depend on object files" Makefile patch broke
some things.

We would need something like this.
---
# - pu: ls-remote: drop storing operation and add documentation.
# + (working tree)
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -172,6 +172,7 @@ init-db.o: init-db.c
 	$(CC) -c $(CFLAGS) -DDEFAULT_GIT_TEMPLATE_DIR='"$(template_dir)"' $*.c
 
 $(LIB_OBJS): $(LIB_H)
+$(patsubst git-%,%.o,$(PROG)): $(LIB_H)
 $(DIFF_OBJS): diffcore.h
 
 $(LIB_FILE): $(LIB_OBJS)

```

## Johannes Schindelin, 2005-08-08 23:38

Subject: Re: A simple script to do the reverse of git-push
Message-ID: <Pine.LNX.4.63.0508090135230.3695@wgmdd8.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0508090135230.3695%40wgmdd8.biozentrum.uni-wuerzburg.de
In-Reply-To: <7vslxk125f.fsf@assigned-by-dhcp.cox.net>

```
Hi,

On Mon, 8 Aug 2005, Junio C Hamano wrote:

> We would need something like this.
>
> [...]
>
> +$(patsubst git-%,%.o,$(PROG)): $(LIB_H)

A short

	for i in git-*; do \
		c=$(echo $i|sed "s/git-\(.*\)/\1.c/g")
		if [ -e $c ]; then \
			if ! grep cache\\.h $c; then \
				echo $c does not depend on cache.h \
			fi \
		fi \
	done

(actually tested) reveals that only get-tar-commit-id.c and stripspace.c 
do not depend on cache.h or rev-cache.h. Thus, your patch is the most 
elegant solution to my problem :-)

Ciao,
Dscho

```

## Petr Baudis, 2005-08-11 22:18

Subject: Re: A simple script to do the reverse of git-push
Message-ID: <20050811221812.GG25280@pasky.ji.cz>
URL: https://gitlist.dev/e/20050811221812.GG25280%40pasky.ji.cz
In-Reply-To: <7vslxk125f.fsf@assigned-by-dhcp.cox.net>

```
Dear diary, on Tue, Aug 09, 2005 at 12:42:36AM CEST, I got a letter
where Junio C Hamano <junkio@cox.net> told me that...
> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > BTW, if you are lazy, like me, you just pull from Junio once in a while 
> > and do a "make test". Turns out there is a missing dependency though:
> >
> > peek-remote.o: cache.h
> >
> > which in my case lead to a git-peek-remote program which was unable to 
> > peek any ref.
> 
> You are right.  Thanks for noticing.
> 
>     $ (make clean ; make ) >/dev/null 2>/dev/null
>     $ touch cache.h
>     $ make 2>&1 | grep peek-remote
>     cc -g -O2 -Wall  '-DSHA1_HEADER=<openssl/sha.h>' -o git-peek-remote peek-remote.o libgit.a -lz -lcrypto
> 
> I think recent "depend on object files" Makefile patch broke
> some things.

Indeed. I took care to make the new dependencies a superset of previous
situation when removing the explicit dependencies list, but before,
rebuilding of libgit.a caused rebuilt from source of all the commands,
which wouldn't happen anymore after adding the object files, which this
way sneakily removed an implicit dependency of the command sources on
$(LIB_H). Eek.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
If you want the holes in your knowledge showing up try teaching
someone.  -- Alan Cox

```
