]> git.wh0rd.org - home.git/commitdiff
git-repack: improve support for submodules
authorMike Frysinger <vapier@gentoo.org>
Sat, 17 Feb 2018 02:52:50 +0000 (21:52 -0500)
committerMike Frysinger <vapier@gentoo.org>
Sat, 17 Feb 2018 02:52:50 +0000 (21:52 -0500)
.bin/git-repack

index 063ff81a8547f93e00cabd2524d2ad8278f019de..da56bea35261d196d2436e96b4a3efccc99c1eb4 100755 (executable)
@@ -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)