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
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)
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
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:
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)