撰寫git info工具以模擬svn info功能
由Subversion切換到Git的環境裡最不能適應的功能之一就是svn info了,Git並沒有對應的指令,搜尋了一下似乎只有Linux Bash的類似腳本... 既然沒有人寫Windows版本的git info,就自己寫一個吧。以下是剛出爐的git-info.exe的背景資料:
- 使用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
- 顏色代碼可參考ANSI Escape Sequences網頁,簡列如下:
===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卻都能正確顯示...
解決方法:
- 中文問題:在DOS-box先執行chcp 65001,把字碼頁改成UTF-8,點擊左上角以顯示功能表→【內容】→【字型】→挑選Consolas或Lucida Console,再選適當的大小後按確定,此時再執行git info就能看到中文了。
- ANSI Color則必須安裝並執行ANSICON,解開壓縮檔後將x86裡的ansicon.exe與ANSI32.DLL複製到PATH指向路徑。在執行ansicon.exe後就能正確顯示git log的顏色了。
- 用ansicon.exe -i 就能把ansicon永遠嵌入cmd.exe。(解除安裝:ansicon.exe -u)
相關資訊
##
您可能也會有興趣的類似文章
- 將Git分支名稱加到提示字元(Prompt)裡 (0則留言, 2014/09/02)
- 建立測試環境以git rebase -i變更Commit歷史 (0則留言, 2014/10/08)
- [Git#9] Gitea 安裝與設定:輕量級程式碼託管解決方案 (0則留言, 2022/05/22)
- [分享] 濱野純訪談:為什麼 Google 接管開發 Git 2.0 了? (0則留言, 2018/05/25)
- Gitea網頁添加自訂選項以開啟說明文件 (0則留言, 2022/09/01)
- [Git#8] 用pre-commit檢查提交時的分支是否正確 (0則留言, 2020/04/20)
- [Windows] 用Gitea架設自用的Git Server (2則留言, 2017/07/21)
- 啟用Gitea Server的SSH服務,可大幅增加連線速度 (0則留言, 2018/02/15)
- [Git#5] 產生Commit的檔案清單與相對路徑的壓縮檔;Windows適用 (0則留言, 2020/04/03)
- Git https/SSL 連線失敗時的解決方法 (0則留言, 2022/09/06)
- [Git#5-2] 補充說明:產生Commit的檔案清單壓縮檔 (0則留言, 2020/04/22)
- 為何無法正確執行git reset --hard HEAD^ (0則留言, 2014/09/20)
- Subversion版本庫匯入Git的步驟與SVN整合步驟 (0則留言, 2014/10/03)
- 幫Gitea網站使用Let’s Encrypt SSL憑證的設定步驟 (0則留言, 2022/09/01)
- [Batch#3 Git#3] 如何在Windows批次檔裡將提示字元變更為Git分支名稱? (初學者的命令行 #7) (0則留言, 2020/01/05)
Sorry, 很久沒用Google sc…