]>
git.wh0rd.org - home.git/blob - .bin/git-repack
da56bea35261d196d2436e96b4a3efccc99c1eb4
3 """Repack git repos fully the way I like them."""
5 from __future__
import print_function
17 """Return dict mapping path to its type"""
19 with
open('/proc/mounts') as fp
:
27 """Whether |path| is a .git dir"""
28 return (os
.path
.isdir(os
.path
.join(path
, 'refs')) and
29 os
.path
.isdir(os
.path
.join(path
, 'objects')) and
30 os
.path
.isfile(os
.path
.join(path
, 'config')))
33 def find_git_dir(path
):
34 """Try to find the .git dir to operate on"""
36 real_path
= path
= os
.path
.realpath(path
)
39 if os
.path
.isdir(os
.path
.join(path
, '.git')):
40 curr_path
= os
.path
.join(path
, '.git')
42 if is_git_dir(curr_path
):
45 path
= os
.path
.dirname(path
)
48 raise ValueError('could not locate .git dir: %s (%s)' %
49 (orig_path
, real_path
))
53 """Find a good temp dir (one backed by tmpfs)"""
58 tempfile
.gettempdir(),
60 mounts
= mount_settings()
61 for path
in SEARCH_PATHS
:
62 if mounts
.get(path
) == 'tmpfs':
68 """Read |path| and return its data"""
69 if os
.path
.isfile(path
):
70 return open(path
).read()
75 """Unlink |path| if it exists else do nothing"""
76 if os
.path
.isfile(path
):
80 def clean_hooks(path
):
81 """Strip out sample files from hooks/"""
82 hooks_path
= os
.path
.join(path
, 'hooks')
83 for hook
in glob
.glob(os
.path
.join(hooks_path
, '*.sample')):
84 print('Trimming hook: %s' % hook
)
88 def clean_packs(path
):
89 """Strip out temp files from objects/packs/"""
90 packs_path
= os
.path
.join(path
, 'objects', 'packs')
91 for pack
in glob
.glob(os
.path
.join(packs_path
, 'tmp_pack_*')):
92 print('Trimming pack: %s' % pack
)
97 """See if the git repo is already packed"""
98 obj_path
= os
.path
.join(path
, 'objects')
99 paths
= set(os
.listdir(obj_path
))
100 if {'info', 'pack'} != paths
and {'pack'} != paths
:
102 packs
= os
.listdir(os
.path
.join(obj_path
, 'pack'))
109 """Clean up and trim cruft and repack |path|"""
110 path
= find_git_dir(path
)
111 print('Repacking %s' % path
)
113 # Repack any submodules this project might use.
114 modules_path
= os
.path
.join(path
, 'modules')
115 if os
.path
.isdir(modules_path
):
116 for root
, dirs
, _
in os
.walk(modules_path
):
119 mod_path
= os
.path
.join(root
, d
)
120 if is_git_dir(mod_path
):
123 tmpdir
= find_temp_dir()
125 tmpdir
= tempfile
.mkdtemp(prefix
='git-repack.', dir=tmpdir
)
126 print('Using tempdir: %s' % tmpdir
)
128 # Doesn't matter for these needs.
129 os
.environ
['GIT_WORK_TREE'] = tmpdir
133 # Push/pop the graft & alternate paths so we don't read them.
134 # XXX: In some cases, this is bad, but I don't use them that way ...
135 graft_file
= os
.path
.join(path
, 'info', 'grafts')
136 grafts
= readfile(graft_file
)
139 alt_file
= os
.path
.join(path
, 'objects', 'info', 'alternates')
140 alts
= readfile(alt_file
)
145 origin_path
= os
.path
.join(path
, 'refs', 'remotes', 'origin')
146 packed_refs
= readfile(os
.path
.join(path
, 'packed-refs'))
147 if os
.path
.exists(origin_path
) or 'refs/remotes/origin/' in packed_refs
:
148 cmd
= ['git', '--git-dir', path
, 'remote', 'prune', 'origin']
149 subprocess
.check_call(cmd
, cwd
='/')
154 print('Git repo is already packed; nothing to do')
158 print('Syncing git repo to tempdir')
159 shutil
.copytree(path
, tmpdir
, symlinks
=True)
164 cmd
= ['git', '--git-dir', rundir
, 'reflog', 'expire', '--all', '--stale-fix']
165 print('Cleaning reflog: %s' % ' '.join(cmd
))
166 subprocess
.check_call(cmd
, cwd
='/')
168 # This also packs refs/tags for us.
169 cmd
= ['git', '--git-dir', rundir
, 'gc', '--aggressive', '--prune=all']
170 print('Repacking git repo: %s' % ' '.join(cmd
))
171 subprocess
.check_call(cmd
, cwd
='/')
174 cmd
= ['find', rundir
, '-depth', '-type', 'd', '-exec', 'rmdir', '{}', '+']
175 subprocess
.call(cmd
, stderr
=open('/dev/null', 'w'))
178 cmd
= ['rsync', '-a', '--delete', tmpdir
+ '/', path
+ '/']
179 print('Syncing back git repo: %s' % ' '.join(cmd
))
180 subprocess
.check_call(cmd
, cwd
='/')
181 cmd
= ['find', path
+ '/', '-exec', 'chmod', 'u+rw', '{}', '+']
182 subprocess
.check_call(cmd
, cwd
='/')
186 open(graft_file
, 'w').write(grafts
)
188 open(alt_file
, 'w').write(alts
)
189 if tmpdir
and os
.path
.exists(tmpdir
):
190 shutil
.rmtree(tmpdir
)
194 """Get the command line parser"""
195 parser
= argparse
.ArgumentParser(description
=__doc__
)
196 parser
.add_argument('dir', help='The git repo to process')
201 """The main script entry point"""
202 parser
= get_parser()
203 opts
= parser
.parse_args(argv
)
207 if __name__
== '__main__':
208 exit(main(sys
.argv
[1:]))