3 # This tool is copyright (c) 2005, Martin Langhoff.
4 # It is released under the Gnu Public License, version 2.
6 # The basic idea is to walk the output of tla abrowse,
7 # fetch the changesets and apply them.
12 git-archimport [ -h ] [ -v ] [ -T ] [ -t tempdir ] <archive>/<branch> [ <archive>/<branch> ]
14 Imports a project from one or more Arch repositories. It will follow branches
15 and repositories within the namespaces defined by the <archive/branch>
16 parameters suppplied. If it cannot find the remote branch a merge comes from
17 it will just import it as a regular commit. If it can find it, it will mark it
18 as a merge whenever possible.
20 See man (1) git-archimport for more details.
24 - create tag objects instead of ref tags
25 - audit shell-escaping of filenames
26 - better handling of temp directories
27 - use GIT_DIR instead of hardcoded ".git"
28 - hide our private tags somewhere smarter
29 - find a way to make "cat *patches | patch" safe even when patchfiles are missing newlines
33 Add print in front of the shell commands invoked via backticks.
41 use File
::Temp
qw(tempfile);
42 use File
::Path
qw(mkpath);
43 use File
::Basename
qw(basename dirname);
44 use String
::ShellQuote
;
48 use POSIX
qw(strftime dup2);
49 use Data
::Dumper qw
/ Dumper /;
52 $SIG{'PIPE'}="IGNORE";
55 our($opt_h,$opt_v, $opt_T,
60 Usage: ${\basename $0} # fetch/update GIT from Arch
61 [ -h ] [ -v ] [ -T ] [ -t tempdir ]
62 repository/arch-branch [ repository/arch-branch] ...
67 getopts
("Thvt:") or usage
();
70 @ARGV >= 1 or usage
();
71 my @arch_roots = @ARGV;
75 $tmp .= '/git-archimport/';
77 my @psets = (); # the collection
78 my %psets = (); # the collection, by name
80 my %rptags = (); # my reverse private tags
81 # to map a SHA1 to a commitid
83 foreach my $root (@arch_roots) {
84 my ($arepo, $abranch) = split(m!/!, $root);
85 open ABROWSE
, "tla abrowse -f -A $arepo --desc --merges $abranch |"
86 or die "Problems with tla abrowse: $!";
88 my %ps = (); # the current one
95 # first record padded w 8 spaces
98 # store the record we just captured
100 my %temp = %ps; # break references
101 push (@psets, \
%temp);
102 $psets{$temp{id
}} = \
%temp;
106 my ($id, $type) = split(m/\s{3}/, $_);
111 if ($type =~ m/^\(simple changeset\)/) {
113 } elsif ($type eq '(initial import)') {
115 } elsif ($type =~ m/^\(tag revision of (.+)\)/) {
119 warn "Unknown type $type";
125 # 10 leading spaces or more
126 # indicate commit metadata
129 if ($lastseen eq 'id' && m/^\d{4}-\d{2}-\d{2}/) {
131 my ($date, $authoremail) = split(m/\s{2,}/, $_);
133 $ps{date
} =~ s/\bGMT$//; # strip off trailign GMT
134 if ($ps{date
} =~ m/\b\w+$/) {
135 warn 'Arch dates not in GMT?! - imported dates will be wrong';
138 $authoremail =~ m/^(.+)\s(\S+)$/;
144 } elsif ($lastseen eq 'date') {
145 # the only hint is position
146 # subject is after date
150 } elsif ($lastseen eq 'subj' && $_ eq 'merges in:') {
152 $lastseen = 'merges';
154 } elsif ($lastseen eq 'merges' && s/^\s{2}//) {
155 push (@
{$ps{merges
}}, $_);
157 warn 'more metadata after merges!?';
164 my %temp = %ps; # break references
165 push (@psets, \
%temp);
166 $psets{ $temp{id
} } = \
%temp;
170 } # end foreach $root
172 ## Order patches by time
173 @psets = sort {$a->{date
}.$b->{id
} cmp $b->{date
}.$b->{id
}} @psets;
175 #print Dumper \@psets;
178 ## TODO cleanup irrelevant patches
179 ## and put an initial import
182 unless (-d
'.git') { # initial import
183 if ($psets[0]{type
} eq 'i' || $psets[0]{type
} eq 't') {
184 print "Starting import from $psets[0]{id}\n";
189 die "Need to start from an import or a tag -- cannot use $psets[0]{id}";
191 } else { # progressing an import
193 opendir(DIR
, ".git/archimport/tags")
194 || die "can't opendir: $!";
195 while (my $file = readdir(DIR
)) {
196 # skip non-interesting-files
197 next unless -f
".git/archimport/tags/$file";
198 next if $file =~ m/--base-0$/; # don't care for base-0
199 my $sha = ptag
($file);
201 # reconvert the 3rd '--' sequence from the end
203 # $file = reverse $file;
204 # $file =~ s!^(.+?--.+?--.+?--.+?)--(.+)$!$1/$2!;
205 # $file = reverse $file;
206 $rptags{$sha} = $file;
212 foreach my $ps (@psets) {
214 $ps->{branch
} = branchname
($ps->{id
});
217 # ensure we have a clean state
219 if (`git diff-files`) {
220 die "Unclean tree when about to process $ps->{id} " .
221 " - did we fail to commit cleanly before?";
226 # skip commits already in repo
228 if (ptag
($ps->{id
})) {
229 $opt_v && print "Skipping already imported: $ps->{id}\n";
234 # create the branch if needed
236 if ($ps->{type
} eq 'i' && !$import) {
237 die "Should not have more than one 'Initial import' per GIT import: $ps->{id}";
240 unless ($import) { # skip for import
241 if ( -e
".git/refs/heads/$ps->{branch}") {
242 # we know about this branch
243 `git checkout $ps->{branch}`;
245 # new branch! we need to verify a few things
246 die "Branch on a non-tag!" unless $ps->{type
} eq 't';
247 my $branchpoint = ptag
($ps->{tag
});
248 die "Tagging from unknown id unsupported: $ps->{tag}"
251 # find where we are supposed to branch from
252 `git checkout -b $ps->{branch} $branchpoint`;
254 # If we trust Arch with the fact that this is just
255 # a tag, and it does not affect the state of the tree
256 # then we just tag and move on
257 tag
($ps->{id
}, $branchpoint);
258 ptag
($ps->{id
}, $branchpoint);
259 print " * Tagged $ps->{id} at $branchpoint\n";
266 # Apply the import/changeset/merge into the working tree
268 if ($ps->{type
} eq 'i' || $ps->{type
} eq 't') {
269 apply_import
($ps) or die $!;
271 } elsif ($ps->{type
} eq 's') {
276 # prepare update git's index, based on what arch knows
277 # about the pset, resolve parents, etc
281 my $commitlog = `tla cat-archive-log -A $ps->{repo} $ps->{id}`;
282 die "Error in cat-archive-log: $!" if $?
;
284 # parselog will git-add/rm files
285 # and generally prepare things for the commit
286 # NOTE: parselog will shell-quote filenames!
287 my ($sum, $msg, $add, $del, $mod, $ren) = parselog
($commitlog);
288 my $logmessage = "$sum\n$msg";
291 # imports don't give us good info
292 # on added files. Shame on them
293 if ($ps->{type
} eq 'i' || $ps->{type
} eq 't') {
294 `find . -type f -print0 | grep -zv '^./.git' | xargs -0 -l100 git-update-index --add`;
295 `git-ls-files --deleted -z | xargs --no-run-if-empty -0 -l100 git-update-index --remove`;
300 my @slice = splice(@
$add, 0, 100);
301 my $slice = join(' ', @slice);
302 `git-update-index --add $slice`;
303 die "Error in git-update-index --add: $!" if $?
;
307 foreach my $file (@
$del) {
308 unlink $file or die "Problems deleting $file : $!";
311 my @slice = splice(@
$del, 0, 100);
312 my $slice = join(' ', @slice);
313 `git-update-index --remove $slice`;
314 die "Error in git-update-index --remove: $!" if $?
;
317 if (@
$ren) { # renamed
319 die "Odd number of entries in rename!?";
323 my $from = pop @
$ren;
326 unless (-d dirname
($to)) {
327 mkpath
(dirname
($to)); # will die on err
329 #print "moving $from $to";
331 die "Error renaming $from $to : $!" if $?
;
332 `git-update-index --remove $from`;
333 die "Error in git-update-index --remove: $!" if $?
;
334 `git-update-index --add $to`;
335 die "Error in git-update-index --add: $!" if $?
;
339 if (@
$mod) { # must be _after_ renames
341 my @slice = splice(@
$mod, 0, 100);
342 my $slice = join(' ', @slice);
343 `git-update-index $slice`;
344 die "Error in git-update-index: $!" if $?
;
348 # warn "errors when running git-update-index! $!";
349 $tree = `git-write-tree`;
350 die "cannot write tree $!" if $?
;
358 if ( -e
".git/refs/heads/$ps->{branch}") {
359 if (open HEAD
, "<.git/refs/heads/$ps->{branch}") {
365 if ($ps->{type
} eq 's') {
366 warn "Could not find the right head for the branch $ps->{branch}";
372 push @par, find_parents
($ps);
374 my $par = join (' ', @par);
377 # Commit, tag and clean state
380 $ENV{GIT_AUTHOR_NAME
} = $ps->{author
};
381 $ENV{GIT_AUTHOR_EMAIL
} = $ps->{email
};
382 $ENV{GIT_AUTHOR_DATE
} = $ps->{date
};
383 $ENV{GIT_COMMITTER_NAME
} = $ps->{author
};
384 $ENV{GIT_COMMITTER_EMAIL
} = $ps->{email
};
385 $ENV{GIT_COMMITTER_DATE
} = $ps->{date
};
387 my ($pid, $commit_rh, $commit_wh);
388 $commit_rh = 'commit_rh';
389 $commit_wh = 'commit_wh';
391 $pid = open2
(*READER
, *WRITER
, "git-commit-tree $tree $par")
393 print WRITER
$logmessage; # write
395 my $commitid = <READER
>; # read
398 waitpid $pid,0; # close;
400 if (length $commitid != 40) {
401 die "Something went wrong with the commit! $! $commitid";
406 open HEAD
, ">.git/refs/heads/$ps->{branch}";
407 print HEAD
$commitid;
409 unlink ('.git/HEAD');
410 symlink("refs/heads/$ps->{branch}",".git/HEAD");
413 ptag
($ps->{id
}, $commitid); # private tag
414 if ($opt_T || $ps->{type
} eq 't' || $ps->{type
} eq 'i') {
415 tag
($ps->{id
}, $commitid);
417 print " * Committed $ps->{id}\n";
418 print " + tree $tree\n";
419 print " + commit $commitid\n";
420 $opt_v && print " + commit date is $ps->{date} \n";
421 $opt_v && print " + parents: $par \n";
427 my @parts = split(m/--/, $id);
428 return join('--', @parts[0..1]);
433 my $bname = branchname
($ps->{id
});
437 `tla get -s --no-pristine -A $ps->{repo} $ps->{id} $tmp/import`;
438 die "Cannot get import: $!" if $?
;
439 `rsync -v --archive --delete --exclude '.git' --exclude '.arch-ids' --exclude '{arch}' $tmp/import/* ./`;
440 die "Cannot rsync import:$!" if $?
;
442 `rm -fr $tmp/import`;
443 die "Cannot remove tempdir: $!" if $?
;
455 `tla get-changeset -A $ps->{repo} $ps->{id} $tmp/changeset`;
456 die "Cannot get changeset: $!" if $?
;
459 if (`find $tmp/changeset/patches -type f -name '*.patch'`) {
460 # this can be sped up considerably by doing
461 # (find | xargs cat) | patch
462 # but that cna get mucked up by patches
463 # with missing trailing newlines or the standard
464 # 'missing newline' flag in the patch - possibly
465 # produced with an old/buggy diff.
466 # slow and safe, we invoke patch once per patchfile
467 `find $tmp/changeset/patches -type f -name '*.patch' -print0 | grep -zv '{arch}' | xargs -iFILE -0 --no-run-if-empty patch -p1 --forward -iFILE`;
468 die "Problem applying patches! $!" if $?
;
471 # apply changed binary files
472 if (my @modified = `find $tmp/changeset/patches -type f -name '*.modified'`) {
473 foreach my $mod (@modified) {
476 $orig =~ s/\.modified$//; # lazy
477 $orig =~ s!^\Q$tmp\E/changeset/patches/!!;
478 #print "rsync -p '$mod' '$orig'";
479 `rsync -p $mod ./$orig`;
480 die "Problem applying binary changes! $!" if $?
;
485 `rsync --archive --exclude '.git' --exclude '.arch-ids' --exclude '{arch}' $tmp/changeset/new-files-archive/* ./`;
487 # deleted files are hinted from the commitlog processing
489 `rm -fr $tmp/changeset`;
494 # A log entry looks like
495 # Revision: moodle-org--moodle--1.3.3--patch-15
496 # Archive: arch-eduforge@catalyst.net.nz--2004
497 # Creator: Penny Leach <penny@catalyst.net.nz>
498 # Date: Wed May 25 14:15:34 NZST 2005
499 # Standard-date: 2005-05-25 02:15:34 GMT
500 # New-files: lang/de/.arch-ids/block_glossary_random.php.id
501 # lang/de/.arch-ids/block_html.php.id
502 # New-directories: lang/de/help/questionnaire
503 # lang/de/help/questionnaire/.arch-ids
504 # Renamed-files: .arch-ids/db_sears.sql.id db/.arch-ids/db_sears.sql.id
505 # db_sears.sql db/db_sears.sql
506 # Removed-files: lang/be/docs/.arch-ids/release.html.id
507 # lang/be/docs/.arch-ids/releaseold.html.id
508 # Modified-files: admin/cron.php admin/delete.php
509 # admin/editor.html backup/lib.php backup/restore.php
510 # New-patches: arch-eduforge@catalyst.net.nz--2004/moodle-org--moodle--1.3.3--patch-15
511 # Summary: Updating to latest from MOODLE_14_STABLE (1.4.5+)
514 # Updating yadda tadda tadda madda
519 my (@add, @del, @mod, @ren, @kw, $sum, $msg );
521 if ($log =~ m/(?:\n|^)New-files:(.*?)(?=\n\w)/s ) {
523 @add = split(m/\s+/s, $files);
526 if ($log =~ m/(?:\n|^)Removed-files:(.*?)(?=\n\w)/s ) {
528 @del = split(m/\s+/s, $files);
531 if ($log =~ m/(?:\n|^)Modified-files:(.*?)(?=\n\w)/s ) {
533 @mod = split(m/\s+/s, $files);
536 if ($log =~ m/(?:\n|^)Renamed-files:(.*?)(?=\n\w)/s ) {
538 @ren = split(m/\s+/s, $files);
542 if ($log =~ m/^Summary:(.+?)$/m ) {
549 if ($log =~ m/\n\n(.+)$/s) {
557 foreach my $ref ( (\
@add, \
@del, \
@mod, \
@ren) ) {
559 while (my $t = pop @
$ref) {
560 next unless length ($t);
561 next if $t =~ m!\{arch\}/!;
562 next if $t =~ m!\.arch-ids/!;
563 next if $t =~ m!\.arch-inventory$!;
564 push (@tmp, shell_quote
($t));
569 #print Dumper [$sum, $msg, \@add, \@del, \@mod, \@ren];
570 return ($sum, $msg, \
@add, \
@del, \
@mod, \
@ren);
575 my ($tag, $commit) = @_;
577 $tag = shell_quote
($tag);
580 open(C
,">.git/refs/tags/$tag")
581 or die "Cannot create tag $tag: $!\n";
583 or die "Cannot write tag $tag: $!\n";
585 or die "Cannot write tag $tag: $!\n";
586 print " * Created tag ' $tag' on '$commit'\n" if $opt_v;
588 open(C
,"<.git/refs/tags/$tag")
589 or die "Cannot read tag $tag: $!\n";
592 die "Error reading tag $tag: $!\n" unless length $commit == 40;
594 or die "Cannot read tag $tag: $!\n";
599 # write/read a private tag
600 # reads fail softly if the tag isn't there
602 my ($tag, $commit) = @_;
604 $tag = shell_quote
($tag);
606 unless (-d
'.git/archimport/tags') {
607 mkpath
('.git/archimport/tags');
610 if ($commit) { # write
611 open(C
,">.git/archimport/tags/$tag")
612 or die "Cannot create tag $tag: $!\n";
614 or die "Cannot write tag $tag: $!\n";
616 or die "Cannot write tag $tag: $!\n";
617 $rptags{$commit} = $tag
618 unless $tag =~ m/--base-0$/;
620 # if the tag isn't there, return 0
621 unless ( -s
".git/archimport/tags/$tag") {
624 open(C
,"<.git/archimport/tags/$tag")
625 or die "Cannot read tag $tag: $!\n";
628 die "Error reading tag $tag: $!\n" unless length $commit == 40;
630 or die "Cannot read tag $tag: $!\n";
631 unless (defined $rptags{$commit}) {
632 $rptags{$commit} = $tag;
640 # Identify what branches are merging into me
641 # and whether we are fully merged
642 # git-merge-base <headsha> <headsha> should tell
643 # me what the base of the merge should be
647 my %branches; # holds an arrayref per branch
648 # the arrayref contains a list of
649 # merged patches between the base
650 # of the merge and the current head
652 my @parents; # parents found for this commit
654 # simple loop to split the merges
656 foreach my $merge (@
{$ps->{merges
}}) {
657 my $branch = branchname
($merge);
658 unless (defined $branches{$branch} ){
659 $branches{$branch} = [];
661 push @
{$branches{$branch}}, $merge;
665 # foreach branch find a merge base and walk it to the
666 # head where we are, collecting the merged patchsets that
667 # Arch has recorded. Keep that in @have
668 # Compare that with the commits on the other branch
669 # between merge-base and the tip of the branch (@need)
670 # and see if we have a series of consecutive patches
671 # starting from the merge base. The tip of the series
672 # of consecutive patches merged is our new parent for
675 foreach my $branch (keys %branches) {
676 my $mergebase = `git-merge-base $branch $ps->{branch}`;
677 die "Cannot find merge base for $branch and $ps->{branch}" if $?
;
680 # now walk up to the mergepoint collecting what patches we have
681 my $branchtip = git_rev_parse
($ps->{branch
});
682 my @ancestors = `git-rev-list --merge-order $branchtip ^$mergebase`;
683 my %have; # collected merges this branch has
684 foreach my $merge (@
{$ps->{merges
}}) {
688 foreach my $par (@ancestors) {
689 $par = commitid2pset
($par);
690 if (defined $par->{merges
}) {
691 foreach my $merge (@
{$par->{merges
}}) {
692 $ancestorshave{$merge}=1;
696 # print "++++ Merges in $ps->{id} are....\n";
697 # my @have = sort keys %have; print Dumper(\@have);
699 # merge what we have with what ancestors have
700 %have = (%have, %ancestorshave);
702 # see what the remote branch has - these are the merges we
703 # will want to have in a consecutive series from the mergebase
704 my $otherbranchtip = git_rev_parse
($branch);
705 my @needraw = `git-rev-list --merge-order $otherbranchtip ^$mergebase`;
707 foreach my $needps (@needraw) { # get the psets
708 $needps = commitid2pset
($needps);
709 # git-rev-list will also
710 # list commits merged in via earlier
711 # merges. we are only interested in commits
712 # from the branch we're looking at
713 if ($branch eq $needps->{branch
}) {
714 push @need, $needps->{id
};
718 # print "++++ Merges from $branch we want are....\n";
719 # print Dumper(\@need);
722 while (my $needed_commit = pop @need) {
723 if ($have{$needed_commit}) {
724 $newparent = $needed_commit;
726 last; # break out of the while
730 push @parents, $newparent;
734 } # end foreach branch
736 # prune redundant parents
738 foreach my $p (@parents) {
741 foreach my $p (@parents) {
742 next unless exists $psets{$p}{merges
};
743 next unless ref $psets{$p}{merges
};
744 my @merges = @
{$psets{$p}{merges
}};
745 foreach my $merge (@merges) {
746 if ($parents{$merge}) {
747 delete $parents{$merge};
751 @parents = keys %parents;
752 @parents = map { " -p " . ptag
($_) } @parents;
758 my $val = `git-rev-parse $name`;
759 die "Error: git-rev-parse $name" if $?
;
764 # resolve a SHA1 to a known patchset
766 my $commitid = shift;
768 my $name = $rptags{$commitid}
769 || die "Cannot find reverse tag mapping for $commitid";
770 # the keys in %rptag are slightly munged; unmunge
771 # reconvert the 3rd '--' sequence from the end
773 $name = reverse $name;
774 $name =~ s!^(.+?--.+?--.+?--.+?)--(.+)$!$1/$2!;
775 $name = reverse $name;
776 my $ps = $psets{$name}
777 || (print Dumper
(sort keys %psets)) && die "Cannot find patchset for $name";