r/git•
mfontani

A git hook to modify the "git rebase -i" template?

Hi,

In my Git workflow, I use branches heavily, as follows: they get created off master, then code-code-code - trying to commit one file at a time / one "logical change" at a time.

Before merging the changes back into master for deployment, the commits are then checked, reworded, reordered, squashed, what-have-you… via git rebase -i master, in order for them to make sense in master's history and provide good archaeology information for $future_self.

Whenever I run a git rebase -i master on a branch I'm actively developing on, I get presented with the standard Git template, such as:

pick f00baa [foo] fix blah problem
pick baaf00 [bar] fix another problem
pick f0f0f0 fixup blah
pick f00f00 [xxx] review this please

# Rebase f00baa..baaf00 onto f0f0f0
# 
# Commands:
# …

It often happens that silly me commits some intermediate commits with kinda-unhelpful commit messages, such as: "fixup last", or "fixup last FILENAME" - as I pretty much screwed up the last commit I did to a specific commit or a file.

To aid with that, I've got a git rr script, which provides me with a much better template from the get go, see git-rr. Since my $EDITOR is vim, I can simply V} to highlight the block of pickwithin the git rebase editor window, and :!git rr to replace that block with the output of my git-rr script, which then becomes something like:

# .... START rebasing branch mf/foobar to master
pick f00baa [foo] fix blah problem
#      FILE000001 design/foo/blah.css
pick baaf00 [bar] fix another problem
#      FILE000002 design/bar/baz.css
pick f0f0f0 fixup blah
#      FILE000001 design/foo/blah.css
pick f00f00 [xxx] review this please
#      FILE000003 design/world_domination.txt

The above output is much easier for me to rebase from, as I can "star" (start searching for word under cursor) a FILE000001 to see all commits in the current branch which touch that file, or look for fixup in the commit messages, then go to the related FILE00001 line below, and copy and paste lines above/below the relevant commit to be fixed up at leisure.

My question is: which hook, if it exists, can I use in order to tell git rebase -i master (or a similar command which effectively does the same thing) to still open up $EDITOR but use my template / my script to display the rebase plan?

Posted Feb 9, 2016, 11:13 AM UTC

53
cypherpunks•

Unfortunately, hooks are only for repository changes, and creating the list of commits does not itself change the repository.

That way to do what you want is to create a script that wraps the editor invocation by pre-fiddling the list, and then wrap the rebase with something that sets $GIT_EDITOR to that script.

One thing that comes to mind is the git commit --fixup command and the rebase.autoSquash setting. They might provide a different way to achieve what you're trying to do.

5
jdh28•

What about doing it in Vim? Vim recognises git rebase files ('gitrebase' filetype) so just create your own ftplugin/gitrebase.vim file and do whatever you want in there.

3
fragmede•

git will pick up programs called git-foobase if they're in your path. So you could have up a short script and call it git-rebase-mfontani so whenever you do git rebase-fontani, git will pick up your extension.

3