/
asf_get_commit_log.py
executable file
·132 lines (110 loc) · 4.63 KB
/
asf_get_commit_log.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!usr/bin/env python
from datetime import datetime
import re, os, subprocess, dbm
import asf_parse
def get_svn_log(repo, project_name):
"""
Download commit log from specified SVN repo.
"""
# check if svn server is reachable
try:
svn_info = subprocess.check_output(["svn", "info", repo], stderr=subprocess.STDOUT).decode("utf-8")
except:
print("Failed to retrieve repo info for {0}. Repository URL: {1}".format(project_name, repo))
return False
# get rev num of latest commit
last_rev_num = re.search("Last Changed Rev:\s(.*)", svn_info).group(1)
# check db for last commit rev num
db = dbm.open('last_rev_num', 'c')
range = ""
if repo in db:
stored_rev_num = db[repo].decode("utf-8")
if last_rev_num == stored_rev_num:
print("Commit log for {0} up to date.".format(project_name))
return True
else:
range = "HEAD:" + stored_rev_num
# get commit log
try:
if range:
log_info = subprocess.check_output(["svn", "log", "-v", "-r", range, repo], stderr=subprocess.STDOUT).decode("utf-8")
else:
log_info = subprocess.check_output(["svn", "log", "-v", repo], stderr=subprocess.STDOUT).decode("utf-8")
except:
print("Failed to retrieve commit log for {0}. Repository URL: {1}".format(project_name, repo))
return False
asf_parse.parse_svn_log(log_info, project_name)
db[repo] = last_rev_num
return True
def get_git_log(repo, project_name):
"""
Download commit log from specified Git repo.
"""
basename = os.path.basename(repo)
m = re.search('([a-zA-Z0-9_\-]*)\.git', basename)
shortname = m.group(1)
cwd = os.getcwd()
os.chdir('git_repos')
# check what remote servers are already configured
try:
remote_info = subprocess.check_output(["git", "remote", "-v"], stderr=subprocess.STDOUT).decode("utf-8")
except:
print("Failed to retrieve remote info.")
os.chdir(cwd)
return False
if not repo in remote_info:
try:
add_repo_info = subprocess.check_output(["git", "remote", "add", shortname, repo], stderr=subprocess.STDOUT).decode("utf-8")
except:
print("Failed to add remote repo {0}.".format(shortname))
os.chdir(cwd)
return False
# pull down all missing data from remote project
try:
fetch_info = subprocess.check_output(["git", "fetch", shortname], stderr=subprocess.STDOUT).decode("utf-8")
except:
print("Failed to fetch from remote repository {0}.".format(shortname))
os.chdir(cwd)
return False
# checkout remote branch into working tree
try:
checkout_info = subprocess.check_output(["git", "checkout", "-f", "remotes/" + shortname + "/master"], stderr=subprocess.STDOUT).decode("utf-8")
except:
try:
checkout_info = subprocess.check_output(["git", "checkout", "-f", "remotes/" + shortname + "/trunk"], stderr=subprocess.STDOUT).decode("utf-8")
except:
print("Failed to checkout {0} master branch".format(shortname))
os.chdir(cwd)
return False
# get hash of latest commit
try:
last_commit_hash = subprocess.check_output(["git", "log", "-1", "--pretty=format:%H"], stderr=subprocess.STDOUT).decode("utf-8")
except:
print("Failed to retrieve last commit hash for {0}. Repository URL: {1}".format(project_name, repo))
os.chdir(cwd)
return False
# check db for last commit hash
db = dbm.open('last_commit_hash', 'c')
range = ""
if repo in db:
stored_hash = db[repo].decode("utf-8")
if last_commit_hash == stored_hash:
print("Commit log for {0} up to date.".format(project_name))
os.chdir(cwd)
return True
else:
range = stored_hash + "..HEAD"
# get commit log
try:
if range:
log_info = subprocess.check_output(["git", "log", range, "--pretty=format:'?||?%n%H|%an|%ci%n>%s%n'", "--name-status"], stderr=subprocess.STDOUT).decode("utf-8")
else:
log_info = subprocess.check_output(["git", "log", "--pretty=format:'?||?%n%H|%an|%ci%n>%s%n'", "--name-status"], stderr=subprocess.STDOUT).decode("utf-8")
except:
print("Failed to retrieve commit log for {0}. Repository URL: {1}".format(project_name, repo))
os.chdir(cwd)
return False
asf_parse.parse_git_log(log_info, project_name)
db[repo] = last_commit_hash
os.chdir(cwd)
return True