Git log is a very useful 'historical 'material for managing project development with git. The requirement is from here. We hope to have a custom Filtering for git log. This script completes this type of task. For commit in all branch of a repo, the script extracts commits containing BUG IDs in the message and provides additional search_key for custom filtering.
Copy codeThe 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 t 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 ')
Failed t Exception, error:
Print error
Pai_git_log = ["git ",
"Log ",
"-- Stat ",
"-- No-merges ",
"-M ",
"-- After =" + date_from,
"-- Before =" + date_to]
Proc = subprocess. Popen (pai_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 (pai_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 t:
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)
Failed t 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)