Add an helper to update subprojects git repos
[platform/upstream/gstreamer.git] / git-update
1 #!/usr/bin/env python3
2 import argparse
3 import os
4 import subprocess
5
6
7 SCRIPTDIR = os.path.dirname(__file__)
8
9 class Colors:
10     HEADER = '\033[95m'
11     OKBLUE = '\033[94m'
12     OKGREEN = '\033[92m'
13     WARNING = '\033[93m'
14     FAIL = '\033[91m'
15     ENDC = '\033[0m'
16
17     force_disable = False
18
19     @classmethod
20     def disable(cls):
21         cls.HEADER = ''
22         cls.OKBLUE = ''
23         cls.OKGREEN = ''
24         cls.WARNING = ''
25         cls.FAIL = ''
26         cls.ENDC = ''
27
28     @classmethod
29     def enable(cls):
30         if cls.force_disable:
31             return
32
33         cls.HEADER = '\033[95m'
34         cls.OKBLUE = '\033[94m'
35         cls.OKGREEN = '\033[92m'
36         cls.WARNING = '\033[93m'
37         cls.FAIL = '\033[91m'
38         cls.ENDC = '\033[0m'
39
40
41
42 def git(args, repository_path):
43     if not isinstance(args, list):
44         args = [args]
45
46     return subprocess.check_output(["git"] + args, cwd=repository_path).decode()
47
48
49 def update_subprojects():
50     subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
51     for repo_name in os.listdir(subprojects_dir):
52         repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
53         if not os.path.exists(os.path.join(repo_dir, '.git')):
54             continue
55
56         print("Updating %s..." % repo_name)
57         try:
58             git(["pull", "--rebase"], repo_dir)
59         except Exception as e:
60             print("\nCould not rebase %s, please fix and try again\nerror:\n  %s" % (repo_dir, e))
61             return False
62
63         commit_message = git("show", repo_dir).split("\n")
64         print(u"  -> %s%s%s — %s" % (Colors.HEADER, commit_message[0][7:14], Colors.ENDC,
65                                        commit_message[4].strip()))
66
67     return True
68
69
70 if __name__ == "__main__":
71     parser = argparse.ArgumentParser(prog="git-update")
72
73     parser.add_argument("--no-color",
74                         default=False,
75                         action='store_true',
76                         help="Do not output ansi colors.")
77     options = parser.parse_args()
78     if options.no_color:
79         Colors.disable()
80
81     exit(not update_subprojects())