From 239b7711d168ef366f3bc71412ed3cfb7570ccf3 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Mon, 3 Aug 2020 13:21:15 +0200 Subject: [PATCH] Record date field The `sources.json` now has a `date` field for `git` repositories. --- src/Niv/Git/Cmd.hs | 22 +++++++++++++--------- src/Niv/Git/Test.hs | 14 ++++++++++---- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Niv/Git/Cmd.hs b/src/Niv/Git/Cmd.hs index ba0547d..48ca156 100644 --- a/src/Niv/Git/Cmd.hs +++ b/src/Niv/Git/Cmd.hs @@ -118,11 +118,13 @@ describeGit = Opts.<$$> " niv add git --repo /my/custom/repo --name custom --ref foobar" ] +data CommitInfo = CommitInfo {revision :: T.Text, date :: T.Text} + gitUpdate :: -- | latest rev - (T.Text -> T.Text -> IO T.Text) -> + (T.Text -> T.Text -> IO CommitInfo) -> -- | latest rev and default ref - (T.Text -> IO (T.Text, T.Text)) -> + (T.Text -> IO (T.Text, CommitInfo)) -> Update () () gitUpdate latestRev' defaultRefAndHEAD' = proc () -> do useOrSet "type" -< ("git" :: Box T.Text) @@ -132,12 +134,14 @@ gitUpdate latestRev' defaultRefAndHEAD' = proc () -> do discoverRefAndRev = proc repository -> do refAndRev <- run defaultRefAndHEAD' -< repository update "ref" -< fst <$> refAndRev - update "rev" -< snd <$> refAndRev + update "rev" -< (revision . snd) <$> refAndRev + update "date" -< (date . snd) <$> refAndRev returnA -< () discoverRev = proc repository -> do ref <- load "ref" -< () rev <- run' (uncurry latestRev') -< (,) <$> repository <*> ref - update "rev" -< rev + update "rev" -< revision <$> rev + update "date" -< date <$> rev returnA -< () -- | The "real" (IO) update @@ -145,7 +149,7 @@ gitUpdate' :: Update () () gitUpdate' = gitUpdate latestRev defaultRefAndHEAD -- TODO: document the git operations -latestRevInfo :: T.Text -> Maybe T.Text -> IO (T.Text, T.Text) +latestRevInfo :: T.Text -> Maybe T.Text -> IO (T.Text, CommitInfo) latestRevInfo repo mref = runGits $ \git -> do void $ git ["init"] void $ git ["remote", "add", "origin", repo] @@ -154,10 +158,10 @@ latestRevInfo repo mref = runGits $ \git -> do void $ git ["checkout", ref] git ["show", "--quiet", "--format=%H%n%aD", ref] >>= \case [] -> abort "Git did not produce enough output while reading commit information" - [rev, _date] -> do + [rev, dte] -> do unless (isRev rev) $ do abort $ "The revision retrieved from git does not look like a revision: '" <> rev <> "'." - pure (ref, rev) + pure (ref, CommitInfo {revision = rev, date = dte}) output -> abort $ T.unlines $ ["Git produced too much output while reading commit information:"] <> output @@ -166,14 +170,14 @@ latestRevInfo repo mref = runGits $ \git -> do Just l -> pure (T.strip l) Nothing -> abort $ T.unlines $ ["could not parse default ref: "] <> ls -latestRev :: T.Text -> T.Text -> IO T.Text +latestRev :: T.Text -> T.Text -> IO CommitInfo latestRev repo ref = snd <$> latestRevInfo repo (Just ref) -- TODO: test this defaultRefAndHEAD :: -- | the repository T.Text -> - IO (T.Text, T.Text) + IO (T.Text, CommitInfo) defaultRefAndHEAD repo = latestRevInfo repo Nothing abortNoRev :: [T.Text] -> T.Text -> IO a diff --git a/src/Niv/Git/Test.hs b/src/Niv/Git/Test.hs index 8b499f9..eaa4791 100644 --- a/src/Niv/Git/Test.hs +++ b/src/Niv/Git/Test.hs @@ -65,8 +65,10 @@ test_gitUpdateRev = do $ error $ "State mismatch: " <> show actualState where - latestRev' _ _ = pure "some-other-rev" - defaultRefAndHEAD' _ = pure ("some-ref", "some-rev") + latestRev' _ _ = pure someOtherCommit + someCommit = CommitInfo {revision = "some-rev", date = "some-date"} + someOtherCommit = CommitInfo {revision = "some-other-rev", date = "some-other-date"} + defaultRefAndHEAD' _ = pure ("some-ref", someCommit) initialState = HMS.fromList [("repo", (Free, "git@github.com:nmattia/niv"))] @@ -75,6 +77,7 @@ test_gitUpdateRev = do [ ("repo", "git@github.com:nmattia/niv"), ("ref", "some-ref"), ("rev", "some-other-rev"), + ("date", "some-other-date"), ("type", "git") ] @@ -115,8 +118,10 @@ test_gitCalledOnce = do $ error $ "State mismatch: " <> show actualState where - latestRev' _ _ = pure "some-other-rev" - defaultRefAndHEAD' _ = pure ("some-ref", "some-rev") + latestRev' _ _ = pure someOtherCommit + someCommit = CommitInfo {revision = "some-rev", date = "some-date"} + someOtherCommit = CommitInfo {revision = "some-other-rev", date = "some-other-date"} + defaultRefAndHEAD' _ = pure ("some-ref", someCommit) initialState = HMS.fromList [("repo", (Free, "git@github.com:nmattia/niv"))] @@ -125,5 +130,6 @@ test_gitCalledOnce = do [ ("repo", "git@github.com:nmattia/niv"), ("ref", "some-ref"), ("rev", "some-other-rev"), + ("date", "some-other-date"), ("type", "git") ]