1
1
mirror of https://github.com/nmattia/niv.git synced 2024-09-18 19:07:19 +03:00

Stop using ref in builtins.fetchGit fetcher

Instead, sources now record either `branch` or `tag`. The fetcher
specifies the correct ref (`/refs/heads/...` or `/refs/tags/...`) which
works in the newest version of Nix. The `ref` attribute can still be set
to override the logic.
This commit is contained in:
Nicolas Mattia 2020-08-20 12:02:11 +02:00
parent f0be4cea7d
commit 1294b321c8
4 changed files with 43 additions and 32 deletions

View File

@ -24,8 +24,15 @@ let
else
pkgs.fetchzip { name = name'; inherit (spec) url sha256; };
fetch_git = spec:
builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
fetch_git = name: spec:
let
ref =
if spec ? ref then spec.ref else
if spec ? branch then "refs/heads/${spec.branch}" else
if spec ? tag then "refs/tags/${spec.tag}" else
abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!";
in
builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; };
fetch_local = spec: spec.path;
@ -77,7 +84,7 @@ let
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
else if spec.type == "file" then fetch_file pkgs name spec
else if spec.type == "tarball" then fetch_tarball pkgs name spec
else if spec.type == "git" then fetch_git spec
else if spec.type == "git" then fetch_git name spec
else if spec.type == "local" then fetch_local spec
else if spec.type == "builtin-tarball" then fetch_builtin-tarball name
else if spec.type == "builtin-url" then fetch_builtin-url name

View File

