From: Rob Browning Date: Tue, 21 Jan 2014 23:31:13 +0000 (-0600) Subject: Allow per-instance specification of the repo_dir for CatPipe X-Git-Tag: 0.27-rc1~34 X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=bup.git;a=commitdiff_plain;h=75d5c2b0c0b18430d4abb637093421c4dd688cab Allow per-instance specification of the repo_dir for CatPipe This allows the creation of CatPipes that refer to repositories other than the default, which in turn allows copying between repositories, etc. Signed-off-by: Rob Browning Tested-by: Rob Browning --- diff --git a/lib/bup/git.py b/lib/bup/git.py index 76145a4..a8f3729 100644 --- a/lib/bup/git.py +++ b/lib/bup/git.py @@ -763,8 +763,12 @@ def _git_date(date): return '%d %s' % (date, time.strftime('%z', time.localtime(date))) -def _gitenv(): - os.environ['GIT_DIR'] = os.path.abspath(repo()) +def _gitenv(repo_dir = None): + if not repo_dir: + repo_dir = repo() + def env(): + os.environ['GIT_DIR'] = os.path.abspath(repo_dir) + return env def list_refs(refname = None): @@ -774,7 +778,7 @@ def list_refs(refname = None): argv = ['git', 'show-ref', '--'] if refname: argv += [refname] - p = subprocess.Popen(argv, preexec_fn = _gitenv, stdout = subprocess.PIPE) + p = subprocess.Popen(argv, preexec_fn = _gitenv(), stdout = subprocess.PIPE) out = p.stdout.read().strip() rv = p.wait() # not fatal if rv: @@ -810,7 +814,7 @@ def rev_list(ref, count=None): if count: opts += ['-n', str(atoi(count))] argv = ['git', 'rev-list', '--pretty=format:%at'] + opts + [ref, '--'] - p = subprocess.Popen(argv, preexec_fn = _gitenv, stdout = subprocess.PIPE) + p = subprocess.Popen(argv, preexec_fn = _gitenv(), stdout = subprocess.PIPE) commit = None for row in p.stdout: s = row.strip() @@ -869,7 +873,7 @@ def update_ref(refname, newval, oldval): assert(refname.startswith('refs/heads/')) p = subprocess.Popen(['git', 'update-ref', refname, newval.encode('hex'), oldval.encode('hex')], - preexec_fn = _gitenv) + preexec_fn = _gitenv()) _git_wait('git update-ref', p) @@ -899,16 +903,16 @@ def init_repo(path=None): if os.path.exists(d) and not os.path.isdir(os.path.join(d, '.')): raise GitError('"%s" exists but is not a directory\n' % d) p = subprocess.Popen(['git', '--bare', 'init'], stdout=sys.stderr, - preexec_fn = _gitenv) + preexec_fn = _gitenv()) _git_wait('git init', p) # Force the index version configuration in order to ensure bup works # regardless of the version of the installed Git binary. p = subprocess.Popen(['git', 'config', 'pack.indexVersion', '2'], - stdout=sys.stderr, preexec_fn = _gitenv) + 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) + stdout=sys.stderr, preexec_fn = _gitenv()) _git_wait('git config', p) @@ -964,7 +968,7 @@ def _git_wait(cmd, p): def _git_capture(argv): - p = subprocess.Popen(argv, stdout=subprocess.PIPE, preexec_fn = _gitenv) + p = subprocess.Popen(argv, stdout=subprocess.PIPE, preexec_fn = _gitenv()) r = p.stdout.read() _git_wait(repr(argv), p) return r @@ -1003,8 +1007,9 @@ class _AbortableIter: _ver_warned = 0 class CatPipe: """Link to 'git cat-file' that is used to retrieve blob data.""" - def __init__(self): + def __init__(self, repo_dir = None): global _ver_warned + self.repo_dir = repo_dir wanted = ('1','5','6') if ver() < wanted: if not _ver_warned: @@ -1030,7 +1035,7 @@ class CatPipe: stdout=subprocess.PIPE, close_fds = True, bufsize = 4096, - preexec_fn = _gitenv) + preexec_fn = _gitenv(self.repo_dir)) def _fast_get(self, id): if not self.p or self.p.poll() != None: @@ -1079,7 +1084,7 @@ class CatPipe: p = subprocess.Popen(['git', 'cat-file', type, id], stdout=subprocess.PIPE, - preexec_fn = _gitenv) + preexec_fn = _gitenv(self.repo_dir)) for blob in chunkyreader(p.stdout): yield blob _git_wait('git cat-file', p)