forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeready
More file actions
executable file
·313 lines (269 loc) · 10.3 KB
/
mergeready
File metadata and controls
executable file
·313 lines (269 loc) · 10.3 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/usr/bin/python
# -*- python -*-
"""
NAME
%(program)s - look for SVN commits that are ready to merge
SYNOPSIS
%(program)s [OPTIONS] ARGS
DESCRIPTION
%(program)s looks in the SVN log for commits which are marked with the
phrase 'Commit ready for merge', and compares the resulting list with
the 'svn:mergeinfo' property on the current directory, in order to
work out which (if any) commits are ready to merge, but not yet
merged. The command requires (and checks) that it's running in a
directory named 'trunk', and requires that to be an SVN working copy.
The files (in the top directory of the working copy) 'ready-for-merge'
and 'hold-for-merge' are also consulted for additions and exceptions to
the merge list.
A list of commit date, committer, and branch@revision for each commit
which is marked ready for merge, but not yet merged, is then written
to standard out.
%(options)s
AUTHOR
Written by Henrik Levkowetz, <henrik@tools.ietf.org>
COPYRIGHT
Copyright 2014 Henrik Levkowetz
This program is free software; you can redistribute it and/or modify
it under the terms of the Simplified BSD license as published by the
Open Source Initiative at http://opensource.org/licenses/BSD-2-Clause.
"""
from __future__ import print_function
import sys, os.path, getopt, re, tzparse, pytz
import debug
version = "0.20"
program = os.path.basename(sys.argv[0])
progdir = os.path.dirname(sys.argv[0])
# ----------------------------------------------------------------------
# Parse options
options = ""
for line in re.findall("\n +(if|elif) +opt in \[(.+)\]:\s+#(.+)\n", open(sys.argv[0]).read()):
if not options:
options += "OPTIONS\n"
options += " %-16s %s\n" % (line[1].replace('"', ''), line[2])
options = options.strip()
# with ' < 1:' on the next line, this is a no-op:
if len(sys.argv) < 1:
print(__doc__ % locals())
sys.exit(1)
try:
opts, files = getopt.gnu_getopt(sys.argv[1:], "hvV", ["help", "version","verbose",])
except Exception, e:
print( "%s: %s" % (program, e))
sys.exit(1)
# ----------------------------------------------------------------------
# Handle options
# set default values, if any
opt_verbose = 0
# handle individual options
for opt, value in opts:
if opt in ["-h", "--help"]: # Output this help, then exit
print( __doc__ % locals() )
sys.exit(1)
elif opt in ["-v", "--version"]: # Output version information, then exit
print( program, version )
sys.exit(0)
elif opt in ["-V", "--verbose"]: # Output version information, then exit
opt_verbose += 1
# ----------------------------------------------------------------------
def say(s):
sys.stderr.write("%s\n" % (s))
# ----------------------------------------------------------------------
def note(s):
if opt_verbose:
sys.stderr.write("%s\n" % (s))
# ----------------------------------------------------------------------
def die(s, error=1):
sys.stderr.write("\n%s: Error: %s\n\n" % (program, s))
sys.exit(error)
# ----------------------------------------------------------------------
# The program itself
import os
import json
cwd = os.getcwd()
if cwd.split(os.path.sep)[-1] != 'trunk':
die("Expected to run this operation in trunk, but the current\ndirectory is '%s'" % cwd)
# ----------------------------------------------------------------------
# Some utility functions
def pipe(cmd, inp=None):
import shlex
from subprocess import Popen, PIPE
args = shlex.split(cmd)
bufsize = 4096
stdin = PIPE if inp else None
pipe = Popen(args, stdin=stdin, stdout=PIPE, stderr=PIPE, bufsize=bufsize)
out, err = pipe.communicate(inp)
code = pipe.returncode
if code != 0:
raise OSError(err)
return out
def split_loginfo(line):
try:
parts = line.split()
rev = parts[0][1:]
who = parts[2]
date = parts[4]
time = parts[5]
tz = parts[6]
when = tzparse.tzparse(" ".join(parts[4:7]), "%Y-%m-%d %H:%M:%S %Z")
when = when.astimezone(pytz.utc)
except ValueError as e:
sys.stderr.write("Bad log line format: %s\n %s\n" % (line, e))
return rev, who, when
# ----------------------------------------------------------------------
# Get repository information
svn_info = {}
for line in pipe('svn info .').splitlines():
if line:
key, value = line.strip().split(':', 1)
svn_info[key] = value.strip()
repo = svn_info["Repository Root"]
head = int(svn_info['Revision'])
# Get current mergeinfo from cache and svn
cachefn = os.path.join(os.environ.get('HOME', '.'), '.mergeinfo')
if os.path.exists(cachefn):
with open(cachefn, "r") as file:
cache = json.load(file)
else:
sys.stderr.write("No merge info cache file found -- will have to extract all information from SVN.\n"+
"This may take some time.\n\n")
cache = {}
mergeinfo = cache[repo] if repo in cache else {}
merged_revs = {}
write_cache = False
for line in pipe('svn propget svn:mergeinfo .').splitlines():
if line in mergeinfo:
merged = mergeinfo[line]
else:
merged = {}
branch, revs = line.strip().split(':',1)
for part in revs.split(','):
if '-' in part:
beg, end = part.split('-')
try:
commit_log = pipe('svn log -v -r %s:%s %s%s' % (beg, end, repo, branch))
for logline in commit_log.splitlines():
if re.search('^r[0-9]+ ', logline):
rev, who, when = split_loginfo(logline)
merged[rev] = branch[1:]
write_cache = True
except OSError:
pass
else:
merged[part] = branch[1:]
write_cache = True
mergeinfo[line] = merged
merged_revs.update(merged)
if write_cache:
cache[repo] = mergeinfo
with open(cachefn, "w") as file:
json.dump(cache, file, indent=2, sort_keys=True)
def get_list(repo, filename):
list = []
note("Reading list from '%s'" % filename)
with open(filename) as file:
for line in file:
line = line.strip()
if line.startswith('#') or line == "":
continue
try:
note(" '%s'" % line)
parts = line.split()
if len(parts) >1 and parts[1] == '@':
branch, rev = parts[0], parts[2]
changeset = "%s@%s" % (branch, rev)
else:
changeset = parts[0]
branch, rev = changeset.split('@')
if branch.startswith('/'):
branch = branch[1:]
if not (rev in merged_revs and branch == merged_revs[rev]):
list += [(rev, repo, branch),]
#elif rev in merged_revs and not branch == merged_revs[rev]:
# sys.stderr.write('Rev %s: %s != %s' % (rev, branch, merged_revs[rev]))
else:
#sys.stderr.write('Already merged: merged_revs[%s]: %s\n' % (rev, merged_revs[rev]))
pass
except ValueError as e:
sys.stderr.write("Bad changeset specification in %s: '%s': %s\n" % (file.name, changeset, e))
return list
def get_ready_commits(repo, tree):
list = []
note("Getting ready commits from '%s'" % tree)
cmd = 'svn log -v -r %s:HEAD %s/%s/' % ((head-500), repo, tree)
if opt_verbose > 1:
note("Running '%s' ..." % cmd)
commit_log = pipe(cmd)
for line in commit_log.splitlines():
if re.search('^r[0-9]+ ', line):
rev, who, when = split_loginfo(line)
branch = None
continue
if (line.startswith(' M') or line.startswith(' A') or line.startswith(' D')) and branch == None:
type, path = line[:4], line[5:]
branch = '/'.join(path.split('/')[1:4])
elif re.search("(?i)((commit|branch) ready (for|to) merge)", line):
if not (rev in merged_revs and branch == merged_revs[rev]):
note(" %s %s: %s@%s" % (when.strftime("%Y-%m-%d %H:%MZ"), who, branch, rev))
list += [(rev, repo, branch),]
elif rev in merged_revs and not branch == merged_revs[rev]:
sys.stderr.write('Rev %s: %s != %s' % (rev, branch, merged_revs[rev]))
else:
pass
else:
pass
return list
ready = get_list(repo, 'ready-for-merge')
hold = get_list(repo, 'hold-for-merge')
ready += get_ready_commits(repo, 'personal')
ready += get_ready_commits(repo, 'branch/amsl')
ready += get_ready_commits(repo, 'branch/iola')
ready_commits = {}
not_passed = {}
for entry in ready:
rev, repo, branch = entry
# Get the time, committer, and commit message
cmd = 'svn log -v -r %s %s/%s/' % (rev, repo, branch)
if opt_verbose > 1:
note("Running '%s' ..." % cmd)
try:
loginfo = pipe(cmd).splitlines()
except OSError:
continue
try:
rev, who, when = split_loginfo(loginfo[1])
except IndexError:
die("Wrong changeset version in %s@%s ?" % (branch, rev))
for line in loginfo[3:]:
type, path = line[:4], line[5:]
if 'M' in type or 'A' in type or 'D' in type:
break
# Get the test status
try:
cmd = 'svn propget --revprop -r %s "test:unittest"' % rev
unittest = pipe(cmd).strip()
except OSError as e:
if "E200017" in str(e):
unittestt = ""
pass
else:
raise
#
merge_path = os.path.join(*path.split(os.path.sep)[:4])
if not (rev, repo, merge_path) in hold:
output_line = "%s %-24s ^/%s@%s" % (when.strftime("%Y-%m-%d_%H:%MZ"), who+":", merge_path, rev)
if unittest == 'passed':
ready_commits[when] = output_line
else:
not_passed[when] = output_line
keys = not_passed.keys()
keys.sort()
if len(keys) > 0:
sys.stderr.write("Commits marked ready which haven't passed the test suite:\n")
for key in keys:
sys.stderr.write(not_passed[key]+'\n')
sys.stderr.write('\n')
keys = ready_commits.keys()
keys.sort()
for key in keys:
print(ready_commits[key])
print("\n%s pending merges" % len(keys))