Skip to main content

grit_lib/error/
diff.rs

1//! [`DiffError`]: patch application, hunk selection, and blame failures.
2
3use thiserror::Error;
4
5use super::ApplyError;
6
7/// Diff-area failure: applying patches, selecting hunks by id, and blame inputs.
8///
9/// Surfaced on the umbrella as [`Error::Diff`](crate::error::Error::Diff).
10#[derive(Debug, Error)]
11#[non_exhaustive]
12pub enum DiffError {
13    /// Patch application failure.
14    #[error(transparent)]
15    Apply(#[from] ApplyError),
16
17    /// `grit diff` arguments could not be resolved into a comparison.
18    #[error(transparent)]
19    Arguments(#[from] crate::diff::ResolveDiffArgumentsError),
20
21    /// A diff driver's function-name pattern does not compile.
22    #[error(transparent)]
23    FuncnamePattern(#[from] crate::userdiff::FuncnamePatternError),
24
25    /// A pickaxe (`-S` / `-G`) pattern is not a valid regular expression.
26    #[error(transparent)]
27    Pickaxe(#[from] crate::diff::pickaxe::PickaxeError),
28
29    /// A `--no-index` comparison of two filesystem paths failed.
30    #[error(transparent)]
31    NoIndex(crate::diff::NoIndexError),
32
33    /// Partial hunk selection failed outside the stash stack.
34    #[error(transparent)]
35    HunkSelection(#[from] crate::porcelain::hunk_select::HunkSelectError),
36
37    /// [`crate::blame::BlameFinalSource::Worktree`] was requested in a bare repository.
38    #[error("cannot blame the working tree of a bare repository")]
39    BlameWorktreeBareRepository,
40
41    /// A blame `--ignore-rev` did not resolve to a commit.
42    #[error("cannot find revision {rev} to ignore")]
43    BlameIgnoreRevUnknown {
44        /// Revision expression from `--ignore-rev`.
45        rev: String,
46    },
47
48    /// A blame ignore-revs file could not be read.
49    #[error("could not open object name list: {path}")]
50    BlameIgnoreRevsFileOpen {
51        /// Path argument (before expansion).
52        path: String,
53        /// Underlying open error.
54        #[source]
55        source: std::io::Error,
56    },
57
58    /// A line in a blame ignore-revs file is not a full-length object id.
59    #[error("invalid object name: {rev}")]
60    BlameIgnoreRevsFileInvalidRev {
61        /// Path argument for the file.
62        path: String,
63        /// Offending line text after comment/trim handling.
64        rev: String,
65    },
66
67    /// Blame copy detection was asked for more than the three supported levels (`-C -C -C`).
68    #[error("blame copy detection supports at most three levels, got {count}")]
69    BlameCopyLevelTooHigh {
70        /// Number of levels requested.
71        count: usize,
72    },
73
74    /// A `diff.<driver>.textconv` command could not be started.
75    #[error("could not run textconv command '{command}': {source}")]
76    TextconvSpawn {
77        /// The configured textconv command.
78        command: String,
79        /// Why the command could not be started.
80        #[source]
81        source: std::io::Error,
82    },
83
84    /// A `diff.<driver>.textconv` command exited unsuccessfully.
85    #[error("textconv command '{command}' exited with status {}", status.map_or_else(|| "unknown".to_owned(), |s| s.to_string()))]
86    TextconvFailed {
87        /// The configured textconv command.
88        command: String,
89        /// Exit status, when the command exited normally.
90        status: Option<i32>,
91    },
92}
93
94impl DiffError {
95    /// Stable snake_case slug for this failure (the former umbrella variant names).
96    #[must_use]
97    pub fn kind_name(&self) -> &'static str {
98        match self {
99            Self::Apply(..) => "apply",
100            Self::Arguments(..) => "diff_arguments",
101            Self::NoIndex(..) => "no_index",
102            Self::FuncnamePattern(..) => "funcname_pattern",
103            Self::Pickaxe(..) => "pickaxe",
104            Self::HunkSelection(..) => "hunk_selection",
105            Self::BlameWorktreeBareRepository => "blame_worktree_bare_repository",
106            Self::BlameIgnoreRevUnknown { .. } => "blame_ignore_rev_unknown",
107            Self::BlameIgnoreRevsFileOpen { .. } => "blame_ignore_revs_file_open",
108            Self::BlameIgnoreRevsFileInvalidRev { .. } => "blame_ignore_revs_file_invalid_rev",
109            Self::BlameCopyLevelTooHigh { .. } => "blame_copy_level_too_high",
110            Self::TextconvSpawn { .. } => "textconv_spawn",
111            Self::TextconvFailed { .. } => "textconv_failed",
112        }
113    }
114}