git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[SCRIPT] cg-rpush & locking

From
Tony Lindgren <tony@atomide.com>
Date
May 31, 2005, 19:00 UTC
Message-ID
<20050531190005.GE18723@atomide.com>
Hello all,

Attached is a little script we're using for pushing changes to the linux-omap tree. I modified it from an earlier script done by Matthias Urlichs.

It uses rsync over ssh and should work with rsync write access too.

In order to remove lock files using rsync, I've added a .git/locks subdirectory that contains the lock file.

The reason for this is that it allows replacing the lock directory with an empty directory using rsync. This removes the lock file after the remote update is done.

Currently the local lock has some issues with chained pushes... If somebody is pushing from a remote repo to the local repo while the local repo is being pushed to some other remote repo, the lock may get removed.

Of course the lock does not protect from local changes either.

Anybody have any better ideas for locking that also works with rsync?

Tony

#!/bin/sh # # Pushes changes from the local git repo to remote repo. # # Copyright (C) 2005 Tony Lindgren <tony@atomide.com> # # Some parts based on an earlier push script, # Copyright (C) 2005 Matthias Urlichs. # # Takes the remote repo's name or url as parameter. # # Most likely the remote repo is not rsync writable, but # you can use rsync over ssh for the push. # # When using rsync over ssh, you must use the real repo # path on the server and an ssh key to avoid typing in # the password multiple times. For example: # # $ export RSYNC_FLAGS="-z --progress" # $ export RSYNC_RSH="ssh -i /home/user/.ssh/my-git-key" # $ cg-rpush some.machine:/home/git/repo #

. ${COGITO_LIB}cg-Xlib
name=$1

BRANCHES=".git/branches" HEADS=".git/refs/heads" OBJECTS=".git/objects" LOCKS=".git/locks" REMOTE_LOCK="write_lock" TMP="/tmp"

uri=""
function usage () {
	echo "Usage: [RSYNC_FLAGS=\"-e ssh\"] $0 some.machine:/home/git/repo"
	exit 1
}
function die () {
	if [ -f $LOCKS/$REMOTE_LOCK ]; then
		rm -f $LOCKS/$REMOTE_LOCK
	fi
	echo cg-rpush: $@ >&2
	exit 1
}
function clean_locks () {
	rm -f $LOCKS/$REMOTE_LOCK
	rsync $RSYNC_FLAGS -r --delete $LOCKS/ $uri/$LOCKS
}
function validate_input () {
	[ "$name" ] || usage
	if [ ! -d ".git" ]; then
		die "Could not find local .git directory"
	fi
	uri=$(cat $BRANCHES/$name 2>/dev/null)
	[ "$uri" ] || uri=$name
	echo $uri
}
#
# We must use a lock directory to allow removing the remote lock
# files with rsync by copying over it with an empty directory.
# Creating the remote lock file should be safe. However, please note
# that we must also be careful not to remove local .git/locks/write_lock
# in case somebody is pushing to our local repo from a remote machine.
# Currently the local lock file creation can conflict with a lock
# file creation from a remote machine to our local machine.
#
function lock_files () {
	echo "Attempting to to create a write lock on remote..."
	if [ ! -d $LOCKS ]; then
		mkdir $LOCKS;
	fi
	if [ -f $LOCKS/$REMOTE_LOCK ]; then
		echo "Local write_lock already exists: $LOCKS/$REMOTE_LOCK"
		exit 1
	fi
	lock_stamp="$USER@$HOSTNAME $(date)"
	echo $lock_stamp > $LOCKS/$REMOTE_LOCK
	rsync $RSYNC_FLAGS -r --ignore-existing $LOCKS/ $uri/$LOCKS
	# Check what the remote .git/locks/write_lock has
	tmpfile=$TMP/remote_lock_$RANDOM
	rsync $RSYNC_FLAGS "$uri/$LOCKS/$REMOTE_LOCK" $tmpfile
	remote_stamp=$(cat $tmpfile)
	rm -f $tmpfile
	if [ "$remote_stamp" != "$lock_stamp" ]; then
		die "Remote locked by $remote_stamp, please try again later"
	fi
}
function check_remote_version () {
	echo "Getting remote version..."
	tmpfile=$TMP/remote_head_$RANDOM
	rsync $RSYNC_FLAGS -Lr "$uri/$HEADS/master" $tmpfile
	remote_head=$(cat $tmpfile)
	rm -f $tmpfile
	if [ -z "$remote_head" ]; then
		clean_locks
		die "Remote repository does not have $uri/$HEADS/master"
	fi
	echo "Remote head is at: $remote_head"
	if [ "$(git-cat-file -t "$remote_head" 2>/dev/null)" != "commit" ]; then
		clean_locks
		die "Remote is ahead, please do a pull first"
	fi
}
function push_git_objects () {
	echo "Pushing .git/objects..."
	rsync $RSYNC_FLAGS --ignore-existing --whole-file -v -r \
		"$OBJECTS/" "$uri/$OBJECTS/"
}
function update_remote_head () {
	local_head=$(cat $HEADS/master)
	echo "Updating remote head to: $local_head"
	rsync $RSYNC_FLAGS -Lr $HEADS/master "$uri/$HEADS/master"
}
function print_note () {
	echo "Remote updated successfully"
	echo "NOTE: Not updating checked out remote files in case they"
	echo "have been edited locally on the remote machine."
	echo "To sync checked out files on remote, you can run cg-cancel"
	echo "on remote machine. You can check for uncommitted changes"
	echo "on remote with cg-diff first, which should only show"
	echo "changes done in this push."
}

# # Main program # uri=$(validate_input) lock_files check_remote_version push_git_objects update_remote_head clean_locks print_note exit

Next: Nicolas Pitre
Message 1 of 13 in “[SCRIPT] cg-rpush & locking”
  1. Tony LindgrenMay 31, 2005
  2. Nicolas PitreMay 31, 2005
  3. Thomas GlanzmannJun 1, 2005
  4. Tony LindgrenJun 1, 2005
  5. Linus TorvaldsJun 2, 2005
  6. Daniel BarkalowJun 2, 2005
  7. Matthias UrlichsJun 2, 2005
  8. Tony LindgrenJun 2, 2005
  9. Matthias UrlichsJun 2, 2005
  10. Linus TorvaldsJun 2, 2005
  11. Tony LindgrenJun 2, 2005
  12. Daniel BarkalowJun 2, 2005
  13. Dan HolmsandJun 2, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.