@ -53,7 +53,7 @@ parseGitShortcut txt'@(T.dropWhileEnd (== '/') -> txt) =
parseGitPackageSpec :: Opts.Parser PackageSpec
parseGitPackageSpec =
(PackageSpec . HMS.fromList)
<$> many (parseRepo <|> parseRef <|> parseRev <|> parseAttr <|> parseSAttr)
<$> many (parseRepo <|> parseBranch <|> parseRev <|> parseAttr <|> parseSAttr)
where
parseRepo =
("repo",) . Aeson.String
@ -67,11 +67,11 @@ parseGitPackageSpec =
( Opts.long "rev"
<> Opts.metavar "SHA"
)
parseRef =
("ref",) . Aeson.String
parseBranch =
("branch",) . Aeson.String
<$> Opts.strOption
( Opts.long "ref"
<> Opts.metavar "REF"
( Opts.long "branch"
<> Opts.metavar "BRANCH"
)
parseAttr =
Opts.option
@ -112,7 +112,7 @@ describeGit =
Opts.<$$> " niv add git git@github.com:stedolan/jq"
Opts.<$$> " niv add git ssh://git@github.com/stedolan/jq --rev deadb33f"
Opts.<$$> " niv add git https://github.com/stedolan/jq.git"
Opts.<$$> " niv add git --repo /my/custom/repo --name custom --ref foobar"
Opts.<$$> " niv add git --repo /my/custom/repo --name custom --branch development"
]
gitUpdate ::
@ -121,34 +121,34 @@ gitUpdate ::
-- | latest rev and default ref
(T.Text -> IO (T.Text, T.Text)) ->
Update () ()
gitUpdate latestRev' defaultRefAndHEAD' = proc () -> do
gitUpdate latestRev' defaultBranchAndRev' = proc () -> do
useOrSet "type" -< ("git" :: Box T.Text)
repository <- load "repo" -< ()
discoverRev <+> discoverRefAndRev -< repository
where
discoverRefAndRev = proc repository -> do
refAndRev <- run defaultRefAndHEAD' -< repository
update "ref" -< fst <$> refAndRev
update "rev" -< snd <$> refAndRev
branchAndRev <- run defaultBranchAndRev' -< repository
update "branch" -< fst <$> branchAndRev
update "rev" -< snd <$> branchAndRev
returnA -< ()
discoverRev = proc repository -> do
ref <- load "ref" -< ()
rev <- run' (uncurry latestRev') -< (,) <$> repository <*> ref
branch <- load "branch" -< ()
rev <- run' (uncurry latestRev') -< (,) <$> repository <*> branch
update "rev" -< rev
returnA -< ()
-- | The "real" (IO) update
gitUpdate' :: Update () ()
gitUpdate' = gitUpdate latestRev defaultRefAndHEAD
gitUpdate' = gitUpdate latestRev defaultBranchAndRev
latestRev ::
-- | the repository
T.Text ->
-- | the ref/branch
-- | the branch
T.Text ->
IO T.Text
latestRev repo ref = do
let gitArgs = ["ls-remote", repo, "refs/heads/" <> ref]
latestRev repo branch = do
let gitArgs = ["ls-remote", repo, "refs/heads/" <> branch]
sout <- runGit gitArgs
case sout of
ls@(_ : _ : _) -> abortTooMuchOutput gitArgs ls
@ -166,14 +166,14 @@ latestRev repo ref = do
abortGitFailure args $ T.unlines $
["Git produced too much output:"] <> map (" " <>) ls
defaultRefAndHEAD ::
defaultBranchAndRev ::
-- | the repository
T.Text ->
IO (T.Text, T.Text)
defaultRefAndHEAD repo = do
defaultBranchAndRev repo = do
sout <- runGit args
case sout of
(l1 : l2 : _) -> (,) <$> parseRef l1 <*> parseRev l2
(l1 : l2 : _) -> (,) <$> parseBranch l1 <*> parseRev l2
_ ->
abortGitFailure args $ T.unlines $
[ "Could not read reference and revision from stdout:"
@ -181,11 +181,11 @@ defaultRefAndHEAD repo = do
<> sout
where
args = ["ls-remote", "--symref", repo, "HEAD"]
parseRef l = maybe (abortNoRef args l) pure $ do
parseBranch l = maybe (abortNoRef args l) pure $ do
-- ref: refs/head/master\tHEAD -> master\tHEAD
refAndSym <- T.stripPrefix "ref: refs/heads/" l
let ref = T.takeWhile (/= '\t') refAndSym
if T.null ref then Nothing else Just ref
let branch = T.takeWhile (/= '\t') refAndSym
if T.null branch then Nothing else Just branch
parseRev l = maybe (abortNoRev args l) pure $ do
checkRev $ T.takeWhile (/= '\t') l
checkRev t = if isRev t then Just t else Nothing

View File

@ -57,7 +57,7 @@ test_gitUpdates =
test_gitUpdateRev :: IO ()
test_gitUpdateRev = do
interState <- evalUpdate initialState $ proc () ->
gitUpdate (error "should be def") defaultRefAndHEAD' -< ()
gitUpdate (error "should be def") defaultBranchAndHEAD' -< ()
let interState' = HMS.map (first (\_ -> Free)) interState
actualState <- evalUpdate interState' $ proc () ->
gitUpdate latestRev' (error "should update") -< ()
@ -66,14 +66,14 @@ test_gitUpdateRev = do
$ "State mismatch: " <> show actualState
where
latestRev' _ _ = pure "some-other-rev"
defaultRefAndHEAD' _ = pure ("some-ref", "some-rev")
defaultBranchAndHEAD' _ = pure ("some-branch", "some-rev")
initialState =
HMS.fromList
[("repo", (Free, "git@github.com:nmattia/niv"))]
expectedState =
HMS.fromList
[ ("repo", "git@github.com:nmattia/niv"),
("ref", "some-ref"),
("branch", "some-branch"),
("rev", "some-other-rev"),
("type", "git")
]
@ -104,10 +104,10 @@ once2 f = do
-- the update
test_gitCalledOnce :: IO ()
test_gitCalledOnce = do
defaultRefAndHEAD'' <- once1 defaultRefAndHEAD'
defaultBranchAndHEAD'' <- once1 defaultBranchAndHEAD'
latestRev'' <- once2 latestRev'
interState <- evalUpdate initialState $ proc () ->
gitUpdate (error "should be def") defaultRefAndHEAD'' -< ()
gitUpdate (error "should be def") defaultBranchAndHEAD'' -< ()
let interState' = HMS.map (first (\_ -> Free)) interState
actualState <- evalUpdate interState' $ proc () ->
gitUpdate latestRev'' (error "should update") -< ()
@ -116,14 +116,14 @@ test_gitCalledOnce = do
$ "State mismatch: " <> show actualState
where
latestRev' _ _ = pure "some-other-rev"
defaultRefAndHEAD' _ = pure ("some-ref", "some-rev")
defaultBranchAndHEAD' _ = pure ("some-branch", "some-rev")
initialState =
HMS.fromList
[("repo", (Free, "git@github.com:nmattia/niv"))]
expectedState =
HMS.fromList
[ ("repo", "git@github.com:nmattia/niv"),
("ref", "some-ref"),
("branch", "some-branch"),
("rev", "some-other-rev"),
("type", "git")
]

View File

@ -166,6 +166,8 @@ data SourcesNixVersion
V20
| -- Use the source name in fetchurl
V21
| -- Stop setting `ref` and use `branch` and `tag` in sources
V22
deriving stock (Bounded, Enum, Eq)
-- | A user friendly version
@ -192,6 +194,7 @@ sourcesVersionToText = \case
V19 -> "19"
V20 -> "20"
V21 -> "21"
V22 -> "22"
latestVersionMD5 :: T.Text
latestVersionMD5 = sourcesVersionToMD5 maxBound
@ -225,6 +228,7 @@ sourcesVersionToMD5 = \case
V19 -> "543621698065cfc6a4a7985af76df718"
V20 -> "ab4263aa63ccf44b4e1510149ce14eff"
V21 -> "c501eee378828f7f49828a140dbdbca3"
V22 -> "935d1d2f0bf95fda977a6e3a7e548ed4"
-- | The MD5 sum of ./nix/sources.nix
sourcesNixMD5 :: IO T.Text