]> git.scottworley.com Git - pinch/commitdiff
Split ensure_git_rev_available out from git_fetch
authorScott Worley <scottworley@scottworley.com>
Fri, 10 Apr 2020 22:29:38 +0000 (15:29 -0700)
committerScott Worley <scottworley@scottworley.com>
Fri, 10 Apr 2020 22:29:38 +0000 (15:29 -0700)
pinch.py

index 3e725e171557e1bd4c484a130dc89422e2277083..0f8f00a611390cff8aaca8e20ff0d1361a4a4274 100644 (file)
--- 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)
 
         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.
 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:
 
 
 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)
 
             ['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'):
     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])
         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)
         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,
         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()
 
                 '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(
 
 
 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)
     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)
 
 
     check_channel_contents(v, channel)