9 from scripts.common import git
10 from scripts.common import Colors
13 SCRIPTDIR = os.path.normpath(os.path.dirname(__file__))
14 SUBPROJECTS_DIR = os.path.normpath(os.path.join(SCRIPTDIR, "subprojects"))
16 def repo_has_branch(repo_dir, branch):
18 git("describe", branch, repository_path=repo_dir)
19 except subprocess.CalledProcessError:
23 def parse_wrapfile(wrapf):
24 cgp = configparser.ConfigParser()
26 if 'wrap-git' not in cgp:
28 section = cgp['wrap-git']
29 return section['directory'], section['revision']
31 def get_wrap_subprojects(srcdir, gst_branch):
33 Parses wrap files in the subprojects directory for the specified source
34 tree and gets the revisions for all common repos.
36 for wrapf in glob.glob(os.path.join(srcdir, 'subprojects', '*.wrap')):
37 entries = parse_wrapfile(wrapf)
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')):
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
49 yield repo_name, repo_branch, parent_repo_dir
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))
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))
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()))
68 def checkout_subprojects(worktree_dir, branch):
69 worktree_subdir = os.path.join(worktree_dir, "subprojects")
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):
79 if __name__ == "__main__":
80 parser = argparse.ArgumentParser(prog="git-worktree")
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",
90 help="Do not output ansi colors.")
91 options = parser.parse_args()
93 if options.no_color or not Colors.can_enable():
96 options.worktree_dir = os.path.abspath(options.worktree_dir)
97 if not checkout_worktree('gst-build', SCRIPTDIR, options.worktree_dir, options.branch):
99 if not checkout_subprojects(options.worktree_dir, options.branch):