chiark / gitweb /
08b6e3766f2c8c51672a235eda4e339eafbd3c6e
[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 sub rewrite_tag ($) {
108     my ($obj) = @_;
109     $_ = getobj $obj, 'tag';
110     m/^type (\w+)\n/m or die "$obj ?";
111     if ($1 ne 'commit') {
112         $count{"oddtags $1"}++;
113         return $obj;
114     }
115     m/^object (\w+)\n/m or die "$obj ?";
116     my $oldref = $1;
117     my $newref = rewrite_commit $oldref;
118     if ($oldref eq $newref) {
119         return $obj;
120     }
121     s/^(object )\w+$/ $1.$newref /me or die "$obj ($_) ?";
122     s/^-----BEGIN PGP SIGNATURE-----\n.*^-----END PGP SIGNATURE-----\n$//sm;
123     return hashobj $_, 'tag';
124 }
125
126 $!=0; $?=0;
127 my $refs=`git for-each-ref`;
128 die "$? $!" if $?;
129
130 chomp $refs;
131
132 our @updates;
133
134 foreach my $rline (split /\n/, $refs) {
135     my ($obj, $type, $refname) = 
136         $rline =~ m/^(\w+)\s+(\w+)\s+(\S.*)/
137         or die "$_ ?";
138     my $rewrite;
139     if ($type eq 'commit') {
140         $rewrite = rewrite_commit($obj);
141     } elsif ($type eq 'tag') {
142         $rewrite = rewrite_tag($obj);
143     } else {
144         warn "ref $refname refers to $type\n";
145         next;
146     }
147     next if $rewrite eq $obj;
148     push @updates, [ $refname, $obj, $rewrite ];
149 }
150
151 our $worktree;
152
153 #print Dumper(\@updates);
154
155 open U, "|git update-ref -m 'dgit bad commit fixup' --stdin" or die $!;
156
157 if ($real && $bare eq 'false') {
158     print "detaching your HEAD\n" or die $!;
159     runcmd 'git checkout --detach';
160 }
161
162 for my $up (@updates) {
163     my ($ref, $old, $new) = @$up;
164     my $otherref = $ref;
165     $otherref =~ s{^refs/}{};
166     if ($real) {
167         print U <<END or die $!;
168 create refs/dgit-badcommit/$otherref $old
169 update $ref $new $old
170 END
171     } else {
172         print U <<END or die $!;
173 update refs/dgit-badfixuptest/$otherref $new
174 END
175     }
176 }
177
178 $?=0; $!=0;
179 close U or die "$? $!";
180 die $? if $?;
181
182 print Dumper(\%count);
183
184 if ($real) {
185     print "old values saved in refs/dgit-badcommit/\n" or die $!;
186 } else {
187     print "testing output saved in refs/dgit-badfixuptest/\n" or die $!;
188 }