on master help sign in
Fix a number of bugs, Text <-> String round trips, and log more
commit88a153b27facdf065bbb16a16a55ebb0203ff3a6 authorJohn Ericson <John.Ericson@Obsidian.Systems> dateWed 17 Oct 2018, 13:38 (8 years ago) parentb02101e
2 files changed+56−44
lib/command/src/Obelisk/Command/Thunk.hs+35−25 lib/command/src/Obelisk/Command/Utils.hs+21−19
lib/command/src/Obelisk/Command/Thunk.hs+35−25
@@ -158,16 +158,16 @@ getNixSha256ForUriUnpacked
158158 => URI
159159 -> m NixSha256
160160getNixSha256ForUriUnpacked uri =
161− withExitFailMessage ("nix-prefetch-url: Failed to determine sha256 hash of URL " <> T.pack (show uri)) $ do
162− withNixRemoteCheck $ readProcessAndLogStderr Debug $
163− proc "nix-prefetch-url" ["--unpack" , "--type" , "sha256" , show uri]
161+ withExitFailMessage ("nix-prefetch-url: Failed to determine sha256 hash of URL " <> render uri) $ do
162+ withNixRemoteCheck $ readProcessAndLogOutput (Debug, Debug) $
163+ proc "nix-prefetch-url" ["--unpack" , "--type" , "sha256" , T.unpack $ render uri]
164164
165165nixPrefetchGit :: MonadObelisk m => URI -> Text -> Bool -> m NixSha256
166166nixPrefetchGit uri rev fetchSubmodules =
167− withExitFailMessage ("nix-prefetch-git: Failed to determine sha256 hash of Git repo " <> T.pack (show uri) <> " at " <> rev) $ do
167+ withExitFailMessage ("nix-prefetch-git: Failed to determine sha256 hash of Git repo " <> render uri <> " at " <> rev) $ do
168168 out <- withNixRemoteCheck $ readProcessAndLogStderr Debug $
169169 proc "nix-prefetch-git" $ filter (/="")
170− [ "--url", show uri
170+ [ "--url", T.unpack $ render uri
171171 , "--rev", T.unpack rev
172172 , if fetchSubmodules then "--fetch-submodules" else ""
173173 , "--quiet"
@@ -578,7 +578,9 @@ unpackThunk' noTrail thunkDir = readThunk thunkDir >>= \case
578578 withSpinner' ("Fetching thunk " <> T.pack thunkName)
579579 (finalMsg noTrail $ const $ "Fetched thunk " <> T.pack thunkName) $ do
580580 let git = callProcessAndLogOutput (Notice, Notice) . gitProc tmpRepo
581− git ["clone", if _gitSource_fetchSubmodules s then "--recursive" else "", show (_gitSource_url s)]
581+ git $ [ "clone" ]
582+ ++ ("--recursive" <$ guard (_gitSource_fetchSubmodules s))
583+ ++ [ T.unpack $ render $ _gitSource_url s ]
582584 git ["reset", "--hard", Ref.toHexString $ _thunkRev_commit $ _thunkPtr_rev tptr]
583585 when (_gitSource_fetchSubmodules s) $
584586 git ["submodule", "update", "--recursive", "--init"]
@@ -632,23 +634,29 @@ getThunkPtr' checkClean thunkDir = do
632634 -- correspondents
633635 (headDump :: [Text]) <- T.lines <$> readGitProcess thunkDir
634636 [ "for-each-ref"
635− , "--format=%(refname:short) %(upstream:short)"
637+ , "--format=%(refname:short) %(upstream:short) %(upstream:remotename)"
636638 , "refs/heads/"
637639 ]
638− (headInfo :: Map Text (Maybe Text)) <- fmap Map.fromList $ forM headDump $ \line -> do
639− (branch : restOfLine) <- pure $ T.words line
640− mUpstream <- case restOfLine of
641− [] -> pure Nothing
642− [u] -> pure $ Just u
643− (_:_) -> failWith "git for-each-ref invalid output"
644− pure (branch, mUpstream)
640+
641+ (headInfo :: Map Text (Maybe (Text, Text)))
642+ <- fmap Map.fromList $ forM headDump $ \line -> do
643+ (branch : restOfLine) <- pure $ T.words line
644+ mUpstream <- case restOfLine of
645+ [] -> pure Nothing
646+ [u, r] -> pure $ Just (u, r)
647+ (_:_) -> failWith "git for-each-ref invalid output"
648+ pure (branch, mUpstream)
649+
650+ putLog Debug $ "branches: " <> T.pack (show headInfo)
645651
646652 let errorMap :: Map Text ()
647− headUpstream :: Map Text Text
653+ headUpstream :: Map Text (Text, Text)
648654 (errorMap, headUpstream) = flip Map.mapEither headInfo $ \case
649655 Nothing -> Left ()
650656 Just b -> Right b
651657
658+ putLog Debug $ "branches with upstream branch set: " <> T.pack (show headUpstream)
659+
652660 -- Check that every branch has a remote equivalent
653661 when checkClean $ do
654662 let untrackedBranches = Map.keys errorMap
@@ -670,7 +678,7 @@ getThunkPtr' checkClean thunkDir = do
670678 ]
671679
672680 -- loosely by https://stackoverflow.com/questions/7773939/show-git-ahead-and-behind-info-for-all-branches-including-remotes
673− stats <- iforM headUpstream $ \branch upstream -> do
681+ stats <- iforM headUpstream $ \branch (upstream, _remote) -> do
674682 (stat :: [Text]) <- T.lines <$> readGitProcess thunkDir
675683 [ "rev-list", "--left-right"
676684 , T.unpack branch <> "..." <> T.unpack upstream
@@ -695,13 +703,12 @@ getThunkPtr' checkClean thunkDir = do
695703
696704 -- We assume it's safe to pack the thunk at this point
697705
698− -- Get current branch `git rev-parse --abbrev-ref HEAD`
706+ -- Get current branch ``
699707 mCurrentBranch <- listToMaybe
700708 <$> T.lines
701709 <$> readGitProcess thunkDir ["rev-parse", "--abbrev-ref", "HEAD"]
702710
703− let remote = maybe "origin" id $
704− flip Map.lookup headUpstream =<< mCurrentBranch
711+ let remote = maybe "origin" snd $ flip Map.lookup headUpstream =<< mCurrentBranch
705712
706713 [remoteUri'] <- fmap T.lines $ readGitProcess thunkDir
707714 [ "config"
@@ -740,9 +747,10 @@ uriThunkPtr uri mbranch = do
740747 case rev of
741748 Right r -> pure (ThunkSource_GitHub s, r)
742749 Left e -> do
743− putLog Alert $ " \
744−\ Failed to fetch archive from GitHub. This is probably a private repo. \
745−\ Falling back on normal fetchgit. Original failure:\n\n" <> T.pack (show e)
750+ putLog Warning $ " \
751+\Failed to fetch archive from GitHub. This is probably a private repo. \
752+\Falling back on normal fetchgit. Original failure:\n\n"
753+ errorToWarning e
746754 let s' = forgetGithub s
747755 (,) (ThunkSource_Git s') <$> gitThunkRev s' commit
748756 ThunkSource_Git s -> (,) (ThunkSource_Git s) <$> gitThunkRev s commit
@@ -846,7 +854,9 @@ gitThunkRev s commit = do
846854gitGetCommitBranch
847855 :: MonadObelisk m => URI -> Maybe Text -> m (Text, CommitId)
848856gitGetCommitBranch uri mbranch = withExitFailMessage ("Failure for git remote " <> uriMsg) $ do
849− bothMaps <- rethrowE =<< liftIO (gitLsRemote (show uri) (GitRef_Branch <$> mbranch))
857+ bothMaps <- gitLsRemote
858+ (T.unpack $ render uri)
859+ (GitRef_Branch <$> mbranch)
850860 branch <- case mbranch of
851861 Nothing -> withExitFailMessage "Failed to find default branch" $ do
852862 b <- rethrowE $ gitLookupDefaultBranch bothMaps
@@ -860,10 +870,10 @@ gitGetCommitBranch uri mbranch = withExitFailMessage ("Failure for git remote "
860870 pure (branch, commit)
861871 where
862872 rethrowE = either failWith pure
863− uriMsg = T.pack $ show uri
873+ uriMsg = render uri
864874
865875parseGitUri :: Text -> Maybe URI
866−parseGitUri x = mkURI x <|> parseSshShorthand x
876+parseGitUri x = parseSshShorthand x <|> mkURI x
867877
868878parseSshShorthand :: Text -> Maybe URI
869879parseSshShorthand uri = do
lib/command/src/Obelisk/Command/Utils.hs+21−19
@@ -60,9 +60,11 @@ initGit repo = do
6060 git ["add", "."]
6161 git ["commit", "-m", "Initial commit."]
6262
63+gitProcNoRepo :: [String] -> P.CreateProcess
64+gitProcNoRepo = P.proc "git"
65+
6366gitProc :: FilePath -> [String] -> P.CreateProcess
64−gitProc repo argsRaw =
65− P.proc "git" $ runGitInDir argsRaw
67+gitProc repo = gitProcNoRepo . runGitInDir
6668 where
6769 runGitInDir args' = case filter (not . null) args' of
6870 args@("clone":_) -> args <> [repo]
@@ -74,7 +76,7 @@ copyDir src dest =
7476 (P.proc "cp" ["-a", ".", dest]) { P.cwd = Just src }
7577
7678readGitProcess :: MonadObelisk m => FilePath -> [String] -> m Text
77−readGitProcess repo = readProcessAndLogStderr Notice . gitProc repo
79+readGitProcess repo = readProcessAndLogOutput (Debug, Notice) . gitProc repo
7880
7981processToShellString :: FilePath -> [String] -> String
8082processToShellString cmd args = unwords $ map quoteAndEscape (cmd : args)
@@ -90,7 +92,7 @@ runProcSilently = callProcessAndLogOutput (Debug, Debug)
9092
9193-- | A simpler wrapper for CliApp's readProcessAndLogStderr with sensible defaults.
9294readProc :: MonadObelisk m => P.CreateProcess -> m Text
93−readProc = readProcessAndLogStderr Error
95+readProc = readProcessAndLogOutput (Debug, Error)
9496
9597tshow :: Show a => a -> Text
9698tshow = T.pack . show
@@ -111,21 +113,22 @@ gitLookupDefaultBranch (refs, _) = do
111113gitLookupCommitForRef :: GitLsRemoteMaps -> GitRef -> Either Text CommitId
112114gitLookupCommitForRef (_, commits) ref = case M.lookup ref commits of
113115 Just a -> pure a
114− Nothing -> throwError $ "Did not find commit for" <> showGitRef ref
116+ Nothing -> throwError $ "Did not find commit for " <> showGitRef ref
115117
116118gitLsRemote
117− :: String
119+ :: MonadObelisk m
120+ => String
118121 -> Maybe GitRef
119− -> IO (Either Text GitLsRemoteMaps)
122+ -> m GitLsRemoteMaps
120123gitLsRemote repository mRef = do
121− res <- P.readProcess "git"
122− (["ls-remote", "--symref", repository]
123− ++ (maybeToList $ T.unpack . showGitRef <$> mRef))
124− mempty
125− let t = T.pack res
126− return $ first (T.pack . MP.parseErrorPretty' t)
127− $ bimap M.fromList M.fromList . partitionEithers
128− <$> MP.runParser (many parseLsRemote) "" t
124+ t <- readProc $ gitProcNoRepo $
125+ ["ls-remote", "--symref", repository]
126+ ++ (maybeToList $ T.unpack . showGitRef <$> mRef)
127+ maps <- case MP.runParser parseLsRemote "" t of
128+ Left err -> failWith $ T.pack $ MP.parseErrorPretty' t err
129+ Right table -> pure $ bimap M.fromList M.fromList $ partitionEithers $ table
130+ putLog Debug $ "git ls-remote maps: " <> T.pack (show maps)
131+ pure maps
129132
130133lexeme :: Parsec Void Text a -> Parsec Void Text a
131134lexeme = ML.lexeme space
@@ -133,8 +136,9 @@ lexeme = ML.lexeme space
133136-- $ git ls-remote --symref git@github.com:obsidiansystems/obelisk.git HEAD
134137-- ref: refs/heads/master HEAD
135138-- d0a8d25dc93f0acd096bc4ff2f550da9e2d0c8f5 refs/heads/master
136−parseLsRemote :: Parsec Void Text (Either (GitRef, GitRef) (GitRef, CommitId))
137−parseLsRemote = try $ fmap Left parseRef <|> fmap Right parseCommit
139+parseLsRemote :: Parsec Void Text [Either (GitRef, GitRef) (GitRef, CommitId)]
140+parseLsRemote =
141+ many ((fmap Left (try parseRef) <|> fmap Right parseCommit) <* try MP.eol) <* MP.eof
138142 where
139143 parseRef :: Parsec Void Text (GitRef, GitRef)
140144 parseRef = do
@@ -143,7 +147,6 @@ parseLsRemote = try $ fmap Left parseRef <|> fmap Right parseCommit
143147 $ not . isSpace
144148 symbolicRef <- lexeme $ MP.takeWhileP (Just "symbolic ref")
145149 $ not . isSpace
146− _ <- MP.eol
147150 return (toGitRef symbolicRef, toGitRef ref)
148151 parseCommit :: Parsec Void Text (GitRef, CommitId)
149152 parseCommit = do
@@ -151,7 +154,6 @@ parseLsRemote = try $ fmap Left parseRef <|> fmap Right parseCommit
151154 $ not . isSpace
152155 ref <- lexeme $ MP.takeWhileP (Just "ref")
153156 $ not . isSpace
154− _ <- MP.eol
155157 return (toGitRef ref, commitId)
156158
157159data GitRef