Search code examples
gitgoindentationgit-rebasegofmt

Any tools rebase a branch in Git while running go fmt each commit separately?


I wish to rebuild/rebase all commits in a Git branch X using a source code formatting tool like go fmt or indent.

I'd expect the workflow to roughly consist of making a new branch off master and iterating the following with $_ ranging over the commits in X:

git cherry-pick $_
go fmt ...
git commit -a --amend

Or maybe even

git cherry-pick -n $_
go fmt ...
git cherry-pick --continue

I wouldn't expect -n and --continue to play together like that, though. Also, one should naturally do a go fmt commit to X and go diff X new when done.

However, there are many steps that can go wrong with this procedure, like the -a seeking to change files that weren't changed in the original commit, go fmt confusing Git's patching, Git changing the commit dates, etc.

None of that is particularly troublesome, but if a quick tool or simpler workflow does this more cleanly, then I'd love to know about it.


Solution

  • but if a quick tool or simpler workflow does this more cleanly, then I'd love to know about it.

    As Joseph K. Strauss mentions in the comments, git filter-branch should be enough, plus go fmt using the three dot notation:

    git filter-branch --tree-filter 'go fmt ...'
    

    You can read (much) more on filter-branch at "git filter-branch - discard the changes to a set of files in a range of commits".

    That command will run against all local branch, and will change the history (since new SHA1 will be generated for each modified commit).
    A git push --force might be needed to publish the new history to its upstream repo: do warn the other collaborators, for them to reset their local repos.