git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Rast <trast@student.ethz.ch>
To: <git@vger.kernel.org>
Subject: git-fixup-assigner.perl -- automatically decide where to "fixup!"
Date: Tue, 14 Dec 2010 03:09:59 +0100	[thread overview]
Message-ID: <201012140309.59378.trast@student.ethz.ch> (raw)

While cleaning up the 'log -L' series I gathered a large number of
little fixups, and decided it would be smart if git could
automatically figure out where to put them.

It works like this:

* Split the diff by hunk.  I'm using -U1 here for finer splits, but it
  could be tunable.

* For each hunk, run blame to find out which commit's lines were
  affected.

* Group the hunks by this commit, and output them with a suitable
  command to make a fixup.

My git-fixup is

  $ g config alias.fixup
  !sh -c 'r=$1; git commit -m"fixup! $(git log -1 --pretty=%s $r)"' -

so that is "suitable".

You would run it with the changes unstaged in your tree as

  ./git-fixup-assigner.perl > fixups

and can then review with 'less fixups', or run 'sh fixups' to commit
them.

It's certainly not perfect, notably the detection logic should ignore
context, but it got the job done.

--- 8< ---
#!/usr/bin/perl

use warnings;
use strict;

sub parse_hunk_header {
        my ($line) = @_;
        my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
            $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
        $o_cnt = 1 unless defined $o_cnt;
        $n_cnt = 1 unless defined $n_cnt;
        return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
}

sub find_commit {
	my ($file, $begin, $end) = @_;
	my $blame;
	open($blame, '-|', 'git', '--no-pager', 'blame', 'HEAD', "-L$begin,$end", $file) or die;
	my %candidate;
	while (<$blame>) {
		$candidate{$1} += 1 if /^([0-9a-f]+)/;
	}
	close $blame or die;
	my @sorted = sort { $candidate{$b} <=> $candidate{$a} } keys %candidate;
	if (1 < scalar @sorted) {
		print STDERR "ambiguous split $file:$begin..$end\n";
		foreach my $c (@sorted) {
			print STDERR "\t$candidate{$c}\t$c\n";
		}
	}
	return $sorted[0];
}

my $diff;
open($diff, '-|', 'git', '--no-pager', 'diff', '-U1') or die;

my %by_commit;
my @cur_hunk = ();
my $cur_commit;
my ($filename, $prefilename, $postfilename);

while (<$diff>) {
        if (m{^diff --git ./(.*) ./\1$}) {
		if (@cur_hunk) {
			push @{$by_commit{$cur_commit}{$filename}}, @cur_hunk;
			@cur_hunk = ();
		}
		$filename = $1;
                $prefilename = "./" . $1;
                $postfilename = "./" . $1;
	} elsif (m{^index}) {
		# ignore
        } elsif (m{^new file}) {
		$prefilename = '/dev/null';
        } elsif (m{^delete file}) {
		$postfilename = '/dev/null';
        } elsif (m{^--- $prefilename$}) {
        } elsif (m{^\+\+\+ $postfilename$}) {
        } elsif (m{^@@ }) {
		if (@cur_hunk) {
			push @{$by_commit{$cur_commit}{$filename}}, @cur_hunk;
			@cur_hunk = ();
		}
		push @cur_hunk, $_;
		die "I don't handle this diff" if ($prefilename ne $postfilename);
                my ($o_ofs, $o_cnt, $n_ofs, $n_cnt)
                        = parse_hunk_header($_);
                my $o_end = $o_ofs + $o_cnt - 1;
		$cur_commit = find_commit($filename, $o_ofs, $o_end);
        } elsif (m{^[-+ \\]}) {
		push @cur_hunk, $_;
	} else {
		die "unhandled diff line: '$_'";
	}
}

close $diff or die;

if (@cur_hunk) {
	push @{$by_commit{$cur_commit}{$filename}}, @cur_hunk;
	@cur_hunk = ();
}

print "#!/bin/sh\n\n";

foreach my $commit (keys %by_commit) {
	print "git apply --cached <<EOF\n";
	foreach my $filename (keys %{$by_commit{$commit}}) {
		print "diff --git a/$filename b/$filename\n";
		print "--- a/$filename\n";
		print "+++ b/$filename\n";
		print @{$by_commit{$commit}{$filename}};
	}
	print "EOF\n\n";
	print "git fixup $commit\n\n";
}
--- >8 ---

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

             reply	other threads:[~2010-12-14  2:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-14  2:09 Thomas Rast [this message]
2011-10-26 14:37 ` git-fixup-assigner.perl -- automatically decide where to "fixup!" fREW Schmidt
2011-10-26 19:40   ` Thomas Rast

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201012140309.59378.trast@student.ethz.ch \
    --to=trast@student.ethz.ch \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).