5 import xml.etree.ElementTree as ET
8 from common import Colors
11 SCRIPTDIR = os.path.dirname(__file__)
14 def manifest_get_commits(manifest):
16 tree = ET.parse(manifest)
19 if child.tag == 'project':
20 res[child.attrib["name"]] = child.attrib["revision"]
24 def update_subprojects(manifest, no_interaction=False):
26 repos_commits = manifest_get_commits(manifest)
30 subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
31 for repo_name in os.listdir(subprojects_dir):
32 repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
33 if not os.path.exists(os.path.join(repo_dir, '.git')):
35 revision = repos_commits.get(repo_name)
36 if not update_repo(repo_name, repo_dir, revision, no_interaction):
42 def update_repo(repo_name, repo_dir, revision, no_interaction, recurse_i=0):
43 print("Updating %s..." % repo_name)
46 git("fetch", repository_path=repo_dir)
47 git("checkout", revision, repository_path=repo_dir)
49 git("pull", "--rebase", repository_path=repo_dir)
50 except Exception as e:
51 out = getattr(e, "output", b"").decode()
52 if not no_interaction:
53 print("====================================="
54 "\n%sEntering a shell in %s to fix that"
55 " just `exit` once done`"
56 "\n=====================================" % (
59 subprocess.check_call(os.environ.get("SHELL", "/bin/sh"),
62 # Result of subshell does not really matter
66 return update_repo(repo_name, repo_dir, revision, no_interaction,
70 print("\nCould not rebase %s, please fix and try again."
71 " Error:\n\n%s %s" % (repo_dir, out, e))
76 commit_message = git("show", repository_path=repo_dir).split("\n")
77 print(u" -> %s%s%s — %s" % (Colors.HEADER, commit_message[0][7:14], Colors.ENDC,
78 commit_message[4].strip()))
83 if __name__ == "__main__":
84 parser = argparse.ArgumentParser(prog="git-update")
86 parser.add_argument("--no-color",
89 help="Do not output ansi colors.")
90 parser.add_argument("--no-interaction",
93 help="Do not allow interaction with the user.")
94 parser.add_argument("--manifest",
96 help="Use a android repo manifest to sync repositories"
97 " Note that it will let all repositories in detached state")
98 options = parser.parse_args()
102 exit(not update_subprojects(options.manifest,
103 options.no_interaction))