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):
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')):
73 print("Updating %s..." % repo_name)
75 revision = repos_commits.get(repo_name)
77 git(["fetch"], repo_dir)
78 git(["checkout", revision], repo_dir)
80 git(["pull", "--rebase"], repo_dir)
81 except Exception as e:
82 print("\nCould not rebase %s, please fix and try again\nerror:\n %s" % (repo_dir, e))
85 commit_message = git("show", repo_dir).split("\n")
86 print(u" -> %s%s%s — %s" % (Colors.HEADER, commit_message[0][7:14], Colors.ENDC,
87 commit_message[4].strip()))
92 if __name__ == "__main__":
93 parser = argparse.ArgumentParser(prog="git-update")
95 parser.add_argument("--no-color",
98 help="Do not output ansi colors.")
99 parser.add_argument("--manifest",
101 help="Use a android repo manifest to sync repositories"
102 " Note that it will let all repositories in detached state")
103 options = parser.parse_args()
107 exit(not update_subprojects(options.manifest))