@@ -67,6 +67,7 @@ import GitHub
67import GitHub.Data.Name
67import GitHub.Data.Name
68import Obelisk.Command.Nix
68import Obelisk.Command.Nix
69import System.Directory
69import System.Directory
70import System.FilePath
71import System.FilePath
71import System.IO.Error
72import System.IO.Error
72import System.IO.Temp
73import System.IO.Temp
@@ -629,14 +630,26 @@ setThunk = setThunk' False
629setThunk' :: MonadObelisk m => Bool -> String -> FilePath -> m ()
630setThunk' :: MonadObelisk m => Bool -> String -> FilePath -> m ()
630setThunk' noTrail branch thunkDir = checkThunkDirectory thunkDir >> readThunk thunkDir >>= \case
631setThunk' noTrail branch thunkDir = checkThunkDirectory thunkDir >> readThunk thunkDir >>= \case
631 Left err -> failWith $ T.pack $ "thunk set : " <> show err
632 Left err -> failWith $ T.pack $ "thunk set : " <> show err
632− Right (ThunkData_Packed _) -> do
633+ Right (ThunkData_Packed thunkptr) -> do
633− -- TODO: create a ThunkPtr from the branch name
634+ let thunkSrc = _thunkPtr_source thunkptr
635+ repository = case thunkSrc of
636+ ThunkSource_Git gitSource -> (T.unpack $ render $ _gitSource_url gitSource)
637+ ThunkSource_GitHub gitHubSource -> T.unpack $ "git@github.com:"
638+ <> (untagName $ _gitHubSource_owner gitHubSource)
640+ <> (untagName $ _gitHubSource_repo gitHubSource)
642+ -- check to see if branch exists before modifying directory
643+ (exitCode, _) <- gitLsRemoteExitCode repository branch
634− _ <- unpackThunk' noTrail thunkDir
646+ _ <- unpackThunk' noTrail thunkDir
635− -- TODO: if git checkout branch errors out, the directory should be restored back to it's previous state
636− callProcessAndLogOutput (Debug, Error) $ gitProc thunkDir ["checkout", branch]
647+ callProcessAndLogOutput (Debug, Error) $ gitProc thunkDir ["checkout", branch]
637− _ <- packThunk' noTrail thunkDir
648+ _ <- packThunk' noTrail thunkDir
638− updateThunkToLatest thunkDir
649+ updateThunkToLatest thunkDir
639− -- TODO: use overwriteThunk thunkDir (someThunkPtr)
650+ ExitFailure errNum -> failWith $ T.pack $ if errNum == 2
651+ then "Error: branch not found"
652+ else ("Error Code: " <> show errNum <> "issue checking out the desired branch")
640 Right (ThunkData_Checkout _) -> do
653 Right (ThunkData_Checkout _) -> do
641 callProcessAndLogOutput (Debug, Error) $ gitProc thunkDir ["checkout", branch]
654 callProcessAndLogOutput (Debug, Error) $ gitProc thunkDir ["checkout", branch]
642 _ <- packThunk' noTrail thunkDir
655 _ <- packThunk' noTrail thunkDir