git-update: Add a way for user to fix any rebasing issue interactively
[platform/upstream/gstreamer.git] / git-update
1 #!/usr/bin/env python3
2 import argparse
3 import os
4 import subprocess
5 import xml.etree.ElementTree as ET
6
7
8 SCRIPTDIR = os.path.dirname(__file__)
9
10 class Colors:
11     HEADER = '\033[95m'
12     OKBLUE = '\033[94m'
13     OKGREEN = '\033[92m'
14     WARNING = '\033[93m'
15     FAIL = '\033[91m'
16     ENDC = '\033[0m'
17
18     force_disable = False
19
20     @classmethod
21     def disable(cls):
22         cls.HEADER = ''
23         cls.OKBLUE = ''
24         cls.OKGREEN = ''
25         cls.WARNING = ''
26         cls.FAIL = ''
27         cls.ENDC = ''
28
29     @classmethod
30     def enable(cls):
31         if cls.force_disable:
32             return
33
34         cls.HEADER = '\033[95m'
35         cls.OKBLUE = '\033[94m'
36         cls.OKGREEN = '\033[92m'
37         cls.WARNING = '\033[93m'
38         cls.FAIL = '\033[91m'
39         cls.ENDC = '\033[0m'
40
41
42
43 def git(args, repository_path):
44     if not isinstance(args, list):
45         args = [args]
46
47     return subprocess.check_output(["git"] + args, cwd=repository_path,
48                                    stderr=subprocess.STDOUT).decode()
49
50
51 def manifest_get_commits(manifest):
52     res = {}
53     tree = ET.parse(manifest)
54     root = tree.getroot()
55     for child in root:
56         if child.tag == 'project':
57             res[child.attrib["name"]] = child.attrib["revision"]
58     return res
59
60
61 def update_subprojects(manifest, no_interaction=False):
62     if manifest:
63         repos_commits = manifest_get_commits(manifest)
64     else:
65         repos_commits = {}
66
67     subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
68     for repo_name in os.listdir(subprojects_dir):
69         repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
70         if not os.path.exists(os.path.join(repo_dir, '.git')):
71             continue
72         revision = repos_commits.get(repo_name)
73         if not update_repo(repo_name, repo_dir, revision, no_interaction):
74             return False
75
76     return True
77
78
79 def update_repo(repo_name, repo_dir, revision, no_interaction, recurse_i=0):
80     print("Updating %s..." % repo_name)
81     try:
82         if revision:
83             git(["fetch"], repo_dir)
84             git(["checkout", revision], repo_dir)
85         else:
86             git(["pull", "--rebase"], repo_dir)
87     except Exception as e:
88         out = getattr(e, "output", b"").decode()
89         if not no_interaction:
90             print("====================================="
91                   "\n%sEntering a shell in %s to fix that"
92                   " just `exit` once done`"
93                   "\n=====================================" % (
94                         out, os.getcwd()))
95             try:
96                 subprocess.check_call(os.environ.get("SHELL", "/bin/sh"),
97                                       cwd=repo_dir)
98             except:
99                 # Result of subshell does not really matter
100                 pass
101
102             if recurse_i < 3:
103                 return update_repo(repo_name, repo_dir, revision, no_interaction,
104                                     recurse_i + 1)
105             return False
106         else:
107             print("\nCould not rebase %s, please fix and try again."
108                     " Error:\n\n%s %s" % (repo_dir, out, e))
109
110             return False
111
112
113     commit_message = git("show", repo_dir).split("\n")
114     print(u"  -> %s%s%s — %s" % (Colors.HEADER, commit_message[0][7:14], Colors.ENDC,
115                                     commit_message[4].strip()))
116
117     return True
118
119
120 if __name__ == "__main__":
121     parser = argparse.ArgumentParser(prog="git-update")
122
123     parser.add_argument("--no-color",
124                         default=False,
125                         action='store_true',
126                         help="Do not output ansi colors.")
127     parser.add_argument("--no-interaction",
128                         default=False,
129                         action='store_true',
130                         help="Do not allow interaction with the user.")
131     parser.add_argument("--manifest",
132                         default=None,
133                         help="Use a android repo manifest to sync repositories"
134                         " Note that it will let all repositories in detached state")
135     options = parser.parse_args()
136     if options.no_color:
137         Colors.disable()
138
139     exit(not update_subprojects(options.manifest,
140                                 options.no_interaction))