1、相關開源jar包
1》使用JGIT訪問git項目
2》使用SVNkit訪問svn
2、Git歷史提交日志導出到文件
在項目根目錄執行如下命令,將日志導出到指定位置:
git log --name-status --date=iso --pretty=format:"%H ,%T ,%P ,%an ,%ae ,%cn ,%ce ,%cd ,%s" --since="100 day ago" >> C:/Users/lfy/Desktop/fyliu_commit.log
如果需要指定導出某些開發者的記錄,可以使用grep過濾:
git log --name-status --date=iso --pretty=format:"%H ,%T ,%P ,%an ,%ae ,%cn ,%ce ,%cd ,%s" --since="100 day ago" |grep "lfy" >> C:/Users/lfy/Desktop/lfy_commit.log
導出成Excel文件:
git log --name-status --date=iso --pretty=format:'"%H","%T","%P","%an","%ae","%cn","%ce","%cd","%s"' >> C:/Users/lfy/Desktop/commit.csv
結果:
git log --pretty=format常用的選項
%H: commit hash %h: 縮短的commit hash %T: tree hash %t: 縮短的 tree hash %P: parent hash %p: 縮短的 parent hash %an: 作者名字 %aN: mailmap的作者名字 (.mailmap對應,詳情參照git-shortlog(1)或者git-blame(1)) %ae: 作者郵箱 %aE: 作者郵箱 (.mailmap對應,詳情參照git-shortlog(1)或者git-blame(1)) %ad: 日期 (--date= 制定的格式) %aD: 日期, RFC2822格式 %ar: 日期, 相對格式(1 day ago) %at: 日期, UNIX timestamp %ai: 日期, ISO 8601 格式 %cn: 提交者名字 %cN: 提交者名字 (.mailmap對應,詳情參照git-shortlog(1)或者git-blame(1)) %ce: 提交者 email %cE: 提交者 email (.mailmap對應,詳情參照git-shortlog(1)或者git-blame(1)) %cd: 提交日期 (--date= 制定的格式) %cD: 提交日期, RFC2822格式 %cr: 提交日期, 相對格式(1 day ago) %ct: 提交日期, UNIX timestamp %ci: 提交日期, ISO 8601 格式 %d: ref名稱 %e: encoding %s: commit信息標題 %f: sanitized subject line, suitable for a filename %b: commit信息內容 %N: commit notes %gD: reflog selector, e.g., refs/stash@{1} %gd: shortened reflog selector, e.g., stash@{1} %gs: reflog subject %Cred: 切換到紅色 %Cgreen: 切換到綠色 %Cblue: 切換到藍色 %Creset: 重設顏色 %C(...): 制定顏色, as described in color.branch.* config option %m: left, right or boundary mark %n: 換行 %%: a raw % %x00: print a byte from a hex code %w([[,[,]]]): switch line wrapping, like the -w option of git-shortlog(1).
git log常用選項
選項 | 說明 |
-p | 按補丁格式顯示每個更新之間的差異。 |
--stat | 顯示每次更新的文件修改統計信息。 |
--shortstat | 只顯示--stat中最后的行數修改添加移除統計。 |
--name-only | 僅在提交信息后顯示已修改的文件清單。 |
--name-status | 顯示新增,修改,刪除的文件清單。 |
--abbrev-commit | 只顯示SHA-1的前幾個字符,而非所有的40個字符。 |
--relative-date | 使用較短的相對時間顯示(比如,“2周前”)。 |
--graph | 顯示ASCII圖形表示的分支合並歷史。 |
--pretty | 使用其他格式顯示歷史提交信息。可用的選項包括oneline,short,full,fuller和format(后跟指定格式)。 |
限制git log輸出的選項
選項 | 說明 |
-(n) | 僅顯示最近的n條提交 |
--since,--after | 僅顯示指定時間之后的提交。 |
--until , --before |
僅顯示指定時間之前的提交。 |
--author | 僅顯示指定作者相關的提交。 |
--committer | 僅顯示指定提交者相關的提交。 |
--grep | 僅顯示含指定關鍵字的提交 |
-S | 僅顯示添加或移除了某個關鍵字的提交 |
3、統計代碼量
//按照時間統計
git log --since="2019-09-05" --pretty=tformat: --numstat |gawk '{ add += $1 ; subs += $2 ; loc += $1 + $2 }
END { printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }'
查看git上個人代碼量 git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' - 統計每個人的增刪行數 git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done 查看倉庫提交者排名前 5 git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5 貢獻者統計: git log --pretty='%aN' | sort -u | wc -l 提交數統計: git log --oneline | wc -l 統計代碼總行數: find . -name "*.m" -or -name "*.h" -or -name "*.xib" -or -name "*.c" |xargs grep -v "^$"|wc -l --------------------------------- --------------------------------- git log 參數說明: –author 指定作者 –stat 顯示每次更新的文件修改統計信息,會列出具體文件列表 –shortstat 統計每個commit 的文件修改行數,包括增加,刪除,但不列出文件列表: –numstat 統計每個commit 的文件修改行數,包括增加,刪除,並列出文件列表: -p 選項展開顯示每次提交的內容差異,用 -2 則僅顯示最近的兩次更新 例如:git log -p -2 –name-only 僅在提交信息后顯示已修改的文件清單 –name-status 顯示新增、修改、刪除的文件清單 –abbrev-commit 僅顯示 SHA-1 的前幾個字符,而非所有的 40 個字符 –relative-date 使用較短的相對時間顯示(比如,“2 weeks ago”) –graph 顯示 ASCII 圖形表示的分支合並歷史 –pretty 使用其他格式顯示歷史提交信息。可用的選項包括 oneline,short,full,fuller 和 format(后跟指定格式) 例如: git log –pretty=oneline ; git log –pretty=short ; git log –pretty=full ; git log –pretty=fuller –pretty=tformat: 可以定制要顯示的記錄格式,這樣的輸出便於后期編程提取分析 例如:git log –pretty=format:”“%h - %an, %ar : %s”” 下面列出了常用的格式占位符寫法及其代表的意義。 選項 說明 %H 提交對象(commit)的完整哈希字串 %h 提交對象的簡短哈希字串 %T 樹對象(tree)的完整哈希字串 %t 樹對象的簡短哈希字串 %P 父對象(parent)的完整哈希字串 %p 父對象的簡短哈希字串 %an 作者(author)的名字 %ae 作者的電子郵件地址 %ad 作者修訂日期(可以用 -date= 選項定制格式) %ar 作者修訂日期,按多久以前的方式顯示 %cn 提交者(committer)的名字 %ce 提交者的電子郵件地址 %cd 提交日期 %cr 提交日期,按多久以前的方式顯示 %s 提交說明 –since 限制顯示輸出的范圍, 例如: git log –since=2.weeks 顯示最近兩周的提交 選項 說明 -(n) 僅顯示最近的 n 條提交 –since, –after 僅顯示指定時間之后的提交。 –until, –before 僅顯示指定時間之前的提交。 –author 僅顯示指定作者相關的提交。 –committer 僅顯示指定提交者相關的提交。 一些例子: git log --until=1.minute.ago // 一分鍾之前的所有log git log --since=1.day.ago //一天之內的log git log --since=1.hour.ago //一個小時之內的log git log --since=`.month.ago --until=2.weeks.ago //一個月之前到半個月之前的log 看看某一個文件的相關歷史記錄 例如:git blame index.html –date short