summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Repo.hs29
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