diff options
| author | Roman Smrž <roman.smrz@seznam.cz> | 2026-11-25 20:25:45 +0100 |
|---|---|---|
| committer | Roman Smrž <roman.smrz@seznam.cz> | 2026-11-27 22:28:01 +0100 |
| commit | c5f4e01a84ee7e348bcde3dc3313c198d1197aa1 (patch) | |
| tree | 7786a030ea453bcdad2844abbf4d1a3b10b2d2a3 /src/Repo.hs | |
| parent | 54bf3a5f5c6f660a461d65aea279e698a51c0f1d (diff) | |
Push job now succeeds on attempt to push ancestor commit
Diffstat (limited to 'src/Repo.hs')
| -rw-r--r-- | src/Repo.hs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/Repo.hs b/src/Repo.hs index fbcded0..0aab60f 100644 --- a/src/Repo.hs +++ b/src/Repo.hs @@ -333,6 +333,13 @@ getGitRefPath GitRepo {..} ref = liftIO $ do ( ExitSuccess, out, _ ) | [ path ] <- lines out -> return (Just path) ( _, _, _ ) -> return Nothing +isAncestor :: (MonadIO m, MonadFail m) => Repo -> CommitId -> CommitId -> m Bool +isAncestor GitRepo {..} cid cid' = liftIO $ do + readProcessWithExitCode "git" [ "--git-dir=" <> gitDir, "merge-base", "--is-ancestor", showCommitId cid, showCommitId cid' ] "" >>= \case + ( ExitSuccess, _, _ ) -> return True + ( ExitFailure 1, _, _ ) -> return False + ( code, _, _ ) -> fail $ "git merge-base --is-ancestor exited with error: " <> show code + getCommitDetails :: (MonadIO m, MonadFail m) => Commit -> m CommitDetails getCommitDetails Commit {..} = do @@ -503,8 +510,30 @@ pushToBranch Branch {..} Commit {..} = do | Just _ <- mbstdout -> do waitForProcess ph >>= \case ExitSuccess -> return $ return () + ExitFailure 1 -> checkIfPushingAncestor code -> return $ fail $ "git push exited with error: " <> show code | otherwise -> error "createProcess must return stdout handle" + where + checkIfPushingAncestor = do + let cmd = (proc "git" [ "--git-dir=" <> gitDir commitRepo, "fetch", "--porcelain", gitDir branchRepo, "refs/heads/" <> T.unpack branchName ]) + { std_in = NoStream + , std_out = CreatePipe + , std_err = NoStream + } + createProcess cmd >>= \( _, mbstdout, _, ph ) -> if + | Just stdout <- mbstdout -> do + waitForProcess ph >>= \case + ExitSuccess -> do + content <- BC.hGetContents stdout + case map BC.words $ BC.lines content of + [ [ _, _, remoteTip, _ ] ] -> do + isAncestor commitRepo commitId_ (CommitId remoteTip) >>= \case + True -> return $ return () + False -> return $ fail $ + "push of commit " <> showCommitId commitId_ <> " to branch ‘" <> T.unpack branchName <> "’ in ‘" <> gitDir branchRepo <> "’ failed" + _ -> return $ fail "unexpected output from git fetch" + code -> return $ fail $ "git fetch exited with error: " <> show code + | otherwise -> error "createProcess must return stdout handle" data GitInotify = GitInotify |