aboutsummaryrefslogtreecommitdiff
path: root/src/util/git-lock.pl
blob: 9980833fa52a62e1f833e3897c8ec3bae133fa76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#! /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 = 0.01;
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 = 1 if $waittime>1 && $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;
}