"""
import os, sys, zlib, time, subprocess, struct, stat, re, tempfile, glob
from bup.helpers import *
-from bup import _helpers, path, midx, bloom
+from bup import _helpers, path, midx, bloom, xstat
max_pack_size = 1000*1000*1000 # larger packs will slow down pruning
max_pack_objects = 200*1000 # cache memory usage is about 83 bytes per object
-SEEK_END=2 # os.SEEK_END is not defined in python 2.4
verbose = 0
ignore_midx = 0
return sum.digest()
-def _shalist_sort_key(ent):
+def shalist_item_sort_key(ent):
(mode, name, id) = ent
assert(mode+0 == mode)
if stat.S_ISDIR(mode):
def tree_encode(shalist):
"""Generate a git tree object from (mode,name,hash) tuples."""
- shalist = sorted(shalist, key = _shalist_sort_key)
+ shalist = sorted(shalist, key = shalist_item_sort_key)
l = []
for (mode,name,bin) in shalist:
assert(mode)
"""Generate a list of (mode,name,hash) from the git tree object in buf."""
ofs = 0
while ofs < len(buf):
- z = buf[ofs:].find('\0')
- assert(z > 0)
- spl = buf[ofs:ofs+z].split(' ', 1)
+ z = buf.find('\0', ofs)
+ assert(z > ofs)
+ spl = buf[ofs:z].split(' ', 1)
assert(len(spl) == 2)
mode,name = spl
- sha = buf[ofs+z+1:ofs+z+1+20]
- ofs += z+1+20
+ sha = buf[z+1:z+1+20]
+ ofs = z+1+20
yield (int(mode, 8), name, sha)
-def _encode_packobj(type, content):
+def _encode_packobj(type, content, compression_level=1):
szout = ''
sz = len(content)
szbits = (sz & 0x0f) | (_typemap[type]<<4)
break
szbits = sz & 0x7f
sz >>= 7
- z = zlib.compressobj(1)
+ if compression_level > 9:
+ compression_level = 9
+ elif compression_level < 0:
+ compression_level = 0
+ z = zlib.compressobj(compression_level)
yield szout
yield z.compress(content)
yield z.flush()
-def _encode_looseobj(type, content):
- z = zlib.compressobj(1)
+def _encode_looseobj(type, content, compression_level=1):
+ z = zlib.compressobj(compression_level)
yield z.compress('%s %d\0' % (type, len(content)))
yield z.compress(content)
yield z.flush()
else:
midxl.append(mx)
midxl.sort(key=lambda ix:
- (-len(ix), -os.stat(ix.name).st_mtime))
+ (-len(ix), -xstat.stat(ix.name).st_mtime))
for ix in midxl:
any_needed = False
for sub in ix.idxnames:
class PackWriter:
"""Writes Git objects inside a pack file."""
- def __init__(self, objcache_maker=_make_objcache):
+ def __init__(self, objcache_maker=_make_objcache, compression_level=1):
self.count = 0
self.outbytes = 0
self.filename = None
self.idx = None
self.objcache_maker = objcache_maker
self.objcache = None
+ self.compression_level = compression_level
def __del__(self):
self.close()
log('>')
if not sha:
sha = calc_hash(type, content)
- size, crc = self._raw_write(_encode_packobj(type, content), sha=sha)
+ size, crc = self._raw_write(_encode_packobj(type, content,
+ self.compression_level),
+ sha=sha)
if self.outbytes >= max_pack_size or self.count >= max_pack_objects:
self.breakpoint()
return sha
idx_f.truncate(ofs64_ofs)
idx_f.seek(0)
idx_map = mmap_readwrite(idx_f, close=False)
- idx_f.seek(0, SEEK_END)
+ idx_f.seek(0, os.SEEK_END)
count = _helpers.write_idx(idx_f, idx_map, idx, self.count)
assert(count == self.count)
idx_map.close()
if parent and not os.path.exists(parent):
raise GitError('parent directory "%s" does not exist\n' % parent)
if os.path.exists(d) and not os.path.isdir(os.path.join(d, '.')):
- raise GitError('"%d" exists but is not a directory\n' % d)
+ raise GitError('"%s" exists but is not a directory\n' % d)
p = subprocess.Popen(['git', '--bare', 'init'], stdout=sys.stderr,
preexec_fn = _gitenv)
_git_wait('git init', p)
p = subprocess.Popen(['git', 'config', 'pack.indexVersion', '2'],
stdout=sys.stderr, preexec_fn = _gitenv)
_git_wait('git config', p)
+ # Enable the reflog
+ p = subprocess.Popen(['git', 'config', 'core.logAllRefUpdates', 'true'],
+ stdout=sys.stderr, preexec_fn = _gitenv)
+ _git_wait('git config', p)
def check_repo_or_die(path=None):
except OSError, e:
if e.errno == errno.ENOENT:
if repodir != home_repodir:
- log('error: %r is not a bup/git repository\n' % repo())
+ log('error: %r is not a bup repository; run "bup init"\n'
+ % repo())
sys.exit(15)
else:
init_repo()
assert(self.p)
assert(self.p.poll() == None)
if self.inprogress:
- log('_fast_get: opening %r while %r is open'
+ log('_fast_get: opening %r while %r is open\n'
% (id, self.inprogress))
assert(not self.inprogress)
assert(id.find('\n') < 0)