使用python分析git log日志示例


Posted in Python onFebruary 27, 2014

用git来管理工程的开发,git log是非常有用的‘历史'资料,需求就是来自这里,我们希望能对git log有一个定制性强的过滤。此段脚本就是在完成这种类型的任务。对于一个repo所有branch中的commit,脚本将会把message中存在BUG ID的一类commits给提取整理出来,并提供了额外的search_key, 用于定制过滤。

# -*- coding: utf-8 -*-
# created by vince67 Feb.2014
# nuovince@gmail.com
import re
import os
import subprocess

def run(project_dir, date_from, date_to, search_key, filename):
    bug_dic = {}
    bug_branch_dic = {}
    try:
        os.chdir(project_dir)
    except Exception, e:
        raise e
    branches_list = []
    branches_list = get_branches()
    for branch in branches_list:
        bug_branch_dic = deal_branch(date_from,
                                     date_to,
                                     branch,
                                     search_key)
        for item in bug_branch_dic:
            if item not in bug_dic:
                bug_dic[item] = bug_branch_dic[item]
            else:
                bug_dic[item] += bug_branch_dic[item]
    log_output(filename, bug_dic)

# abstract log of one branch
def deal_branch(date_from, date_to, branch, search_key):
    try:
        os.system('git checkout ' + branch)
        os.system('git pull ')
    except Exception, error:
        print error
    cmd_git_log = ["git",
                   "log",
                   "--stat",
                   "--no-merges", 
                   "-m",
                   "--after="+date_from,
                   "--before="+date_to]
    proc = subprocess.Popen(cmd_git_log,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    bug_branch_dic = deal_lines(date_from,
                                date_to,
                                search_key,
                                stdout)
    return bug_branch_dic
# write commits log to file
def log_output(filename, bug_dic):
    fi = open(filename, 'w')
    for item in bug_dic:
        m1 = '--'*5 + 'BUG:' + item + '--'*20 + '\n'
        fi.write(m1)
        for commit in bug_dic[item]:
            fi.write(commit)
    fi.close()

# analyze log 
def deal_lines(date_from, date_to, search_key, stdout):
    bug_dic = {}
    for line in stdout.split('commit '):
        if re.search('Bug: \d+', line) is not None and re.search(search_key, line) is not None:
            bug_id = line.split('Bug: ')[1].split('\n')[0]
            if bug_id not in bug_dic:
                bug_dic[bug_id] = [line]
            else:
                bug_dic[bug_id] += [line]
    return bug_dic

# get all branches of a project
def get_branches():
    branch_list = []
    branches = []
    tmp_str = ''
    try:
        cmd_git_remote = 'git remote show origin'
        proc = subprocess.Popen(cmd_git_remote.split(),
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        stdout, stderr = proc.communicate()
        tmp_str = stdout.split('Local branches configured')[0]
        try:
            tmp_str = tmp_str.split('Remote branches:\n')[1]
        except:
            tmp_str = tmp_str.split('Remote branch:\n')[1]
        branches = tmp_str.split('\n')
        for branch in branches[0:-1]:
            if re.search(' tracked', branch) is not None:
                branch = branch.replace('tracked', '').strip(' ')
                branch_list.append(branch)
    except Exception, error:
        if branch_list == []:
            print "Can not get any branch!"
    return branch_list

if __name__ == '__main__':
    # path of the .git project. example: "/home/username/projects/jekyll_vincent"
    project_dir = ""
    date_from = "2014-01-25"
    date_to = "2014-02-26"
    # only search 'Bug: \d+' for default
    search_key = ""
    # name of output file. example:"/home/username/jekyll_0125_0226.log"
    filename = ""
    run(project_dir, date_from, date_to, search_key, filename)
Python 相关文章推荐
python ip正则式
May 07 Python
跟老齐学Python之集合的关系
Sep 24 Python
Python是编译运行的验证方法
Jan 30 Python
Python多线程编程简单介绍
Apr 13 Python
利用python微信库itchat实现微信自动回复功能
May 18 Python
Vue的el-scrollbar实现自定义滚动
May 29 Python
python最小生成树kruskal与prim算法详解
Jan 17 Python
python操作文件的参数整理
Jun 11 Python
pygame实现贪吃蛇游戏(上)
Oct 29 Python
Python实现队列的方法示例小结【数组,链表】
Feb 22 Python
python 使用tkinter+you-get实现视频下载器
Nov 17 Python
Opencv+Python识别PCB板图片的步骤
Jan 07 Python
python去掉字符串中重复字符的方法
Feb 27 #Python
tornado捕获和处理404错误的方法
Feb 26 #Python
python为tornado添加recaptcha验证码功能
Feb 26 #Python
python实现博客文章爬虫示例
Feb 26 #Python
python处理中文编码和判断编码示例
Feb 26 #Python
python实现网页链接提取的方法分享
Feb 25 #Python
python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)
Feb 24 #Python
You might like
PHP生成压缩文件实例
2015/02/07 PHP
PHP 极验验证码实例讲解
2016/09/29 PHP
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
2019/11/19 PHP
jQuery EasyUI API 中文文档 - Documentation 文档
2011/09/29 Javascript
jquery+css+ul模拟列表菜单具体实现思路
2013/04/15 Javascript
jquery validate在ie8下的bug解决方法
2013/11/13 Javascript
jQuery 鼠标经过(hover)事件的延时处理示例
2014/04/14 Javascript
js制作简易年历完整实例
2015/01/28 Javascript
jQuery中事件与动画的总结分享
2016/05/24 Javascript
轻松掌握JavaScript装饰者模式
2016/08/27 Javascript
Vue.js绑定HTML class数组语法错误的原因分析
2016/10/19 Javascript
防止重复发送 Ajax 请求
2017/02/15 Javascript
jQuery动态添加.active 实现导航效果代码思路详解
2017/08/29 jQuery
微信小程序出现wx.navigateTo页面不跳转问题的解决方法
2017/12/26 Javascript
bootstrap自定义样式之bootstrap实现侧边导航栏功能
2018/09/10 Javascript
详解vue组件中使用路由方法
2019/02/12 Javascript
在Vue项目中使用snapshot测试的具体使用
2019/04/16 Javascript
vue 路由懒加载中给 Webpack Chunks 命名的方法
2020/04/24 Javascript
javascript实现贪吃蛇小练习
2020/07/05 Javascript
[01:03:38]2014 DOTA2国际邀请赛中国区预选赛5.21 CNB VS CIS
2014/05/22 DOTA
[00:12]DAC SOLO赛卫冕冠军 VG.Paparazi灬展现SOLO技巧
2018/04/06 DOTA
Python实现调度算法代码详解
2017/12/01 Python
python与C、C++混编的四种方式(小结)
2019/07/15 Python
利用python实现短信和电话提醒功能的例子
2019/08/08 Python
Python获取当前脚本文件夹(Script)的绝对路径方法代码
2019/08/27 Python
Python发起请求提示UnicodeEncodeError错误代码解决方法
2020/04/21 Python
详解Python中的Lock和Rlock
2021/01/26 Python
德国旅游网站:weg.de
2018/06/03 全球购物
学校七一活动方案
2014/01/19 职场文书
房地产项目建议书
2014/03/12 职场文书
标准的毕业生自荐信
2014/04/20 职场文书
小学优秀学生评语
2014/12/29 职场文书
鲁迅故居导游词
2015/02/05 职场文书
化妆品促销活动总结
2015/05/07 职场文书
使用jpa之动态插入与修改(重写save)
2021/11/23 Java/Android
python实现对doc、txt、xls等文档的读写操作
2022/04/02 Python