@@ -67,6 +67,7 @@ import GitHub
6767import GitHub.Data.Name
6868import Obelisk.Command.Nix
6969import System.Directory
70+import System.Exit
7071import System.FilePath
7172import System.IO.Error
7273import System.IO.Temp
@@ -629,14 +630,26 @@ setThunk = setThunk' False
629630setThunk' :: MonadObelisk m => Bool -> String -> FilePath -> m ()
630631setThunk' noTrail branch thunkDir = checkThunkDirectory thunkDir >> readThunk thunkDir >>= \case
631632 Left err -> failWith $ T.pack $ "thunk set : " <> show err
632− Right (ThunkData_Packed _) -> do
633− -- TODO: create a ThunkPtr from the branch name
634− _ <- 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]
637− _ <- packThunk' noTrail thunkDir
638− updateThunkToLatest thunkDir
639− -- TODO: use overwriteThunk thunkDir (someThunkPtr)
633+ Right (ThunkData_Packed thunkptr) -> do
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)
639+ <> "/"
640+ <> (untagName $ _gitHubSource_repo gitHubSource)
641+ <> ".git"
642+ -- check to see if branch exists before modifying directory
643+ (exitCode, _) <- gitLsRemoteExitCode repository branch
644+ case exitCode of
645+ ExitSuccess -> do
646+ _ <- unpackThunk' noTrail thunkDir
647+ callProcessAndLogOutput (Debug, Error) $ gitProc thunkDir ["checkout", branch]
648+ _ <- packThunk' noTrail thunkDir
649+ updateThunkToLatest thunkDir
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")
640653 Right (ThunkData_Checkout _) -> do
641654 callProcessAndLogOutput (Debug, Error) $ gitProc thunkDir ["checkout", branch]
642655 _ <- packThunk' noTrail thunkDir