update: Ensure that a revision is used when updating a detached repo
[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 import sys
7
8 from common import git
9 from common import Colors
10 from common import accept_command
11
12
13 SCRIPTDIR = os.path.normpath(os.path.dirname(__file__))
14
15
16 def manifest_get_commits(manifest):
17     res = {}
18     tree = ET.parse(manifest)
19     root = tree.getroot()
20     remotes = {}
21     for child in root:
22         if child.tag == 'remote':
23             remotes[child.attrib['name']] = child.attrib['fetch']
24         if child.tag == 'project':
25             name = child.attrib['name']
26
27             remote = child.attrib.get('remote')
28             if remote:
29                 res[name] = ['FETCH_HEAD', [os.path.join(remotes[remote], name), child.attrib['revision']]]
30             else:
31                 res[name] = [child.attrib["revision"], []]
32
33     return res
34
35
36 def ensure_revision_if_necessary(repo_dir, revision):
37     """
38     Makes sure that @revision is set if the current repo is detached.
39     """
40     if not revision:
41         ret = git('-C', repo_dir, 'rev-parse', '--symbolic-full-name', 'HEAD')
42         if ret.strip() == 'HEAD':
43             revision = git('-C', repo_dir, 'rev-parse', 'HEAD').strip()
44
45     return revision
46
47
48 def update_subprojects(repos_commits, no_interaction=False):
49     subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
50     for repo_name in os.listdir(subprojects_dir):
51         repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
52         if not os.path.exists(os.path.join(repo_dir, '.git')):
53             continue
54
55         revision, args = repos_commits.get(repo_name, [None, []])
56         if not update_repo(repo_name, repo_dir, revision, no_interaction, args):
57             return False
58
59     return True
60
61
62 def update_repo(repo_name, repo_dir, revision, no_interaction, fetch_args=[], recurse_i=0):
63     revision = ensure_revision_if_necessary(repo_dir, revision)
64     git("config", "rebase.autoStash", "true", repository_path=repo_dir)
65     try:
66         if revision:
67             git("fetch", *fetch_args, repository_path=repo_dir)
68             git("checkout", revision, repository_path=repo_dir)
69         else:
70             git("pull", "--rebase", repository_path=repo_dir)
71         git("submodule", "update", repository_path=repo_dir)
72     except Exception as e:
73         out = getattr(e, "output", b"").decode()
74         if not no_interaction:
75             print("====================================="
76                   "\n%s\nEntering a shell in %s to fix that"
77                   " just `exit 0` once done, or `exit 255`"
78                   " to skip update for that repository"
79                   "\n=====================================" % (
80                         out, repo_dir))
81             try:
82                 if os.name is 'nt':
83                     shell = os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")
84                 else:
85                     shell = os.environ.get("SHELL", os.path.realpath("/bin/sh"))
86                 subprocess.check_call(shell, cwd=repo_dir)
87             except subprocess.CalledProcessError as e:
88                 if e.returncode == 255:
89                     print("Skipping '%s' update" % repo_name)
90                     return True
91             except:
92                 # Result of subshell does not really matter
93                 pass
94
95             if recurse_i < 3:
96                 return update_repo(repo_name, repo_dir, revision, no_interaction,
97                                     recurse_i + 1)
98             return False
99         else:
100             print("\nCould not rebase %s, please fix and try again."
101                     " Error:\n\n%s %s" % (repo_dir, out, e))
102
103             return False
104
105
106     commit_message = git("show", "--shortstat", repository_path=repo_dir).split("\n")
107     print(u"  -> %s%s%s - %s" % (Colors.HEADER, commit_message[0][7:14], Colors.ENDC,
108                                     commit_message[4].strip()))
109
110     return True
111
112
113 if __name__ == "__main__":
114     parser = argparse.ArgumentParser(prog="git-update")
115
116     parser.add_argument("--no-color",
117                         default=False,
118                         action='store_true',
119                         help="Do not output ansi colors.")
120     parser.add_argument("--builddir",
121                         default=None,
122                         help="Specifies the build directory where to"
123                         " invoke ninja after updating.")
124     parser.add_argument("--no-interaction",
125                         default=False,
126                         action='store_true',
127                         help="Do not allow interaction with the user.")
128     parser.add_argument("--manifest",
129                         default=None,
130                         help="Use a android repo manifest to sync repositories"
131                         " Note that it will let all repositories in detached state")
132     options = parser.parse_args()
133     if options.no_color:
134         Colors.disable()
135
136     if options.no_interaction:
137         sys.stdin.close()
138
139     if options.manifest:
140         repos_commits = manifest_get_commits(options.manifest)
141     else:
142         repos_commits = {}
143
144     revision, args = repos_commits.get('gst-build', [None, []])
145     if not update_repo('gst-build', SCRIPTDIR, revision, options.no_interaction, args):
146         exit(1)
147
148     if not update_subprojects(repos_commits, options.no_interaction):
149         exit(1)
150
151     if options.builddir:
152         ninja = accept_command(["ninja", "ninja-build"])
153         if not ninja:
154             print("Can't find ninja, other backends are not supported for rebuilding")
155             exit(1)
156
157         if not os.path.exists(os.path.join (options.builddir, 'build.ninja')):
158             print("Can't rebuild in %s as no build.ninja file found." % options.builddir)
159
160         print("Rebuilding all GStreamer modules.")
161         exit(subprocess.call([ninja, '-C', options.builddir]))