使用python分析git log日志示例

 更新时间:2014年02月27日 09:48:12   作者:  
这篇文章主要介绍了使用python分析git log日志示例,需要的朋友可以参考下

用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)

相关文章

  • requests.gPython 用requests.get获取网页内容为空 ’ ’问题

    requests.gPython 用requests.get获取网页内容为空 ’ ’问题

    这篇文章主要介绍了requests.gPython 用requests.get获取网页内容为空 ’ ’,温行首先举例说明,具有一定得参考价值,需要的小伙伴可以参考一下
    2022-01-01
  • TensorFlow人工智能学习数据类型信息及转换

    TensorFlow人工智能学习数据类型信息及转换

    这篇文章主要为大家介绍了TensorFlow人工智能学习数据类型信息及转换,
    2021-11-11
  • Python接口自动化测试的实现

    Python接口自动化测试的实现

    这篇文章主要介绍了Python接口自动化测试的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Python中的NumPy实用函数整理之percentile详解

    Python中的NumPy实用函数整理之percentile详解

    这篇文章主要介绍了Python中的NumPy实用函数整理之percentile详解,NumPy函数percentile()用于计算指定维度上数组元素的第 n 个百分位数,返回值为标量或者数组,需要的朋友可以参考下
    2023-09-09
  • 基于matplotlib xticks用法详解

    基于matplotlib xticks用法详解

    这篇文章主要介绍了基于matplotlib xticks用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • 详解Python字符串对象的实现

    详解Python字符串对象的实现

    本文介绍了 python 内部是如何管理字符串对象,以及字符串查找操作是如何实现的,感兴趣的小伙伴们可以参考一下
    2015-12-12
  • python编写微信公众号首图思路详解

    python编写微信公众号首图思路详解

    这篇文章主要介绍了python编写微信公众号首图的思路,根据微信公众号首图要求,可以上传一个不超过5M的图片,且图片尺寸要是2.35:1的尺寸,具体实现思路及代码感兴趣的朋友跟随小编一起看看吧
    2019-12-12
  • Python+wxPython实现合并多个文本文件

    Python+wxPython实现合并多个文本文件

    在 Python 编程中,我们经常需有时候,我们可能需要将多个文本文件合并成一个文件,要处理文本文件,本文就来介绍下如何使用 wxPython 模块编写一个简单的程序,能够让用户选择多个文本文件,感兴趣的可以了解下
    2023-08-08
  • Python解决asyncio文件描述符最大数量限制的问题

    Python解决asyncio文件描述符最大数量限制的问题

    这篇文章主要介绍了Python解决asyncio文件描述符最大数量限制的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • python自动填写问卷星问卷以及提交问卷等功能

    python自动填写问卷星问卷以及提交问卷等功能

    这篇文章主要给大家介绍了关于python自动填写问卷星问卷以及提交问卷等功能的相关资料,包括使用Selenium库模拟浏览器操作、定位元素、填写表单等,通过本文的学习,读者可以了解如何利用Python自动化技术提高问卷填写效率,需要的朋友可以参考下
    2023-03-03

最新评论