chiark / gitweb /
46637899d91920f234cb78bb27b4f7eb58af912b
[dgit.git] / dgit-badcommit-fixup
1 #!/usr/bin/perl -w
2 #
3 # Script to help with fallout from #849041.
4 #
5 # usage:
6 #   dgit-badcommit-fixup --test
7 #   dgit-badcommit-fixup --real
8
9 use strict;
10
11 use POSIX;
12 use IPC::Open2;
13 use Data::Dumper;
14
15 my $real;
16
17 foreach my $a (@ARGV) {
18     if ($a eq '--test') {
19         $real = 0;
20     } elsif ($a eq '--real') {
21         $real = 1;
22     } else {
23         die "$a ?";
24     }
25 }
26
27 die unless defined $real;
28
29 my $gcfpid = open2 \*GCFO, \*GCFI, 'git cat-file --batch' or die $!;
30
31 our %count;
32
33 no warnings qw(recursion);
34
35 sub runcmd {
36     system @_ and die "@_ $! $?";
37 }
38
39 $!=0; $?=0;
40 my $bare = `git rev-parse --is-bare-repository`;
41 die "$? $!" if $?;
42 chomp $bare or die;
43
44 sub getobj ($$) {
45     my ($obj, $type) = @_;
46     print GCFI $obj, "\n" or die $!;
47     my $x = <GCFO>;
48     my ($gtype, $gsize) = $x =~ m/^\w+ (\w+) (\d+)\n/ or die "$obj ?";
49     $gtype eq $type or die "$obj $gtype != $type ?";
50     my $gdata;
51     (read GCFO, $gdata, $gsize) == $gsize or die "$obj $!";
52     $x = <GCFO>;
53     $x eq "\n" or die "$obj ($_) $!";
54     $count{inspected}++;
55     return $gdata;
56 }
57
58 sub hashobj ($$) {
59     my ($data,$type) = @_;
60     my $gwopid = open2 \*GWO, \*GWI,
61         "git hash-object -w -t $type --stdin"
62         or die $!;
63     print GWI $data or die $!;
64     close GWI or die $!;
65     $_ = <GWO>;
66     close GWO or die $!;
67     waitpid $gwopid,0 == $gwopid or die $!;
68     die $? if $?;
69     m/^(\w+)\n/ or die "$_ ?";
70     $count{"rewritten $type"}++;
71     return $1;
72 }
73
74 our %memo;
75
76 sub rewrite_commit ($);
77 sub rewrite_commit ($) {
78     my ($obj) = @_;
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 ?";
83     my $msg = $';
84     local $_ = $`;
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 ?";
92         $_ .= $1;
93         $count{'fix import'}++;
94     }
95     my $newdata = $_.$msg;
96     my $newobj;
97     if ($newdata eq $olddata) {
98         $newobj = $obj;
99         $count{unchanged}++;
100     } else {
101         $newobj = hashobj $newdata, 'commit';
102     }
103     $$m= $newobj;
104     return $newobj;
105 }
106
107 our @updates;
108
109 sub filter_updates () {
110     @updates = grep { $_->[1] ne $_->[2] } @updates;
111 }
112
113 sub rewrite_tag ($) {
114     my ($obj) = @_;
115     $_ = getobj $obj, 'tag';
116     m/^type (\w+)\n/m or die "$obj ?";
117     if ($1 ne 'commit') {
118         $count{"oddtags $1"}++;
119         return $obj;
120     }
121     m/^object (\w+)\n/m or die "$obj ?";
122     my $oldref = $1;
123     my $newref = rewrite_commit $oldref;
124     if ($oldref eq $newref) {
125         return $obj;
126     }
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';
130 }
131
132 $!=0; $?=0;
133 my $refs=`git for-each-ref`;
134 die "$? $!" if $?;
135
136 chomp $refs;
137
138 foreach my $rline (split /\n/, $refs) {
139     my ($obj, $type, $refname) = 
140         $rline =~ m/^(\w+)\s+(\w+)\s+(\S.*)/
141         or die "$_ ?";
142     my $rewrite;
143     if ($type eq 'commit') {
144         $rewrite = rewrite_commit($obj);
145     } elsif ($type eq 'tag') {
146         $rewrite = rewrite_tag($obj);
147     } else {
148         warn "ref $refname refers to $type\n";
149         next;
150     }
151     push @updates, [ $refname, $obj, $rewrite ];
152 }
153
154 filter_updates();
155
156 #print Dumper(\@updates);
157
158 open U, "|git update-ref -m 'dgit bad commit fixup' --stdin" or die $!;
159
160 if ($real && $bare eq 'false') {
161     print "detaching your HEAD\n" or die $!;
162     runcmd 'git checkout --detach';
163 }
164
165 for my $up (@updates) {
166     my ($ref, $old, $new) = @$up;
167     my $otherref = $ref;
168     $otherref =~ s{^refs/}{};
169     if ($real) {
170         print U <<END or die $!;
171 create refs/dgit-badcommit/$otherref $old
172 update $ref $new $old
173 END
174     } else {
175         print U <<END or die $!;
176 update refs/dgit-badfixuptest/$otherref $new
177 END
178     }
179 }
180
181 $?=0; $!=0;
182 close U or die "$? $!";
183 die $? if $?;
184
185 print Dumper(\%count);
186
187 if ($real) {
188     print "old values saved in refs/dgit-badcommit/\n" or die $!;
189 } else {
190     print "testing output saved in refs/dgit-badfixuptest/\n" or die $!;
191 }