5 import xml.etree.ElementTree as ET
8 SCRIPTDIR = os.path.dirname(__file__)
34 cls.HEADER = '\033[95m'
35 cls.OKBLUE = '\033[94m'
36 cls.OKGREEN = '\033[92m'
37 cls.WARNING = '\033[93m'
43 def git(args, repository_path):
44 if not isinstance(args, list):
47 return subprocess.check_output(["git"] + args, cwd=repository_path,
48 stderr=subprocess.STDOUT).decode()
51 def manifest_get_commits(manifest):
53 tree = ET.parse(manifest)
56 if child.tag == 'project':
57 res[child.attrib["name"]] = child.attrib["revision"]
61 def update_subprojects(manifest, no_interaction=False):
63 repos_commits = manifest_get_commits(manifest)
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')):
72 revision = repos_commits.get(repo_name)
73 if not update_repo(repo_name, repo_dir, revision, no_interaction):
79 def update_repo(repo_name, repo_dir, revision, no_interaction, recurse_i=0):
80 print("Updating %s..." % repo_name)
83 git(["fetch"], repo_dir)
84 git(["checkout", revision], repo_dir)
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=====================================" % (
96 subprocess.check_call(os.environ.get("SHELL", "/bin/sh"),
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", 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("--no-interaction",
130 help="Do not allow interaction with the user.")
131 parser.add_argument("--manifest",
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()
139 exit(not update_subprojects(options.manifest,
140 options.no_interaction))