forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathgit-llvm
executable file
·331 lines (263 loc) · 9 KB
/
git-llvm
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
#
# ======- git-llvm - LLVM Git Help Integration ---------*- python -*--========#
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# ==------------------------------------------------------------------------==#
"""
git-llvm integration
====================
This file provides integration for git.
The git llvm push sub-command can be used to push changes to GitHub. It is
designed to be a thin wrapper around git, and its main purpose is to
detect and prevent merge commits from being pushed to the main repository.
Usage:
git-llvm push <upstream-branch>
This will push changes from the current HEAD to the branch <upstream-branch>.
"""
from __future__ importprint_function
importargparse
importcollections
importos
importre
importshutil
importsubprocess
importsys
importtime
importgetpass
assertsys.version_info>= (2, 7)
try:
dict.iteritems
exceptAttributeError:
# Python 3
defiteritems(d):
returniter(d.items())
else:
# Python 2
defiteritems(d):
returnd.iteritems()
try:
# Python 3
fromshleximportquote
exceptImportError:
# Python 2
frompipesimportquote
# It's *almost* a straightforward mapping from the monorepo to svn...
LLVM_MONOREPO_SVN_MAPPING= {
d: (d+'/trunk')
fordin [
'clang-tools-extra',
'compiler-rt',
'debuginfo-tests',
'dragonegg',
'klee',
'libc',
'libclc',
'libcxx',
'libcxxabi',
'libunwind',
'lld',
'lldb',
'llgo',
'llvm',
'openmp',
'parallel-libs',
'polly',
'pstl',
]
}
LLVM_MONOREPO_SVN_MAPPING.update({'clang': 'cfe/trunk'})
LLVM_MONOREPO_SVN_MAPPING.update({'': 'monorepo-root/trunk'})
SPLIT_REPO_NAMES= {'llvm-'+d: d+'/trunk'
fordin ['www', 'zorg', 'test-suite', 'lnt']}
VERBOSE=False
QUIET=False
dev_null_fd=None
GIT_ORG='llvm'
GIT_REPO='llvm-project'
GIT_URL='github.com/{}/{}.git'.format(GIT_ORG, GIT_REPO)
defeprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
deflog(*args, **kwargs):
ifQUIET:
return
print(*args, **kwargs)
deflog_verbose(*args, **kwargs):
ifnotVERBOSE:
return
print(*args, **kwargs)
defdie(msg):
eprint(msg)
sys.exit(1)
defask_confirm(prompt):
# Python 2/3 compatibility
try:
read_input=raw_input
exceptNameError:
read_input=input
whileTrue:
query=read_input('%s (y/N): '% (prompt))
ifquery.lower() notin ['y','n', '']:
print('Expect y or n!')
continue
returnquery.lower() =='y'
defget_dev_null():
"""Lazily create a /dev/null fd for use in shell()"""
globaldev_null_fd
ifdev_null_fdisNone:
dev_null_fd=open(os.devnull, 'w')
returndev_null_fd
defshell(cmd, strip=True, cwd=None, stdin=None, die_on_failure=True,
ignore_errors=False, text=True, print_raw_stderr=False):
# Escape args when logging for easy repro.
quoted_cmd= [quote(arg) forargincmd]
log_verbose('Running in %s: %s'% (cwd, ' '.join(quoted_cmd)))
err_pipe=subprocess.PIPE
ifignore_errors:
# Silence errors if requested.
err_pipe=get_dev_null()
start=time.time()
p=subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=err_pipe,
stdin=subprocess.PIPE,
universal_newlines=text)
stdout, stderr=p.communicate(input=stdin)
elapsed=time.time() -start
log_verbose('Command took %0.1fs'%elapsed)
ifp.returncode==0orignore_errors:
ifstderrandnotignore_errors:
ifnotprint_raw_stderr:
eprint('`%s` printed to stderr:'%' '.join(quoted_cmd))
eprint(stderr.rstrip())
ifstrip:
iftext:
stdout=stdout.rstrip('\r\n')
else:
stdout=stdout.rstrip(b'\r\n')
ifVERBOSE:
forlinstdout.splitlines():
log_verbose("STDOUT: %s"%l)
returnstdout
err_msg='`%s` returned %s'% (' '.join(quoted_cmd), p.returncode)
eprint(err_msg)
ifstderr:
eprint(stderr.rstrip())
ifdie_on_failure:
sys.exit(2)
raiseRuntimeError(err_msg)
defgit(*cmd, **kwargs):
returnshell(['git'] +list(cmd), **kwargs)
defsvn(cwd, *cmd, **kwargs):
returnshell(['svn'] +list(cmd), cwd=cwd, **kwargs)
defprogram_exists(cmd):
ifsys.platform=='win32'andnotcmd.endswith('.exe'):
cmd+='.exe'
forpathinos.environ["PATH"].split(os.pathsep):
ifos.access(os.path.join(path, cmd), os.X_OK):
returnTrue
returnFalse
defget_fetch_url():
return'https://{}'.format(GIT_URL)
defget_push_url(user='', ssh=False):
ifssh:
return'ssh://git@{}'.format(GIT_URL)
return'https://{}'.format(GIT_URL)
defget_revs_to_push(branch):
# Fetch the latest upstream to determine which commits will be pushed.
git('fetch', get_fetch_url(), branch)
commits=git('rev-list', '--ancestry-path', 'FETCH_HEAD..HEAD').splitlines()
# Reverse the order so we commit the oldest commit first
commits.reverse()
returncommits
defgit_push_one_rev(rev, dry_run, branch, ssh):
# Check if this a merge commit by counting the number of parent commits.
# More than 1 parent commmit means this is a merge.
num_parents=len(git('show', '--no-patch', '--format="%P"', rev).split())
ifnum_parents>1:
raiseException("Merge commit detected, cannot push ", rev)
ifnum_parents!=1:
raiseException("Error detecting number of parents for ", rev)
ifdry_run:
print("[DryRun] Would push", rev)
return
# Second push to actually push the commit
git('push', get_push_url(ssh=ssh), '{}:{}'.format(rev, branch), print_raw_stderr=True)
defcmd_push(args):
'''Push changes to git:'''
dry_run=args.dry_run
revs=get_revs_to_push(args.branch)
ifnotrevs:
die('Nothing to push')
log('%sPushing %d commit%s:\n%s'%
('[DryRun] 'ifdry_runelse'', len(revs),
's'iflen(revs) !=1else'',
'\n'.join(' '+git('show', '--oneline', '--quiet', c)
forcinrevs)))
# Ask confirmation if multiple commits are about to be pushed
ifnotargs.forceandlen(revs) >1:
ifnotask_confirm("Are you sure you want to create %d commits?"%len(revs)):
die("Aborting")
forrinrevs:
git_push_one_rev(r, dry_run, args.branch, args.ssh)
if__name__=='__main__':
ifnotprogram_exists('git'):
die('error: git-llvm needs git command, but git is not installed.')
argv=sys.argv[1:]
p=argparse.ArgumentParser(
prog='git llvm', formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__)
subcommands=p.add_subparsers(title='subcommands',
description='valid subcommands',
help='additional help')
verbosity_group=p.add_mutually_exclusive_group()
verbosity_group.add_argument('-q', '--quiet', action='store_true',
help='print less information')
verbosity_group.add_argument('-v', '--verbose', action='store_true',
help='print more information')
parser_push=subcommands.add_parser(
'push', description=cmd_push.__doc__,
help='push changes back to the LLVM SVN repository')
parser_push.add_argument(
'-n',
'--dry-run',
dest='dry_run',
action='store_true',
help='Do everything other than commit to svn. Leaves junk in the svn '
'repo, so probably will not work well if you try to commit more '
'than one rev.')
parser_push.add_argument(
'-s',
'--ssh',
dest='ssh',
action='store_true',
help='Use the SSH protocol for authentication, '
'instead of HTTPS with username and password.')
parser_push.add_argument(
'-f',
'--force',
action='store_true',
help='Do not ask for confirmation when pushing multiple commits.')
parser_push.add_argument(
'branch',
metavar='GIT_BRANCH',
type=str,
default='master',
nargs='?',
help="branch to push (default: everything not in the branch's "
'upstream)')
parser_push.set_defaults(func=cmd_push)
args=p.parse_args(argv)
VERBOSE=args.verbose
QUIET=args.quiet
# Python3 workaround, for when not arguments are provided.
# See https://bugs.python.org/issue16308
try:
func=args.func
exceptAttributeError:
# No arguments or subcommands were given.
parser.print_help()
parser.exit()
# Dispatch to the right subcommand
args.func(args)