Edmund Edmund - 1 month ago 8
Git Question

How to make command similar to "git rebase -i"

I'm trying to write a custom command for

git
. I want it to behave like
git rebase -i
where a file opens up in VIM and writing + quitting out of that file will trigger more code.

Example of what what I'm referring to

When you run
git rebase -i
, this file opens:

noop

# Rebase 3ffddbe..3ffddbe onto 3ffddbe (1 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out


Normally there are commit hashes there and you can squash or whatever. I'd like something similar where I could run my custom
git edmund
command and it would open the results in a file.

So my goal is:

1) git edmund

2) edit the file that opens up. I want the contents to look like this (basically the result of
git log -n 3 --pretty=format:"%H %cN"
):

3ffddbebbfb8eef48e69ca455628bd8b94f00eed Edmund
5ae79d3d7b0e8b33d0f3dcc366439e62e5703e1d Charlie
2063a5fce18972fee0bfa589ee2e2668f5a026f9 Kevin


3) run code that parses that file

Answer

Create a script name git-edmund, place it somewhere in your $PATH, and make sure it is executable. The script should probably do something like...

#!/bin/sh
tmpfile=$(mktemp gitXXXXXX)
trap "rm -f $tmpfile" EXIT
git log -n 3 --pretty=format:"%H %cN" > $tmpfile
${VISUAL:-${EDITOR:-vi}} $tmpfile
...do stuff with $tmpfile here...

Now when you run git edmund, it will run your git-edmund script, in which you can perform whatever logic you require.

Comments