5 import xml.etree.ElementTree as ET
8 from scripts.common import git
9 from scripts.common import Colors
10 from scripts.common import accept_command
11 from scripts.common import get_meson
14 SCRIPTDIR = os.path.normpath(os.path.dirname(__file__))
17 def manifest_get_commits(manifest):
19 tree = ET.parse(manifest)
23 if child.tag == 'remote':
24 remotes[child.attrib['name']] = child.attrib['fetch']
25 if child.tag == 'project':
26 name = child.attrib['name']
27 path = child.attrib.get('path', name)
29 remote = child.attrib.get('remote')
31 res[path] = [child.attrib["revision"], [os.path.join(remotes[remote], name), child.attrib.get('refname', child.attrib["revision"])]]
33 res[path] = [child.attrib["revision"], []]
38 def get_branch_name(repo_dir):
39 return git('-C', repo_dir, 'rev-parse', '--symbolic-full-name', 'HEAD').strip()
42 def ensure_revision_if_necessary(repo_dir, revision):
44 Makes sure that @revision is set if the current repo is detached.
47 if get_branch_name(repo_dir) == 'HEAD':
48 revision = git('-C', repo_dir, 'rev-parse', 'HEAD').strip()
53 def update_subprojects(repos_commits, no_interaction=False):
54 subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
55 for repo_name in os.listdir(subprojects_dir):
56 repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
57 if not os.path.exists(os.path.join(repo_dir, '.git')):
60 revision, args = repos_commits.get(repo_name, [None, []])
61 if not update_repo(repo_name, repo_dir, revision, no_interaction, args):
67 def update_repo(repo_name, repo_dir, revision, no_interaction, fetch_args=[], recurse_i=0):
68 revision = ensure_revision_if_necessary(repo_dir, revision)
69 git("config", "rebase.autoStash", "true", repository_path=repo_dir)
72 print("Checking out %s in %s" % (revision, repo_name))
73 git("fetch", *fetch_args, repository_path=repo_dir)
74 git("checkout", "--detach", revision, repository_path=repo_dir)
76 print("Updating branch %s in %s" % (get_branch_name(repo_dir), repo_name))
77 git("pull", "--rebase", repository_path=repo_dir)
78 git("submodule", "update", repository_path=repo_dir)
79 except Exception as e:
80 out = getattr(e, "output", b"").decode()
81 if not no_interaction:
82 print("====================================="
83 "\n%s\nEntering a shell in %s to fix that"
84 " just `exit 0` once done, or `exit 255`"
85 " to skip update for that repository"
86 "\n=====================================" % (
90 shell = os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")
92 shell = os.environ.get("SHELL", os.path.realpath("/bin/sh"))
93 subprocess.check_call(shell, cwd=repo_dir)
94 except subprocess.CalledProcessError as e:
95 if e.returncode == 255:
96 print("Skipping '%s' update" % repo_name)
99 # Result of subshell does not really matter
103 return update_repo(repo_name, repo_dir, revision, no_interaction,
107 print("\nCould not rebase %s, please fix and try again."
108 " Error:\n\n%s %s" % (repo_dir, out, e))
113 commit_message = git("show", "--shortstat", repository_path=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()))
120 if __name__ == "__main__":
121 parser = argparse.ArgumentParser(prog="git-update")
123 parser.add_argument("--no-color",
126 help="Do not output ansi colors.")
127 parser.add_argument("--builddir",
129 help="Specifies the build directory where to"
130 " invoke ninja after updating.")
131 parser.add_argument("--no-interaction",
134 help="Do not allow interaction with the user.")
135 parser.add_argument("--manifest",
137 help="Use a android repo manifest to sync repositories"
138 " Note that it will let all repositories in detached state")
139 options = parser.parse_args()
140 if options.no_color or not Colors.can_enable():
143 if options.no_interaction:
148 targets_s = subprocess.check_output(meson + ['subprojects', 'download'])
149 repos_commits = manifest_get_commits(options.manifest)
153 revision, args = repos_commits.get('gst-build', [None, []])
154 if not update_repo('gst-build', SCRIPTDIR, revision, options.no_interaction, args):
157 if not update_subprojects(repos_commits, options.no_interaction):
161 ninja = accept_command(["ninja", "ninja-build"])
163 print("Can't find ninja, other backends are not supported for rebuilding")
166 if not os.path.exists(os.path.join (options.builddir, 'build.ninja')):
167 print("Can't rebuild in %s as no build.ninja file found." % options.builddir)
169 print("Rebuilding all GStreamer modules.")
170 exit(subprocess.call([ninja, '-C', options.builddir]))