... |
... |
@@ -4,27 +4,38 @@ |
4
|
4
|
Useful tools for working on tor-browser repository.
|
5
|
5
|
"""
|
6
|
6
|
|
7
|
|
-import sys
|
8
|
|
-import termios
|
9
|
|
-import os
|
|
7
|
+import argparse
|
10
|
8
|
import atexit
|
11
|
|
-import tempfile
|
12
|
|
-import subprocess
|
13
|
|
-import re
|
14
|
9
|
import json
|
|
10
|
+import os
|
|
11
|
+import re
|
|
12
|
+import subprocess
|
|
13
|
+import sys
|
|
14
|
+import tempfile
|
|
15
|
+import termios
|
15
|
16
|
import urllib.request
|
16
|
|
-import argparse
|
|
17
|
+
|
17
|
18
|
import argcomplete
|
18
|
19
|
|
19
|
20
|
GIT_PATH = "/usr/bin/git"
|
20
|
|
-UPSTREAM_URLS = [
|
21
|
|
- "https://gitlab.torproject.org/tpo/applications/tor-browser.git",
|
22
|
|
- "git@gitlab.torproject.org:tpo/applications/tor-browser.git",
|
23
|
|
-]
|
|
21
|
+UPSTREAM_URLS = {
|
|
22
|
+ "tor-browser": [
|
|
23
|
+ "https://gitlab.torproject.org/tpo/applications/tor-browser.git",
|
|
24
|
+ "git@gitlab.torproject.org:tpo/applications/tor-browser.git",
|
|
25
|
+ ],
|
|
26
|
+ "mullvad-browser": [
|
|
27
|
+ "https://gitlab.torproject.org/tpo/applications/mullvad-browser.git",
|
|
28
|
+ "git@gitlab.torproject.org:tpo/applications/mullvad-browser.git",
|
|
29
|
+ ],
|
|
30
|
+}
|
24
|
31
|
FIXUP_PREPROCESSOR_EDITOR = "git-rebase-fixup-preprocessor"
|
25
|
32
|
USER_EDITOR_ENV_NAME = "GIT_REBASE_FIXUP_PREPROCESSOR_USER_EDITOR"
|
26
|
33
|
|
27
|
34
|
|
|
35
|
+class TbDevException(Exception):
|
|
36
|
+ pass
|
|
37
|
+
|
|
38
|
+
|
28
|
39
|
def git_run(args, check=True, env=None):
|
29
|
40
|
"""
|
30
|
41
|
Run a git command with output sent to stdout.
|
... |
... |
@@ -34,16 +45,22 @@ def git_run(args, check=True, env=None): |
34
|
45
|
for key, value in env.items():
|
35
|
46
|
tmp_env[key] = value
|
36
|
47
|
env = tmp_env
|
37
|
|
- subprocess.run([GIT_PATH, *args], check=check, env=env)
|
|
48
|
+ try:
|
|
49
|
+ subprocess.run([GIT_PATH, *args], check=check, env=env)
|
|
50
|
+ except subprocess.CalledProcessError as err:
|
|
51
|
+ raise TbDevException(str(err)) from err
|
38
|
52
|
|
39
|
53
|
|
40
|
54
|
def git_get(args):
|
41
|
55
|
"""
|
42
|
56
|
Run a git command with each non-empty line returned in a list.
|
43
|
57
|
"""
|
44
|
|
- git_process = subprocess.run(
|
45
|
|
- [GIT_PATH, *args], text=True, stdout=subprocess.PIPE, check=True
|
46
|
|
- )
|
|
58
|
+ try:
|
|
59
|
+ git_process = subprocess.run(
|
|
60
|
+ [GIT_PATH, *args], text=True, stdout=subprocess.PIPE, check=True
|
|
61
|
+ )
|
|
62
|
+ except subprocess.CalledProcessError as err:
|
|
63
|
+ raise TbDevException(str(err)) from err
|
47
|
64
|
return [line for line in git_process.stdout.split("\n") if line]
|
48
|
65
|
|
49
|
66
|
|
... |
... |
@@ -57,35 +74,68 @@ def get_local_root(): |
57
|
74
|
global local_root
|
58
|
75
|
if local_root is None:
|
59
|
76
|
try:
|
60
|
|
- # Make sure we have a matching remote in this git repository. Should raise Exception if we don't.
|
61
|
|
- get_upstream_name()
|
62
|
|
- git_root = git_get(["rev-parse", "--show-toplevel"])[0]
|
63
|
|
- except Exception:
|
64
|
|
- git_root = None
|
65
|
|
- if git_root is None:
|
|
77
|
+ # Make sure we have a matching remote in this git repository.
|
|
78
|
+ if get_upstream_details()["is-browser-repo"]:
|
|
79
|
+ local_root = git_get(["rev-parse", "--show-toplevel"])[0]
|
|
80
|
+ else:
|
|
81
|
+ local_root = ""
|
|
82
|
+ except TbDevException:
|
66
|
83
|
local_root = ""
|
67
|
|
- else:
|
68
|
|
- local_root = git_root
|
69
|
84
|
return local_root
|
70
|
85
|
|
71
|
86
|
|
72
|
|
-upstream_name = None
|
|
87
|
+def determine_upstream_details():
|
|
88
|
+ """
|
|
89
|
+ Determine details about the upstream.
|
|
90
|
+ """
|
|
91
|
+ remote_urls = {
|
|
92
|
+ remote: git_get(["remote", "get-url", remote])[0]
|
|
93
|
+ for remote in git_get(["remote"])
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ matches = {
|
|
97
|
+ remote: repo
|
|
98
|
+ for repo, url_list in UPSTREAM_URLS.items()
|
|
99
|
+ for url in url_list
|
|
100
|
+ for remote, fetch_url in remote_urls.items()
|
|
101
|
+ if fetch_url == url
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ is_browser_repo = len(matches) > 0
|
|
105
|
+ details = {"is-browser-repo": is_browser_repo}
|
73
|
106
|
|
|
107
|
+ origin_remote_repo = matches.get("origin", None)
|
|
108
|
+ upstream_remote_repo = matches.get("upstream", None)
|
74
|
109
|
|
75
|
|
-def get_upstream_name():
|
|
110
|
+ if origin_remote_repo is not None:
|
|
111
|
+ if upstream_remote_repo is None:
|
|
112
|
+ details["remote"] = "origin"
|
|
113
|
+ details["repo-name"] = origin_remote_repo
|
|
114
|
+ # Else, both "upstream" and "origin" point to a remote repo. Not clear
|
|
115
|
+ # which should be used.
|
|
116
|
+ elif upstream_remote_repo is not None:
|
|
117
|
+ details["remote"] = "upstream"
|
|
118
|
+ details["repo-name"] = upstream_remote_repo
|
|
119
|
+ elif len(matches) == 1:
|
|
120
|
+ remote = next(iter(matches.keys()))
|
|
121
|
+ details["remote"] = remote
|
|
122
|
+ details["repo-name"] = matches[remote]
|
|
123
|
+ # Else, the upstream is ambiguous.
|
|
124
|
+
|
|
125
|
+ return details
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+cached_upstream_details = None
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+def get_upstream_details():
|
76
|
132
|
"""
|
77
|
|
- Get the name of the upstream remote.
|
|
133
|
+ Get details about the upstream repository.
|
78
|
134
|
"""
|
79
|
|
- global upstream_name
|
80
|
|
- if upstream_name is None:
|
81
|
|
- for remote in git_get(["remote"]):
|
82
|
|
- fetch_url = git_get(["remote", "get-url", remote])[0]
|
83
|
|
- if fetch_url in UPSTREAM_URLS:
|
84
|
|
- upstream_name = remote
|
85
|
|
- break
|
86
|
|
- if upstream_name is None:
|
87
|
|
- raise Exception("No upstream remote found.")
|
88
|
|
- return upstream_name
|
|
135
|
+ global cached_upstream_details
|
|
136
|
+ if cached_upstream_details is None:
|
|
137
|
+ cached_upstream_details = determine_upstream_details()
|
|
138
|
+ return cached_upstream_details
|
89
|
139
|
|
90
|
140
|
|
91
|
141
|
class Reference:
|
... |
... |
@@ -139,7 +189,7 @@ def get_nearest_ref(ref_type, name_start, search_from): |
139
|
189
|
if commit == ref.commit:
|
140
|
190
|
return ref
|
141
|
191
|
|
142
|
|
- raise Exception(f"No {name_start} commit found in the last 1000 commits")
|
|
192
|
+ raise TbDevException(f"No {name_start} commit found in the last 1000 commits")
|
143
|
193
|
|
144
|
194
|
|
145
|
195
|
def get_firefox_ref(search_from):
|
... |
... |
@@ -167,7 +217,7 @@ def get_upstream_basis_commit(search_from): |
167
|
217
|
upstream_firefox = get_firefox_ref(upstream_branch).commit
|
168
|
218
|
search_firefox = get_firefox_ref(search_from).commit
|
169
|
219
|
if upstream_firefox != search_firefox:
|
170
|
|
- raise Exception(
|
|
220
|
+ raise TbDevException(
|
171
|
221
|
f"Upstream of {search_from} has a different FIREFOX base. "
|
172
|
222
|
"You might want to set the upstream tracking branch to a new value."
|
173
|
223
|
)
|
... |
... |
@@ -210,12 +260,15 @@ def get_gitlab_default(): |
210
|
260
|
"""
|
211
|
261
|
Get the name of the default branch on gitlab.
|
212
|
262
|
"""
|
213
|
|
- query = """
|
214
|
|
- query {
|
215
|
|
- project(fullPath: "tpo/applications/tor-browser") {
|
216
|
|
- repository { rootRef }
|
217
|
|
- }
|
218
|
|
- }
|
|
263
|
+ repo_name = get_upstream_details().get("repo-name", None)
|
|
264
|
+ if repo_name is None:
|
|
265
|
+ raise TbDevException("Cannot determine the repository name")
|
|
266
|
+ query = f"""
|
|
267
|
+ query {{
|
|
268
|
+ project(fullPath: "tpo/applications/{repo_name}") {{
|
|
269
|
+ repository {{ rootRef }}
|
|
270
|
+ }}
|
|
271
|
+ }}
|
219
|
272
|
"""
|
220
|
273
|
request_data = {"query": re.sub(r"\s+", "", query)}
|
221
|
274
|
gitlab_request = urllib.request.Request(
|
... |
... |
@@ -231,7 +284,7 @@ def get_gitlab_default(): |
231
|
284
|
return json.load(response)["data"]["project"]["repository"]["rootRef"]
|
232
|
285
|
|
233
|
286
|
|
234
|
|
-def within_tor_browser_root():
|
|
287
|
+def within_browser_root():
|
235
|
288
|
"""
|
236
|
289
|
Whether we are with the tor browser root.
|
237
|
290
|
"""
|
... |
... |
@@ -279,7 +332,7 @@ def show_files_containing(args): |
279
|
332
|
try:
|
280
|
333
|
regex = re.compile(args.regex)
|
281
|
334
|
except re.error as err:
|
282
|
|
- raise Exception(f"{args.regex} is not a valid python regex") from err
|
|
335
|
+ raise TbDevException(f"{args.regex} is not a valid python regex") from err
|
283
|
336
|
|
284
|
337
|
file_list = get_changed_files(get_firefox_ref("HEAD").commit)
|
285
|
338
|
|
... |
... |
@@ -428,7 +481,7 @@ def auto_fixup(_args): |
428
|
481
|
|
429
|
482
|
staged_files = get_changed_files("HEAD", staged=True)
|
430
|
483
|
if staged_files:
|
431
|
|
- raise Exception(f"Have already staged files: {staged_files}")
|
|
484
|
+ raise TbDevException(f"Have already staged files: {staged_files}")
|
432
|
485
|
|
433
|
486
|
fixups = {}
|
434
|
487
|
for filename in get_changed_files("HEAD"):
|
... |
... |
@@ -475,7 +528,9 @@ def show_default(_args): |
475
|
528
|
Print the default branch name from gitlab.
|
476
|
529
|
"""
|
477
|
530
|
default_branch = get_gitlab_default()
|
478
|
|
- upstream = get_upstream_name()
|
|
531
|
+ upstream = get_upstream_details().get("remote", None)
|
|
532
|
+ if upstream is None:
|
|
533
|
+ raise TbDevException("Cannot determine the upstream remote")
|
479
|
534
|
print(f"{upstream}/{default_branch}")
|
480
|
535
|
|
481
|
536
|
|
... |
... |
@@ -484,10 +539,20 @@ def branch_from_default(args): |
484
|
539
|
Fetch the default gitlab branch from upstream and create a new local branch.
|
485
|
540
|
"""
|
486
|
541
|
default_branch = get_gitlab_default()
|
487
|
|
- upstream = get_upstream_name()
|
|
542
|
+ upstream = get_upstream_details().get("remote", None)
|
|
543
|
+ if upstream is None:
|
|
544
|
+ raise TbDevException("Cannot determine the upstream remote")
|
488
|
545
|
|
489
|
546
|
git_run(["fetch", upstream, default_branch])
|
490
|
|
- git_run(["switch", "--create", args.branchname, "--track", f"{upstream}/{default_branch}"])
|
|
547
|
+ git_run(
|
|
548
|
+ [
|
|
549
|
+ "switch",
|
|
550
|
+ "--create",
|
|
551
|
+ args.branchname,
|
|
552
|
+ "--track",
|
|
553
|
+ f"{upstream}/{default_branch}",
|
|
554
|
+ ]
|
|
555
|
+ )
|
491
|
556
|
|
492
|
557
|
|
493
|
558
|
def move_to_default(args):
|
... |
... |
@@ -504,17 +569,21 @@ def move_to_default(args): |
504
|
569
|
try:
|
505
|
570
|
branch_name = git_get(["branch", "--show-current"])[0]
|
506
|
571
|
except IndexError:
|
507
|
|
- raise Exception("No current branch")
|
|
572
|
+ raise TbDevException("No current branch")
|
508
|
573
|
|
509
|
574
|
current_upstream_branch = get_upstream_tracking_branch(branch_name)
|
510
|
575
|
default_branch = get_gitlab_default()
|
511
|
|
- upstream = get_upstream_name()
|
|
576
|
+ upstream = get_upstream_details().get("remote", None)
|
|
577
|
+ if upstream is None:
|
|
578
|
+ raise TbDevException("Cannot determine the upstream remote")
|
512
|
579
|
|
513
|
580
|
git_run(["fetch", upstream, default_branch])
|
514
|
581
|
|
515
|
582
|
new_upstream_branch = f"{upstream}/{default_branch}"
|
516
|
583
|
if current_upstream_branch == new_upstream_branch:
|
517
|
|
- print(f"{branch_name} is already set to track the default branch {new_upstream_branch}.")
|
|
584
|
+ print(
|
|
585
|
+ f"{branch_name} is already set to track the default branch {new_upstream_branch}."
|
|
586
|
+ )
|
518
|
587
|
return
|
519
|
588
|
|
520
|
589
|
# We want to avoid checking out the old branch because this can cause
|
... |
... |
@@ -560,7 +629,7 @@ def show_diff_diff(args): |
560
|
629
|
"""
|
561
|
630
|
config_res = git_get(["config", "--get", "diff.tool"])
|
562
|
631
|
if not config_res:
|
563
|
|
- raise Exception("No diff.tool configured for git")
|
|
632
|
+ raise TbDevException("No diff.tool configured for git")
|
564
|
633
|
diff_tool = config_res[0]
|
565
|
634
|
|
566
|
635
|
# Filter out parts of the diff we expect to be different.
|
... |
... |
@@ -590,13 +659,13 @@ def show_diff_diff(args): |
590
|
659
|
lines_match = lines_regex.match(line)
|
591
|
660
|
if lines_match:
|
592
|
661
|
# Fake data that will match.
|
593
|
|
- file.write("@@ ?,? ?,? @@" + lines_match.group('rest'))
|
|
662
|
+ file.write("@@ ?,? ?,? @@" + lines_match.group("rest"))
|
594
|
663
|
continue
|
595
|
664
|
file.write(line)
|
596
|
665
|
|
597
|
666
|
status = diff_process.poll()
|
598
|
667
|
if status != 0:
|
599
|
|
- raise Exception(f"git diff exited with status {status}")
|
|
668
|
+ raise TbDevException(f"git diff exited with status {status}")
|
600
|
669
|
|
601
|
670
|
return file_name
|
602
|
671
|
|
... |
... |
@@ -614,7 +683,7 @@ def branch_complete(prefix, parsed_args, **kwargs): |
614
|
683
|
"""
|
615
|
684
|
Complete the argument with a branch name.
|
616
|
685
|
"""
|
617
|
|
- if not within_tor_browser_root():
|
|
686
|
+ if not within_browser_root():
|
618
|
687
|
return []
|
619
|
688
|
try:
|
620
|
689
|
branches = [ref.name for ref in get_refs("head", "")]
|
... |
... |
@@ -738,8 +807,11 @@ for name, details in { |
738
|
807
|
|
739
|
808
|
argcomplete.autocomplete(parser)
|
740
|
809
|
|
741
|
|
-if not within_tor_browser_root():
|
742
|
|
- raise Exception("Must be within a tor-browser directory")
|
743
|
|
-parsed_args = parser.parse_args()
|
|
810
|
+try:
|
|
811
|
+ if not within_browser_root():
|
|
812
|
+ raise TbDevException("Must be within a browser directory")
|
|
813
|
+ parsed_args = parser.parse_args()
|
744
|
814
|
|
745
|
|
-parsed_args.func(parsed_args) |
|
815
|
+ parsed_args.func(parsed_args)
|
|
816
|
+except TbDevException as err:
|
|
817
|
+ print(f"\x1b[1m{err}\x1b[0m", file=sys.stderr) |