chiark / gitweb /
badcommit-fixup: reorganise arg parsing
[dgit.git] / badcommit-fixup
index af4b89d2bbb30a39e6d78a93adcd849e9bbf1a72..ca2e0dfbdd28a8f02db50d38cd45244c06464d1b 100755 (executable)
@@ -1,11 +1,29 @@
 #!/usr/bin/perl -w
 
+# usage:
+#   .../badcommit-fixup [-- --test
+#   .../badcommit-fixup --real
+
 use strict;
 
 use POSIX;
 use IPC::Open2;
 use Data::Dumper;
 
+my $real;
+
+foreach my $a (@ARGV) {
+    if ($a eq '--test') {
+       $real = 0;
+    } elsif ($a eq '--real') {
+       $real = 1;
+    } else {
+       die "$a ?";
+    }
+}
+
+die unless defined $real;
+
 my $gcfpid = open2 \*GCFO, \*GCFI, 'git cat-file --batch' or die $!;
 
 our %count;
@@ -30,7 +48,7 @@ sub getobj ($$) {
 sub hashobj ($$) {
     my ($data,$type) = @_;
     my $gwopid = open2 \*GWO, \*GWI,
-       "git hash-object -t $type --stdin"
+       "git hash-object -w -t $type --stdin"
        or die $!;
     print GWI $data or die $!;
     close GWI or die $!;
@@ -55,14 +73,14 @@ sub rewrite_commit ($) {
     my $msg = $';
     $_ = $`;
     s{^(parent )(\w+)$}{ $1 . rewrite_commit($2) }gme;
-    $count{fix_overwrite} += s{^commiter }{committer }gm;
+    $count{'fix overwrite'} += s{^commiter }{committer }gm;
     if (!m{^author }m && !m{^committer }m) {
        m{^parent (\w+)}m or die "$obj ?";
        my $parent = getobj $1, 'commit';
        $parent =~ m/^(?:.+\n)+(author .*\ncommitter .*\n)/;
        m/\n$/ or die "$obj ?";
        $_ .= $1;
-       $count{fix_import}++;
+       $count{'fix import'}++;
     }
     my $newdata = $_.$msg;
     my $newobj;
@@ -82,7 +100,7 @@ sub rewrite_tag ($) {
     m/^type (\w+)\n/m or die "$obj ?";
     if ($1 ne 'commit') {
        $count{"oddtags $1"}++;
-       return;
+       return $obj;
     }
     m/^object (\w+)\n/m or die "$obj ?";
     my $oldref = $1;
@@ -96,7 +114,7 @@ sub rewrite_tag ($) {
 }
 
 $!=0; $?=0;
-my $refs=`git-for-each-ref`;
+my $refs=`git for-each-ref`;
 die "$? $!" if $?;
 
 chomp $refs;
@@ -111,13 +129,51 @@ foreach my $rline (split /\n/, $refs) {
     if ($type eq 'commit') {
        $rewrite = rewrite_commit($obj);
     } elsif ($type eq 'tag') {
-       my $rewrite = rewrite_tag($obj);
+       $rewrite = rewrite_tag($obj);
     } else {
        warn "ref $refname refers to $type\n";
        next;
     }
-    next if $rewrite = $obj;
-    push @updates, [ $refname, $rewrite ];
+    next if $rewrite eq $obj;
+    push @updates, [ $refname, $obj, $rewrite ];
 }
 
-print Dumper(\@updates, \%count);
+open U, "|git update-ref -m 'dgit bad commit fixup' --stdin" or die $!;
+
+if ($real) {
+    $!=0; $?=0;
+    my $bare = `git rev-parse --is-bare-repository`;
+    die "$? $!" if $?;
+    if ($bare eq 'false') {
+       print "detaching your HEAD\n" or die $!;
+       system 'git checkout --detach' and die "$! $?";
+    }
+}
+
+for my $up (@updates) {
+    my ($ref, $old, $new) = @$up;
+    my $otherref = $ref;
+    $otherref =~ s{^refs/}{};
+    if ($real) {
+       print U <<END or die $!;
+create refs/dgit-badcommit/$otherref $old
+update $ref $new $old
+END
+    } else {
+       print U <<END or die $!;
+update refs/dgit-badfixuptest/$otherref $new
+END
+    }
+}
+
+$?=0; $!=0;
+close U or die "$? $!";
+die $? if $?;
+
+print Dumper(\%count);
+
+if ($real) {
+    print "old values saved in refs/dgit-badcommit/\n" or die $!;
+} else {
+    print "testing output saved in refs/dgit-badfixuptest/\n" or die $!;
+}