1
|
|
-#!/usr/bin/python
|
|
1
|
+#!/usr/bin/env python3
|
2
|
2
|
# PYTHON_ARGCOMPLETE_OK
|
3
|
3
|
"""
|
4
|
4
|
Useful tools for working on tor-browser repository.
|
... |
... |
@@ -148,12 +148,28 @@ def get_firefox_ref(search_from): |
148
|
148
|
return get_nearest_ref("tag", "FIREFOX_", search_from)
|
149
|
149
|
|
150
|
150
|
|
151
|
|
-def get_upstream_commit(search_from):
|
|
151
|
+def get_upstream_tracking_branch(search_from):
|
|
152
|
+ return git_get(["rev-parse", "--abbrev-ref", f"{search_from}@{{upstream}}"])[0]
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+def get_upstream_basis_commit(search_from):
|
152
|
156
|
"""
|
153
|
157
|
Get the first common ancestor of search_from that is also in its upstream
|
154
|
158
|
branch.
|
155
|
159
|
"""
|
156
|
|
- return git_get(["merge-base", search_from, f"{search_from}@{{upstream}}"])[0]
|
|
160
|
+ upstream_branch = get_upstream_tracking_branch(search_from)
|
|
161
|
+ commit = git_get(["merge-base", search_from, upstream_branch])[0]
|
|
162
|
+ # Verify that the upstream commit shares the same firefox basis. Otherwise,
|
|
163
|
+ # this would indicate that the upstream is on an early or later FIREFOX
|
|
164
|
+ # base.
|
|
165
|
+ upstream_firefox = get_firefox_ref(upstream_branch).commit
|
|
166
|
+ search_firefox = get_firefox_ref(search_from).commit
|
|
167
|
+ if upstream_firefox != search_firefox:
|
|
168
|
+ raise Exception(
|
|
169
|
+ f"Upstream of {search_from} has a different FIREFOX base. "
|
|
170
|
+ "You might want to set the upstream tracking branch to a new value."
|
|
171
|
+ )
|
|
172
|
+ return commit
|
157
|
173
|
|
158
|
174
|
|
159
|
175
|
def get_changed_files(from_commit, staged=False):
|
... |
... |
@@ -239,11 +255,11 @@ def show_firefox_commit(_args): |
239
|
255
|
print(ref.commit)
|
240
|
256
|
|
241
|
257
|
|
242
|
|
-def show_upstream_commit(_args):
|
|
258
|
+def show_upstream_basis_commit(_args):
|
243
|
259
|
"""
|
244
|
260
|
Print the last upstream commit for the current HEAD.
|
245
|
261
|
"""
|
246
|
|
- print(get_upstream_commit("HEAD"))
|
|
262
|
+ print(get_upstream_basis_commit("HEAD"))
|
247
|
263
|
|
248
|
264
|
|
249
|
265
|
def show_log(args):
|
... |
... |
@@ -278,7 +294,7 @@ def show_changed_files(_args): |
278
|
294
|
"""
|
279
|
295
|
List all the files that have been modified relative to upstream.
|
280
|
296
|
"""
|
281
|
|
- for filename in get_changed_files(get_upstream_commit("HEAD")):
|
|
297
|
+ for filename in get_changed_files(get_upstream_basis_commit("HEAD")):
|
282
|
298
|
print(filename)
|
283
|
299
|
|
284
|
300
|
|
... |
... |
@@ -289,7 +305,7 @@ def lint_changed_files(args): |
289
|
305
|
os.chdir(get_local_root())
|
290
|
306
|
file_list = [
|
291
|
307
|
f
|
292
|
|
- for f in get_changed_files(get_upstream_commit("HEAD"))
|
|
308
|
+ for f in get_changed_files(get_upstream_basis_commit("HEAD"))
|
293
|
309
|
if os.path.isfile(f) # Not deleted
|
294
|
310
|
]
|
295
|
311
|
command_base = ["./mach", "lint"]
|
... |
... |
@@ -495,25 +511,49 @@ def branch_from_default(args): |
495
|
511
|
git_run(["switch", "--create", args.branchname, "--track", default_branch])
|
496
|
512
|
|
497
|
513
|
|
498
|
|
-def rebase_on_default(_args):
|
|
514
|
+def move_to_default(args):
|
499
|
515
|
"""
|
500
|
|
- Fetch the default gitlab branch from upstream and rebase the current branch
|
501
|
|
- on top.
|
|
516
|
+ Fetch the default gitlab branch from upstream and move the specified
|
|
517
|
+ branch's commits on top. A new branch will be created tracking the default
|
|
518
|
+ branch, and the old branch will be renamed with a suffix for the old
|
|
519
|
+ tracking branch. This method will switch to the new branch, but will avoid
|
|
520
|
+ switching to the old branch to prevent triggering a CLOBBER build.
|
502
|
521
|
"""
|
503
|
|
- try:
|
504
|
|
- branch_name = git_get(["branch", "--show-current"])[0]
|
505
|
|
- except IndexError:
|
506
|
|
- raise Exception("No current branch")
|
|
522
|
+ branch_name = args.branch
|
|
523
|
+ if branch_name is None:
|
|
524
|
+ # Use current branch as default.
|
|
525
|
+ try:
|
|
526
|
+ branch_name = git_get(["branch", "--show-current"])[0]
|
|
527
|
+ except IndexError:
|
|
528
|
+ raise Exception("No current branch")
|
507
|
529
|
|
508
|
|
- current_upstream = get_upstream_commit("HEAD")
|
|
530
|
+ current_upstream_branch = get_upstream_tracking_branch(branch_name)
|
509
|
531
|
default_branch = get_gitlab_default()
|
510
|
532
|
|
511
|
533
|
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
|
|
- )
|
|
534
|
+
|
|
535
|
+ if current_upstream_branch == default_branch:
|
|
536
|
+ print(f"{branch_name} is already set to track the default branch {default_branch}.")
|
|
537
|
+ return
|
|
538
|
+
|
|
539
|
+ # We want to avoid checking out the old branch because this can cause
|
|
540
|
+ # mozilla ./mach to do a CLOBBER build.
|
|
541
|
+ # Instead we create a new branch with the same name and cherry pick.
|
|
542
|
+ current_basis = get_upstream_basis_commit(branch_name)
|
|
543
|
+ old_branch_name = branch_name + "-" + get_firefox_ref(branch_name).name
|
|
544
|
+
|
|
545
|
+ print(f"Moving old branch {branch_name} to {old_branch_name}")
|
|
546
|
+ git_run(["branch", "-m", branch_name, old_branch_name])
|
|
547
|
+
|
|
548
|
+ try:
|
|
549
|
+ git_run(["switch", "--create", branch_name, "--track", default_branch])
|
|
550
|
+ except subprocess.CalledProcessError as err:
|
|
551
|
+ print(f"Moving {old_branch_name} back to {branch_name}")
|
|
552
|
+ git_run(["branch", "-m", old_branch_name, branch_name])
|
|
553
|
+ raise err
|
|
554
|
+
|
|
555
|
+ # Set check to False since cherry-pick might fail due to a merge conflict.
|
|
556
|
+ git_run(["cherry-pick", f"{current_basis}..{old_branch_name}"], check=False)
|
517
|
557
|
|
518
|
558
|
|
519
|
559
|
def show_range_diff(args):
|
... |
... |
@@ -608,8 +648,8 @@ parser = argparse.ArgumentParser() |
608
|
648
|
subparsers = parser.add_subparsers(required=True)
|
609
|
649
|
|
610
|
650
|
for name, details in {
|
611
|
|
- "show-upstream-commit": {
|
612
|
|
- "func": show_upstream_commit,
|
|
651
|
+ "show-upstream-basis-commit": {
|
|
652
|
+ "func": show_upstream_basis_commit,
|
613
|
653
|
},
|
614
|
654
|
"changed-files": {
|
615
|
655
|
"func": show_changed_files,
|
... |
... |
@@ -641,8 +681,16 @@ for name, details in { |
641
|
681
|
},
|
642
|
682
|
},
|
643
|
683
|
},
|
644
|
|
- "rebase-on-default": {
|
645
|
|
- "func": rebase_on_default,
|
|
684
|
+ "move-to-default": {
|
|
685
|
+ "func": move_to_default,
|
|
686
|
+ "args": {
|
|
687
|
+ "branch": {
|
|
688
|
+ "help": "the branch to move, else uses the current branch",
|
|
689
|
+ "metavar": "<branch>",
|
|
690
|
+ "nargs": "?",
|
|
691
|
+ "completer": branch_complete,
|
|
692
|
+ },
|
|
693
|
+ },
|
646
|
694
|
},
|
647
|
695
|
"show-firefox-commit": {
|
648
|
696
|
"func": show_firefox_commit,
|