forked from KDE/digikam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gits
executable file
·2731 lines (2243 loc) · 72.4 KB
/
gits
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
#
# Support .gitslave files, recursive processing of git commands into slave directories
#
# ++Copyright LIBBK++
#
# Copyright (c) 2003 The Authors. All rights reserved.
#
# This source code is licensed to you under the terms of the file
# LICENSE.TXT in this release for further details.
#
# Mail <[email protected]> for further information
#
# --Copyright LIBBK--
#
# This version was modified to work for the digiKam Software Compilation
#
use strict;
use warnings;
no warnings 'uninitialized';
no warnings 'io';
use Cwd;
use File::Spec;
use constant GITSLAVE => '.gitslave';
use Getopt::Long;
use File::Path;
use File::Basename;
my $eval_errs = '';
my ($want_parallel) = (0);
eval 'use Parallel::Iterator;';
if ($@)
{
undef($want_parallel);
my $err = $@;
$err =~ s/\(\@INC [^)]*\)//g;
$err =~ s/ at .* line \d+\.//g;
$eval_errs .= $err;
}
my ($want_progress,$progress_count) = (0,0);
eval 'use Term::ProgressBar;';
if ($@)
{
undef($want_progress);
my $err = $@;
$err =~ s/\(\@INC [^)]*\)//g;
$err =~ s/ at .* line \d+\.//g;
$eval_errs .= $err;
}
my ($progress);
our($GITSLAVE) = $ENV{'GITSLAVE'}||GITSLAVE;
our($GITS_DIR);
our(@slaves);
my($USAGE) = "usage: gits [--exclude /slave-regexp/] [--version] [--eval-args] [--with-ifpresent] [--no-master] [--no-hide] [--no-commit] [--keep-going] [--no-progress] [-p|--parallel <count>][-v|--verbose]+ [--quiet] [--help] [-- <git-options>...] <subcommand> [args]...\n";
our(%OPTIONS);
my ($returncode) = 0;
delete($ENV{'GIT_DIR'});
our($fromcheckout);
our($git) = 'git';
# Some people are stupid enough to set CDPATH and that screws up the formatting
delete($ENV{'CDPATH'});
######################################################################
#
# Translate wait status ($?) into human-readable terms
#
sub exitcode(;$)
{
my ($ret) = @_;
$ret = $? if !defined($ret);
if ($ret & 127)
{
return "killed by signal " . ($ret & 127);
}
return "exit " . ($ret >> 8);
}
######################################################################
#
# Perform substitution of path and directory components
#
sub gitsubst($$)
{
my ($data,$slave) = @_;
my ($pattern) = quotemeta('%%dir%%');
if ($data =~ /$pattern/)
{
$data =~ s/$pattern/$slave/g;
}
$pattern = quotemeta('%%path%%');
if ($data =~ /$pattern/)
{
my $subst = Cwd::realpath($GITS_DIR."/".$slave);
$data =~ s/$pattern/$subst/g;
}
$pattern = quotemeta('%%basename%%');
if ($data =~ /$pattern/)
{
my $subst = basename(Cwd::realpath($GITS_DIR."/".$slave));
$data =~ s/$pattern/$subst/g;
}
$data;
}
######################################################################
#
# Run a command where we don't care about the output
#
sub docmd($)
{
my ($cmd) = @_;
$cmd .= " 2>/dev/null" if ($OPTIONS{'quiet'});
print STDERR "Running command: `$cmd`\n" if ($OPTIONS{'verbose'} > 0);
system($cmd);
print STDERR "Command " . exitcode() . "\n" if ($OPTIONS{'verbose'} > 1);
$? == 0;
}
######################################################################
#
# Run a command, retrieving the output
#
sub getcmd($;$)
{
my ($cmd,$slave) = @_;
$cmd = gitsubst($cmd,$slave) if ($slave);
$cmd = "($cmd) 2>&1";
print STDERR "Running command: `$cmd`\n" if ($OPTIONS{'verbose'} > 0);
my $out = `$cmd`;
if ($OPTIONS{'verbose'} > 1)
{
my $tout = $out;
chomp($tout);
print STDERR "Command " . exitcode() . "\n ($tout)\n";
}
wantarray?($?, $out):$out;
}
######################################################################
#
# Find the absolute path to repository (relatives are relative to upstream repository)
#
sub resolve_repository($$;$)
{
my ($relrepos,$relative,$absrepos) = @_;
# Check for already absolute
return $relrepos if ($relrepos =~ m-^(/|\w+:/)-);
# Break into method and path sections
my ($prefix, $suffix) = ("", $relrepos);
($prefix, $suffix) = ($1, $2) if ($relrepos =~ m-^(\w+:)(.*)-);
# Find upstream origin URL
if (!$absrepos)
{
my ($ret, $master_repo) = getcmd("$git config remote.origin.url");
chomp($master_repo);
die "Could not find git upstream clone which this is relative to\n" unless ($ret == 0 && $master_repo);
$absrepos = $master_repo;
}
# Nuke trailing .git, if necessary
$absrepos =~ s:/.git$::;
if ($fromcheckout)
{
$relrepos = "$absrepos/$relative";
}
else
{
my ($mprefix, $msuffix) = ("", $absrepos);
($mprefix, $msuffix) = ($1, $2) if ($absrepos =~ m-^([\w\@\.]+:(?://[^/]*|))(.*)-);
# Is this repository-relative? (keep method://host from master)
if ($relrepos =~ m:^\^(/?.*):)
{
my $path = $1;
if ($path =~ m-^/- || $mprefix =~ m-[:/]$-)
{
return "$mprefix$path";
}
else
{
return "$mprefix/$path";
}
}
# Do we have a replacement method://? (Seems like an unlikely case)
if ($prefix)
{
$absrepos = $prefix.$msuffix;
}
# Handle smushing together, relative paths, etc
$relrepos = "$absrepos/$suffix";
while ($relrepos =~ s:/\./:/:) {;}
while ($relrepos =~ s:/[^/]+?/\.\./:/:) {;}
while ($relrepos =~ s-(^\w+://.*)//-$1-g) {;}
while ($relrepos !~ m-^\w+://- && $relrepos =~ s://:/:g) {;}
}
$relrepos =~ s:/\.$::;
$relrepos;
}
######################################################################
#
# check whether command requires slave configuration
#
sub ephemeral_command($)
{
my ($cmd) = @_;
return ($cmd eq "prepare" or $cmd eq "resolve" or $cmd eq "clone" or $cmd eq "help");
}
######################################################################
#
# Recursive slave list population
#
sub git_slave_list_recursive($$$$$);
sub git_slave_list_recursive($$$$$)
{
my ($file,$gitsp,$seenp,$cmd,$base) = @_;
my ($r);
if (!open($r, "<", $file))
{
die "Could not find $file file\n"
unless (!defined($cmd) or ephemeral_command($cmd));
return;
}
my ($lineno) = 0;
while (<$r>)
{
$lineno++;
if (/^\#include\s+\"([^\"]+)\"\s*$/)
{
my $slavefile = $1;
$slavefile = "$base/$slavefile" unless ($slavefile =~ m:^/:);
my $newbase = $slavefile;
$newbase =~ s:^(.*)/.*:$1:;
git_slave_list_recursive($slavefile,$gitsp,$seenp,$cmd,$newbase);
next;
}
next if (/^\s*(?:\#.*)$/);
next if ($OPTIONS{'exclude'} && /$OPTIONS{'exclude'}/);
die "Bad line $lineno in $file\n" unless (/^\"(.*)\" \"(.*)\"( ifpresent)?$/);
my ($baserel,$ckoutrel,$flags) = ($1,$2,$3);
if ($flags !~ /ifpresent/ || $OPTIONS{'with-ifpresent'} || -d "$base/$ckoutrel/.")
{
my ($relbase) = $base;
my ($qbase) = quotemeta($GITS_DIR);
$relbase =~ s:^$qbase/*::;
$relbase = "$relbase/" if ($relbase);
$ckoutrel = "$relbase$ckoutrel";
push(@$gitsp,[$baserel, $ckoutrel]) if (!$seenp->{$ckoutrel});
$seenp->{$ckoutrel} = 1;
}
}
close($r);
}
######################################################################
#
# Get list of currently configured slaves
#
sub git_slave_list($)
{
my ($cmd) = @_;
my (@gits);
my (%seen);
# Discover the current fromcheckout status
my $thisfromcheckout = `$git config gits.fromcheckout`;
$fromcheckout = 1 if ($thisfromcheckout && $thisfromcheckout ne "false");
foreach my $slavefile (split(/, /,$GITSLAVE))
{
$slavefile = "$GITS_DIR/$slavefile" unless ($slavefile =~ m:^/:);
git_slave_list_recursive($slavefile,\@gits,\%seen,$cmd,$GITS_DIR);
}
@gits;
}
######################################################################
#
# Configure persistent fromcheckout
#
sub set_fromcheckout($)
{
if ($_[0])
{
`$git config gits.fromcheckout 1`;
$fromcheckout = 1;
}
else
{
`$git config --unset gits.fromcheckout`;
$fromcheckout = 0;
}
$? == 0;
}
######################################################################
#
# Check for presence of slave
#
my ($warngitslavesync) = 0;
sub missing_slave($)
{
my ($slave) = @_;
if (!-d $slave)
{
warn "Missing one or more git slaves including $slave, consider 'gits populate'.\n" unless ($warngitslavesync);
$warngitslavesync++;
return 1;
}
return 0;
}
######################################################################
#
# Add shell quoting to avoid problems
#
sub quoteit(@)
{
my $str;
foreach my $item (@_)
{
my ($i) = $item;
my $o;
my $len = length($i);
foreach(my $x=0;$x<$len;$x++)
{
my $c = substr($i,$x,1);
if ($OPTIONS{'eval-args'})
{
$o .= '\\' if ($c =~ /[\\\"]/);
}
else
{
$o .= '\\' if ($c =~ /[\\\$\`\"]/);
}
$o .= $c;
}
$str .= '"'.$o.'" ';
}
chop($str);
$str;
}
######################################################################
#
# Populate - clone projects listed in .gitslaves
#
sub do_populate(;$)
{
my ($nocheckout)=@_;
my $curbranch;
$nocheckout='-n' if ($nocheckout);
foreach my $group (@slaves)
{
my $slave = $group->[1];
if (! -d $slave)
{
print "gits: Cloning $slave\n" if ($OPTIONS{'verbose'});
my ($repos) = resolve_repository($group->[0],$group->[1]);
docmd(qq^$git clone $nocheckout "$repos" "$slave"^) || die "Could not clone $repos onto $slave\n";
$curbranch = (grep(s/\s*\*\s+//, split(/\n/,getcmd("$git branch"))))[0] unless ($curbranch);
if ($curbranch ne "master" && $curbranch !~ /no branch/)
{
print qq^Switching "$ARGV[2]" to branch "$curbranch"\n^;
docmd(qq^cd "$slave"; $git checkout -b $curbranch origin/$curbranch^) || die "Branch inconsistency, branch $curbranch does not exist for $slave\n";
}
}
else
{
print "gits: $slave already exists\n" if ($OPTIONS{'verbose'});
}
# Update hooks from master (-regex '.*\\*\$' matches bogus symlink '*')
docmd(qq^cd $slave/.git/hooks; find . -type l -a '(' -lname '../../../git-hooks*' -o -regex '.*\\*\$' ')' -exec rm -f '{}' ';'; find ../../../git-hooks/ -type f 8>&2 2>/dev/null -exec sh -c 'ln -s \$0 . 2>&8' '{}' ';'^);
}
# Update master's hooks as well
docmd(qq^cd .git/hooks; find . -type l -a '(' -lname '../../git-hooks*' -o -regex '.*\\*\$' ')' -exec rm -f '{}' ';'; find ../../git-hooks/ -type f 8>&2 2>/dev/null -exec sh -c 'ln -s \$0 . 2>&8' '{}' ';'^);
}
######################################################################
#
# git checkout, gits style
#
# pushprocessing: 0 -- want progress bar
# pushprocessing: 1 -- print informational messages, revert to oldbranch on die
# (oldbranch is first argument, popped from @args)
# pushprocessing: 2 -- do not abort on errors - return warning
#
sub do_checkout($@);
sub do_checkout($@)
{
my ($pushprocessing,@args) = @_;
my ($oldbranch);
my ($ret, $msg) = (0, undef);
my ($slavecnt) = -1;
my ($ok, $okcnt);
my (%msg);
my ($group, $slave) = (undef, ".");
$oldbranch = shift(@args) if ($pushprocessing == 1);
if (!$OPTIONS{'no-master'})
{
my ($cmd) = qq^cd %%dir%% && $git checkout ^.quoteit(@args);
$cmd .= " --" if ($pushprocessing);
$cmd .= " >/dev/null 2>&1" if ($pushprocessing == 2);
($ret, $msg) = getcmd($cmd,$slave);
if ($ret == 0)
{
# Reload list of slaves, which might have changed due to superproject checkout
@slaves = git_slave_list("checkout");
# New list of slaves might have stuff we need to check out
do_populate(1);
}
}
my ($totcnt) = $#slaves + ($OPTIONS{'no-master'} ? 1 : 2);
my ($progress) = Term::ProgressBar->new({remove=>1, count=>($#slaves+1)}) if ($want_progress && !$pushprocessing);
$progress->max_update_rate(1) if ($progress);
# Tricky: This backwards loop is designed so that the return code processing can be shared between superproject and slaves
my $failed;
do
{
if ($ret != 0)
{
chomp($msg);
my $warn = "gits checkout (@{[join(' ',@args)]}), failed for: '$slave': " . exitcode($ret);
if ($pushprocessing > 1)
{
$failed .= " $slave";
}
elsif ($OPTIONS{'keep-going'})
{
warn "Error in $warn (continuing)\n $msg\n";
}
else
{
my $diemsg = "Aborting $warn\n ";
if ($ok)
{
$diemsg .= "(ran fine on: $ok, did not run on @{[$totcnt-$okcnt]} other(s); no rollbacks)";
}
else
{
$diemsg .= "(first entry, no other successfully executed)";
}
# <TODO>try to restore old branch, if desirable/possible</TODO>
$msg .= do_checkout(2, $oldbranch) if ($pushprocessing == 1);
die "$diemsg\n $msg\n";
}
}
else
{
$ok .= " $slave";
$okcnt++;
if ($pushprocessing)
{
$msg =~ s/Switched to branch.*\n//;
$msg =~ s/Already on \".*\n//;
push(@{$msg{"$args[0]\@$slave"}}, $slave);
}
else
{
push(@{$msg{$msg}}, $slave);
}
}
# Forward through the slave list
if (++$slavecnt <= $#slaves)
{
$group = $slaves[$slavecnt];
$slave = $group->[1];
my ($cmd) = qq^cd %%dir%% && $git checkout ^.quoteit(@args);
$cmd .= " --" if ($pushprocessing);
$cmd .= " >/dev/null 2>&1" if ($pushprocessing == 2);
($ret, $msg) = getcmd($cmd,$slave);
}
$progress->update($slavecnt) if ($progress);
} while ($slavecnt <= $#slaves);
if ($pushprocessing > 1)
{
my $msg = "\nUnable to checkout original $args[0] for$failed" if ($failed);
$msg .= " (successful for $okcnt other(s))" if ($failed);
return $msg;
}
%msg;
}
######################################################################
#
# Standard gits output summmarization routine
#
sub stdout($;$)
{
my ($msgp,$suppressempty) = @_;
# Skip output if all commands returned empty output strings unless we are in verbose mode
return if (!$OPTIONS{'verbose'} && scalar(keys(%{$msgp})) == 1 && defined($msgp->{""}));
foreach my $msg (sort { $#{$msgp->{$a}} <=> $#{$msgp->{$b}} } keys %{$msgp})
{
next if ($suppressempty && !$OPTIONS{'verbose'} && $msg eq "");
print "On: ", join(', ',@{$msgp->{$msg}}), ":\n";
$msg =~ s/^/ /mg if (length($msg));
print $msg;
}
}
######################################################################
#
# Main functionality
#
Getopt::Long::Configure("bundling", "no_ignore_case", "no_auto_abbrev", "no_getopt_compat", "require_order");
GetOptions(\%OPTIONS, 'no-progress', 'exclude=s', 'version', 'no-master', 'eval-args', 'with-ifpresent', 'no-hide', 'no-commit', 'keep-going', 'parallel|p=i', 'press-on', 'verbose|v+', 'quiet', 'help') || die $USAGE;
while ($ARGV[0] =~ /^--/)
{
$git .= " " . shift @ARGV;
}
# provide baka terminology compatibility
my $BAKANAMES = { 'inits' => 'prepare', 'clones' => 'attach',
'checkouts' => 'populate', 'execs' => 'exec',
'resolves' => 'resolve', 'stati' => 'statuses' };
$ARGV[0] = $BAKANAMES->{$ARGV[0]} if $BAKANAMES->{$ARGV[0]};
$OPTIONS{'keep-going'} = 1 if $OPTIONS{'press-on'};
$want_parallel = $OPTIONS{'parallel'} if (defined($want_parallel));
$OPTIONS{'no-progress'} = 1 if ($want_parallel);
die "$USAGE" if ($#ARGV < 0 and !($OPTIONS{'version'} or $OPTIONS{'help'}));
print STDERR $eval_errs if ($eval_errs ne '' && $OPTIONS{'verbose'} > 1);
# Find GITS_DIR -- location of git slave file
if ($ENV{'GITS_DIR'} && -f $ENV{'GITS_DIR'}."/".GITSLAVE)
{ # Use gitslave location user told us
$GITS_DIR = $ENV{'GITS_DIR'};
}
else
{ # Probe for gitslave location
$GITS_DIR = '.';
my (@S,$dev,$ino) = stat($GITS_DIR);
do
{
if (!-f $GITS_DIR."/".GITSLAVE)
{
$GITS_DIR .= '/..';
$dev = $S[0];
$ino = $S[1];
@S = stat($GITS_DIR);
}
} while (!-f $GITS_DIR."/".GITSLAVE && $S[0] == $dev && $S[1] != $ino);
}
# All operations are relative to gitslave base
if (-f $GITS_DIR."/".GITSLAVE)
{
$GITS_DIR = Cwd::realpath($GITS_DIR);
chdir($GITS_DIR);
print STDERR "gits: Gitslave found in $GITS_DIR\n" if ($OPTIONS{'verbose'} > 1);
}
else
{
die "Could not find @{[GITSLAVE]}, either you are not inside a meta-module\nor one of its slaves or this is a new meta-module which has not yet\nbeen configured for gits. Probably it is the first case so you simply\nneed to cd into the correct git checkout with a @{[GITSLAVE]} file, but in\nthe rare event that this is a new meta-module, you need run `gits\nprepare` and then a few `gits attach` commands to set up gits for this\nnew meta-module.\n"
unless ($#ARGV < 0 or ephemeral_command($ARGV[0]));
}
@slaves = git_slave_list($ARGV[0]);
if (!$OPTIONS{'no-progress'})
{
if (defined($want_progress) && -t STDERR)
{
$want_progress = 1;
}
else
{
warn "Progress bar unavailable - install Term::ProgressBar" .
" (perl-Term-ProgressBar RPM)\n"
if ($OPTIONS{'verbose'} > 0);
}
}
if ($OPTIONS{'parallel'})
{
if (!defined($want_parallel))
{
warn "Parallelism unavailable - install Parallel::Iterator" .
" (no RPM exists, use CPAN)\n";
}
}
if ($ARGV[0] eq 'version' or $OPTIONS{'version'})
{
my $version = "1.3";
# <TRICKY> If the version string is still UNTAGGED, try to run the local
# git commands to get the current hash and possibly tag; this is used by
# Makefile to get the correct string to replace it with. Otherwise, the
# installation has replaced it with a version; don't mess with it. Use
# string concatenation to keep this check from getting replaced. </TRICKY>
if ($version eq "{" . "UNTAGGED" . "}")
{
my ($ret,$hash) = getcmd("$git log --pretty=format:%H -n 1");
chomp($hash);
my ($dret,$desc) = getcmd("$git describe --candidates=1 --tags $hash 2>/dev/null");
if ($dret)
{
$desc = "Untagged ($hash)\n";
}
else
{
# strip out leading tag alphabetic and space, get just the numbers
$desc =~ s/^.*?([0-9][0-9._-]*[0-9]).*/$1/;
$desc =~ y=/_=--=;
}
print $desc;
}
else
{
print "gits version $version\n";
my ($vret,$vers) = getcmd("$git --version");
if ($vret || ($vers !~ /version ([2-9]|1\.[6-9])/))
{
print STDERR "$git version >= 1.6 required!\n";
$returncode = 1;
}
print $vers;
($vret,$vers) = getcmd("perl --version");
$vers =~ s/^\n//;
$vers =~ s/This is perl,/Perl/;
$vers =~ s/\nCopyright.*//s;
print $vers;
}
}
elsif ($ARGV[0] eq 'help' or $OPTIONS{'help'})
{
print "$USAGE";
use FindBin qw($Bin $Script);
docmd("pod2text < $Bin/$Script |" .
"sed -n -e '/^DESCRIPTION/,/^BUGS/H'" .
" -e '/^BUGS/{x;s/\\n[[:upper:]]*/\\n/g;s/\\n\\n/\\n/;p;}'");
exit;
}
elsif ($ARGV[0] eq 'prepare')
{
die "gits prepare takes no arguments\n" if ($#ARGV != 0);
$GITS_DIR=$ENV{'GITS_DIR'} || ".";
die "Refusing to prepare, \$GITSLAVE variable is multipath\n" if ($GITSLAVE =~ /, /);
die "Refusing to prepare, $GITS_DIR/@{[$GITSLAVE]} already exists\n" if (-f $GITS_DIR."/".$GITSLAVE);
open(W, ">", $GITS_DIR."/".$GITSLAVE) || die "Could not create $GITS_DIR/@{[$GITSLAVE]}\n";
close(W);
docmd("$git add @{[$GITSLAVE]}");
docmd(qq^$git commit -m "gits creating @{[$GITSLAVE]}" @{[$GITSLAVE]}^) unless ($OPTIONS{'no-commit'});
}
elsif ($ARGV[0] eq 'attach')
{
die "gits attach requires two arguments\nusage: gits attach <repository> <directory> [flags]\n" if ($#ARGV < 2 || $#ARGV > 3 || !$ARGV[1] || !$ARGV[2]);
my ($repos) = resolve_repository($ARGV[1],$ARGV[2]);
die "Unknown flag $ARGV[3]\n" if ($ARGV[3] && $ARGV[3] ne "ifpresent");
die "Refusing to attach, \$GITSLAVE variable is multipath\n" if ($GITSLAVE =~ /, /);
die "Destination ($ARGV[2]) already exists\n" if (-e $ARGV[2]);
if ($ARGV[2] =~ m:(.*)/(.*):)
{
die "Destination ($ARGV[2]) cannot end in a slash" if (length($2) < 1);
die "Destination parent ($1) does not exist\n" if (!-d $1);
}
my $files = $GITS_DIR."/".$GITSLAVE;
docmd(qq^$git clone "$repos" "$ARGV[2]"^) || die "Could not clone $repos onto $ARGV[2]\n";
open(W, ">>", $files);
print W qq^"$ARGV[1]" "$ARGV[2]"^;
print W " $ARGV[3]" if ($ARGV[3]);
print W "\n";
close(W);
if ($ARGV[2] !~ m:/:)
{
my ($needadd) = 0;
$needadd++ if (! -f "$GITS_DIR/.gitignore");
open(W, ">>", $GITS_DIR."/.gitignore");
print W qq^$ARGV[2]\n^;
close(W);
$files .= " .gitignore";
docmd(qq^$git add .gitignore^) if ($needadd);
}
docmd(qq^$git commit -m 'gits adding "$ARGV[1]" "$ARGV[2]"' $files^) unless ($OPTIONS{'no-commit'});
my $curbranch = (grep(s/\s*\*\s+//, split(/\n/,getcmd("$git branch"))))[0];
if ($curbranch ne "master")
{
print qq^Switching "$ARGV[2]" to branch "$curbranch"\n^;
docmd(qq^cd "$ARGV[2]"; $git checkout $curbranch --^) || die "Branch inconsistency, branch $curbranch does not exist\n";
}
}
elsif ($ARGV[0] eq 'detach')
{
die "gits detach requires one directory (sub-module) argument which must exist\nusage: gits detach <directory>\n" if ($#ARGV != 1);
die "$ARGV[1] is not a git repository or does not exist\n" unless (-d "$ARGV[1]/.git");
my $files = $GITS_DIR."/".$GITSLAVE;
die "$files, the gitslave management file, does not exist\n" unless (-f $files);
open(R,"<",$files) || die "Cannot open $files";
my $save;
my ($lineno) = 0;
my $found = 0;
while (<R>)
{
$lineno++;
if (/^\s*(?:\#.*)$/)
{
$save .= $_;
next;
}
die "Bad line $lineno in $files\n" unless (/^\"(.*)\" \"(.*)\"( ifpresent)?$/);
if ($2 eq $ARGV[1])
{
$found++;
next;
}
$save .= $_;
}
close(R);
die "Could not requested repository $ARGV[1] in $files for detaching. Aborting\n" unless ($found);
open(W,">",$files) || die "Cannot open $files for writing";
print W $save;
close(W) || die "Cannot write $files";
if (open(R,"<",".gitignore"))
{
my $save2;
while (<R>)
{
my ($c) = $_;
chomp($c);
next if ($c eq "$ARGV[1]");
$save2 .= $_;
}
close(R);
open(W,">",".gitignore") || die "Cannot open .gitignore for writing";
print W $save2;
close(W) || die "Cannot write .gitignore";
}
docmd(qq^rm -rf ^.quoteit($ARGV[1])) unless ($OPTIONS{'no-commit'});
docmd(qq^$git commit -m 'gits removing ^.quoteit($ARGV[1])."' ".quoteit($GITSLAVE)." .gitignore") unless ($OPTIONS{'no-commit'});
}
elsif ($ARGV[0] eq 'clone')
{
if (grep(/^--fromcheckout$/,@ARGV))
{
set_fromcheckout(1);
@ARGV = grep(!/^--fromcheckout$/,@ARGV);
}
if (grep(/^--no-fromcheckout$/,@ARGV))
{
set_fromcheckout(0);
@ARGV = grep(!/^--no-fromcheckout$/,@ARGV);
}
my ($ret,$out) = getcmd(qq^git ^.quoteit(@ARGV));
print STDERR $out;
if ($ret != 0 || $out !~ /^Cloning into (.*)\.\.\.\n/)
{
die "Could not parse git clone output\n";
}
chdir($1);
my $cmd = "$0 ";
foreach my $arg (keys %OPTIONS)
{
if ($arg eq "exclude" || $arg eq "parallel")
{
$cmd .= qq^'--$arg=$OPTIONS{"$arg"}' ^;
}
else
{
$cmd .= "--$arg ";
}
}
$cmd .= "populate";
docmd($cmd);
}
elsif ($ARGV[0] eq 'populate')
{
if (grep(/^--fromcheckout$/,@ARGV))
{
set_fromcheckout(1);
@ARGV = grep(!/^--fromcheckout$/,@ARGV);
}
if (grep(/^--no-fromcheckout$/,@ARGV))
{
set_fromcheckout(0);
@ARGV = grep(!/^--no-fromcheckout$/,@ARGV);
}
die "gits populate only accepts --(no-)fromcheckout option argument\n" if ($#ARGV != 0);
do_populate();
}
elsif ($ARGV[0] eq 'resolve')
{
if (grep(/^--fromcheckout$/,@ARGV))
{
$fromcheckout = 1;
@ARGV = grep(!/^--fromcheckout$/,@ARGV);
}
if (grep(/^--no-fromcheckout$/,@ARGV))
{
$fromcheckout = 0;
@ARGV = grep(!/^--no-fromcheckout$/,@ARGV);
}
die "gits resolve requires two non-option arguments\n" if ($#ARGV != 2 || !$ARGV[1] || !$ARGV[2]);
print resolve_repository($ARGV[1], $ARGV[2])."\n";
}
elsif ($ARGV[0] eq 'pulls')
{
shift(@ARGV);
my ($ok,$okcnt,%msg);
my (@list) = @slaves;
unshift(@list,[undef,"."]) unless ($OPTIONS{'no-master'});
my $oldbranch = (grep(s/\s*\*\s+//, split(/\n/,getcmd("$git branch"))))[0];
my @branches = grep(s/branch\.(.*)\.remote=.*/$1/,split(/\n/,getcmd(qq%$git config -l%)));
$progress = Term::ProgressBar->new({remove=>1, ETA => 'linear', count=>(($#branches + 1)*($#list+1))}) if ($want_progress);
$progress->max_update_rate(1) if ($progress);
foreach my $branch (@branches)
{
my (%newmsg) = do_checkout(1, $oldbranch, $branch);
my (%results);
foreach my $group (@list)
{
my $slave = $group->[1];
next if missing_slave($slave);
$results{$group} = gitsubst("cd %%dir%% && $git pull ".quoteit(@ARGV),$slave);
}
if ($want_parallel)
{
%results = Parallel::Iterator::iterate_as_hash({'workers' => $want_parallel},sub { [getcmd($_[1])] }, \%results);
}
foreach my $group (@list)
{
my $slave = $group->[1];
next if missing_slave($slave);
my ($ret, $msg);
if ($want_parallel)
{
($ret, $msg) = @{$results{$group}};
}
else
{
($ret, $msg) = getcmd($results{$group});
}
if ($ret != 0)
{
chomp($msg);
my $warn = "gits pulls, failed on branch $branch for: '$slave': " . exitcode($ret);
if ($OPTIONS{'keep-going'} || $want_parallel)
{
warn "Error in $warn (continuing)\n $msg\n";
$returncode = 2;
next;
}
my $diemsg = "Aborting $warn\n ";
if ($ok)
{
$diemsg .= "(ran fine on: $ok, did not run on @{[$#list-$okcnt]} other(s); no rollbacks)";
}
else
{
$diemsg .= "(first entry, no other successfully executed)";
}
# <TODO>some way for do_checkout to only operate on some slaves</TODO>
$msg .= do_checkout(2, $oldbranch);
die "$diemsg\n $msg\n";
}
$msg = $newmsg{$slave}.$msg;
$msg =~ s/Switched to branch.*\n//;
$msg =~ s/Already on \".*\n//;
$ok .= " $slave";
$okcnt++;
# replace module name with %MODULE% to collapse redundant messages
my $mod = $slave;
if ($mod eq '.')
{
$mod = `$git config remote.origin.url`;
chomp $mod;
$mod =~ s=.*/==;
}
$msg =~ s/\b$mod\b/%MODULE%/g;
my $submod = $mod;
$submod =~ s=.*/==;
$msg =~ s/\b$submod\b/%MODULE%/g if ($mod ne $submod);
# remove hash ids in various places to collapse redundant messages
if (!$OPTIONS{'no-hide'})
{
$msg =~ s/\n {3}[0-9a-fA-F]{7}\.\.[0-9a-fA-F]{7} /\n /g;
$msg =~ s/\nUpdating [0-9a-fA-F]{7}\.\.[0-9a-fA-F]{7}\n/\n/g;
$msg =~ s/(Fast-forwarded \S+) to [0-9a-fA-F]*\./$1/g;
}
push(@{$msg{$msg}}, "$branch\@$slave");
$progress->update(++$progress_count) if ($progress);
}
}
stdout(\%msg);
print do_checkout(2, $oldbranch) . "\n";
}
elsif ($ARGV[0] eq 'pull')
{
my ($ok,$okcnt,%msg);
my (@list) = @slaves;
unshift(@list,[undef,"."]) unless ($OPTIONS{'no-master'});
$progress = Term::ProgressBar->new({remove=>1, ETA => 'linear', count=>($#list+1)}) if ($want_progress);
$progress->max_update_rate(1) if ($progress);
my (%results,%worklist);
foreach my $group (@list)
{
my $slave = $group->[1];
next if missing_slave($slave);