Skip to main content

grit_lib/error/
notes.rs

1//! [`NotesError`]: notes ref, notes tree, and notes merge failures.
2
3use thiserror::Error;
4
5/// Notes failure: notes refs, the notes tree layout, rewrite configuration, and notes merges.
6///
7/// Surfaced on the umbrella as [`Error::Notes`](crate::error::Error::Notes).
8#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum NotesError {
11    /// [`RepositoryNotes::add`](crate::repo_handles::RepositoryNotes::add) was called without
12    /// `force` when a note already exists on the object.
13    #[error("note for object {object} already exists")]
14    AlreadyExists {
15        /// Annotated object that already has a note.
16        object: crate::objects::ObjectId,
17    },
18
19    /// A configured or environment notes ref is not under `refs/notes/`.
20    #[error("notes ref '{name}' must start with 'refs/notes/'")]
21    InvalidRef {
22        /// The notes ref as configured.
23        name: String,
24    },
25
26    /// Unknown `notes.rewriteMode` or `GIT_NOTES_REWRITE_MODE` value.
27    #[error("unknown notes rewrite mode '{mode}'")]
28    UnknownRewriteMode {
29        /// The rewrite mode as configured.
30        mode: String,
31    },
32
33    /// A notes ref resolves to an object that cannot hold notes (notes reads accept a commit or
34    /// a tree; notes merges require a commit).
35    #[error("notes ref '{notes_ref}' points to a {found}, not a notes commit")]
36    RefNotCommit {
37        /// Notes ref (or revision) that was read.
38        notes_ref: String,
39        /// Kind of object the ref resolved to.
40        found: crate::objects::ObjectKind,
41    },
42
43    /// An object reached while reading a notes tree has the wrong kind (for example a note
44    /// entry that is not a blob).
45    #[error("notes object {oid} is a {found}, expected a {expected}")]
46    UnexpectedObjectKind {
47        /// Object that was read.
48        oid: crate::objects::ObjectId,
49        /// Kind the notes layout requires at this position.
50        expected: crate::objects::ObjectKind,
51        /// Kind actually stored.
52        found: crate::objects::ObjectKind,
53    },
54
55    /// [`notes_merge_inner`](crate::notes::notes_merge_inner) was asked to merge two notes refs
56    /// that both have no notes commit.
57    #[error("cannot merge notes: neither '{local}' nor '{remote}' has any notes")]
58    MergeBothEmpty {
59        /// Local notes ref (merge destination).
60        local: String,
61        /// Remote notes ref (merge source).
62        remote: String,
63    },
64
65    /// A manual notes merge was started while an earlier one still has conflict files in the
66    /// notes merge worktree.
67    #[error("unfinished notes merge: conflict files remain in {}", crate::git_path::user_display_path(.path))]
68    MergeInProgress {
69        /// The notes merge worktree directory.
70        path: std::path::PathBuf,
71    },
72}
73
74impl NotesError {
75    /// Stable snake_case slug for this failure (the former umbrella variant names).
76    #[must_use]
77    pub fn kind_name(&self) -> &'static str {
78        match self {
79            Self::AlreadyExists { .. } => "note_already_exists",
80            Self::InvalidRef { .. } => "invalid_notes_ref",
81            Self::UnknownRewriteMode { .. } => "notes_rewrite_mode",
82            Self::RefNotCommit { .. } => "notes_ref_not_commit",
83            Self::UnexpectedObjectKind { .. } => "notes_object_kind",
84            Self::MergeBothEmpty { .. } => "notes_merge_both_empty",
85            Self::MergeInProgress { .. } => "notes_merge_in_progress",
86        }
87    }
88}