forked from tobami/codespeed
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathgit.py
More file actions
96 lines (75 loc) · 3.18 KB
/
git.py
File metadata and controls
96 lines (75 loc) · 3.18 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import datetime
import logging
import os
from subprocess import Popen, PIPE
from django.conf import settings
from .exceptions import CommitLogError
logger = logging.getLogger(__name__)
def execute_command(cmd, cwd):
p = Popen(cmd, stdout=PIPE, stderr=PIPE, cwd=cwd)
stdout, stderr = p.communicate()
stdout = stdout.decode('utf8') if stdout is not None else stdout
stderr = stderr.decode('utf8') if stderr is not None else stderr
return (p, stdout, stderr)
def updaterepo(project, update=True):
if os.path.exists(project.working_copy):
if not update:
return
p, _, stderr = execute_command(['git', 'pull'], cwd=project.working_copy)
if p.returncode != 0:
raise CommitLogError("git pull returned %s: %s" % (p.returncode,
stderr))
else:
return [{'error': False}]
else:
cmd = ['git', 'clone', project.repo_path, project.repo_name]
p, stdout, stderr = execute_command(cmd, settings.REPOSITORY_BASE_PATH)
logger.debug('Cloning Git repo {0} for project {1}'.format(
project.repo_path, project))
if p.returncode != 0:
raise CommitLogError("%s returned %s: %s" % (
" ".join(cmd), p.returncode, stderr))
else:
return [{'error': False}]
def getlogs(endrev, startrev):
updaterepo(endrev.branch.project, update=False)
# NULL separated values delimited by 0x1e record separators
# See PRETTY FORMATS in git-log(1):
if hasattr(settings, 'GIT_USE_COMMIT_DATE') and settings.GIT_USE_COMMIT_DATE:
logfmt = '--format=format:%h%x00%H%x00%ct%x00%an%x00%ae%x00%s%x00%b%x1e'
else:
logfmt = '--format=format:%h%x00%H%x00%at%x00%an%x00%ae%x00%s%x00%b%x1e'
cmd = ["git", "log", logfmt]
if endrev.commitid != startrev.commitid:
cmd.append("%s...%s" % (startrev.commitid, endrev.commitid))
else:
cmd.append("-1") # Only return one commit
cmd.append(endrev.commitid)
working_copy = endrev.branch.project.working_copy
p, stdout, stderr = execute_command(cmd, working_copy)
if p.returncode != 0:
raise CommitLogError("%s returned %s: %s" % (
" ".join(cmd), p.returncode, stderr))
logs = []
for log in filter(None, stdout.split('\x1e')):
(short_commit_id, commit_id, date_t, author_name, author_email,
subject, body) = map(lambda s: s.strip(), log.split('\x00', 7))
cmd = ["git", "tag", "--points-at", commit_id]
try:
p, stdout, stderr = execute_command(cmd, working_copy)
except Exception:
logger.debug('Failed to get tag', exc_info=True)
tag = stdout.strip() if p.returncode == 0 else ""
date = datetime.datetime.fromtimestamp(
int(date_t)).strftime("%Y-%m-%d %H:%M:%S")
logs.append({
'date': date,
'message': subject,
'commitid': commit_id,
'author': author_name,
'author_email': author_email,
'body': body,
'short_commit_id': short_commit_id,
'tag': tag
})
return logs