open(
channel.table['git-revision'].file).read(999) == channel.git_revision)
+
def git_cachedir(git_repo: str) -> str:
# TODO: Consider using pyxdg to find this path.
- return os.path.expanduser('~/.cache/nix-pin-channel/git/%s' % digest_string(git_repo.encode()))
+ return os.path.expanduser(
+ '~/.cache/nix-pin-channel/git/%s' %
+ digest_string(
+ git_repo.encode()))
+
+
+def verify_git_ancestry(v: Verification, channel: Channel) -> None:
+ cachedir = git_cachedir(channel.git_repo)
+ v.status('Verifying rev is an ancestor of ref')
+ process = subprocess.run(['git',
+ '-C',
+ cachedir,
+ 'merge-base',
+ '--is-ancestor',
+ channel.git_revision,
+ channel.git_ref])
+ v.result(process.returncode == 0)
+
+ if hasattr(channel, 'old_git_revision'):
+ v.status(
+ 'Verifying rev is an ancestor of previous rev %s' %
+ channel.old_git_revision)
+ process = subprocess.run(['git',
+ '-C',
+ cachedir,
+ 'merge-base',
+ '--is-ancestor',
+ channel.old_git_revision,
+ channel.git_revision])
+ v.result(process.returncode == 0)
def git_fetch(v: Verification, channel: Channel) -> None:
['git', 'init', '--bare', cachedir])
v.result(process.returncode == 0)
- have_rev = False
+ v.status('Fetching ref "%s" from %s' % (channel.git_ref, channel.git_repo))
+ # We don't use --force here because we want to abort and freak out if forced
+ # updates are happening.
+ process = subprocess.run(['git',
+ '-C',
+ cachedir,
+ 'fetch',
+ channel.git_repo,
+ '%s:%s' % (channel.git_ref,
+ channel.git_ref)])
+ v.result(process.returncode == 0)
+
if hasattr(channel, 'git_revision'):
- v.status('Checking if we already have this rev:')
+ v.status('Verifying that fetch retrieved this rev')
process = subprocess.run(
['git', '-C', cachedir, 'cat-file', '-e', channel.git_revision])
- if process.returncode == 0:
- v.status('yes')
- if process.returncode == 1:
- v.status('no')
- v.result(process.returncode == 0 or process.returncode == 1)
- have_rev = process.returncode == 0
-
- if not have_rev:
- v.status(
- 'Fetching ref "%s" from %s' %
- (channel.git_ref, channel.git_repo))
- # We don't use --force here because we want to abort and freak out if forced
- # updates are happening.
- process = subprocess.run(['git',
- '-C',
- cachedir,
- 'fetch',
- channel.git_repo,
- '%s:%s' % (channel.git_ref,
- channel.git_ref)])
v.result(process.returncode == 0)
- if hasattr(channel, 'git_revision'):
- v.status('Verifying that fetch retrieved this rev')
- process = subprocess.run(
- ['git', '-C', cachedir, 'cat-file', '-e', channel.git_revision])
- v.result(process.returncode == 0)
-
- if not hasattr(channel, 'git_revision'):
+ else:
channel.git_revision = open(
os.path.join(
cachedir,
'heads',
channel.git_ref)).read(999).strip()
- v.status('Verifying rev is an ancestor of ref')
- process = subprocess.run(['git',
- '-C',
- cachedir,
- 'merge-base',
- '--is-ancestor',
- channel.git_revision,
- channel.git_ref])
- v.result(process.returncode == 0)
+ verify_git_ancestry(v, channel)
- if hasattr(channel, 'old_git_revision'):
- v.status(
- 'Verifying rev is an ancestor of previous rev %s' %
- channel.old_git_revision)
- process = subprocess.run(['git',
- '-C',
- cachedir,
- 'merge-base',
- '--is-ancestor',
- channel.old_git_revision,
- channel.git_revision])
- v.result(process.returncode == 0)
+
+def ensure_git_rev_available(v: Verification, channel: Channel) -> None:
+ cachedir = git_cachedir(channel.git_repo)
+ if os.path.exists(cachedir):
+ v.status('Checking if we already have this rev:')
+ process = subprocess.run(
+ ['git', '-C', cachedir, 'cat-file', '-e', channel.git_revision])
+ if process.returncode == 0:
+ v.status('yes')
+ if process.returncode == 1:
+ v.status('no')
+ v.result(process.returncode == 0 or process.returncode == 1)
+ if process.returncode == 0:
+ verify_git_ancestry(v, channel)
+ return
+ git_fetch(v, channel)
def compare_tarball_and_git(
fetch(v, channel)
parse_channel(v, channel)
fetch_resources(v, channel)
- git_fetch(v, channel)
+ ensure_git_rev_available(v, channel)
check_channel_contents(v, channel)