From cd294552c547cdd18fe55e3d6e45a8569a6eceb5 Mon Sep 17 00:00:00 2001 From: Joe Hermaszewski Date: Sat, 14 Nov 2020 17:43:33 +0800 Subject: [PATCH] Pass `--heads --tags` to `git ls-remote` to avoid fetching remote refs Don't pass when the revision begins with `refs` --- CHANGELOG.md | 3 +++ src/Update/Nix/FetchGit/Prefetch.hs | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a1bba5..70f9713 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## WIP +- Pass `--heads --tags` to `git ls-remote` to avoid fetching remote + refs. Don't pass when the revision begins with `refs` + ## [0.2.4] - 2020-11-11 - Add --dry-run option diff --git a/src/Update/Nix/FetchGit/Prefetch.hs b/src/Update/Nix/FetchGit/Prefetch.hs index 5a396ac..28696ce 100644 --- a/src/Update/Nix/FetchGit/Prefetch.hs +++ b/src/Update/Nix/FetchGit/Prefetch.hs @@ -103,16 +103,23 @@ getGitRevision repo revision = do | otherwise -> pure hash Nothing -> refute1 $ NoSuchRef (unRevision revision) --- | Run git ls-remote --sort=-v:refname and return the first match if any +-- | Run git ls-remote --heads --tags --sort=-v:refname and return the first +-- match if any. Use '--heads --tags' if the revision doesn't start with +-- 'refs/' to avoid getting 'remote' refs. gitLsRemotes :: Text -> Revision -> M (Maybe (Text, Text)) gitLsRemotes repo revision = do + let headsTags = if T.isPrefixOf "refs/" (unRevision revision) + then [] + else ["--heads", "--tags"] (exitCode, nsStdout, nsStderr) <- liftIO $ readProcessWithExitCode "git" - [ "ls-remote" - , "--sort=-v:refname" - , T.unpack repo - , T.unpack (unRevision revision) - ] + ( [ "ls-remote" + , "--sort=-v:refname" + , T.unpack repo + , T.unpack (unRevision revision) + ] + <> headsTags + ) "" case exitCode of ExitFailure e -> refute1 (NixPrefetchGitFailed e (pack nsStderr))