Using Python to parse git log logs sample _python

Source: Internet
Author: User

Using Git to manage the development of the project, git log is a very useful ' historical ' material that needs to come from here and we want to have a custom strong filter for git log. This script is doing this type of task. For a commit in all branch of a repo, the script will extract a class of commits with the bug ID in the message and provide additional Search_key for custom filtering.

Copy Code code as follows:

#-*-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 ') are not none and Re.search (Search_key, line) are 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 ' bugs: \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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.