|
1
|
+#!/usr/bin/python
|
|
2
|
+# PYTHON_ARGCOMPLETE_OK
|
|
3
|
+"""
|
|
4
|
+Useful tools for working on tor-browser repository.
|
|
5
|
+"""
|
|
6
|
+
|
|
7
|
+import sys
|
|
8
|
+import termios
|
|
9
|
+import os
|
|
10
|
+import atexit
|
|
11
|
+import tempfile
|
|
12
|
+import subprocess
|
|
13
|
+import re
|
|
14
|
+import json
|
|
15
|
+import urllib.request
|
|
16
|
+import argparse
|
|
17
|
+import argcomplete
|
|
18
|
+
|
|
19
|
+GIT_PATH = "/usr/bin/git"
|
|
20
|
+UPSTREAM_URLS = [
|
|
21
|
+ "https://gitlab.torproject.org/tpo/applications/tor-browser.git",
|
|
22
|
+ "git@xxxxxxxxxxxxxxxxxxxxx:tpo/applications/tor-browser.git",
|
|
23
|
+]
|
|
24
|
+FIXUP_PREPROCESSOR_EDITOR = "git-rebase-fixup-preprocessor"
|
|
25
|
+USER_EDITOR_ENV_NAME = "GIT_REBASE_FIXUP_PREPROCESSOR_USER_EDITOR"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+def git_run(args, check=True, env=None):
|
|
29
|
+ """
|
|
30
|
+ Run a git command with output sent to stdout.
|
|
31
|
+ """
|
|
32
|
+ if env is not None:
|
|
33
|
+ tmp_env = dict(os.environ)
|
|
34
|
+ for key, value in env.items():
|
|
35
|
+ tmp_env[key] = value
|
|
36
|
+ env = tmp_env
|
|
37
|
+ subprocess.run([GIT_PATH, *args], check=check, env=env)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+def git_get(args):
|
|
41
|
+ """
|
|
42
|
+ Run a git command with each non-empty line returned in a list.
|
|
43
|
+ """
|
|
44
|
+ git_process = subprocess.run(
|
|
45
|
+ [GIT_PATH, *args], text=True, stdout=subprocess.PIPE, check=True
|
|
46
|
+ )
|
|
47
|
+ return [line for line in git_process.stdout.split("\n") if line]
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+local_root = None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+def get_local_root():
|
|
54
|
+ """
|
|
55
|
+ Get the path for the tor-browser root directory.
|
|
56
|
+ """
|
|
57
|
+ global local_root
|
|
58
|
+ if local_root is None:
|
|
59
|
+ try:
|
|
60
|
+ git_root = git_get(["rev-parse", "--show-toplevel"])[0]
|
|
61
|
+ except subprocess.CalledProcessError:
|
|
62
|
+ git_root = None
|
|
63
|
+ if git_root is None or os.path.basename(git_root) != "tor-browser":
|
|
64
|
+ local_root = ""
|
|
65
|
+ else:
|
|
66
|
+ local_root = git_root
|
|
67
|
+ return local_root
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+upstream_name = None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+def get_upstream_name():
|
|
74
|
+ """
|
|
75
|
+ Get the name of the upstream remote.
|
|
76
|
+ """
|
|
77
|
+ global upstream_name
|
|
78
|
+ if upstream_name is None:
|
|
79
|
+ for remote in git_get(["remote"]):
|
|
80
|
+ fetch_url = git_get(["remote", "get-url", remote])[0]
|
|
81
|
+ if fetch_url in UPSTREAM_URLS:
|
|
82
|
+ upstream_name = remote
|
|
83
|
+ break
|
|
84
|
+ if upstream_name is None:
|
|
85
|
+ raise Exception("No upstream remote found.")
|
|
86
|
+ return upstream_name
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+class Reference:
|
|
90
|
+ """Represents a git reference to a commit."""
|
|
91
|
+
|
|
92
|
+ def __init__(self, name, commit):
|
|
93
|
+ self.name = name
|
|
94
|
+ self.commit = commit
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+def get_refs(ref_type, name_start):
|
|
98
|
+ """
|
|
99
|
+ Get a list of references that match the given 'ref_type' ("tag" or "remote"
|
|
100
|
+ or "head") that starts with the given 'name_start'.
|
|
101
|
+ """
|
|
102
|
+ if ref_type == "tag":
|
|
103
|
+ # Instead of returning tag hash, return the commit hash it points to.
|
|
104
|
+ fstring = "%(*objectname)"
|
|
105
|
+ ref_start = "refs/tags/"
|
|
106
|
+ elif ref_type == "remote":
|
|
107
|
+ fstring = "%(objectname)"
|
|
108
|
+ ref_start = "refs/remotes/"
|
|
109
|
+ elif ref_type == "head":
|
|
110
|
+ fstring = "%(objectname)"
|
|
111
|
+ ref_start = "refs/heads/"
|
|
112
|
+ else:
|
|
113
|
+ raise TypeError(f"Unknown type {ref_type}")
|
|
114
|
+
|
|
115
|
+ fstring = f"{fstring},%(refname)"
|
|
116
|
+ pattern = f"{ref_start}{name_start}**"
|
|
117
|
+
|
|
118
|
+ def line_to_ref(line):
|
|
119
|
+ [commit, ref_name] = line.split(",", 1)
|
|
120
|
+ return Reference(ref_name.replace(ref_start, "", 1), commit)
|
|
121
|
+
|
|
122
|
+ return [
|
|
123
|
+ line_to_ref(line)
|
|
124
|
+ for line in git_get(["for-each-ref", f"--format={fstring}", pattern])
|
|
125
|
+ ]
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+def get_nearest_ref(ref_type, name_start, search_from):
|
|
129
|
+ """
|
|
130
|
+ Search backwards from the 'search_from' commit to find the first commit
|
|
131
|
+ that matches the given 'ref_type' that starts with the given 'name_start'.
|
|
132
|
+ """
|
|
133
|
+ ref_list = get_refs(ref_type, name_start)
|
|
134
|
+
|
|
135
|
+ for commit in git_get(["rev-list", "-1000", search_from]):
|
|
136
|
+ for ref in ref_list:
|
|
137
|
+ if commit == ref.commit:
|
|
138
|
+ return ref
|
|
139
|
+
|
|
140
|
+ raise Exception(f"No {name_start} commit found in the last 1000 commits")
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+def get_firefox_ref(search_from):
|
|
144
|
+ """
|
|
145
|
+ Search backwards from the 'search_from' commit to find the commit that comes
|
|
146
|
+ from firefox.
|
|
147
|
+ """
|
|
148
|
+ return get_nearest_ref("tag", "FIREFOX_", search_from)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+def get_upstream_commit(search_from):
|
|
152
|
+ """
|
|
153
|
+ Get the first common ancestor of search_from that is also in its upstream
|
|
154
|
+ branch.
|
|
155
|
+ """
|
|
156
|
+ return git_get(["merge-base", search_from, f"{search_from}@{{upstream}}"])[0]
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+def get_changed_files(from_commit, staged=False):
|
|
160
|
+ """
|
|
161
|
+ Get a list of filenames relative to the current working directory that have
|
|
162
|
+ been changed since 'from_commit' (non-inclusive).
|
|
163
|
+ """
|
|
164
|
+ args = ["diff"]
|
|
165
|
+ if staged:
|
|
166
|
+ args.append("--staged")
|
|
167
|
+ args.append("--name-only")
|
|
168
|
+ args.append(from_commit)
|
|
169
|
+ return [
|
|
170
|
+ os.path.relpath(os.path.join(get_local_root(), filename))
|
|
171
|
+ for filename in git_get(args)
|
|
172
|
+ ]
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+def file_contains(filename, regex):
|
|
176
|
+ """
|
|
177
|
+ Return whether the file is a utf-8 text file containing the regular
|
|
178
|
+ _expression_ given by 'regex'.
|
|
179
|
+ """
|
|
180
|
+ with open(filename, "r", encoding="utf-8") as file:
|
|
181
|
+ try:
|
|
182
|
+ for line in file:
|
|
183
|
+ if regex.search(line):
|
|
184
|
+ return True
|
|
185
|
+ except UnicodeDecodeError:
|
|
186
|
+ # Not a text file
|
|
187
|
+ pass
|
|
188
|
+ return False
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+def get_gitlab_default():
|
|
192
|
+ """
|
|
193
|
+ Get the name of the default branch on gitlab.
|
|
194
|
+ """
|
|
195
|
+ query = """
|
|
196
|
+ query {
|
|
197
|
+ project(fullPath: "tpo/applications/tor-browser") {
|
|
198
|
+ repository { rootRef }
|
|
199
|
+ }
|
|
200
|
+ }
|
|
201
|
+ """
|
|
202
|
+ request_data = {"query": re.sub(r"\s+", "", query)}
|
|
203
|
+ gitlab_request = urllib.request.Request(
|
|
204
|
+ "https://gitlab.torproject.org/api/graphql",
|
|
205
|
+ headers={
|
|
206
|
+ "Content-Type": "application/json",
|
|
207
|
+ "User-Agent": "",
|
|
208
|
+ },
|
|
209
|
+ data=json.dumps(request_data).encode("ascii"),
|
|
210
|
+ )
|
|
211
|
+
|
|
212
|
+ with urllib.request.urlopen(gitlab_request, timeout=20) as response:
|
|
213
|
+ branch_name = json.load(response)["data"]["project"]["repository"]["rootRef"]
|
|
214
|
+ return f"{get_upstream_name()}/{branch_name}"
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+def within_tor_browser_root():
|
|
218
|
+ """
|
|
219
|
+ Whether we are with the tor browser root.
|
|
220
|
+ """
|
|
221
|
+ root = get_local_root()
|
|
222
|
+ if not root:
|
|
223
|
+ return False
|
|
224
|
+ return os.path.commonpath([os.getcwd(), root]) == root
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+# * -------------------- *
|
|
228
|
+# | Methods for commands |
|
|
229
|
+# * -------------------- *
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+def show_firefox_commit(_args):
|
|
233
|
+ """
|
|
234
|
+ Print the tag name and commit for the last firefox commit below the current
|
|
235
|
+ HEAD.
|
|
236
|
+ """
|
|
237
|
+ ref = get_firefox_ref("HEAD")
|
|
238
|
+ print(ref.name)
|
|
239
|
+ print(ref.commit)
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+def show_upstream_commit(_args):
|
|
243
|
+ """
|
|
244
|
+ Print the last upstream commit for the current HEAD.
|
|
245
|
+ """
|
|
246
|
+ print(get_upstream_commit("HEAD"))
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+def show_log(args):
|
|
250
|
+ """
|
|
251
|
+ Show the git log between the current HEAD and the last firefox commit.
|
|
252
|
+ """
|
|
253
|
+ commit = get_firefox_ref("HEAD").commit
|
|
254
|
+ git_run(["log", f"{commit}..HEAD", *args.gitargs], check=False)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+def show_files_containing(args):
|
|
258
|
+ """
|
|
259
|
+ List all the files that that have been modified for tor browser, that also
|
|
260
|
+ contain a regular _expression_.
|
|
261
|
+ """
|
|
262
|
+ try:
|
|
263
|
+ regex = re.compile(args.regex)
|
|
264
|
+ except re.error as err:
|
|
265
|
+ raise Exception(f"{args.regex} is not a valid python regex") from err
|
|
266
|
+
|
|
267
|
+ file_list = get_changed_files(get_firefox_ref("HEAD").commit)
|
|
268
|
+
|
|
269
|
+ for filename in file_list:
|
|
270
|
+ if not os.path.isfile(filename):
|
|
271
|
+ # deleted ofile
|
|
272
|
+ continue
|
|
273
|
+ if file_contains(filename, regex):
|
|
274
|
+ print(filename)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+def show_changed_files(_args):
|
|
278
|
+ """
|
|
279
|
+ List all the files that have been modified relative to upstream.
|
|
280
|
+ """
|
|
281
|
+ for filename in get_changed_files(get_upstream_commit("HEAD")):
|
|
282
|
+ print(filename)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+def lint_changed_files(args):
|
|
286
|
+ """
|
|
287
|
+ Lint all the files that have been modified relative to upstream.
|
|
288
|
+ """
|
|
289
|
+ os.chdir(get_local_root())
|
|
290
|
+ file_list = [
|
|
291
|
+ f
|
|
292
|
+ for f in get_changed_files(get_upstream_commit("HEAD"))
|
|
293
|
+ if os.path.isfile(f) # Not deleted
|
|
294
|
+ ]
|
|
295
|
+ command_base = ["./mach", "lint"]
|
|
296
|
+ lint_process = subprocess.run(
|
|
297
|
+ [*command_base, "--list"], text=True, stdout=subprocess.PIPE, check=True
|
|
298
|
+ )
|
|
299
|
+
|
|
300
|
+ linters = []
|
|
301
|
+ for line in lint_process.stdout.split("\n"):
|
|
302
|
+ if not line:
|
|
303
|
+ continue
|
|
304
|
+ if line.startswith("Note that clang-tidy"):
|
|
305
|
+ # Note at end
|
|
306
|
+ continue
|
|
307
|
+ if line == "license":
|
|
308
|
+ # don't lint the license
|
|
309
|
+ continue
|
|
310
|
+ if line.startswith("android-"):
|
|
311
|
+ continue
|
|
312
|
+ # lint everything else
|
|
313
|
+ linters.append("-l")
|
|
314
|
+ linters.append(line)
|
|
315
|
+
|
|
316
|
+ if not linters:
|
|
317
|
+ raise Exception("No linters found")
|
|
318
|
+
|
|
319
|
+ if args.fix:
|
|
320
|
+ command_base.append("--fix")
|
|
321
|
+ # We add --warnings since clang only reports whitespace issues as warnings.
|
|
322
|
+ lint_process = subprocess.run(
|
|
323
|
+ [*command_base, "--warnings", *linters, *file_list], check=False
|
|
324
|
+ )
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+def prompt_user(prompt, convert):
|
|
328
|
+ """
|
|
329
|
+ Ask the user for some input until the given converter returns without
|
|
330
|
+ throwing a ValueError.
|
|
331
|
+ """
|
|
332
|
+ while True:
|
|
333
|
+ # Flush out stdin.
|
|
334
|
+ termios.tcflush(sys.stdin, termios.TCIFLUSH)
|
|
335
|
+ print(prompt, end="")
|
|
336
|
+ sys.stdout.flush()
|
|
337
|
+ try:
|
|
338
|
+ return convert(sys.stdin.readline().strip())
|
|
339
|
+ except ValueError:
|
|
340
|
+ # Continue to prompt.
|
|
341
|
+ pass
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+def binary_reply_default_no(value):
|
|
345
|
+ """Process a 'y' or 'n' reply, defaulting to 'n' if empty."""
|
|
346
|
+ if value == "":
|
|
347
|
+ return False
|
|
348
|
+ if value.lower() == "y":
|
|
349
|
+ return True
|
|
350
|
+ if value.lower() == "n":
|
|
351
|
+ return False
|
|
352
|
+ raise ValueError()
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+def get_fixup_for_file(filename, firefox_commit):
|
|
356
|
+ """Find the commit the given file should fix up."""
|
|
357
|
+
|
|
358
|
+ def parse_log_line(line):
|
|
359
|
+ [commit, short_ref, title] = line.split(",", 2)
|
|
360
|
+ return {"commit": commit, "short-ref": short_ref, "title": title}
|
|
361
|
+
|
|
362
|
+ options = [
|
|
363
|
+ parse_log_line(line)
|
|
364
|
+ for line in git_get(
|
|
365
|
+ [
|
|
366
|
+ "log",
|
|
367
|
+ "--pretty=format:%H,%h,%s",
|
|
368
|
+ f"{firefox_commit}..HEAD",
|
|
369
|
+ "--",
|
|
370
|
+ filename,
|
|
371
|
+ ]
|
|
372
|
+ )
|
|
373
|
+ ]
|
|
374
|
+ if not options:
|
|
375
|
+ print(f"No commit found for {filename}")
|
|
376
|
+ return None
|
|
377
|
+
|
|
378
|
+ def valid_index(val):
|
|
379
|
+ if val == "d":
|
|
380
|
+ return val
|
|
381
|
+
|
|
382
|
+ is_patch = val.startswith("p")
|
|
383
|
+ if is_patch:
|
|
384
|
+ val = val[1:]
|
|
385
|
+
|
|
386
|
+ # May raise a ValueError.
|
|
387
|
+ as_index = int(val)
|
|
388
|
+ if as_index < 0 or as_index > len(options):
|
|
389
|
+ raise ValueError()
|
|
390
|
+
|
|
391
|
+ if as_index == 0:
|
|
392
|
+ if is_patch:
|
|
393
|
+ raise ValueError()
|
|
394
|
+ return None
|
|
395
|
+
|
|
396
|
+ return (is_patch, options[as_index - 1]["commit"])
|
|
397
|
+
|
|
398
|
+ while True:
|
|
399
|
+ print(f"For {filename}:\n")
|
|
400
|
+ print(" \x1b[1m0\x1b[0m: None")
|
|
401
|
+ for index, opt in enumerate(options):
|
|
402
|
+ print(
|
|
403
|
+ f" \x1b[1m{index + 1}\x1b[0m: "
|
|
404
|
+ + f"\x1b[1;38;5;212m{opt['short-ref']}\x1b[0m "
|
|
405
|
+ + opt["title"]
|
|
406
|
+ )
|
|
407
|
+ print("")
|
|
408
|
+ response = prompt_user(
|
|
409
|
+ "Choose an <index> to fixup, or '0' to skip this file, "
|
|
410
|
+ "or 'd' to view the pending diff, "
|
|
411
|
+ "or 'p<index>' to view the patch for the index: ",
|
|
412
|
+ valid_index,
|
|
413
|
+ )
|
|
414
|
+ if response is None:
|
|
415
|
+ # Skip this file.
|
|
416
|
+ return None
|
|
417
|
+
|
|
418
|
+ if response == "d":
|
|
419
|
+ git_run(["diff", "--", filename])
|
|
420
|
+ continue
|
|
421
|
+
|
|
422
|
+ view_patch, commit = response
|
|
423
|
+ if view_patch:
|
|
424
|
+ git_run(["log", "-p", "-1", commit, "--", filename])
|
|
425
|
+ continue
|
|
426
|
+
|
|
427
|
+ return commit
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+def auto_fixup(_args):
|
|
431
|
+ """
|
|
432
|
+ Automatically find and fix up commits using the current unstaged changes.
|
|
433
|
+ """
|
|
434
|
+ # Only want to search as far back as the firefox commit.
|
|
435
|
+ firefox_commit = get_firefox_ref("HEAD").commit
|
|
436
|
+
|
|
437
|
+ staged_files = get_changed_files("HEAD", staged=True)
|
|
438
|
+ if staged_files:
|
|
439
|
+ raise Exception(f"Have already staged files: {staged_files}")
|
|
440
|
+
|
|
441
|
+ fixups = {}
|
|
442
|
+ for filename in get_changed_files("HEAD"):
|
|
443
|
+ commit = get_fixup_for_file(filename, firefox_commit)
|
|
444
|
+ if commit is None:
|
|
445
|
+ continue
|
|
446
|
+ if commit not in fixups:
|
|
447
|
+ fixups[commit] = [filename]
|
|
448
|
+ else:
|
|
449
|
+ fixups[commit].append(filename)
|
|
450
|
+ print("")
|
|
451
|
+
|
|
452
|
+ for commit, files in fixups.items():
|
|
453
|
+ print("")
|
|
454
|
+ git_run(["add", *files])
|
|
455
|
+ git_run(["commit", f"--fixup={commit}"])
|
|
456
|
+ print("")
|
|
457
|
+
|
|
458
|
+ if prompt_user(
|
|
459
|
+ "Edit fixup commit message? (y/\x1b[4mn\x1b[0m)", binary_reply_default_no
|
|
460
|
+ ):
|
|
461
|
+ git_run(["commit", "--amend"])
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+def clean_fixups(_args):
|
|
465
|
+ """
|
|
466
|
+ Perform an interactive rebase that automatically applies fixups, similar to
|
|
467
|
+ --autosquash but also works on fixups of fixups.
|
|
468
|
+ """
|
|
469
|
+ user_editor = git_get(["var", "GIT_SEQUENCE_EDITOR"])[0]
|
|
470
|
+ sub_editor = os.path.join(
|
|
471
|
+ os.path.dirname(os.path.realpath(__file__)), FIXUP_PREPROCESSOR_EDITOR
|
|
472
|
+ )
|
|
473
|
+
|
|
474
|
+ git_run(
|
|
475
|
+ ["rebase", "--interactive"],
|
|
476
|
+ check=False,
|
|
477
|
+ env={"GIT_SEQUENCE_EDITOR": sub_editor, USER_EDITOR_ENV_NAME: user_editor},
|
|
478
|
+ )
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+def show_default(_args):
|
|
482
|
+ """
|
|
483
|
+ Print the default branch name from gitlab.
|
|
484
|
+ """
|
|
485
|
+ print(get_gitlab_default())
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+def branch_from_default(args):
|
|
489
|
+ """
|
|
490
|
+ Fetch the default gitlab branch from upstream and create a new local branch.
|
|
491
|
+ """
|
|
492
|
+ default_branch = get_gitlab_default()
|
|
493
|
+
|
|
494
|
+ git_run(["fetch"], get_upstream_name())
|
|
495
|
+ git_run(["switch", "--create", args.branchname, "--track", default_branch])
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+def rebase_on_default(_args):
|
|
499
|
+ """
|
|
500
|
+ Fetch the default gitlab branch from upstream and rebase the current branch
|
|
501
|
+ on top.
|
|
502
|
+ """
|
|
503
|
+ try:
|
|
504
|
+ branch_name = git_get(["branch", "--show-current"])[0]
|
|
505
|
+ except IndexError:
|
|
506
|
+ raise Exception("No current branch")
|
|
507
|
+
|
|
508
|
+ current_upstream = get_upstream_commit("HEAD")
|
|
509
|
+ default_branch = get_gitlab_default()
|
|
510
|
+
|
|
511
|
+ git_run(["fetch"], get_upstream_name())
|
|
512
|
+ # We set the new upstream before the rebase in case there are conflicts.
|
|
513
|
+ git_run(["branch", f"--set-upstream-to={default_branch}"])
|
|
514
|
+ git_run(
|
|
515
|
+ ["rebase", "--onto", default_branch, current_upstream, branch_name], check=False
|
|
516
|
+ )
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+def show_range_diff(args):
|
|
520
|
+ """
|
|
521
|
+ Show the range diff between two branches, from their firefox bases.
|
|
522
|
+ """
|
|
523
|
+ firefox_commit_1 = get_firefox_ref(args.branch1).commit
|
|
524
|
+ firefox_commit_2 = get_firefox_ref(args.branch2).commit
|
|
525
|
+ git_run(
|
|
526
|
+ [
|
|
527
|
+ "range-diff",
|
|
528
|
+ f"{firefox_commit_1}..{args.branch1}",
|
|
529
|
+ f"{firefox_commit_2}..{args.branch2}",
|
|
530
|
+ ],
|
|
531
|
+ check=False,
|
|
532
|
+ )
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+def show_diff_diff(args):
|
|
536
|
+ """
|
|
537
|
+ Show the diff between the diffs of two branches, relative to their firefox
|
|
538
|
+ bases.
|
|
539
|
+ """
|
|
540
|
+ config_res = git_get(["config", "--get", "diff.tool"])
|
|
541
|
+ if not config_res:
|
|
542
|
+ raise Exception("No diff.tool configured for git")
|
|
543
|
+ diff_tool = config_res[0]
|
|
544
|
+
|
|
545
|
+ # Filter out parts of the diff we expect to be different.
|
|
546
|
+ index_regex = re.compile(r"index [0-9a-f]{12}\.\.[0-9a-f]{12}")
|
|
547
|
+ lines_regex = re.compile(r"@@ -[0-9]+,[0-9]+ \+[0-9]+,[0-9]+ @@(?P<rest>.*)")
|
|
548
|
+
|
|
549
|
+ def save_diff(branch):
|
|
550
|
+ firefox_commit = get_firefox_ref(branch).commit
|
|
551
|
+ file_desc, file_name = tempfile.mkstemp(
|
|
552
|
+ text=True, prefix=f'{branch.split("/")[-1]}-'
|
|
553
|
+ )
|
|
554
|
+ # Register deleting the file at exit.
|
|
555
|
+ atexit.register(os.remove, file_name)
|
|
556
|
+
|
|
557
|
+ diff_process = subprocess.Popen(
|
|
558
|
+ [GIT_PATH, "diff", f"{firefox_commit}..{branch}"],
|
|
559
|
+ stdout=subprocess.PIPE,
|
|
560
|
+ text=True,
|
|
561
|
+ )
|
|
562
|
+
|
|
563
|
+ with os.fdopen(file_desc, "w") as file:
|
|
564
|
+ for line in diff_process.stdout:
|
|
565
|
+ if index_regex.match(line):
|
|
566
|
+ # Fake data that will match.
|
|
567
|
+ file.write("index ????????????..????????????\n")
|
|
568
|
+ continue
|
|
569
|
+ lines_match = lines_regex.match(line)
|
|
570
|
+ if lines_match:
|
|
571
|
+ # Fake data that will match.
|
|
572
|
+ file.write("@@ ?,? ?,? @@" + lines_match.group('rest'))
|
|
573
|
+ continue
|
|
574
|
+ file.write(line)
|
|
575
|
+
|
|
576
|
+ status = diff_process.poll()
|
|
577
|
+ if status != 0:
|
|
578
|
+ raise Exception(f"git diff exited with status {status}")
|
|
579
|
+
|
|
580
|
+ return file_name
|
|
581
|
+
|
|
582
|
+ file_1 = save_diff(args.branch1)
|
|
583
|
+ file_2 = save_diff(args.branch2)
|
|
584
|
+ subprocess.run([diff_tool, file_1, file_2], check=False)
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+# * -------------------- *
|
|
588
|
+# | Command line parsing |
|
|
589
|
+# * -------------------- *
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+def branch_complete(prefix, parsed_args, **kwargs):
|
|
593
|
+ """
|
|
594
|
+ Complete the argument with a branch name.
|
|
595
|
+ """
|
|
596
|
+ if not within_tor_browser_root():
|
|
597
|
+ return []
|
|
598
|
+ try:
|
|
599
|
+ branches = [ref.name for ref in get_refs("head", "")]
|
|
600
|
+ branches.extend([ref.name for ref in get_refs("remote", "")])
|
|
601
|
+ branches.append("HEAD")
|
|
602
|
+ except Exception:
|
|
603
|
+ return []
|
|
604
|
+ return [br for br in branches if br.startswith(prefix)]
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+parser = argparse.ArgumentParser()
|
|
608
|
+subparsers = parser.add_subparsers(required=True)
|
|
609
|
+
|
|
610
|
+for name, details in {
|
|
611
|
+ "show-upstream-commit": {
|
|
612
|
+ "func": show_upstream_commit,
|
|
613
|
+ },
|
|
614
|
+ "changed-files": {
|
|
615
|
+ "func": show_changed_files,
|
|
616
|
+ },
|
|
617
|
+ "lint-changed-files": {
|
|
618
|
+ "func": lint_changed_files,
|
|
619
|
+ "args": {
|
|
620
|
+ "--fix": {
|
|
621
|
+ "help": "whether to fix the files",
|
|
622
|
+ "action": "store_true",
|
|
623
|
+ },
|
|
624
|
+ },
|
|
625
|
+ },
|
|
626
|
+ "auto-fixup": {
|
|
627
|
+ "func": auto_fixup,
|
|
628
|
+ },
|
|
629
|
+ "clean-fixups": {
|
|
630
|
+ "func": clean_fixups,
|
|
631
|
+ },
|
|
632
|
+ "show-default": {
|
|
633
|
+ "func": show_default,
|
|
634
|
+ },
|
|
635
|
+ "branch-from-default": {
|
|
636
|
+ "func": branch_from_default,
|
|
637
|
+ "args": {
|
|
638
|
+ "branchname": {
|
|
639
|
+ "help": "the name for the new local branch",
|
|
640
|
+ "metavar": "<branch-name>",
|
|
641
|
+ },
|
|
642
|
+ },
|
|
643
|
+ },
|
|
644
|
+ "rebase-on-default": {
|
|
645
|
+ "func": rebase_on_default,
|
|
646
|
+ },
|
|
647
|
+ "show-firefox-commit": {
|
|
648
|
+ "func": show_firefox_commit,
|
|
649
|
+ },
|
|
650
|
+ "log": {
|
|
651
|
+ "func": show_log,
|
|
652
|
+ "args": {
|
|
653
|
+ "gitargs": {
|
|
654
|
+ "help": "argument to pass to git log",
|
|
655
|
+ "metavar": "-- git-log-arg",
|
|
656
|
+ "nargs": "*",
|
|
657
|
+ },
|
|
658
|
+ },
|
|
659
|
+ },
|
|
660
|
+ "branch-range-diff": {
|
|
661
|
+ "func": show_range_diff,
|
|
662
|
+ "args": {
|
|
663
|
+ "branch1": {
|
|
664
|
+ "help": "the first branch to compare",
|
|
665
|
+ "metavar": "<branch-1>",
|
|
666
|
+ "completer": branch_complete,
|
|
667
|
+ },
|
|
668
|
+ "branch2": {
|
|
669
|
+ "help": "the second branch to compare",
|
|
670
|
+ "metavar": "<branch-2>",
|
|
671
|
+ "completer": branch_complete,
|
|
672
|
+ },
|
|
673
|
+ },
|
|
674
|
+ },
|
|
675
|
+ "branch-diff-diff": {
|
|
676
|
+ "func": show_diff_diff,
|
|
677
|
+ "args": {
|
|
678
|
+ "branch1": {
|
|
679
|
+ "help": "the first branch to compare",
|
|
680
|
+ "metavar": "<branch-1>",
|
|
681
|
+ "completer": branch_complete,
|
|
682
|
+ },
|
|
683
|
+ "branch2": {
|
|
684
|
+ "help": "the second branch to compare",
|
|
685
|
+ "metavar": "<branch-2>",
|
|
686
|
+ "completer": branch_complete,
|
|
687
|
+ },
|
|
688
|
+ },
|
|
689
|
+ },
|
|
690
|
+ "files-containing": {
|
|
691
|
+ "func": show_files_containing,
|
|
692
|
+ "args": {
|
|
693
|
+ "regex": {"help": "the regex that the files must contain"},
|
|
694
|
+ },
|
|
695
|
+ },
|
|
696
|
+}.items():
|
|
697
|
+ help_message = re.sub(r"\s+", " ", details["func"].__doc__).strip()
|
|
698
|
+ sub = subparsers.add_parser(name, help=help_message)
|
|
699
|
+ sub.set_defaults(func=details["func"])
|
|
700
|
+ for arg, keywords in details.get("args", {}).items():
|
|
701
|
+ completer = None
|
|
702
|
+ if "completer" in keywords:
|
|
703
|
+ completer = keywords["completer"]
|
|
704
|
+ del keywords["completer"]
|
|
705
|
+ sub_arg = sub.add_argument(arg, **keywords)
|
|
706
|
+ if completer is not None:
|
|
707
|
+ sub_arg.completer = completer
|
|
708
|
+
|
|
709
|
+argcomplete.autocomplete(parser)
|
|
710
|
+
|
|
711
|
+if not within_tor_browser_root():
|
|
712
|
+ raise Exception("Must be within a tor-browser directory")
|
|
713
|
+parsed_args = parser.parse_args()
|
|
714
|
+
|
|
715
|
+parsed_args.func(parsed_args) |