Skip to main content

grit_lib/porcelain/
merge_error.rs

1//! Typed failures for merge, cherry-pick, revert, and sequencer state.
2
3use thiserror::Error;
4
5use crate::objects::ObjectId;
6use crate::state::InProgressOperation;
7
8/// Merge, replay, and in-progress operation failures.
9///
10/// Wrapped by [`Error::Merge`](crate::error::Error::Merge). Stable [`kind_name`](Self::kind_name)
11/// slugs match the former umbrella variant names for scripts and the CLI error map.
12#[derive(Debug, Error)]
13#[non_exhaustive]
14pub enum MergeError {
15    /// A merge was requested with fast-forward only, but the histories diverged.
16    #[error("not possible to fast-forward; refusing to merge")]
17    NotFastForward,
18
19    /// Merge histories share no common ancestor.
20    #[error("refusing to merge unrelated histories")]
21    UnrelatedHistories {
22        /// Our side commit.
23        ours: ObjectId,
24        /// Their side commit.
25        theirs: ObjectId,
26    },
27
28    /// Pick/revert does not replay merge commits without an explicit mainline.
29    #[error("merge commit {oid}")]
30    MergeCommit {
31        /// Object id of the merge commit that cannot be replayed yet.
32        oid: ObjectId,
33    },
34
35    /// Cherry-pick source is already checked out as `HEAD`.
36    #[error("commit already at HEAD")]
37    ReplaySourceAtHead,
38
39    /// Sequencer todo lists a command grit does not implement yet.
40    #[error("unknown sequencer command '{command}'")]
41    SequencerUnknownCommand {
42        /// Verb read from `.git/sequencer/todo`.
43        command: String,
44    },
45
46    /// Continue replay refuses while unmerged index entries remain.
47    #[error("cannot continue replay: unmerged index entries remain")]
48    ReplayContinueUnmerged,
49
50    /// Continue replay with a dirty index after the step was already committed.
51    #[error("cannot continue replay: index differs from HEAD after the step was committed")]
52    ReplayContinueDirtyIndex,
53
54    /// Skip replay when the current step was already committed on a moved branch tip.
55    #[error("nothing to skip: current step already committed; run continue instead")]
56    ReplaySkipAlreadyCommitted,
57
58    /// No cherry-pick or revert sequence is in progress.
59    #[error("no cherry-pick or revert in progress")]
60    NoReplayInProgress,
61
62    /// No merge is in progress (abort/continue/skip).
63    #[error("there is no merge in progress")]
64    NoMergeInProgress,
65
66    /// Another paused operation blocks the requested command.
67    #[error("{} in progress", .0.description())]
68    OperationInProgress(InProgressOperation),
69
70    /// Stage-1/2/3 entries remain when finishing a merge.
71    #[error("you still have unmerged paths: {}", paths.join(", "))]
72    ContinueUnmergedPaths {
73        /// Repository-relative paths still in conflict stages.
74        paths: Vec<String>,
75    },
76
77    /// Work tree resolved but index still has conflict stages.
78    #[error("resolved in the work tree but not staged: {}", paths.join(", "))]
79    ContinueResolvedNotStaged {
80        /// Repository-relative paths cleared of conflict markers but not staged.
81        paths: Vec<String>,
82    },
83
84    /// Continue merge message would be empty after cleanup.
85    #[error("empty commit message")]
86    ContinueEmptyMessage,
87
88    /// A pick/revert sequence is already in progress.
89    #[error("cherry-pick or revert is already in progress")]
90    ReplayAlreadyInProgress,
91
92    /// `message_override` was set while replaying more than one commit.
93    #[error("message_override is only valid when replaying a single commit")]
94    MessageOverrideNotForMultiCommit,
95
96    /// Merge or replay state on disk is incomplete for the requested operation.
97    #[error("merge state incomplete: missing {missing}")]
98    MergeStateIncomplete {
99        /// Recorded merge artifact that was expected but absent.
100        missing: &'static str,
101    },
102
103    /// Pull-specific failure (no upstream, detached `HEAD`, rejected fetch).
104    #[error(transparent)]
105    Pull(#[from] crate::porcelain::pull::PullError),
106
107    /// A `.git/sequencer/created` journal line could not be parsed.
108    #[error("invalid sequencer created record: {detail}")]
109    SequencerCreatedParse {
110        /// What was wrong with the line.
111        detail: &'static str,
112    },
113}
114
115impl MergeError {
116    /// Stable snake_case slug for this failure (matches former [`Error::kind_name`](crate::error::Error::kind_name) values).
117    #[must_use]
118    pub fn kind_name(&self) -> &'static str {
119        match self {
120            Self::NotFastForward => "not_fast_forward",
121            Self::UnrelatedHistories { .. } => "unrelated_histories",
122            Self::MergeCommit { .. } => "merge_commit",
123            Self::ReplaySourceAtHead => "replay_source_at_head",
124            Self::SequencerUnknownCommand { .. } => "sequencer_unknown_command",
125            Self::ReplayContinueUnmerged => "replay_continue_unmerged",
126            Self::ReplayContinueDirtyIndex => "replay_continue_dirty_index",
127            Self::ReplaySkipAlreadyCommitted => "replay_skip_already_committed",
128            Self::NoReplayInProgress => "no_replay_in_progress",
129            Self::NoMergeInProgress => "no_merge_in_progress",
130            Self::OperationInProgress(..) => "operation_in_progress",
131            Self::ContinueUnmergedPaths { .. } => "index_unmerged",
132            Self::ContinueResolvedNotStaged { .. } => "index_unmerged",
133            Self::ContinueEmptyMessage => "empty_commit_message",
134            Self::ReplayAlreadyInProgress => "operation_in_progress",
135            Self::MessageOverrideNotForMultiCommit => "message",
136            Self::MergeStateIncomplete { .. } => "message",
137            Self::SequencerCreatedParse { .. } => "message",
138            Self::Pull(..) => "pull",
139        }
140    }
141}