scripts: Add a script to check that all repos are clean
[platform/upstream/gstreamer.git] / checkout-branch-worktree
1 #!/usr/bin/env python3
2
3 import argparse
4 import json
5 import os
6 import subprocess
7 import xml.etree.ElementTree as ET
8 import sys
9
10 from scripts.common import git
11 from scripts.common import Colors
12 from scripts.common import get_meson
13 from scripts.common import accept_command
14
15
16 SCRIPTDIR = os.path.normpath(os.path.dirname(__file__))
17
18
19 def checkout_subprojects(worktree_dir, branch):
20     subprojects_dir = os.path.join(SCRIPTDIR, "subprojects")
21     worktree_subdir = os.path.join(worktree_dir, "subprojects")
22
23     meson = get_meson()
24     installed_s = subprocess.check_output(meson + ['introspect', options.builddir, '--projectinfo'])
25     for subproj in json.loads(installed_s.decode())["subprojects"]:
26         repo_name = subproj["name"]
27         if not repo_name.startswith("gst"):
28             continue
29
30         repo_dir = os.path.normpath(os.path.join(SCRIPTDIR, subprojects_dir, repo_name))
31         if not os.path.exists(os.path.join(repo_dir, '.git')):
32             continue
33
34         workdir = os.path.normpath(os.path.join(worktree_subdir, repo_name))
35         if not checkout_worktree(repo_name, repo_dir, workdir, branch):
36             return False
37
38     return True
39
40
41 def checkout_worktree(repo_name, repo_dir, worktree_dir, branch):
42     print("Checking out worktree %s in %s (branch %s)" % (repo_name, worktree_dir, branch))
43     try:
44         git("worktree", "add", worktree_dir, branch, repository_path=repo_dir)
45     except Exception as e:
46         out = getattr(e, "output", b"").decode()
47         print("\nCould not checkout worktree %s, please fix and try again."
48               " Error:\n\n%s %s" % (repo_dir, out, e))
49
50         return False
51
52     commit_message = git("show", "--shortstat", repository_path=repo_dir).split("\n")
53     print(u"  -> %s%s%s - %s" % (Colors.HEADER, repo_dir, Colors.ENDC,
54                                     commit_message[4].strip()))
55
56     return True
57
58
59 if __name__ == "__main__":
60     parser = argparse.ArgumentParser(prog="git-update")
61
62
63     parser.add_argument('worktree_dir', metavar='worktree_dir', type=str,
64                         help='The directory where to checkout the new worktree')
65     parser.add_argument('branch', metavar='branch', type=str,
66                         help='The branch to checkout')
67     parser.add_argument("--no-color",
68                         default=False,
69                         action='store_true',
70                         help="Do not output ansi colors.")
71     parser.add_argument("--builddir", '-C',
72                         default=os.path.join(SCRIPTDIR, "build"),
73                         help="The meson build directory")
74     options = parser.parse_args()
75
76     if options.no_color or not Colors.can_enable():
77         Colors.disable()
78
79     if not os.path.exists(options.builddir):
80         print("GStreamer not built in %s\n\nBuild it and try again" %
81               options.builddir)
82         exit(1)
83
84     options.worktree_dir = os.path.abspath(options.worktree_dir)
85     if not checkout_worktree('gst-build', SCRIPTDIR, options.worktree_dir, options.branch):
86         exit(1)
87     if not checkout_subprojects(options.worktree_dir, options.branch):
88         exit(1)