1240277b0e3623bc54c8bb1d17592365cd186859
[platform/upstream/gstreamer.git] / checkout-branch-worktree
1 #!/usr/bin/env python3
2
3 import os
4 import glob
5 import argparse
6 import subprocess
7 import configparser
8
9 from scripts.common import git
10 from scripts.common import Colors
11
12
13 SCRIPTDIR = os.path.normpath(os.path.dirname(__file__))
14 SUBPROJECTS_DIR = os.path.normpath(os.path.join(SCRIPTDIR, "subprojects"))
15
16 def repo_has_branch(repo_dir, branch):
17     try:
18         git("describe", branch, repository_path=repo_dir)
19     except subprocess.CalledProcessError:
20         return False
21     return True
22
23 def parse_wrapfile(wrapf):
24     cgp = configparser.ConfigParser()
25     cgp.read(wrapf)
26     if 'wrap-git' not in cgp:
27         return None
28     section = cgp['wrap-git']
29     return section['directory'], section['revision']
30
31 def get_wrap_subprojects(srcdir, gst_branch):
32     '''
33     Parses wrap files in the subprojects directory for the specified source
34     tree and gets the revisions for all common repos.
35     '''
36     for wrapf in glob.glob(os.path.join(srcdir, 'subprojects', '*.wrap')):
37         entries = parse_wrapfile(wrapf)
38         if not entries:
39             continue
40
41         repo_name, repo_branch = entries
42         parent_repo_dir = os.path.join(SUBPROJECTS_DIR, repo_name)
43         if not os.path.exists(os.path.join(parent_repo_dir, '.git')):
44             continue
45         # If a branch of the same name exists in the gst subproject, use it
46         if repo_name.startswith('gst') and repo_has_branch(parent_repo_dir, gst_branch):
47             repo_branch = gst_branch
48
49         yield repo_name, repo_branch, parent_repo_dir
50
51 def checkout_worktree(repo_name, repo_dir, worktree_dir, branch):
52     print('Checking out worktree for project {!r} into {!r} '
53           '(branch {})'.format(repo_name, worktree_dir, branch))
54     try:
55         git("worktree", "add", worktree_dir, branch, repository_path=repo_dir)
56     except Exception as e:
57         out = getattr(e, "output", b"").decode()
58         print("\nCould not checkout worktree %s, please fix and try again."
59               " Error:\n\n%s %s" % (repo_dir, out, e))
60
61         return False
62
63     commit_message = git("show", "--shortstat", repository_path=repo_dir).split("\n")
64     print(u"  -> %s%s%s - %s" % (Colors.HEADER, repo_dir, Colors.ENDC,
65                                     commit_message[4].strip()))
66     return True
67
68 def checkout_subprojects(worktree_dir, branch):
69     worktree_subdir = os.path.join(worktree_dir, "subprojects")
70
71     for repo_name, repo_branch, parent_repo_dir in get_wrap_subprojects(worktree_dir, branch):
72         workdir = os.path.normpath(os.path.join(worktree_subdir, repo_name))
73         if not checkout_worktree(repo_name, parent_repo_dir, workdir, repo_branch):
74             return False
75
76     return True
77
78
79 if __name__ == "__main__":
80     parser = argparse.ArgumentParser(prog="git-worktree")
81
82
83     parser.add_argument('worktree_dir', metavar='worktree_dir', type=str,
84                         help='The directory where to checkout the new worktree')
85     parser.add_argument('branch', metavar='branch', type=str,
86                         help='The branch to checkout')
87     parser.add_argument("--no-color",
88                         default=False,
89                         action='store_true',
90                         help="Do not output ansi colors.")
91     options = parser.parse_args()
92
93     if options.no_color or not Colors.can_enable():
94         Colors.disable()
95
96     options.worktree_dir = os.path.abspath(options.worktree_dir)
97     if not checkout_worktree('gst-build', SCRIPTDIR, options.worktree_dir, options.branch):
98         exit(1)
99     if not checkout_subprojects(options.worktree_dir, options.branch):
100         exit(1)