2016-10-11 24 views
1

我正在嘗試編寫git的自定義命令。我希望它的行爲與git rebase -i一樣,在VIM中打開一個文件,寫入+退出該文件將觸發更多代碼。如何使命令類似於「git rebase -i」

什麼,我指的是

什麼的實例在運行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命令,它會打開文件中的結果。

所以我的目標是:

1)git的埃德蒙

2)編輯打開的文件。我想要的內容看起來像這樣(的git log -n 3 --pretty=format:"%H %cN"基本的結果):

3ffddbebbfb8eef48e69ca455628bd8b94f00eed Edmund 
5ae79d3d7b0e8b33d0f3dcc366439e62e5703e1d Charlie 
2063a5fce18972fee0bfa589ee2e2668f5a026f9 Kevin 

3)解析該文件

回答

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腳本,您可以在其中執行自己需要的任何邏輯。

+0

你真正的MVP。謝謝!! – Edmund

+0

這是否使用git配置的'core.editor'? – rubenvb

+0

不是書面的,但顯然你可以修改調用編輯器的行,也要求'git config'提供這些信息。 – larsks

1

你可以做的是用自定義shell函數創建你自己的git alias。它可能是這個樣子:

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

,所以當你將執行git edmund它會打開3所最新提交的vim編輯器。這不是理想的行爲,但你可以從它開始並改進它。我希望它有幫助。

相關問題