File: git_update_all.py

package info (click to toggle)
perfetto 54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 133,812 kB
  • sloc: cpp: 338,350; python: 74,464; sql: 46,895; ansic: 18,340; javascript: 2,557; java: 2,160; sh: 1,444; yacc: 776; xml: 563; makefile: 226
file content (79 lines) | stat: -rwxr-xr-x 2,805 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/env python3
import argparse
import sys
from typing import Dict, List, Optional, Set
import os

# Allow importing of root-relative modules.
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.join(ROOT_DIR))

#pylint: disable=wrong-import-position
from python.tools.git_utils import (
    run_git_command,
    topological_sort_branches,
    get_current_branch,
)
#pylint: enable=wrong-import-position


def main():
  parser = argparse.ArgumentParser(
      description='Updates ALL stacked branches via merges based on parent config.'
  )
  args = parser.parse_args()

  print("--- Starting update for ALL stacks ---")
  sorted_branches: List[str] = []
  graph: Dict[str, Optional[str]] = {}
  try:
    sorted_branches, graph = topological_sort_branches()
  except ValueError as e:  # Cycle detected
    print(f"Error: {e}", file=sys.stderr)
    print("Cannot update due to cycles.", file=sys.stderr)
    sys.exit(1)
  except Exception as e:
    print(f"Dependency analysis error: {e}", file=sys.stderr)
    sys.exit(1)

  if not sorted_branches:
    print("No branches with parent configurations found.")
    sys.exit(0)

  print(f"Branches to update (topological order): {', '.join(sorted_branches)}")
  original_branch_to_restore = get_current_branch()

  for branch in sorted_branches:
    parent = graph.get(branch)
    if parent:
      print(f"\nUpdating '{branch}' by merging '{parent}'...")
      try:
        run_git_command(['checkout', branch])
        run_git_command(['merge', parent])
        print(f"Merge successful.")
      except SystemExit as e:  # Merge conflict
        print(
            f"MERGE FAILED: Conflicts merging '{parent}' into '{branch}'.",
            file=sys.stderr)
        print("Resolve conflicts manually and commit.", file=sys.stderr)
        if original_branch_to_restore and get_current_branch(
        ) != original_branch_to_restore:
          print(f"Restoring original branch '{original_branch_to_restore}'...")
          run_git_command(['checkout', original_branch_to_restore], check=False)
        sys.exit(e.code if isinstance(e.code, int) else 1)
      except Exception as e:
        print(f"\nUnexpected error updating '{branch}': {e}", file=sys.stderr)
        if original_branch_to_restore and get_current_branch(
        ) != original_branch_to_restore:
          run_git_command(['checkout', original_branch_to_restore], check=False)
        sys.exit(1)

  if original_branch_to_restore and get_current_branch(
  ) != original_branch_to_restore:
    print(f"\nRestoring original branch '{original_branch_to_restore}'...")
    run_git_command(['checkout', original_branch_to_restore], check=False)
  print("\n--- Update process finished for ALL stacks ---")


if __name__ == "__main__":
  main()