+def HasBranchHeads():
+ cmd = ['git', 'for-each-ref', '--format=%(refname)',
+ 'refs/remotes/branch-heads']
+ output = subprocess.check_output(cmd).splitlines()
+ return output != []
+
+
+def CheckoutDirectories(dest_dirname, refname, root_path, patterns=None):
+ treeish = '%s:%s' % (refname, root_path)
+ cmd = ['git', 'ls-tree', '--full-tree', '-r', treeish]
+ if patterns:
+ cmd.extend(patterns)
+
+ Trace('Running \"%s\":' % ' '.join(cmd))
+ output = subprocess.check_output(cmd)
+ for line in output.splitlines():
+ info, rel_filename = line.split('\t')
+ sha = info.split(' ')[2]
+
+ Trace(' %s %s' % (sha, rel_filename))
+
+ cmd = ['git', 'show', sha]
+ blob = subprocess.check_output(cmd)
+ filename = os.path.join(dest_dirname, rel_filename)
+ dirname = os.path.dirname(filename)
+ if not os.path.exists(dirname):
+ os.makedirs(dirname)
+
+ Trace(' writing to %s' % filename)
+ with open(filename, 'w') as f:
+ f.write(blob)