Skip to content

torbiak/git-autofixup

Repository files navigation

NAME

App::Git::Autofixup - create fixup commits for topic branches

SYNOPSIS

git-autofixup [<options>] [<revision>]

DESCRIPTION

git-autofixup parses hunks of changes in the working directory out of git diff output and uses git blame to assign those hunks to commits in <revision>..HEAD, which will typically represent a topic branch, and then creates fixup commits to be used with git rebase --interactive --autosquash. It is assumed that hunks near changes that were previously committed to the topic branch are related. <revision> defaults to git merge-base --fork-point HEAD @{upstream} || git merge-base HEAD @{upstream}, but this will only work if the current branch has an upstream/tracking branch. See git help revisions for info about how to specify revisions.

If any changes have been staged to the index using git add, then git-autofixup will only consider staged hunks when trying to create fixup commits. A temporary index is used to create any resulting commits.

By default a hunk will be included in a fixup commit if all the lines in the hunk's context blamed on topic branch commits refer to the same commit, so there's no ambiguity about which commit the hunk corresponds to. If there is ambiguity the assignment behaviour used under --strict 1 will be used to attempt to resolve it. If --strict 1 is given the same topic branch commit must be blamed for every removed line and at least one of the lines adjacent to each added line, and added lines must not be adjacent to lines blamed on other topic branch commits. All the same restrictions apply when --strict 2 is given, but each added line must be surrounded by lines blamed on the same topic branch commit.

For example, the added line in the hunk below is adjacent to lines committed by commits 99f370af and a1eadbe2. If these are both topic branch commits then it's ambiguous which commit the added line is fixing up and the hunk will be ignored.

COMMIT  |LINE|HEAD                          |WORKING DIRECTORY
99f370af|   1|first line                    | first line
        |    |                              |+added line
a1eadbe2|   2|second line                   | second line

But if that second line were instead blamed on an upstream commit (denoted by ^), the hunk would be added to a fixup commit for 99f370af:

99f370af|   1|first line                    | first line
        |    |                              |+added line
^       |   2|second line                   | second line

Output similar to this example can be generated by setting verbosity to 2 or greater by using the verbosity option multiple times, eg. git-autofixup -vv, and can be helpful in determining how a hunk will be handled.

git-autofixup is not to be used mindlessly. Always inspect the created fixup commits to ensure hunks have been assigned correctly, especially when used on a working directory that has been changed with a mix of fixups and new work.

Articles

OPTIONS

-h

Show usage.

--help

Show manpage.

--version

Show version.

-v, --verbose

Increase verbosity. Can be used up to two times.

-c N, --context N

Change the number of context lines git diff uses around hunks. Default: 3. This can change how hunks are assigned to fixup commits, especially with --strict 0.

-s N, --strict N

Set how strict git-autofixup is about assigning hunks to fixup commits. Default: 0. Strictness levels are described under DESCRIPTION.

-g ARG, --gitopt ARG

Specify option for git. Can be used multiple times. Useful for testing, to override config options that break git-autofixup, or to override global diff options to tweak what git-autofixup considers a hunk. Deprecated in favor of GIT_CONFIG_{COUNT,KEY,VALUE} environment variables; see git help config.

Note ARG won't be wordsplit, so to give multiple arguments, such as for setting a config option like -c diff.algorithm, this option must be used multiple times: -g -c -g diff.algorithm=patience.

-e, --exit-code

Use more detailed exit codes:

0:

All hunks have been assigned.

1:

Only some hunks have been assigned.

2:

No hunks have been assigned.

3:

There was nothing to be assigned.

255:

Unexpected error occurred.

INSTALLATION

If cpan is available, run cpan -i App::Git::Autofixup. Otherwise, copy git-autofixup to a directory in PATH and ensure it has execute permissions. It can then be invoked as either git-autofixup or git autofixup, since git searches PATH for appropriately named binaries.

Git is distributed with Perl 5 for platforms not expected to already have it installed, but installing modules with cpan requires other tools that might not be available, such as make. This script has no dependencies outside of the standard library, so it is hoped that it works on any platform that Git does without much trouble.

Requires a git supporting commit --fixup: 1.7.4 or later.

BUGS/LIMITATIONS

git-autofixup works on Windows, but be careful not to use a perl compiled for cygwin with a git compiled for msys, such as Git for Windows. It can be used from Git for Windows' "Git Bash" or "Git CMD", or you can install git using Cygwin's package manager and use git-autofixup from Cygwin. Note that while every release gets tested on Cygwin via the CPAN Testers network, testing with Git for Windows requires more effort since it's a constrained environment; thus it doesn't get tested as often. If you run into any issues, please report them on GitHub.

If a topic branch adds some lines in one commit and subsequently removes some of them in another, a hunk in the working directory that re-adds those lines will be assigned to fixup the first commit, and during rebasing they'll be removed again by the later commit.

ACKNOWLEDGEMENTS

git-autofixup was inspired by a description of hg absorb in the Mercurial Sprint Notes. While I was working on it I found git-superfixup, by oktal3700, which was helpful to examine.

COPYRIGHT AND LICENSE

Copyright (C) 2017, Jordan Torbiak.

This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License v2.0.