Allow passing a manifest to update subprojects repository
[platform/upstream/gstreamer.git] / git-update
1 #!/usr/bin/env python3
2 import argparse
3 import os
4 import subprocess
5 import xml.etree.ElementTree as ET
6
7
8 SCRIPTDIR = os.path.dirname(__file__)
9
10 class Colors:
11     HEADER = '\033[95m'
12     OKBLUE = '\033[94m'
13     OKGREEN = '\033[92m'
14     WARNING = '\033[93m'
15     FAIL = '\033[91m'
16     ENDC = '\033[0m'
17
18     force_disable = False
19
20     @classmethod
21     def disable(cls):
22         cls.HEADER = ''
23         cls.OKBLUE = ''
24         cls.OKGREEN = ''
25         cls.WARNING = ''
26         cls.FAIL = ''
27         cls.ENDC = ''
28
29     @classmethod
30     def enable(cls):
31         if cls.force_disable:
32             return
33
34         cls.HEADER = '\033[95m'
35         cls.OKBLUE = '\033[94m'
36         cls.OKGREEN = '\033[92m'
37         cls.WARNING = '\033[93m'
38         cls.FAIL = '\033[91m'
39         cls.ENDC = '\033[0m'
40
41
42
43 def git(args, repository_path):
44     if not isinstance(args, list):
45         args = [args]
46
47     return subprocess.check_output(["git"] + args, cwd=repository_path,
48                                    stderr=subprocess.STDOUT).decode()
49
50
51 def manifest_get_commits(manifest):
52     res = {}
53     tree = ET.parse(manifest)
54     root = tree.getroot()
55     for child in root:
56         if child.tag == 'project':
57             res[child.attrib["name"]] = child.attrib["revision"]
58     return res
59
60
61 def update_subprojects(manifest):
62     if manifest:
63         repos_commits = manifest_get_commits(manifest)
64     else:
65         repos_commits = {}
66
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')):
71             continue
72
73         print("Updating %s..." % repo_name)
74         try:
75             revision = repos_commits.get(repo_name)
76             if revision:
77                 git(["fetch"], repo_dir)
78                 git(["checkout", revision], repo_dir)
79             else:
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))
83             return False
84
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()))
88
89     return True
90
91
92 if __name__ == "__main__":
93     parser = argparse.ArgumentParser(prog="git-update")
94
95     parser.add_argument("--no-color",
96                         default=False,
97                         action='store_true',
98                         help="Do not output ansi colors.")
99     parser.add_argument("--manifest",
100                         default=None,
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()
104     if options.no_color:
105         Colors.disable()
106
107     exit(not update_subprojects(options.manifest))