aboutsummaryrefslogtreecommitdiff
path: root/src/util/git-lock.pl
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/git-lock.pl')
-rwxr-xr-xsrc/util/git-lock.pl58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/util/git-lock.pl b/src/util/git-lock.pl
new file mode 100755
index 0000000..0eb7922
--- /dev/null
+++ b/src/util/git-lock.pl
@@ -0,0 +1,58 @@
+#! /usr/bin/perl -w
+
+# Obtain a lock for a git repository
+
+# 2008-03-06 Erik Schnetter <schnetter@cct.lsu.edu>
+
+use strict;
+use Cwd;
+#use Fcntl ':flock';
+use sigtrap qw(die normal-signals);
+
+
+
+# Obtain exclusive access to the repository
+
+# We cannot use flock since the file system may be remote
+#open LOCKFILE, "$cctk_home/Makefile";
+#flock LOCKFILE, LOCK_EX;
+
+my $git_dir = $ENV{'GIT_DIR'};
+$git_dir = getcwd unless defined $git_dir;
+$git_dir =~ s+/.git/*$+/+;
+
+# We are very conservative as to what characters we allow. Other
+# characters can be added, but be careful.
+$git_dir =~ m|^[[:alnum:]+-._/]+$| or die;
+
+my $lockdir = "$git_dir/GITLOCK";
+
+
+
+my $waittime = 1;
+my $maxwaittime = 10;
+while (! (mkdir $lockdir)) {
+ # Wait some time
+ my $unit = $waittime==1 ? "second" : "seconds";
+ print "Git repository is busy; waiting $waittime $unit...\n";
+ system "sleep $waittime";
+ # Back off exponentially
+ $waittime *= 2;
+ $waittime = $maxwaittime if $waittime > $maxwaittime;
+}
+
+
+
+# Execute the command
+system @ARGV;
+$? != -1 or die;
+exit $? >> 8;
+
+
+
+# Release the lock
+END {
+ my $retval = $?;
+ rmdir $lockdir;
+ $? = $retval;
+}