3 # Script to help with fallout from #849041.
6 # dgit-badcommit-fixup --test
7 # dgit-badcommit-fixup --real
17 foreach my $a (@ARGV) {
20 } elsif ($a eq '--real') {
27 die unless defined $real;
29 my $gcfpid = open2 \*GCFO, \*GCFI, 'git cat-file --batch' or die $!;
33 no warnings qw(recursion);
36 system @_ and die "@_ $! $?";
40 my $bare = `git rev-parse --is-bare-repository`;
45 my ($obj, $type) = @_;
46 print GCFI $obj, "\n" or die $!;
48 my ($gtype, $gsize) = $x =~ m/^\w+ (\w+) (\d+)\n/ or die "$obj ?";
49 $gtype eq $type or die "$obj $gtype != $type ?";
51 (read GCFO, $gdata, $gsize) == $gsize or die "$obj $!";
53 $x eq "\n" or die "$obj ($_) $!";
59 my ($data,$type) = @_;
60 my $gwopid = open2 \*GWO, \*GWI,
61 "git hash-object -w -t $type --stdin"
63 print GWI $data or die $!;
67 waitpid $gwopid,0 == $gwopid or die $!;
69 m/^(\w+)\n/ or die "$_ ?";
70 $count{"rewritten $type"}++;
76 sub rewrite_commit ($);
77 sub rewrite_commit ($) {
79 my $m = \ $memo{$obj};
80 return $$m if defined $$m;
81 my $olddata = getobj $obj, 'commit';
82 $olddata =~ m/(?<=\n)(?=\n)/ or die "$obj ?";
85 s{^(parent )(\w+)$}{ $1 . rewrite_commit($2) }gme;
86 $count{'fix overwrite'} += s{^commiter }{committer }gm;
87 if (!m{^author }m && !m{^committer }m) {
88 m{^parent (\w+)}m or die "$obj ?";
89 my $parent = getobj $1, 'commit';
90 $parent =~ m/^(?:.+\n)+(author .*\ncommitter .*\n)/;
91 m/\n$/ or die "$obj ?";
93 $count{'fix import'}++;
95 my $newdata = $_.$msg;
97 if ($newdata eq $olddata) {
101 $newobj = hashobj $newdata, 'commit';
109 sub filter_updates () {
110 @updates = grep { $_->[1] ne $_->[2] } @updates;
113 sub rewrite_tag ($) {
115 $_ = getobj $obj, 'tag';
116 m/^type (\w+)\n/m or die "$obj ?";
117 if ($1 ne 'commit') {
118 $count{"oddtags $1"}++;
121 m/^object (\w+)\n/m or die "$obj ?";
123 my $newref = rewrite_commit $oldref;
124 if ($oldref eq $newref) {
127 s/^(object )\w+$/ $1.$newref /me or die "$obj ($_) ?";
128 s/^-----BEGIN PGP SIGNATURE-----\n.*^-----END PGP SIGNATURE-----\n$//sm;
129 return hashobj $_, 'tag';
133 my $refs=`git for-each-ref`;
138 foreach my $rline (split /\n/, $refs) {
139 my ($obj, $type, $refname) =
140 $rline =~ m/^(\w+)\s+(\w+)\s+(\S.*)/
143 if ($type eq 'commit') {
144 $rewrite = rewrite_commit($obj);
145 } elsif ($type eq 'tag') {
146 $rewrite = rewrite_tag($obj);
148 warn "ref $refname refers to $type\n";
151 push @updates, [ $refname, $obj, $rewrite ];
156 #print Dumper(\@updates);
158 open U, "|git update-ref -m 'dgit bad commit fixup' --stdin" or die $!;
160 if ($real && $bare eq 'false') {
161 print "detaching your HEAD\n" or die $!;
162 runcmd 'git checkout --detach';
165 for my $up (@updates) {
166 my ($ref, $old, $new) = @$up;
168 $otherref =~ s{^refs/}{};
170 print U <<END or die $!;
171 create refs/dgit-badcommit/$otherref $old
172 update $ref $new $old
175 print U <<END or die $!;
176 update refs/dgit-badfixuptest/$otherref $new
182 close U or die "$? $!";
185 print Dumper(\%count);
188 print "old values saved in refs/dgit-badcommit/\n" or die $!;
190 print "testing output saved in refs/dgit-badfixuptest/\n" or die $!;