-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitspreadd
executable file
·326 lines (277 loc) · 8.6 KB
/
gitspreadd
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/perl
#=======================================================================
# gitspreadd
# File ID: 6c4d0b82-6111-11e0-8250-e509f64f5c24
#
# Push received commits and tags to other remotes to save bandwith
#
# Character set: UTF-8
# ©opyleft 2011– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of
# file for legal stuff.
#=======================================================================
use strict;
use warnings;
use Getopt::Long;
use Cwd qw{ abs_path };
use Fcntl ':flock';
use IO::Handle;
use POSIX 'setsid';
local $| = 1;
my %Std = (
'repodir' => "$ENV{'HOME'}/Git-spread",
);
our %Opt = (
'help' => 0,
'quiet' => 0,
'repodir' => '',
'run-once' => 0,
'verbose' => 0,
'version' => 0,
);
our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = '0.11.0+git';
Getopt::Long::Configure('bundling');
GetOptions(
'help|h' => \$Opt{'help'},
'quiet|q+' => \$Opt{'quiet'},
'repodir|r=s' => \$Opt{'repodir'},
'run-once|1' => \$Opt{'run-once'},
'verbose|v+' => \$Opt{'verbose'},
'version' => \$Opt{'version'},
) || die("$progname: Option error. Use -h for help.\n");
$Opt{'verbose'} -= $Opt{'quiet'};
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
print_version();
exit(0);
}
my $CMD_GIT = defined($ENV{'GITSPREAD_GIT'}) ? $ENV{'GITSPREAD_GIT'} : 'git';
my $repodir = abs_path($Std{'repodir'});
defined($ENV{'GITSPREAD_REPODIR'}) &&
length($ENV{'GITSPREAD_REPODIR'}) &&
($repodir = abs_path($ENV{'GITSPREAD_REPODIR'}));
length($Opt{'repodir'}) && ($repodir = abs_path($Opt{'repodir'}));
my $spooldir = abs_path("$repodir/spool");
my $logfile = abs_path("$repodir/$progname.log");
my $pidfile = abs_path("$repodir/pid");
my $stopfile = abs_path("$repodir/stop");
my $logfh;
exit(main(%Opt));
sub main {
# {{{
my %Opt = @_;
my $Retval = 0;
if (! -d "$repodir/.") {
warn("$progname: $repodir: Missing repository top directory\n");
exit(1);
}
if (! -d "$spooldir/.") {
mkdir($spooldir) or die("$progname: $spooldir: Cannot create spool directory: $!\n");
}
open($logfh, '>>', $logfile)
or die("$progname: $logfile: Cannot open logfile: $!\n");
$logfh->autoflush(1);
flock($logfh, LOCK_EX | LOCK_NB)
or die("$progname: $logfile: Cannot lock file: $!.\n" .
"Is $progname already running?\n");
my $do_loop = $Opt{'run-once'} ? 0 : 1;
defined(my $pid = fork) or die("$progname: Cannot fork: $!");
if ($pid) {
my $start_str = "Starting $progname $VERSION, PID = $pid";
print("$start_str\n");
logmsg($start_str);
if (open(my $pidfh, '>', $pidfile)) {
print($pidfh "$pid\n");
close($pidfh);
}
}
exit if $pid;
setsid or die("$progname: Cannot start a new session: $!\n");
do {
if (-e $stopfile) {
logmsg("$stopfile exists, terminating properly");
unlink($pidfile) || logmsg("WARNING: Cannot remove $pidfile: $!");
unlink($stopfile) || logmsg("WARNING: Cannot remove $stopfile: $!");
exit(0);
}
chdir($spooldir) || (logmsg("$spooldir: Cannot chdir to spool directory: $!. Aborting."), exit(1));
my @repos = glob('*');
if (scalar @repos) {
logmsg('Found new commits: ' . join(' ', @repos));
for my $repo (@repos) {
process_repo($repo);
}
}
sleep(1);
} while($do_loop);
return($Retval);
# }}}
} # main()
sub process_repo {
# {{{
my $repo = shift;
logmsg("Processing $repo");
my $repogit = "$repodir/$repo.git";
unless (chdir($repogit)) {
logmsg("$repogit: Cannot chdir: $!. Ignoring repository.");
goto endfunc;
}
my @deleted_branches = ();
if (open(my $fh, '<', "$spooldir/$repo")) {
while (<$fh>) {
chomp(my $line = $_);
if ($line =~ /^[0-9a-f]{40} 0{40} refs\/heads\/(.*)$/) {
my $deleted_branch = $1;
push(@deleted_branches, $deleted_branch);
}
}
close($fh);
} else {
logmsg("$spooldir/$repo: Cannot open file for read: $!. Ignoring repository $repo.");
goto endfunc;
}
my $force = git_config_value('gitspread.forcepush');
if ($force !~ /^(|false|true)$/) {
logmsg("WARNING: $repo: gitspread.forcepush contains invalid value \"$force\". " .
'Using "false".');
$force = 'false';
}
my $force_opt = $force eq 'true' ? '-f' : '';
for my $remote (git_remotes()) {
if (scalar(@deleted_branches)) {
for my $branch (@deleted_branches) {
exec_command($CMD_GIT, 'push', $remote, ":$branch");
}
}
exec_command($CMD_GIT, 'push', $force_opt, '--all', $remote);
exec_command($CMD_GIT, 'push', $force_opt, '--tags', $remote);
}
endfunc:
unlink("$spooldir/$repo") || logmsg("WARNING: $spooldir/$repo: Cannot delete file: $!");
return;
# }}}
} # process_repo()
sub exec_command {
# {{{
my @args = @_;
logmsg("Executing '" . join(' ', @args) . "'");
system(join(' ', @args, ">>$logfile", '2>&1'));
return;
# }}}
} # exec_command()
sub git_config_value {
# {{{
my $option = shift;
my $retval = '';
if (open(my $pipefh, "$CMD_GIT config --get $option |")) {
local $/ = undef;
$retval = <$pipefh>;
close($pipefh);
} else {
logmsg("FATAL: git_config_value(): " .
"Cannot open \"$CMD_GIT config --get\" pipe: $!. Aborting.");
exit(1);
}
chomp($retval);
return($retval);
# }}}
} # git_config_value()
sub git_remotes {
# {{{
my @retval = ();
if (open(my $pipefh, "$CMD_GIT remote |")) {
while (<$pipefh>) {
chomp;
length($_) && push(@retval, $_);
}
close($pipefh) or
logmsg("WARNING: Could not close \$pipefh in git_remotes(): $!");
} else {
logmsg("FATAL: git_remotes(): " .
"Cannot open \"$CMD_GIT remote\" pipe: $!. Aborting.");
exit(1);
}
return(@retval);
# }}}
} # git_remotes()
sub logmsg {
# {{{
my @txt = @_;
my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $is_dst) =
gmtime(time);
my $timestamp = sprintf('%04u-%02u-%02u %02u:%02u:%02uZ',
$year+1900, $mon+1, $day, $hour, $min, $sec);
print($logfh "$timestamp - ", @txt, "\n");
return;
# }}}
} # logmsg()
sub print_version {
# Print program version {{{
printf("%s$VERSION\n", $Opt{'verbose'} >= 0 ? "$progname " : "");
return;
# }}}
} # print_version()
sub usage {
# Send the help message to stdout {{{
my $Retval = shift;
if ($Opt{'verbose'} > 0) {
print("\n");
print_version();
}
print(<<"END");
Usage: $progname [options]
END
if ($Opt{'verbose'} >= 0) {
print(<<END);
Options:
-h, --help
Show this help.
-r X, --repodir X
Use X as directory to store bare repos.
Default: "$Std{'repodir'}"
-1, --run-once
Just pass through the program once instead of looping. Used for
testing purposes.
-q, --quiet
Be more quiet. Can be repeated to increase silence.
-v, --verbose
Increase level of verbosity. Can be repeated.
--version
Print version information. "Semantic versioning" is used, described
at <http://semver.org>.
To use an alternative version of git, set the \$GITSPREAD_GIT
environment variable to the git executable to use. For example:
export GITSPREAD_GIT=/usr/local/bin/git
./gitspreadd
END
}
exit($Retval);
# }}}
} # usage()
sub msg {
# Print a status message to stderr based on verbosity level {{{
my ($verbose_level, $Txt) = @_;
if ($Opt{'verbose'} >= $verbose_level) {
print(STDERR "$progname: $Txt\n");
}
return;
# }}}
} # msg()
__END__
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License in the file COPYING for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with this program.
# If not, see L<http://www.gnu.org/licenses/gpl-2.0.txt>.
# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :