如何使命令类似于"git rebase -i"



我正在尝试为git编写自定义命令。我希望它的行为像git rebase -i,其中一个文件在VIM中打开,写入+退出该文件将触发更多的代码。

我所指的例子

当你运行git rebase -i时,这个文件打开:

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

通常那里有提交哈希,你可以压缩或其他。我想要类似的东西,我可以运行我的自定义git edmund命令,它会在一个文件中打开结果。

所以我的目标是:

2)编辑打开的文件。我希望内容看起来像这样(基本上是git log -n 3 --pretty=format:"%H %cN"的结果):
3ffddbebbfb8eef48e69ca455628bd8b94f00eed Edmund
5ae79d3d7b0e8b33d0f3dcc366439e62e5703e1d Charlie
2063a5fce18972fee0bfa589ee2e2668f5a026f9 Kevin
3)运行解析该文件的代码

创建一个名为git-edmund的脚本,将其放在$PATH的某个位置,并确保它是可执行的。脚本应该像这样做…

#!/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...

现在,当您运行git edmund时,它将运行您的git-edmund脚本,您可以在其中执行任何您需要的逻辑。

你能做的是用一个自定义shell函数创建你自己的git别名。它可能看起来像这样:

[alias]    
edmund = "!ed() { git log -n 3 --pretty=format:"%H %cN" | vim - ; }; ed"

,所以当你执行git edmund时,它会打开vim编辑器,其中有3个最新的提交。这不是理想的行为,但你可以从它开始并改进它。我希望这对你有帮助。

最新更新