使用git log可以查到git上项目的更新日志。
如下两个git项目,我想把git的日志信息解析成一个便于在浏览器上查看的页面。
![](https://images2015.cnblogs.com/blog/449477/201601/449477-20160125174655707-1386521659.png)
https://github.com/gityf/lua
https://github.com/gityf/db
使用git log命令获取git更新日志信息:
D:\git\github\lua>git log > ..\lua.gitlogD:\git\github\db>git log > ..\dbutils.gitlog
生成两个git更新日志文件lua.gitlog和dbutils.gitlog。
文件内容格式是这样的:
commit a5ff94375f48c7de067a1c191cb60d720bf4f726Author: Mr.YF Date: Tue Jan 12 19:43:33 2016 +0800 add thrift binary analysis code and doc.commit 9dc9437070867e8d8ba7c9d4365efe3f2c317cafAuthor: Mr.YF Date: Mon Jan 11 19:51:48 2016 +0800 add comments.
git log命令会输出关键字"commit","Merge","Author","Date"
使用下面脚本分析git日志,生成html的页面,便于查看。
'''Created on 2016-1-25@author: Mr.YF'''import osimport sysdef header(): headerHtml = ''' Statistics Report for gitcheck. GitCheck Center
> General git information
''' return headerHtmldef footer(): footHtml = '''
''' return footHtmldef row(): rowHtml = '''%s%s%s%s%s''' return rowHtmldef indexMainRow(): rowHtml = '''IDbranch git log info.''' return rowHtmldef indexRow(): rowHtml = '''%d
%s''' return rowHtmldef tdClassId(pageId): pageId += 1 if pageId >= 7: pageId = 1 return pageIddef gitcheck(inFileName="gitcheck.in", outFileName="gitcheck.out.html"): fn = open(inFileName, "r") fnOut = open(outFileName, "w") fnOut.write(header()) fnOut.write(row() % (0, "commit","Merge","Author","Date", "Comment")) lines = fn.readlines() beginTag = False pageId = 1 goodLine = {0:"",1:"",2:"",3:"",4:""} for line in lines: if len(line) <= 2: continue if line.startswith("commit"): beginTag = True goodLine[0] = line[6:] pageId = tdClassId(pageId) continue if beginTag: if line.startswith("Merge: "): goodLine[1] = line[7:] elif line.startswith("Author: "): goodLine[2] = line[8:] elif line.startswith("Date: "): goodLine[3] = line[6:] else : goodLine[4] = line beginTag = False fnOut.write(row() % (pageId,goodLine[0],goodLine[1],goodLine[2],goodLine[3],goodLine[4])) goodLine = {0:"",1:"",2:"",3:"",4:""} fn.close() fnOut.write(footer()) fnOut.flush() fnOut.close()def lsHtmls(dirName="gitinfo"): htmlFiles = [] allFile = os.listdir(dirName) for f in allFile: if f.endswith(".html"): htmlFiles.append(f) print f return htmlFilesdef createIndex(dirName): files = lsHtmls(dirName) if len(files) > 0: fnOut = open("index.html", "w") fnOut.write(header()) fnOut.write(indexMainRow()) pageId = 1 ii = 1 for f in files: fnOut.write(indexRow() % (ii, pageId, f, f)) ii = tdClassId(ii) pageId += 1 fnOut.write(footer()); fnOut.close()if __name__ == '__main__': if len(sys.argv) >= 3: gitcheck(sys.argv[1], sys.argv[2]) elif len(sys.argv) >= 2: createIndex(sys.argv[1])
把python代码保存为gitcheck.py
使用如下命令解析日志文件lua.gitlog和dbutils.gitlog。
F:\pyworkspace\gitcheck>python gitcheck.py dbutils.gitlog gitinfo\dbutils.htmlF:\pyworkspace\gitcheck>python gitcheck.py lua.gitlog gitinfo\lua_thrift_demo.htmlF:\pyworkspace\gitcheck>python gitcheck.py gitinfodbutils.htmllua_thrift_demo.html
python gitcheck.py gitinfo会把目录gitinfo下所有的html文件汇总生成一个index.html主页。
效果如下:
主页
![](https://images2015.cnblogs.com/blog/449477/201601/449477-20160125174308863-1554839139.png)
跳转到某个项目查看git日志信息
![](https://images2015.cnblogs.com/blog/449477/201601/449477-20160125174359692-2101306092.png)
Done.