From: Scott Worley Date: Fri, 10 Apr 2020 22:29:38 +0000 (-0700) Subject: Split ensure_git_rev_available out from git_fetch X-Git-Tag: 1.0~2 X-Git-Url: http://git.scottworley.com/pinch/commitdiff_plain/971d3659a4f2ba4e3daeba142668809529ff65c1?ds=sidebyside;hp=9836141c70e29986f4f2664ad6fb1670eac5f40f Split ensure_git_rev_available out from git_fetch --- diff --git a/pinch.py b/pinch.py index 3e725e1..0f8f00a 100644 --- a/pinch.py +++ b/pinch.py @@ -210,9 +210,39 @@ def fetch_resources(v: Verification, channel: Channel) -> None: 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: @@ -229,39 +259,24 @@ 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, @@ -269,28 +284,24 @@ def git_fetch(v: Verification, channel: Channel) -> None: '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( @@ -394,7 +405,7 @@ def pin_channel(v: Verification, channel: Channel) -> None: 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)