1use std::{fmt::Display, io, string};
2
3#[derive(Debug)]
4pub enum Error {
5 StashList(git2::Error),
6 ReadLog(git2::Error),
7 OpenRepo(git2::Error),
8 Term(io::Error),
9 Config(Box<figment::Error>),
10 EditConfig(String),
11 Bindings {
12 bad_key_bindings: Vec<String>,
13 },
14 FileWatcher(notify::Error),
15 ReadRebaseStatusFile(io::Error),
16 ReadRebaseTodo(io::Error),
17 ReadPipedInput(io::Error),
18 WriteRebaseTodo(io::Error),
19 ReadBranchName(io::Error),
20 BranchNameUtf8(Utf8Error),
21 GitDiff(io::Error),
22 GitDiffUtf8(string::FromUtf8Error),
23 GitShow(io::Error),
24 GitShowUtf8(string::FromUtf8Error),
25 GitShowMeta(git2::Error),
26 NotOnBranch,
27 GetHead(git2::Error),
28 CurrentBranchName(git2::Error),
29 GetCurrentBranchUpstream(git2::Error),
30 GetCurrentBranchUpstreamUtf8(Utf8Error),
31 RemoteNameUtf8(Utf8Error),
32 CannotDeleteCurrentBranch,
33 BranchNameRequired,
34 IsBranchMerged(git2::Error),
35 GetRemote(git2::Error),
36 ReadGitConfig(git2::Error),
37 ReadGitConfigUtf8(Utf8Error),
38 DeleteGitConfig(git2::Error),
39 SetGitConfig(git2::Error),
40 RemoteHasNoName,
41 ReadOid(git2::Error),
42 ArgMustBePositiveNumber,
43 ArgInvalidRegex(regex::Error),
44 Clipboard(arboard::Error),
45 FindGitRev(git2::Error),
46 NoEditorSet,
47 GitStatus(git2::Error),
48 CmdAlreadyRunning,
49 StashWorkTreeEmpty,
50 CouldntAwaitCmd(io::Error),
51 NoRepoWorkdir,
52 SpawnCmd(io::Error),
53 CmdBadExit(String, Option<i32>, String),
56 CouldntReadCmdOutput(io::Error),
57 ListGitReferences(git2::Error),
58 OpenLogFile(io::Error),
59 PromptAborted,
60 ReadPromptHistory(io::Error),
61 WritePromptHistory(io::Error),
62 NoMoreEvents,
63 CannotSpinoffCurrentBranch,
64 SpinoffBranchExists(String),
65 DoesBranchExist(git2::Error),
66 GetBranchName(git2::Error),
67 BaseCommitOid,
68 UpstreamCommitOid,
69 GitBlame(io::Error),
70 NoSearchMatch(String),
71 NoPreviousSearch,
72 InvalidSearchRegex(regex::Error),
73 EditedCommandQuotes,
74 EditedCommandFixed,
75 EditedCommandWrites(String),
76 GitRefusedTheCommand(String),
77}
78
79impl std::error::Error for Error {}
80
81impl Display for Error {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 match self {
84 Error::StashList(e) => f.write_fmt(format_args!("Couldn't list stash: {e}")),
85 Error::ReadLog(e) => f.write_fmt(format_args!("Couldn't read log: {e}")),
86 Error::ReadPipedInput(e) => {
87 f.write_fmt(format_args!("Couldn't read the piped input: {e}"))
88 }
89 Error::ReadRebaseTodo(e) => {
90 f.write_fmt(format_args!("Couldn't read the rebase todo list: {e}"))
91 }
92 Error::WriteRebaseTodo(e) => {
93 f.write_fmt(format_args!("Couldn't write the rebase todo list: {e}"))
94 }
95 Error::OpenRepo(e) => match e.code() {
96 git2::ErrorCode::NotFound => f.write_str("No .git found in the current directory"),
97 _ => f.write_fmt(format_args!("Couldn't open repo: {e:?}")),
98 },
99 Error::Term(e) => f.write_fmt(format_args!("Terminal error: {}", e)),
100 Error::Config(e) => f.write_fmt(format_args!("Configuration error: {e}")),
101 Error::EditConfig(e) => {
102 f.write_fmt(format_args!("Couldn't save to the config file: {e}"))
103 }
104 Error::Bindings { bad_key_bindings } => {
105 let mut error_string = String::from("Errors while parsing key bindings:");
106 for item in bad_key_bindings {
107 error_string.push_str(&format!("\n{item}"));
108 }
109 f.write_fmt(format_args!("{error_string}"))
110 }
111 Error::FileWatcher(e) => f.write_fmt(format_args!("File watcher error: {e}")),
112 Error::ReadRebaseStatusFile(e) => {
113 f.write_fmt(format_args!("Couldn't read rebase status file: {e}"))
114 }
115 Error::ReadBranchName(e) => f.write_fmt(format_args!("Couldn't read branch name: {e}")),
116 Error::BranchNameUtf8(_e) => f.write_str("Branch name error"),
117 Error::GitDiff(e) => f.write_fmt(format_args!("Git diff error: {e}")),
118 Error::GitDiffUtf8(e) => {
119 f.write_fmt(format_args!("Git diff output is not valid UTF-8: {e}"))
120 }
121 Error::GitShow(e) => f.write_fmt(format_args!("Git show error: {e}")),
122 Error::GitShowUtf8(e) => {
123 f.write_fmt(format_args!("Git show output is not valid UTF-8: {e}"))
124 }
125 Error::GitShowMeta(e) => f.write_fmt(format_args!("Git show metadata error: {e}")),
126 Error::NotOnBranch => f.write_str("Head is not a branch"),
127 Error::GetHead(e) => f.write_fmt(format_args!("Couldn't get HEAD: {e}")),
128 Error::CurrentBranchName(e) => {
129 f.write_fmt(format_args!("Couldn't get current branch name: {e}"))
130 }
131 Error::GetCurrentBranchUpstream(e) => {
132 f.write_fmt(format_args!("Couldn't get current branch upstream: {e}"))
133 }
134 Error::GetCurrentBranchUpstreamUtf8(_e) => {
135 f.write_str("Current branch upstream is not valid UTF-8")
136 }
137 Error::RemoteNameUtf8(_e) => f.write_str("Remote name is not valid UTF-8"),
138 Error::CannotDeleteCurrentBranch => f.write_str("Cannot delete current branch"),
139 Error::BranchNameRequired => f.write_str("Branch name required"),
140 Error::IsBranchMerged(e) => {
141 f.write_fmt(format_args!("Couldn't check if branch is merged: {e}"))
142 }
143 Error::GetRemote(e) => f.write_fmt(format_args!("Couldn't get remote: {e}")),
144 Error::ReadGitConfig(e) => f.write_fmt(format_args!("Couldn't read git config: {e}")),
145 Error::ReadGitConfigUtf8(_e) => f.write_str("Git config is not valid UTF-8"),
146 Error::DeleteGitConfig(e) => {
147 f.write_fmt(format_args!("Couldn't delete git config: {e}"))
148 }
149 Error::SetGitConfig(e) => f.write_fmt(format_args!("Couldn't set git config: {e}")),
150 Error::RemoteHasNoName => f.write_str("Remote has no name"),
151 Error::ReadOid(e) => f.write_fmt(format_args!("Couldn't read OID: {e}")),
152 Error::ArgMustBePositiveNumber => f.write_str("Value must be a number greater than 0"),
153 Error::ArgInvalidRegex(e) => f.write_fmt(format_args!("Invalid regex: {e}")),
154 Error::Clipboard(e) => f.write_fmt(format_args!("Clipboard error: {e}")),
155 Error::FindGitRev(e) => f.write_fmt(format_args!("Couldn't find git revision: {e}")),
156 Error::NoEditorSet => f.write_fmt(format_args!(
157 "No editor environment variable set ({})",
158 crate::ops::show::EDITOR_VARS.join(", ")
159 )),
160 Error::GitStatus(e) => f.write_fmt(format_args!("Git status error: {e}")),
161 Error::CmdAlreadyRunning => f.write_str("A command is already running"),
162 Error::StashWorkTreeEmpty => f.write_str("Cannot stash: working tree is empty"),
163 Error::CouldntAwaitCmd(e) => f.write_fmt(format_args!("Couldn't await command: {e}")),
164 Error::NoRepoWorkdir => f.write_str("No repository working directory"),
165 Error::SpawnCmd(e) => f.write_fmt(format_args!("Failed to spawn command: {e}")),
166 Error::CmdBadExit(command, code, stderr) => {
168 writeln!(f, "{command}")?;
169 match code {
170 Some(code) => write!(f, "exit code: {code}")?,
171 None => write!(f, "ended by a signal")?,
172 }
173 let stderr = stderr.trim_end();
174 if !stderr.is_empty() {
175 write!(f, "\nstderr:\n{stderr}")?;
176 }
177 Ok(())
178 }
179 Error::CouldntReadCmdOutput(e) => {
180 f.write_fmt(format_args!("Couldn't read command output: {e}"))
181 }
182 Error::ListGitReferences(e) => {
183 f.write_fmt(format_args!("Couldn't list git references: {e}"))
184 }
185 Error::OpenLogFile(e) => f.write_fmt(format_args!("Couldn't open log file: {e}")),
186 Error::PromptAborted => f.write_str("Aborted"),
187 Error::ReadPromptHistory(e) => {
188 f.write_fmt(format_args!("Couldn't read prompt history: {e}"))
189 }
190 Error::WritePromptHistory(e) => {
191 f.write_fmt(format_args!("Couldn't write prompt history: {e}"))
192 }
193 Error::NoMoreEvents => unimplemented!(),
194 Error::CannotSpinoffCurrentBranch => f.write_str("Cannot spin-off current branch"),
195 Error::SpinoffBranchExists(new_branch_name) => f.write_fmt(format_args!(
196 "Cannot spin-off {new_branch_name}. It already exists"
197 )),
198 Error::DoesBranchExist(e) => {
199 f.write_fmt(format_args!("Couldn't check if branch exists: {}", e))
200 }
201 Error::GetBranchName(e) => f.write_fmt(format_args!("Couldn't get branch name: {}", e)),
202 Error::BaseCommitOid => f.write_str("Could not resolve OID of base commit"),
203 Error::UpstreamCommitOid => {
204 f.write_str("Could not resolve OID of upstream branch commit")
205 }
206 Error::GitBlame(e) => f.write_fmt(format_args!("Git blame error: {e}")),
207 Error::NoSearchMatch(query) => f.write_fmt(format_args!("No match: {query}")),
208 Error::NoPreviousSearch => f.write_str("No previous search"),
209 Error::InvalidSearchRegex(e) => f.write_fmt(format_args!("Invalid search: {e}")),
210 Error::EditedCommandQuotes => f.write_str("Unbalanced quotes"),
211 Error::EditedCommandFixed => f.write_str(
212 "Only git's arguments can be edited, not the program or git's own options",
213 ),
214 Error::EditedCommandWrites(subcommand) => f.write_fmt(format_args!(
215 "gitu re-runs this command on every refresh, so it must only report: {subcommand}"
216 )),
217 Error::GitRefusedTheCommand(stderr) => f.write_str(stderr),
218 }
219 }
220}
221
222#[derive(Debug)]
223pub enum Utf8Error {
224 Str(std::str::Utf8Error),
225 String(string::FromUtf8Error),
226}
227
228impl std::error::Error for Utf8Error {}
229
230impl Display for Utf8Error {
231 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
232 f.write_str("not valid UTF-8")
233 }
234}