5 import xml.etree.ElementTree as ET
9 from common import Colors
10 from common import accept_command
13 SCRIPTDIR = os.path.normpath(os.path.dirname(__file__))
16 def manifest_get_commits(manifest):
18 tree = ET.parse(manifest)
22 if child.tag == 'remote':
23 remotes[child.attrib['name']] = child.attrib['fetch']
24 if child.tag == 'project':
25 name = child.attrib['name']
27 remote = child.attrib.get('remote')
29 res[name] = ['FETCH_HEAD', [os.path.join(remotes[remote], name), child.attrib['revision']]]
31 res[name] = [child.attrib["revision"], []]
36 def get_branch_name(repo_dir):
37 return git('-C', repo_dir, 'rev-parse', '--symbolic-full-name', 'HEAD').strip()
40 def ensure_revision_if_necessary(repo_dir, revision):
42 Makes sure that @revision is set if the current repo is detached.
45 if get_branch_name(repo_dir) == 'HEAD':
46 revision = git('-C', repo_dir, 'rev-parse', 'HEAD').strip()
51 def update_subprojects(repos_commits, no_interaction=False):
52 subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
53 for repo_name in os.listdir(subprojects_dir):
54 repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
55 if not os.path.exists(os.path.join(repo_dir, '.git')):
58 revision, args = repos_commits.get(repo_name, [None, []])
59 if not update_repo(repo_name, repo_dir, revision, no_interaction, args):
65 def update_repo(repo_name, repo_dir, revision, no_interaction, fetch_args=[], recurse_i=0):
66 revision = ensure_revision_if_necessary(repo_dir, revision)
67 git("config", "rebase.autoStash", "true", repository_path=repo_dir)
70 print("Checking out %s in %s" % (revision, repo_name))
71 git("fetch", *fetch_args, repository_path=repo_dir)
72 git("checkout", revision, repository_path=repo_dir)
74 print("Updating branch %s in %s" % (get_branch_name(repo_dir), repo_name))
75 git("pull", "--rebase", repository_path=repo_dir)
76 git("submodule", "update", repository_path=repo_dir)
77 except Exception as e:
78 out = getattr(e, "output", b"").decode()
79 if not no_interaction:
80 print("====================================="
81 "\n%s\nEntering a shell in %s to fix that"
82 " just `exit 0` once done, or `exit 255`"
83 " to skip update for that repository"
84 "\n=====================================" % (
88 shell = os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")
90 shell = os.environ.get("SHELL", os.path.realpath("/bin/sh"))
91 subprocess.check_call(shell, cwd=repo_dir)
92 except subprocess.CalledProcessError as e:
93 if e.returncode == 255:
94 print("Skipping '%s' update" % repo_name)
97 # Result of subshell does not really matter
101 return update_repo(repo_name, repo_dir, revision, no_interaction,
105 print("\nCould not rebase %s, please fix and try again."
106 " Error:\n\n%s %s" % (repo_dir, out, e))
111 commit_message = git("show", "--shortstat", repository_path=repo_dir).split("\n")
112 print(u" -> %s%s%s - %s" % (Colors.HEADER, commit_message[0][7:14], Colors.ENDC,
113 commit_message[4].strip()))
118 if __name__ == "__main__":
119 parser = argparse.ArgumentParser(prog="git-update")
121 parser.add_argument("--no-color",
124 help="Do not output ansi colors.")
125 parser.add_argument("--builddir",
127 help="Specifies the build directory where to"
128 " invoke ninja after updating.")
129 parser.add_argument("--no-interaction",
132 help="Do not allow interaction with the user.")
133 parser.add_argument("--manifest",
135 help="Use a android repo manifest to sync repositories"
136 " Note that it will let all repositories in detached state")
137 options = parser.parse_args()
141 if options.no_interaction:
145 repos_commits = manifest_get_commits(options.manifest)
149 revision, args = repos_commits.get('gst-build', [None, []])
150 if not update_repo('gst-build', SCRIPTDIR, revision, options.no_interaction, args):
153 if not update_subprojects(repos_commits, options.no_interaction):
157 ninja = accept_command(["ninja", "ninja-build"])
159 print("Can't find ninja, other backends are not supported for rebuilding")
162 if not os.path.exists(os.path.join (options.builddir, 'build.ninja')):
163 print("Can't rebuild in %s as no build.ninja file found." % options.builddir)
165 print("Rebuilding all GStreamer modules.")
166 exit(subprocess.call([ninja, '-C', options.builddir]))