diff options
| -rw-r--r-- | src/Repo.hs | 29 | ||||
| -rw-r--r-- | test/script/publish.et | 56 |
2 files changed, 83 insertions, 2 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 diff --git a/test/script/publish.et b/test/script/publish.et index e895b93..57cbb6e 100644 --- a/test/script/publish.et +++ b/test/script/publish.et @@ -108,13 +108,15 @@ test PushRepo: $git rev-parse HEAD $git rev-parse HEAD^{tree} + $git reset --hard HEAD~4 + touch workdir/e $git add e $commit -m e $git rev-parse HEAD $git rev-parse HEAD^{tree} - $git reset --hard HEAD~5 + $git reset --hard HEAD~1 $git rev-parse HEAD expect /([0-9a-f]+)/ from git_init capture c_init @@ -133,6 +135,40 @@ test PushRepo: guard (c_init == c_end) let cmd_run = [ "--repo=source_repo:workdir", "--writable-repo=target_repo:target.git", "run" ] + + # Normal push + local: + spawn on n as p args (cmd_run ++ [ "push_job:$cd" ]) killwith SIGINT + expect from p: + /job-enqueue push_job:$cd/ + /job-start push_job:$cd/ + /job-finish push_job:$cd done/ + + expect /(.*)/ from p capture finish + guard (finish == "run-finish") + + shell on n as git_check: + git -C target.git rev-parse HEAD + expect /([0-9a-f]+)/ from git_check capture cur_target + guard (cur_target == cd) + + # Push the same commit as is in target + local: + spawn on n as p args (cmd_run ++ [ "push_job:$cd" ]) killwith SIGINT + expect from p: + /job-enqueue push_job:$cd/ + /job-start push_job:$cd/ + /job-finish push_job:$cd done/ + + expect /(.*)/ from p capture finish + guard (finish == "run-finish") + + shell on n as git_check: + git -C target.git rev-parse HEAD + expect /([0-9a-f]+)/ from git_check capture cur_target + guard (cur_target == cd) + + # Push ancestor commit local: spawn on n as p args (cmd_run ++ [ "push_job:$cb" ]) killwith SIGINT expect from p: @@ -146,7 +182,23 @@ test PushRepo: shell on n as git_check: git -C target.git rev-parse HEAD expect /([0-9a-f]+)/ from git_check capture cur_target - guard (cur_target == cb) + guard (cur_target == cd) + + # Push conflicting commit + local: + spawn on n as p args (cmd_run ++ [ "push_job:$ce" ]) killwith SIGINT + expect from p: + /job-enqueue push_job:$ce/ + /job-start push_job:$ce/ + /job-finish push_job:$ce failed/ + + expect /(.*)/ from p capture finish + guard (finish == "run-finish") + + shell on n as git_check: + git -C target.git rev-parse HEAD + expect /([0-9a-f]+)/ from git_check capture cur_target + guard (cur_target == cd) test PushRepoWritable: |