|
|
@@ -0,0 +1,100 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+
|
|
|
+
|
|
|
+DEBUG = 0
|
|
|
+
|
|
|
+def main():
|
|
|
+ global DEBUG
|
|
|
+ p = parser()
|
|
|
+ args = p.parse_args()
|
|
|
+ DEBUG = args.debug
|
|
|
+ if args.source_path.find("://") < 0:
|
|
|
+ args.source_path = "ssh://" + args.source_path.replace( ":", "/", 1)
|
|
|
+
|
|
|
+ recurse_abe_submodules( args.target_path, args.source_path, args.branch, clone_repo)
|
|
|
+
|
|
|
+
|
|
|
+def recurse_abe_submodules(path, remote, ref=None, func=None):
|
|
|
+ if func != None:
|
|
|
+ func( path, remote, ref)
|
|
|
+
|
|
|
+ subm = get_abe_submodules( path)
|
|
|
+
|
|
|
+ import os
|
|
|
+ import urllib.parse
|
|
|
+ url = urllib.parse.urlparse(remote)
|
|
|
+ print(url)
|
|
|
+ for su in subm:
|
|
|
+ newpath = os.path.realpath(os.path.join( path, su.subdir))
|
|
|
+ newurl = url._replace(path=su.remote)
|
|
|
+ print(newurl.geturl())
|
|
|
+ recurse_abe_submodules( newpath, newurl.geturl(), su.ref, func)
|
|
|
+
|
|
|
+def clone_repo(path, remote, ref=None):
|
|
|
+ print('Repo:', path) if DEBUG else None
|
|
|
+ from git import Repo
|
|
|
+ repo = Repo.init(path, bare=False)
|
|
|
+ if 'origin' in repo.remotes:
|
|
|
+ origin = repo.remotes['origin']
|
|
|
+ origin.fetch()
|
|
|
+ else:
|
|
|
+ origin = repo.create_remote('origin', remote)
|
|
|
+ origin.fetch()
|
|
|
+
|
|
|
+ print('Refs:', origin.refs) if DEBUG else None
|
|
|
+ print('Heads:', repo.heads) if DEBUG else None
|
|
|
+
|
|
|
+ if ref in repo.tags:
|
|
|
+ checkout_ref = repo.tags[ref]
|
|
|
+ active_branch = repo.create_head("local_tag_branch_"+ref, checkout_ref)
|
|
|
+ elif ref in repo.heads:
|
|
|
+ active_branch = repo.heads[ref]
|
|
|
+ checkout_ref = active_branch.tracking_branch
|
|
|
+ else:
|
|
|
+ checkout_ref = origin.refs[ref]
|
|
|
+ active_branch = repo.create_head(ref, checkout_ref)
|
|
|
+ active_branch.set_tracking_branch(checkout_ref)
|
|
|
+ active_branch.checkout()
|
|
|
+ try:
|
|
|
+ origin.pull()
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return repo
|
|
|
+
|
|
|
+def parser():
|
|
|
+ import argparse
|
|
|
+ p = argparse.ArgumentParser()
|
|
|
+ p.add_argument('-d', '--debug', action='count', default=0)
|
|
|
+ p.add_argument('-t', '--target_path', default='.')
|
|
|
+ p.add_argument('-b', '--branch', default='master')
|
|
|
+ p.add_argument('source_path')
|
|
|
+ return p
|
|
|
+
|
|
|
+class AbeSubmodule():
|
|
|
+ def __init__(self, sub_info):
|
|
|
+ self.subdir = sub_info[0]
|
|
|
+ self.remote = sub_info[1]
|
|
|
+ self.ref = sub_info[2]
|
|
|
+
|
|
|
+ def __repr__(self):
|
|
|
+ return f"AbeSubmodule(['{self.subdir}','{self.remote}','{self.ref}'])"
|
|
|
+
|
|
|
+def get_abe_submodules(repo_dir):
|
|
|
+ import os
|
|
|
+ subfile_path = os.path.join(repo_dir, '.abe', 'submodules')
|
|
|
+ if os.path.isfile(subfile_path):
|
|
|
+ with open(subfile_path) as subfile:
|
|
|
+ for line in subfile.readlines():
|
|
|
+ strline = line.strip()
|
|
|
+ if strline.startswith('#'):
|
|
|
+ continue
|
|
|
+ sline = strline.split()
|
|
|
+ if len(sline) < 3:
|
|
|
+ continue
|
|
|
+ print("Submodule:", sline) if DEBUG else None
|
|
|
+ yield AbeSubmodule(sline)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+if __name__=='__main__':
|
|
|
+ main()
|