# -*- 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:
            match = re.search('Bug:? \d+ ', line).group()
            try:
                bug_id = match.split('Bug: ')[1].split('\n')[0]
            except Exception, e:
                bug_id = match.split('Bug ')[1].split(' ')[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 git log的更多相关文章

  1. python解析git log后生成页面显示git更新日志信息

    使用git log可以查到git上项目的更新日志. 如下两个git项目,我想把git的日志信息解析成一个便于在浏览器上查看的页面. https://github.com/gityf/lua https ...

  2. git log命令全解析,打log还能这么随心所欲!

    git log命令非常强大而好用,在复杂系统的版本管理中扮演着重要的角色,但默认的git log命令显示出的东西实在太丑,不好好打扮一下根本没法见人,打扮好了用alias命令拍个照片,就正式出道了! ...

  3. [译]git log进阶

    格式化log输出 oneline --oneline标记将每个commit压缩成一行. 默认情况下显示一个commit ID和commit描述的第一行. 输出如下: 0e25143 Merge bra ...

  4. [git]Git log 输出格式化(转载)

    转载于:https://havee.me/linux/2015-05/git-pretty-output.html 美化完的格式: git log --graph --pretty=format:'% ...

  5. git log 格式化输出

    Git log --graph --pretty=format: '%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)& ...

  6. git log 常用命令及技巧

    git log常用命令以及技巧 1.git log 如果不带任何参数,它会列出所有历史记录,最近的排在最上方,显示提交对象的哈希值,作者.提交日期.和提交说明.如果记录过多,则按Page Up.Pag ...

  7. git log --stat常用命令

    ​1,显示被修改文件的修改统计信息,添加或删除了多少行. git log --stat 2,显示最近两条的修改 git log --stat -2 3,显示具体的修改 git log -p -2 4, ...

  8. [译]git log

    git log git log命令用来显示提交的快照. 能列出来你项目的历史, 能过滤和搜索你指定的一些修改. git status能让你检查工作目录和stage区的状态, git log只提供被co ...

  9. git log控制输出宽度

    %<(N, trunc) 下一个单元的输出宽度限制为N列, 左对齐 %<|(N, trunc) 下一个单元输出至全局第N列, 左对齐 %>, %>|, %>>, % ...

随机推荐

  1. TCP面试题之HTTP和HTTPS的请求过程

    HTTP的请求过程: 1.TCP建立连接后,客户端会发送报文给服务端: 2.服务端接收报文并作出响应: 3.客户端收到响应后解析给用户: HTTPS的请求过程: 1.客户端发送请求到服务端: 2.服务 ...

  2. sql 查找表引用的存储过程

    USE [master] GO /****** Object: StoredProcedure [dbo].[uspGetDepends] Script Date: 05/12/2016 14:11: ...

  3. matlab-bgl 工具包配置环境 TDM-GCC-64 mex

    Windows 64 + Matlab 64 MEX混合编程初步: http://blog.csdn.net/enjoyyl/article/details/46545263 使用的是 TDM-GCC ...

  4. win7兼容visual studio 2005 的方法

    http://blog.sina.com.cn/s/blog_74d572890100xv7p.html 今天花了4个小时,结合网上的介绍,本人终于找到了一个可以在win7环境下运行visual st ...

  5. zabbix 硬盘健康监控

    #!/bin/sh function sh { sd=`ls /dev/ | grep '^sd' |grep -v '[0-9]$'` echo '' > /usr/local/zabbix/ ...

  6. HTML5 canvas上画文字出现乱码

    不是这个的问题, <html> <head> <meta charset="utf-8"/> </head> 是因为从网上复制粘贴下 ...

  7. C#实时读取数据----局部页面刷新【转】

    I)现在刚开始学习C#,对一些基本的控件了解的不够,有个实时监控的系统,需要页面中的数据每5秒钟刷新一次, 要是每5秒钟页面全部的刷新,那页面根本就没法看了,对这个问题在CSDN上也专门开了帖子,问了 ...

  8. poj 1151(离散化+矩形面积并)

    题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...

  9. Mysql优化的方法

    一.表的优化: 1: 定长与变长分离 如 time.手机号等,每一单元值占的字节是固定的. 核心且常用字段,宜建成定长,放在一张表,查询速度会很快 而varchar, text,blob,这种变长字段 ...

  10. Delphi 7 在程序中直接执行SQL脚本文件

    Delphi 7 在程序中直接执行SQL脚本文件 在处理MSDE一些操作中.需要执行一些SQL脚本.有的是从SQLServer 2000中生成的SQL为后缀的脚本.在MSDE中没有企业管理器,操作都是 ...