-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitutils.py
75 lines (68 loc) · 2.61 KB
/
gitutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""git log utilities
Copyright (c) 2019 The University of Texas at Austin. All rights reserved.
Use and redistribution of this file is governed by the license terms in
the LICENSE file found in the project's top-level directory.
"""
import sys
import subprocess
import datetime
import tzutils
def get_git_log():
""""Retrieve the git log in the current repo as a list of commits"""
git_log_out = subprocess.run(
['git', 'log', '--pretty=raw', '--no-color', '--encoding=UTF-8', '-z'],
check=True,
stdout=subprocess.PIPE,
stderr=sys.stderr.fileno(),
encoding='utf-8',
errors='ignore',
).stdout
git_log_split = git_log_out.split('\0')
git_log = []
for entry in git_log_split:
header_cmtmsg = tuple(entry.rstrip('\n').split('\n\n', 1))
headers = header_cmtmsg[0].replace('\n', '\0').replace('\0 ',
'\n').split('\0')
log_entry = {}
if len(header_cmtmsg) >= 2:
log_entry['commit message'] = header_cmtmsg[1].replace(
' ', '', 1).replace('\n ', '\n')
for header in headers:
s = header.split(' ', 1)
log_entry[s[0]] = s[1]
if 'committer' in log_entry:
s = log_entry['committer'].rsplit(' ', 3)
log_entry['committer.name'] = s[0]
log_entry['committer.email'] = s[1]
log_entry['committer.date'] = datetime.datetime.fromtimestamp(
float(s[2]), tzutils.get_tz_for_offset(s[3]))
if 'author' in log_entry:
s = log_entry['author'].rsplit(' ', 3)
log_entry['author.name'] = s[0]
log_entry['author.email'] = s[1]
log_entry['author.date'] = datetime.datetime.fromtimestamp(
float(s[2]), tzutils.get_tz_for_offset(s[3]))
git_log.append(log_entry)
return git_log
def git_diff(from_commit, to_commit):
""""Compute the diff between two commits"""
git_diff_out = subprocess.run(
['git', 'diff', from_commit, to_commit],
check=True,
stdout=subprocess.PIPE,
stderr=sys.stderr.fileno(),
encoding='utf-8',
errors='ignore',
).stdout
return git_diff_out
def git_diff_parents(commit):
""""Compute the diff between a commit and its parents, ignoring uninteresting merge diff hunks"""
git_diff_out = subprocess.run(
['git', 'diff-tree', '--cc', commit],
check=True,
stdout=subprocess.PIPE,
stderr=sys.stderr.fileno(),
encoding='utf-8',
errors='ignore',
).stdout
return git_diff_out