]>
Commit | Line | Data |
---|---|---|
d3968363 ML |
1 | #!/usr/bin/perl -w |
2 | # | |
3 | # This tool is copyright (c) 2005, Martin Langhoff. | |
4 | # It is released under the Gnu Public License, version 2. | |
5 | # | |
6 | # The basic idea is to walk the output of tla abrowse, | |
7 | # fetch the changesets and apply them. | |
8 | # | |
241b5967 | 9 | |
d3968363 ML |
10 | =head1 Invocation |
11 | ||
241b5967 | 12 | git-archimport [ -h ] [ -v ] [ -T ] [ -t tempdir ] <archive>/<branch> [ <archive>/<branch> ] |
d3968363 | 13 | |
241b5967 ML |
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. | |
d3968363 | 19 | |
241b5967 | 20 | See man (1) git-archimport for more details. |
d3968363 | 21 | |
241b5967 | 22 | =head1 TODO |
d3968363 | 23 | |
241b5967 | 24 | - create tag objects instead of ref tags |
d3968363 | 25 | - audit shell-escaping of filenames |
241b5967 ML |
26 | - hide our private tags somewhere smarter |
27 | - find a way to make "cat *patches | patch" safe even when patchfiles are missing newlines | |
d3968363 ML |
28 | |
29 | =head1 Devel tricks | |
30 | ||
31 | Add print in front of the shell commands invoked via backticks. | |
32 | ||
33 | =cut | |
34 | ||
35 | use strict; | |
36 | use warnings; | |
37 | use Getopt::Std; | |
38 | use File::Spec; | |
127bf00f | 39 | use File::Temp qw(tempfile tempdir); |
d3968363 ML |
40 | use File::Path qw(mkpath); |
41 | use File::Basename qw(basename dirname); | |
42 | use String::ShellQuote; | |
43 | use Time::Local; | |
44 | use IO::Socket; | |
45 | use IO::Pipe; | |
46 | use POSIX qw(strftime dup2); | |
47 | use Data::Dumper qw/ Dumper /; | |
48 | use IPC::Open2; | |
49 | ||
50 | $SIG{'PIPE'}="IGNORE"; | |
51 | $ENV{'TZ'}="UTC"; | |
52 | ||
1d4710d0 ML |
53 | my $git_dir = $ENV{"GIT_DIR"} || ".git"; |
54 | $ENV{"GIT_DIR"} = $git_dir; | |
55 | ||
d3968363 | 56 | our($opt_h,$opt_v, $opt_T, |
3292ae47 | 57 | $opt_C,$opt_t); |
d3968363 ML |
58 | |
59 | sub usage() { | |
60 | print STDERR <<END; | |
61 | Usage: ${\basename $0} # fetch/update GIT from Arch | |
241b5967 | 62 | [ -h ] [ -v ] [ -T ] [ -t tempdir ] |
d3968363 ML |
63 | repository/arch-branch [ repository/arch-branch] ... |
64 | END | |
65 | exit(1); | |
66 | } | |
67 | ||
241b5967 | 68 | getopts("Thvt:") or usage(); |
d3968363 ML |
69 | usage if $opt_h; |
70 | ||
71 | @ARGV >= 1 or usage(); | |
72 | my @arch_roots = @ARGV; | |
73 | ||
127bf00f ML |
74 | my ($tmpdir, $tmpdirname) = tempdir('git-archimport-XXXXXX', TMPDIR => 1, CLEANUP => 1); |
75 | my $tmp = $opt_t || 1; | |
76 | $tmp = tempdir('git-archimport-XXXXXX', TMPDIR => 1, CLEANUP => 1); | |
77 | $opt_v && print "+ Using $tmp as temporary directory\n"; | |
d3968363 | 78 | |
d3968363 | 79 | my @psets = (); # the collection |
b779d5f0 ML |
80 | my %psets = (); # the collection, by name |
81 | ||
82 | my %rptags = (); # my reverse private tags | |
83 | # to map a SHA1 to a commitid | |
d3968363 ML |
84 | |
85 | foreach my $root (@arch_roots) { | |
86 | my ($arepo, $abranch) = split(m!/!, $root); | |
87 | open ABROWSE, "tla abrowse -f -A $arepo --desc --merges $abranch |" | |
88 | or die "Problems with tla abrowse: $!"; | |
89 | ||
90 | my %ps = (); # the current one | |
91 | my $mode = ''; | |
92 | my $lastseen = ''; | |
93 | ||
94 | while (<ABROWSE>) { | |
95 | chomp; | |
96 | ||
97 | # first record padded w 8 spaces | |
98 | if (s/^\s{8}\b//) { | |
99 | ||
100 | # store the record we just captured | |
101 | if (%ps) { | |
102 | my %temp = %ps; # break references | |
103 | push (@psets, \%temp); | |
b779d5f0 | 104 | $psets{$temp{id}} = \%temp; |
d3968363 ML |
105 | %ps = (); |
106 | } | |
107 | ||
108 | my ($id, $type) = split(m/\s{3}/, $_); | |
109 | $ps{id} = $id; | |
110 | $ps{repo} = $arepo; | |
111 | ||
112 | # deal with types | |
113 | if ($type =~ m/^\(simple changeset\)/) { | |
114 | $ps{type} = 's'; | |
115 | } elsif ($type eq '(initial import)') { | |
116 | $ps{type} = 'i'; | |
117 | } elsif ($type =~ m/^\(tag revision of (.+)\)/) { | |
118 | $ps{type} = 't'; | |
119 | $ps{tag} = $1; | |
120 | } else { | |
121 | warn "Unknown type $type"; | |
122 | } | |
123 | $lastseen = 'id'; | |
124 | } | |
125 | ||
126 | if (s/^\s{10}//) { | |
127 | # 10 leading spaces or more | |
128 | # indicate commit metadata | |
129 | ||
130 | # date & author | |
131 | if ($lastseen eq 'id' && m/^\d{4}-\d{2}-\d{2}/) { | |
132 | ||
133 | my ($date, $authoremail) = split(m/\s{2,}/, $_); | |
134 | $ps{date} = $date; | |
135 | $ps{date} =~ s/\bGMT$//; # strip off trailign GMT | |
136 | if ($ps{date} =~ m/\b\w+$/) { | |
137 | warn 'Arch dates not in GMT?! - imported dates will be wrong'; | |
138 | } | |
139 | ||
140 | $authoremail =~ m/^(.+)\s(\S+)$/; | |
141 | $ps{author} = $1; | |
142 | $ps{email} = $2; | |
143 | ||
144 | $lastseen = 'date'; | |
145 | ||
146 | } elsif ($lastseen eq 'date') { | |
147 | # the only hint is position | |
148 | # subject is after date | |
149 | $ps{subj} = $_; | |
150 | $lastseen = 'subj'; | |
151 | ||
152 | } elsif ($lastseen eq 'subj' && $_ eq 'merges in:') { | |
153 | $ps{merges} = []; | |
154 | $lastseen = 'merges'; | |
155 | ||
156 | } elsif ($lastseen eq 'merges' && s/^\s{2}//) { | |
157 | push (@{$ps{merges}}, $_); | |
158 | } else { | |
159 | warn 'more metadata after merges!?'; | |
160 | } | |
161 | ||
162 | } | |
163 | } | |
164 | ||
165 | if (%ps) { | |
166 | my %temp = %ps; # break references | |
b779d5f0 ML |
167 | push (@psets, \%temp); |
168 | $psets{ $temp{id} } = \%temp; | |
d3968363 ML |
169 | %ps = (); |
170 | } | |
171 | close ABROWSE; | |
172 | } # end foreach $root | |
173 | ||
174 | ## Order patches by time | |
175 | @psets = sort {$a->{date}.$b->{id} cmp $b->{date}.$b->{id}} @psets; | |
176 | ||
177 | #print Dumper \@psets; | |
178 | ||
179 | ## | |
180 | ## TODO cleanup irrelevant patches | |
181 | ## and put an initial import | |
182 | ## or a full tag | |
3292ae47 | 183 | my $import = 0; |
1d4710d0 | 184 | unless (-d $git_dir) { # initial import |
d3968363 ML |
185 | if ($psets[0]{type} eq 'i' || $psets[0]{type} eq 't') { |
186 | print "Starting import from $psets[0]{id}\n"; | |
3292ae47 ML |
187 | `git-init-db`; |
188 | die $! if $?; | |
189 | $import = 1; | |
d3968363 ML |
190 | } else { |
191 | die "Need to start from an import or a tag -- cannot use $psets[0]{id}"; | |
192 | } | |
b779d5f0 ML |
193 | } else { # progressing an import |
194 | # load the rptags | |
1d4710d0 | 195 | opendir(DIR, "$git_dir/archimport/tags") |
b779d5f0 ML |
196 | || die "can't opendir: $!"; |
197 | while (my $file = readdir(DIR)) { | |
198 | # skip non-interesting-files | |
1d4710d0 | 199 | next unless -f "$git_dir/archimport/tags/$file"; |
b779d5f0 ML |
200 | next if $file =~ m/--base-0$/; # don't care for base-0 |
201 | my $sha = ptag($file); | |
202 | chomp $sha; | |
203 | # reconvert the 3rd '--' sequence from the end | |
204 | # into a slash | |
205 | # $file = reverse $file; | |
206 | # $file =~ s!^(.+?--.+?--.+?--.+?)--(.+)$!$1/$2!; | |
207 | # $file = reverse $file; | |
208 | $rptags{$sha} = $file; | |
209 | } | |
210 | closedir DIR; | |
d3968363 ML |
211 | } |
212 | ||
3292ae47 | 213 | # process patchsets |
d3968363 ML |
214 | foreach my $ps (@psets) { |
215 | ||
216 | $ps->{branch} = branchname($ps->{id}); | |
217 | ||
218 | # | |
219 | # ensure we have a clean state | |
220 | # | |
221 | if (`git diff-files`) { | |
222 | die "Unclean tree when about to process $ps->{id} " . | |
223 | " - did we fail to commit cleanly before?"; | |
224 | } | |
225 | die $! if $?; | |
226 | ||
3292ae47 ML |
227 | # |
228 | # skip commits already in repo | |
229 | # | |
230 | if (ptag($ps->{id})) { | |
231 | $opt_v && print "Skipping already imported: $ps->{id}\n"; | |
232 | next; | |
233 | } | |
234 | ||
d3968363 ML |
235 | # |
236 | # create the branch if needed | |
237 | # | |
3292ae47 ML |
238 | if ($ps->{type} eq 'i' && !$import) { |
239 | die "Should not have more than one 'Initial import' per GIT import: $ps->{id}"; | |
d3968363 ML |
240 | } |
241 | ||
3292ae47 | 242 | unless ($import) { # skip for import |
1d4710d0 | 243 | if ( -e "$git_dir/refs/heads/$ps->{branch}") { |
d3968363 ML |
244 | # we know about this branch |
245 | `git checkout $ps->{branch}`; | |
246 | } else { | |
247 | # new branch! we need to verify a few things | |
248 | die "Branch on a non-tag!" unless $ps->{type} eq 't'; | |
249 | my $branchpoint = ptag($ps->{tag}); | |
250 | die "Tagging from unknown id unsupported: $ps->{tag}" | |
251 | unless $branchpoint; | |
252 | ||
253 | # find where we are supposed to branch from | |
254 | `git checkout -b $ps->{branch} $branchpoint`; | |
52586ecb ML |
255 | |
256 | # If we trust Arch with the fact that this is just | |
257 | # a tag, and it does not affect the state of the tree | |
258 | # then we just tag and move on | |
259 | tag($ps->{id}, $branchpoint); | |
260 | ptag($ps->{id}, $branchpoint); | |
261 | print " * Tagged $ps->{id} at $branchpoint\n"; | |
262 | next; | |
d3968363 ML |
263 | } |
264 | die $! if $?; | |
265 | } | |
266 | ||
d3968363 ML |
267 | # |
268 | # Apply the import/changeset/merge into the working tree | |
269 | # | |
270 | if ($ps->{type} eq 'i' || $ps->{type} eq 't') { | |
d3968363 | 271 | apply_import($ps) or die $!; |
3292ae47 | 272 | $import=0; |
d3968363 ML |
273 | } elsif ($ps->{type} eq 's') { |
274 | apply_cset($ps); | |
275 | } | |
276 | ||
277 | # | |
278 | # prepare update git's index, based on what arch knows | |
279 | # about the pset, resolve parents, etc | |
280 | # | |
281 | my $tree; | |
282 | ||
283 | my $commitlog = `tla cat-archive-log -A $ps->{repo} $ps->{id}`; | |
284 | die "Error in cat-archive-log: $!" if $?; | |
285 | ||
286 | # parselog will git-add/rm files | |
287 | # and generally prepare things for the commit | |
288 | # NOTE: parselog will shell-quote filenames! | |
289 | my ($sum, $msg, $add, $del, $mod, $ren) = parselog($commitlog); | |
290 | my $logmessage = "$sum\n$msg"; | |
291 | ||
292 | ||
293 | # imports don't give us good info | |
294 | # on added files. Shame on them | |
295 | if ($ps->{type} eq 'i' || $ps->{type} eq 't') { | |
1d4710d0 | 296 | `find . -type f -print0 | grep -zv '^./$git_dir' | xargs -0 -l100 git-update-index --add`; |
215a7ad1 | 297 | `git-ls-files --deleted -z | xargs --no-run-if-empty -0 -l100 git-update-index --remove`; |
d3968363 ML |
298 | } |
299 | ||
300 | if (@$add) { | |
301 | while (@$add) { | |
302 | my @slice = splice(@$add, 0, 100); | |
303 | my $slice = join(' ', @slice); | |
215a7ad1 JH |
304 | `git-update-index --add $slice`; |
305 | die "Error in git-update-index --add: $!" if $?; | |
d3968363 ML |
306 | } |
307 | } | |
308 | if (@$del) { | |
309 | foreach my $file (@$del) { | |
310 | unlink $file or die "Problems deleting $file : $!"; | |
311 | } | |
312 | while (@$del) { | |
313 | my @slice = splice(@$del, 0, 100); | |
314 | my $slice = join(' ', @slice); | |
215a7ad1 JH |
315 | `git-update-index --remove $slice`; |
316 | die "Error in git-update-index --remove: $!" if $?; | |
d3968363 ML |
317 | } |
318 | } | |
319 | if (@$ren) { # renamed | |
320 | if (@$ren % 2) { | |
321 | die "Odd number of entries in rename!?"; | |
322 | } | |
323 | ; | |
324 | while (@$ren) { | |
325 | my $from = pop @$ren; | |
326 | my $to = pop @$ren; | |
327 | ||
328 | unless (-d dirname($to)) { | |
329 | mkpath(dirname($to)); # will die on err | |
330 | } | |
331 | #print "moving $from $to"; | |
332 | `mv $from $to`; | |
333 | die "Error renaming $from $to : $!" if $?; | |
215a7ad1 JH |
334 | `git-update-index --remove $from`; |
335 | die "Error in git-update-index --remove: $!" if $?; | |
336 | `git-update-index --add $to`; | |
337 | die "Error in git-update-index --add: $!" if $?; | |
d3968363 ML |
338 | } |
339 | ||
340 | } | |
341 | if (@$mod) { # must be _after_ renames | |
342 | while (@$mod) { | |
343 | my @slice = splice(@$mod, 0, 100); | |
344 | my $slice = join(' ', @slice); | |
215a7ad1 JH |
345 | `git-update-index $slice`; |
346 | die "Error in git-update-index: $!" if $?; | |
d3968363 ML |
347 | } |
348 | } | |
349 | ||
215a7ad1 | 350 | # warn "errors when running git-update-index! $!"; |
d3968363 ML |
351 | $tree = `git-write-tree`; |
352 | die "cannot write tree $!" if $?; | |
353 | chomp $tree; | |
354 | ||
355 | ||
356 | # | |
357 | # Who's your daddy? | |
358 | # | |
359 | my @par; | |
1d4710d0 ML |
360 | if ( -e "$git_dir/refs/heads/$ps->{branch}") { |
361 | if (open HEAD, "<$git_dir/refs/heads/$ps->{branch}") { | |
d3968363 ML |
362 | my $p = <HEAD>; |
363 | close HEAD; | |
364 | chomp $p; | |
365 | push @par, '-p', $p; | |
366 | } else { | |
367 | if ($ps->{type} eq 's') { | |
368 | warn "Could not find the right head for the branch $ps->{branch}"; | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
b779d5f0 ML |
373 | if ($ps->{merges}) { |
374 | push @par, find_parents($ps); | |
375 | } | |
d3968363 ML |
376 | my $par = join (' ', @par); |
377 | ||
378 | # | |
379 | # Commit, tag and clean state | |
380 | # | |
381 | $ENV{TZ} = 'GMT'; | |
382 | $ENV{GIT_AUTHOR_NAME} = $ps->{author}; | |
383 | $ENV{GIT_AUTHOR_EMAIL} = $ps->{email}; | |
384 | $ENV{GIT_AUTHOR_DATE} = $ps->{date}; | |
385 | $ENV{GIT_COMMITTER_NAME} = $ps->{author}; | |
386 | $ENV{GIT_COMMITTER_EMAIL} = $ps->{email}; | |
387 | $ENV{GIT_COMMITTER_DATE} = $ps->{date}; | |
388 | ||
389 | my ($pid, $commit_rh, $commit_wh); | |
390 | $commit_rh = 'commit_rh'; | |
391 | $commit_wh = 'commit_wh'; | |
392 | ||
393 | $pid = open2(*READER, *WRITER, "git-commit-tree $tree $par") | |
394 | or die $!; | |
395 | print WRITER $logmessage; # write | |
396 | close WRITER; | |
397 | my $commitid = <READER>; # read | |
398 | chomp $commitid; | |
399 | close READER; | |
400 | waitpid $pid,0; # close; | |
401 | ||
402 | if (length $commitid != 40) { | |
403 | die "Something went wrong with the commit! $! $commitid"; | |
404 | } | |
405 | # | |
406 | # Update the branch | |
407 | # | |
1d4710d0 | 408 | open HEAD, ">$git_dir/refs/heads/$ps->{branch}"; |
d3968363 ML |
409 | print HEAD $commitid; |
410 | close HEAD; | |
1d4710d0 ML |
411 | unlink ("$git_dir/HEAD"); |
412 | symlink("refs/heads/$ps->{branch}","$git_dir/HEAD"); | |
d3968363 ML |
413 | |
414 | # tag accordingly | |
415 | ptag($ps->{id}, $commitid); # private tag | |
416 | if ($opt_T || $ps->{type} eq 't' || $ps->{type} eq 'i') { | |
417 | tag($ps->{id}, $commitid); | |
418 | } | |
419 | print " * Committed $ps->{id}\n"; | |
420 | print " + tree $tree\n"; | |
421 | print " + commit $commitid\n"; | |
b779d5f0 ML |
422 | $opt_v && print " + commit date is $ps->{date} \n"; |
423 | $opt_v && print " + parents: $par \n"; | |
d3968363 ML |
424 | } |
425 | ||
426 | sub branchname { | |
427 | my $id = shift; | |
428 | $id =~ s#^.+?/##; | |
429 | my @parts = split(m/--/, $id); | |
430 | return join('--', @parts[0..1]); | |
431 | } | |
432 | ||
433 | sub apply_import { | |
434 | my $ps = shift; | |
435 | my $bname = branchname($ps->{id}); | |
436 | ||
437 | `mkdir -p $tmp`; | |
438 | ||
439 | `tla get -s --no-pristine -A $ps->{repo} $ps->{id} $tmp/import`; | |
440 | die "Cannot get import: $!" if $?; | |
1d4710d0 | 441 | `rsync -v --archive --delete --exclude '$git_dir' --exclude '.arch-ids' --exclude '{arch}' $tmp/import/* ./`; |
d3968363 ML |
442 | die "Cannot rsync import:$!" if $?; |
443 | ||
444 | `rm -fr $tmp/import`; | |
445 | die "Cannot remove tempdir: $!" if $?; | |
446 | ||
447 | ||
448 | return 1; | |
449 | } | |
450 | ||
451 | sub apply_cset { | |
452 | my $ps = shift; | |
453 | ||
454 | `mkdir -p $tmp`; | |
455 | ||
456 | # get the changeset | |
457 | `tla get-changeset -A $ps->{repo} $ps->{id} $tmp/changeset`; | |
458 | die "Cannot get changeset: $!" if $?; | |
459 | ||
460 | # apply patches | |
461 | if (`find $tmp/changeset/patches -type f -name '*.patch'`) { | |
462 | # this can be sped up considerably by doing | |
463 | # (find | xargs cat) | patch | |
464 | # but that cna get mucked up by patches | |
465 | # with missing trailing newlines or the standard | |
466 | # 'missing newline' flag in the patch - possibly | |
467 | # produced with an old/buggy diff. | |
468 | # slow and safe, we invoke patch once per patchfile | |
469 | `find $tmp/changeset/patches -type f -name '*.patch' -print0 | grep -zv '{arch}' | xargs -iFILE -0 --no-run-if-empty patch -p1 --forward -iFILE`; | |
470 | die "Problem applying patches! $!" if $?; | |
471 | } | |
472 | ||
473 | # apply changed binary files | |
474 | if (my @modified = `find $tmp/changeset/patches -type f -name '*.modified'`) { | |
475 | foreach my $mod (@modified) { | |
476 | chomp $mod; | |
477 | my $orig = $mod; | |
478 | $orig =~ s/\.modified$//; # lazy | |
479 | $orig =~ s!^\Q$tmp\E/changeset/patches/!!; | |
480 | #print "rsync -p '$mod' '$orig'"; | |
481 | `rsync -p $mod ./$orig`; | |
482 | die "Problem applying binary changes! $!" if $?; | |
483 | } | |
484 | } | |
485 | ||
486 | # bring in new files | |
1d4710d0 | 487 | `rsync --archive --exclude '$git_dir' --exclude '.arch-ids' --exclude '{arch}' $tmp/changeset/new-files-archive/* ./`; |
d3968363 ML |
488 | |
489 | # deleted files are hinted from the commitlog processing | |
490 | ||
491 | `rm -fr $tmp/changeset`; | |
492 | } | |
493 | ||
494 | ||
495 | # =for reference | |
496 | # A log entry looks like | |
497 | # Revision: moodle-org--moodle--1.3.3--patch-15 | |
498 | # Archive: arch-eduforge@catalyst.net.nz--2004 | |
499 | # Creator: Penny Leach <penny@catalyst.net.nz> | |
500 | # Date: Wed May 25 14:15:34 NZST 2005 | |
501 | # Standard-date: 2005-05-25 02:15:34 GMT | |
502 | # New-files: lang/de/.arch-ids/block_glossary_random.php.id | |
503 | # lang/de/.arch-ids/block_html.php.id | |
504 | # New-directories: lang/de/help/questionnaire | |
505 | # lang/de/help/questionnaire/.arch-ids | |
506 | # Renamed-files: .arch-ids/db_sears.sql.id db/.arch-ids/db_sears.sql.id | |
507 | # db_sears.sql db/db_sears.sql | |
508 | # Removed-files: lang/be/docs/.arch-ids/release.html.id | |
509 | # lang/be/docs/.arch-ids/releaseold.html.id | |
510 | # Modified-files: admin/cron.php admin/delete.php | |
511 | # admin/editor.html backup/lib.php backup/restore.php | |
512 | # New-patches: arch-eduforge@catalyst.net.nz--2004/moodle-org--moodle--1.3.3--patch-15 | |
513 | # Summary: Updating to latest from MOODLE_14_STABLE (1.4.5+) | |
514 | # Keywords: | |
515 | # | |
516 | # Updating yadda tadda tadda madda | |
517 | sub parselog { | |
518 | my $log = shift; | |
519 | #print $log; | |
520 | ||
521 | my (@add, @del, @mod, @ren, @kw, $sum, $msg ); | |
522 | ||
523 | if ($log =~ m/(?:\n|^)New-files:(.*?)(?=\n\w)/s ) { | |
524 | my $files = $1; | |
525 | @add = split(m/\s+/s, $files); | |
526 | } | |
527 | ||
528 | if ($log =~ m/(?:\n|^)Removed-files:(.*?)(?=\n\w)/s ) { | |
529 | my $files = $1; | |
530 | @del = split(m/\s+/s, $files); | |
531 | } | |
532 | ||
533 | if ($log =~ m/(?:\n|^)Modified-files:(.*?)(?=\n\w)/s ) { | |
534 | my $files = $1; | |
535 | @mod = split(m/\s+/s, $files); | |
536 | } | |
537 | ||
538 | if ($log =~ m/(?:\n|^)Renamed-files:(.*?)(?=\n\w)/s ) { | |
539 | my $files = $1; | |
540 | @ren = split(m/\s+/s, $files); | |
541 | } | |
542 | ||
543 | $sum =''; | |
544 | if ($log =~ m/^Summary:(.+?)$/m ) { | |
545 | $sum = $1; | |
546 | $sum =~ s/^\s+//; | |
547 | $sum =~ s/\s+$//; | |
548 | } | |
549 | ||
550 | $msg = ''; | |
551 | if ($log =~ m/\n\n(.+)$/s) { | |
552 | $msg = $1; | |
553 | $msg =~ s/^\s+//; | |
554 | $msg =~ s/\s+$//; | |
555 | } | |
556 | ||
557 | ||
558 | # cleanup the arrays | |
559 | foreach my $ref ( (\@add, \@del, \@mod, \@ren) ) { | |
560 | my @tmp = (); | |
561 | while (my $t = pop @$ref) { | |
562 | next unless length ($t); | |
563 | next if $t =~ m!\{arch\}/!; | |
564 | next if $t =~ m!\.arch-ids/!; | |
565 | next if $t =~ m!\.arch-inventory$!; | |
566 | push (@tmp, shell_quote($t)); | |
567 | } | |
568 | @$ref = @tmp; | |
569 | } | |
570 | ||
571 | #print Dumper [$sum, $msg, \@add, \@del, \@mod, \@ren]; | |
572 | return ($sum, $msg, \@add, \@del, \@mod, \@ren); | |
573 | } | |
574 | ||
575 | # write/read a tag | |
576 | sub tag { | |
577 | my ($tag, $commit) = @_; | |
578 | $tag =~ s|/|--|g; | |
579 | $tag = shell_quote($tag); | |
580 | ||
581 | if ($commit) { | |
1d4710d0 | 582 | open(C,">$git_dir/refs/tags/$tag") |
d3968363 ML |
583 | or die "Cannot create tag $tag: $!\n"; |
584 | print C "$commit\n" | |
585 | or die "Cannot write tag $tag: $!\n"; | |
586 | close(C) | |
587 | or die "Cannot write tag $tag: $!\n"; | |
b779d5f0 | 588 | print " * Created tag ' $tag' on '$commit'\n" if $opt_v; |
d3968363 | 589 | } else { # read |
1d4710d0 | 590 | open(C,"<$git_dir/refs/tags/$tag") |
d3968363 ML |
591 | or die "Cannot read tag $tag: $!\n"; |
592 | $commit = <C>; | |
593 | chomp $commit; | |
594 | die "Error reading tag $tag: $!\n" unless length $commit == 40; | |
595 | close(C) | |
596 | or die "Cannot read tag $tag: $!\n"; | |
597 | return $commit; | |
598 | } | |
599 | } | |
600 | ||
601 | # write/read a private tag | |
602 | # reads fail softly if the tag isn't there | |
603 | sub ptag { | |
604 | my ($tag, $commit) = @_; | |
605 | $tag =~ s|/|--|g; | |
606 | $tag = shell_quote($tag); | |
607 | ||
1d4710d0 ML |
608 | unless (-d "$git_dir/archimport/tags") { |
609 | mkpath("$git_dir/archimport/tags"); | |
d3968363 ML |
610 | } |
611 | ||
612 | if ($commit) { # write | |
1d4710d0 | 613 | open(C,">$git_dir/archimport/tags/$tag") |
d3968363 ML |
614 | or die "Cannot create tag $tag: $!\n"; |
615 | print C "$commit\n" | |
616 | or die "Cannot write tag $tag: $!\n"; | |
617 | close(C) | |
618 | or die "Cannot write tag $tag: $!\n"; | |
b779d5f0 ML |
619 | $rptags{$commit} = $tag |
620 | unless $tag =~ m/--base-0$/; | |
d3968363 ML |
621 | } else { # read |
622 | # if the tag isn't there, return 0 | |
1d4710d0 | 623 | unless ( -s "$git_dir/archimport/tags/$tag") { |
d3968363 ML |
624 | return 0; |
625 | } | |
1d4710d0 | 626 | open(C,"<$git_dir/archimport/tags/$tag") |
d3968363 ML |
627 | or die "Cannot read tag $tag: $!\n"; |
628 | $commit = <C>; | |
629 | chomp $commit; | |
630 | die "Error reading tag $tag: $!\n" unless length $commit == 40; | |
631 | close(C) | |
632 | or die "Cannot read tag $tag: $!\n"; | |
b779d5f0 ML |
633 | unless (defined $rptags{$commit}) { |
634 | $rptags{$commit} = $tag; | |
635 | } | |
d3968363 ML |
636 | return $commit; |
637 | } | |
638 | } | |
b779d5f0 ML |
639 | |
640 | sub find_parents { | |
641 | # | |
642 | # Identify what branches are merging into me | |
643 | # and whether we are fully merged | |
644 | # git-merge-base <headsha> <headsha> should tell | |
645 | # me what the base of the merge should be | |
646 | # | |
647 | my $ps = shift; | |
648 | ||
649 | my %branches; # holds an arrayref per branch | |
650 | # the arrayref contains a list of | |
651 | # merged patches between the base | |
652 | # of the merge and the current head | |
653 | ||
654 | my @parents; # parents found for this commit | |
655 | ||
656 | # simple loop to split the merges | |
657 | # per branch | |
658 | foreach my $merge (@{$ps->{merges}}) { | |
659 | my $branch = branchname($merge); | |
660 | unless (defined $branches{$branch} ){ | |
661 | $branches{$branch} = []; | |
662 | } | |
663 | push @{$branches{$branch}}, $merge; | |
664 | } | |
665 | ||
666 | # | |
667 | # foreach branch find a merge base and walk it to the | |
668 | # head where we are, collecting the merged patchsets that | |
669 | # Arch has recorded. Keep that in @have | |
670 | # Compare that with the commits on the other branch | |
671 | # between merge-base and the tip of the branch (@need) | |
672 | # and see if we have a series of consecutive patches | |
673 | # starting from the merge base. The tip of the series | |
674 | # of consecutive patches merged is our new parent for | |
675 | # that branch. | |
676 | # | |
677 | foreach my $branch (keys %branches) { | |
678 | my $mergebase = `git-merge-base $branch $ps->{branch}`; | |
679 | die "Cannot find merge base for $branch and $ps->{branch}" if $?; | |
680 | chomp $mergebase; | |
681 | ||
682 | # now walk up to the mergepoint collecting what patches we have | |
683 | my $branchtip = git_rev_parse($ps->{branch}); | |
684 | my @ancestors = `git-rev-list --merge-order $branchtip ^$mergebase`; | |
685 | my %have; # collected merges this branch has | |
686 | foreach my $merge (@{$ps->{merges}}) { | |
687 | $have{$merge} = 1; | |
688 | } | |
689 | my %ancestorshave; | |
690 | foreach my $par (@ancestors) { | |
691 | $par = commitid2pset($par); | |
692 | if (defined $par->{merges}) { | |
693 | foreach my $merge (@{$par->{merges}}) { | |
694 | $ancestorshave{$merge}=1; | |
695 | } | |
696 | } | |
697 | } | |
698 | # print "++++ Merges in $ps->{id} are....\n"; | |
699 | # my @have = sort keys %have; print Dumper(\@have); | |
700 | ||
701 | # merge what we have with what ancestors have | |
702 | %have = (%have, %ancestorshave); | |
703 | ||
704 | # see what the remote branch has - these are the merges we | |
705 | # will want to have in a consecutive series from the mergebase | |
706 | my $otherbranchtip = git_rev_parse($branch); | |
707 | my @needraw = `git-rev-list --merge-order $otherbranchtip ^$mergebase`; | |
708 | my @need; | |
709 | foreach my $needps (@needraw) { # get the psets | |
710 | $needps = commitid2pset($needps); | |
711 | # git-rev-list will also | |
712 | # list commits merged in via earlier | |
713 | # merges. we are only interested in commits | |
714 | # from the branch we're looking at | |
715 | if ($branch eq $needps->{branch}) { | |
716 | push @need, $needps->{id}; | |
717 | } | |
718 | } | |
719 | ||
720 | # print "++++ Merges from $branch we want are....\n"; | |
721 | # print Dumper(\@need); | |
722 | ||
723 | my $newparent; | |
724 | while (my $needed_commit = pop @need) { | |
725 | if ($have{$needed_commit}) { | |
726 | $newparent = $needed_commit; | |
727 | } else { | |
728 | last; # break out of the while | |
729 | } | |
730 | } | |
731 | if ($newparent) { | |
732 | push @parents, $newparent; | |
733 | } | |
734 | ||
735 | ||
736 | } # end foreach branch | |
737 | ||
738 | # prune redundant parents | |
739 | my %parents; | |
740 | foreach my $p (@parents) { | |
741 | $parents{$p} = 1; | |
742 | } | |
743 | foreach my $p (@parents) { | |
744 | next unless exists $psets{$p}{merges}; | |
745 | next unless ref $psets{$p}{merges}; | |
746 | my @merges = @{$psets{$p}{merges}}; | |
747 | foreach my $merge (@merges) { | |
748 | if ($parents{$merge}) { | |
749 | delete $parents{$merge}; | |
750 | } | |
751 | } | |
752 | } | |
753 | @parents = keys %parents; | |
754 | @parents = map { " -p " . ptag($_) } @parents; | |
755 | return @parents; | |
756 | } | |
757 | ||
758 | sub git_rev_parse { | |
759 | my $name = shift; | |
760 | my $val = `git-rev-parse $name`; | |
761 | die "Error: git-rev-parse $name" if $?; | |
762 | chomp $val; | |
763 | return $val; | |
764 | } | |
765 | ||
766 | # resolve a SHA1 to a known patchset | |
767 | sub commitid2pset { | |
768 | my $commitid = shift; | |
769 | chomp $commitid; | |
770 | my $name = $rptags{$commitid} | |
771 | || die "Cannot find reverse tag mapping for $commitid"; | |
772 | # the keys in %rptag are slightly munged; unmunge | |
773 | # reconvert the 3rd '--' sequence from the end | |
774 | # into a slash | |
775 | $name = reverse $name; | |
776 | $name =~ s!^(.+?--.+?--.+?--.+?)--(.+)$!$1/$2!; | |
777 | $name = reverse $name; | |
778 | my $ps = $psets{$name} | |
779 | || (print Dumper(sort keys %psets)) && die "Cannot find patchset for $name"; | |
780 | return $ps; | |
781 | } |