撰寫git info工具以模擬svn info功能

由Subversion切換到Git的環境裡最不能適應的功能之一就是svn info了,Git並沒有對應的指令,搜尋了一下似乎只有Linux Bash的類似腳本... 既然沒有人寫Windows版本的git info,就自己寫一個吧。以下是剛出爐的git-info.exe的背景資料:

git info

  • 使用Lazarus IDE以Free Pascal撰寫
  • 目前只有Win32執行檔,如果有時間會再加Linux版
  • 原始碼在GitHub上,歡迎修改或分支
  • 如果只想下載執行檔試試,也可以由我的Dropbox連結下載

使用說明

  • 將git-info.exe複製到 <Git安裝資料夾>\libexec\git-core資料夾裡,就能以下列指令來執行:
git info
git info -?
git info && %TEMP%\git-prompt.bat
git info && git-prompt
  
  • git info -? 會顯示簡要的操作說明
  • git info第一次執行時會在git-info.exe所在資料夾裡產生git-info.ini,所有顯示的git資訊都設定在此ini檔裡,可依需求增加cmd?設定,一個指令以 .title 與 .exec 組成
[Command]
cmd1.title====Remote URL:
cmd1.exec=git remote -v
cmd2.title====All branches:
cmd2.exec=git branch -a
cmd3.title====Recent commits:
cmd3.exec=git log --pretty=format:"%C(yellow)%h %C(cyan)%ai %C(bold green)[%cn]%C(bold red)%d %C(bold green)%s%C(reset)" -10 --abbrev-commit --abbrev=4
cmd4.title====Current Status:
cmd4.exec=git status
  
  • 每次執行git info時會產生git-prompt.bat,產生的提示符號樣式與路徑於git-info.ini裡設定;建議將之設定到PATH指到的資料夾裡。執行git-prompt.bat後會將目前分支名稱加到提示字元裡:(git-info.exe v0.02將變更Prompt的指令移到獨立的git-prompt.exe了,請參考:將Git分支名稱加到提示字元(Prompt)裡
[Prompt]
DefaultFG=light green
DefaultBG=black
HighlightFG=light yellow
HighlightBG=black
PromptBatch=d:\util\git-prompt.bat
  
  • git-priompt.bat的內容範例,分支名稱的顯示顏色是由git-info.ini裡的[Prompt] HighlightFG等設定組成。
@echo off
if exist .git\HEAD goto GIT
goto NOT_GIT
:GIT
  @prompt $p ($E[1;33;40m分支名稱$E[1;32;40m)$g
  goto END
:NOT_GITn
  prompt $p$g
:END
  
===Text attributes===
0   All attributes off
1   Bold on
4   Underscore (on monochrome display adapter only)
5   Blink on
7   Reverse video on
8   Concealed on

===Foreground colors===
30  Black
31  Red
32  Green
33  Yellow
34  Blue
35  Magenta
36  Cyan
37  White

===Background colors===
40  Black
41  Red
42  Green
43  Yellow
44  Blue
45  Magenta
46  Cyan
47  White
  

2014/09/02補充:cmd.exe必須的加強

以上設定在我使用的ConEmu裡能很正常的執行,沒想到標準的cmd.exe卻有些問題:1. 無法顯示中文的commit message,2. 無法顯示ANSI Color。而git log卻都能正確顯示...

解決方法:

  1. 中文問題:在DOS-box先執行chcp 65001,把字碼頁改成UTF-8,點擊左上角以顯示功能表→【內容】→【字型】→挑選Consolas或Lucida Console,再選適當的大小後按確定,此時再執行git info就能看到中文了。
  2. ANSI Color則必須安裝並執行ANSICON,解開壓縮檔後將x86裡的ansicon.exe與ANSI32.DLL複製到PATH指向路徑。在執行ansicon.exe後就能正確顯示git log的顏色了。
    enter image description here
  3. 用ansicon.exe -i 就能把ansicon永遠嵌入cmd.exe。(解除安裝:ansicon.exe -u)

相關資訊

##

您可能也會有興趣的類似文章

簡睿

服務於軟體業的資訊老兵。興趣廣泛,學習力佳,樂於分享所知所學。

您可能也會喜歡…

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *