Parsing git log log samples using Python

Source: Internet
Author: User
Using Git to manage the development of the project, git log is a very useful ' history ' material, where demand is coming from, and we want to have a custom filter for git log. This script is to complete this type of task. For a commit in all branch of a repo, the script will collate a class of commits with the bug ID in the message and provide additional Search_key for custom filtering.

The code is 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)
F or 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 = '--' + + ' 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.s Plit (' Commit '):
If Re.search (' Bug: \d+ ', line) was not none and Re.search (Search_key, line) was 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[bu G_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) are not None:
Bra nch = 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.