From: Mike Frysinger Date: Sat, 17 Feb 2018 02:52:50 +0000 (-0500) Subject: git-repack: improve support for submodules X-Git-Url: https://git.wh0rd.org/?p=home.git;a=commitdiff_plain;h=c3fa90a1e846b66b91e1e01f87c0f9b8bf8d52bc git-repack: improve support for submodules --- diff --git a/.bin/git-repack b/.bin/git-repack index 063ff81..da56bea 100755 --- a/.bin/git-repack +++ b/.bin/git-repack @@ -23,6 +23,13 @@ def mount_settings(): return ret +def is_git_dir(path): + """Whether |path| is a .git dir""" + return (os.path.isdir(os.path.join(path, 'refs')) and + os.path.isdir(os.path.join(path, 'objects')) and + os.path.isfile(os.path.join(path, 'config'))) + + def find_git_dir(path): """Try to find the .git dir to operate on""" orig_path = path @@ -32,9 +39,7 @@ def find_git_dir(path): if os.path.isdir(os.path.join(path, '.git')): curr_path = os.path.join(path, '.git') - if (os.path.isdir(os.path.join(curr_path, 'refs')) and - os.path.isdir(os.path.join(curr_path, 'objects')) and - os.path.isfile(os.path.join(curr_path, 'config'))): + if is_git_dir(curr_path): return curr_path path = os.path.dirname(path) @@ -90,9 +95,11 @@ def clean_packs(path): def is_packed(path): """See if the git repo is already packed""" - if set(('info', 'pack')) != set(os.listdir(path)): + obj_path = os.path.join(path, 'objects') + paths = set(os.listdir(obj_path)) + if {'info', 'pack'} != paths and {'pack'} != paths: return False - packs = os.listdir(os.path.join(path, 'pack')) + packs = os.listdir(os.path.join(obj_path, 'pack')) if len(packs) != 2: return False return True @@ -103,11 +110,23 @@ def repack(path): path = find_git_dir(path) print('Repacking %s' % path) + # Repack any submodules this project might use. + modules_path = os.path.join(path, 'modules') + if os.path.isdir(modules_path): + for root, dirs, _ in os.walk(modules_path): + dirs.sort() + for d in dirs: + mod_path = os.path.join(root, d) + if is_git_dir(mod_path): + repack(mod_path) + tmpdir = find_temp_dir() if tmpdir: tmpdir = tempfile.mkdtemp(prefix='git-repack.', dir=tmpdir) print('Using tempdir: %s' % tmpdir) os.rmdir(tmpdir) + # Doesn't matter for these needs. + os.environ['GIT_WORK_TREE'] = tmpdir grafts = alts = None try: @@ -167,7 +186,7 @@ def repack(path): open(graft_file, 'w').write(grafts) if alts: open(alt_file, 'w').write(alts) - if tmpdir: + if tmpdir and os.path.exists(tmpdir): shutil.rmtree(tmpdir)