1use std::path::Path;
16
17pub const LOG_COMMAND: &str = "journalctl --user --unit 'cli-git@*.service'";
19
20pub const LINGER_COMMAND: &str = "loginctl enable-linger";
22
23pub const RESTART_COMMAND: &str = "systemctl --user restart 'cli-git@*.service'";
25
26pub const CHECK_COMMANDS: [&str; 2] = ["git log -1", "git status"];
28
29pub const STARTING_NOTICE: &str = "Starting the commit service...";
31
32pub const SILENT_NOTICE: &str = "The commit service stopped answering; asking it again with the same commit, which cannot land twice.";
34
35pub const UPDATING_NOTICE: &str = "Updating the commit service...";
37
38#[derive(Clone, Copy, Debug, Eq, PartialEq)]
48pub enum Platform {
49 Windows,
51 Bsd,
53 LinuxWithoutUserServices,
55 Other,
57}
58
59#[derive(Clone, Debug, Eq, PartialEq)]
69pub enum ClientFailure {
70 DidNotStart,
72 NoActivation(Platform),
74 OtherAccount,
76 MayNotHaveLanded,
78 NoCommonVersion,
80 OtherRelease,
82 RepositoryFull(String),
84 RepositoryReadOnly(String),
86 BranchKeepsChanging { branch: String, times: usize },
88 BranchMoved { branch: String },
90 BranchMovedMessageSaved { branch: String, file: String, amend: bool },
92 BranchSwitched { branch: String, now: String, file: Option<String> },
95 ServiceSilent,
97 StillWithService { minutes: u64, blocker: Option<String> },
99 WithdrawalUnconfirmed,
101 StepTooSlow,
103 Refused,
105 IndexInUse(String),
107 Unavailable,
109 ProgramMissing(String),
111 CouldNotStart(String),
113 UnitInTheWay(String),
115 WorkNotHandedOver(String),
117 ContentPruned,
119}
120
121pub const NOTHING_CHANGED: &str = "Nothing in the repository changed.";
123
124pub fn render(failure: &ClientFailure) -> String {
134 return match failure {
135 ClientFailure::DidNotStart => format!(
136 "The commit service did not start, so this commit was not made. Its log shows why: `{LOG_COMMAND}`. \
137 Run the commit again after fixing that. {NOTHING_CHANGED}"
138 ),
139 ClientFailure::NoActivation(platform) => format!(
140 "This computer cannot start the commit service on demand, so this commit was not made. {} {NOTHING_CHANGED}",
141 platform_remedy(*platform)
142 ),
143 ClientFailure::OtherAccount => format!(
144 "Another account runs the commit service here, so this commit was not made. Use your own account, or ask \
145 the other account to stop its service, then run the commit again. {NOTHING_CHANGED}"
146 ),
147 ClientFailure::MayNotHaveLanded => format!(
148 "The commit service stopped before it confirmed this commit. Run `{}` to see whether it was made, and `{}` \
149 to see what is still staged. If it was not made, run the commit again.",
150 CHECK_COMMANDS[0], CHECK_COMMANDS[1]
151 ),
152 ClientFailure::NoCommonVersion => format!(
153 "This git is older than the installed commit service and cannot work with it, so this commit was not made. \
154 Update the git wrapper with the tool that installed it, and run the commit again. {NOTHING_CHANGED}"
155 ),
156 ClientFailure::OtherRelease => format!(
157 "The commit service that is running was started from another release of this git wrapper and did not stop when \
158 asked, so this commit was not made. Restart it with `{RESTART_COMMAND}`, and run the commit again. {NOTHING_CHANGED}"
159 ),
160 ClientFailure::RepositoryFull(path) => format!(
161 "There is no space left for {path}, so this commit was not made. Free some space and run the commit again. \
162 {NOTHING_CHANGED}"
163 ),
164 ClientFailure::RepositoryReadOnly(path) => format!(
165 "{path} cannot be written to, so this commit was not made. Make the repository writable (check its \
166 permissions and whether the disk is mounted read-only) and run the commit again. {NOTHING_CHANGED}"
167 ),
168 ClientFailure::BranchKeepsChanging { branch, times } => format!(
169 "The branch {branch} changed {times} times while this commit was being prepared, so it was not made. Run \
170 the commit again; if other people commit to the same branch at the same moment, it will succeed once they \
171 pause. {NOTHING_CHANGED}"
172 ),
173 ClientFailure::BranchMoved { branch } => format!(
174 "The branch {branch} moved while this commit was being prepared, and a commit that replaces the last one, or one \
175 that was written or signed by hand, cannot be moved onto a different last commit, so it was not made. Run the \
176 commit again to make it on the branch as it is now. {NOTHING_CHANGED}"
177 ),
178 ClientFailure::BranchMovedMessageSaved { branch, file, amend } => format!(
179 "The branch {branch} moved while this commit was being prepared, so it was not made. The message you wrote is saved \
180 in {file}. Run `git commit {}-F {file}` to make the commit on the branch as it is now. {NOTHING_CHANGED}",
181 if *amend { "--amend " } else { "" }
182 ),
183 ClientFailure::BranchSwitched { branch, now, file } => format!(
184 "The worktree switched from {branch} to {now} while this commit was being prepared, so it was not made. {} Switch back \
185 to {branch} and run the commit again, or run it here to commit on {now}. {NOTHING_CHANGED}",
186 match file {
187 Some(saved) => format!("The message you wrote is saved in {saved}; `git commit -F {saved}` uses it."),
188 None => String::new(),
189 }
190 ),
191 ClientFailure::ServiceSilent => format!(
192 "The commit service stopped answering while this commit waited, and it did not answer when asked again, so it is \
193 not known whether the commit was made. Run `{}` to see whether it was made, and `{}` to see what is still \
194 staged. If it was not made, look at why the service is stuck with `{LOG_COMMAND}`, restart it with \
195 `{RESTART_COMMAND}`, and run the commit again.",
196 CHECK_COMMANDS[0], CHECK_COMMANDS[1]
197 ),
198 ClientFailure::StillWithService { minutes, blocker } => format!(
199 "This commit waited {minutes} minutes for the commit service and was withdrawn, so it will not land later by itself.{} \
200 Your staged changes and your worktree are as they were. Run the commit again; if the service is stuck, look at why with \
201 `{LOG_COMMAND}` and restart it with `{RESTART_COMMAND}`. {NOTHING_CHANGED}",
202 blocker.as_ref().map_or_else(String::new, |text| { return format!(" It was waiting for: {text}."); })
203 ),
204 ClientFailure::WithdrawalUnconfirmed => format!(
205 "This commit waited for the commit service until the longest wait the wrapper allows, and the service did not confirm \
206 that it withdrew it, so it is not known whether the commit was made. Run `{}` to see whether it was made, and `{}` to \
207 see what is still staged. If it was not made, run the commit again; if the service is stuck, look at why with \
208 `{LOG_COMMAND}` and restart it with `{RESTART_COMMAND}`.",
209 CHECK_COMMANDS[0], CHECK_COMMANDS[1]
210 ),
211 ClientFailure::StepTooSlow => format!(
212 "A Git step of this commit ran longer than the commit service allows and was ended, so this commit was not made and will not \
213 land later by itself. The step is named above, and the commit built for you is kept on the side ref named there when one \
214 is. Run the commit again; if it happens again, look at why with `{LOG_COMMAND}`. {NOTHING_CHANGED}"
215 ),
216 ClientFailure::Refused => format!(
217 "The commit service refused this commit because it names a path or a value that cannot be committed, so it \
218 was not made. Check the paths you passed to the commit. {NOTHING_CHANGED}"
219 ),
220 ClientFailure::IndexInUse(holder) => format!(
221 "{holder} is using this repository's index, so this commit was not made. Wait for it to finish, or close \
222 it, and run the commit again. {NOTHING_CHANGED}"
223 ),
224 ClientFailure::ProgramMissing(path) => format!(
225 "The commit service program was not found at {path}, so this commit was not made. Reinstall the git wrapper \
226 with the tool that installed it, so that the program sits next to it, then run the commit again. \
227 {NOTHING_CHANGED}"
228 ),
229 ClientFailure::UnitInTheWay(path) => format!(
230 "The file `{path}` has the name of a settings file of the commit service but was not written by the git wrapper, \
231 so the commit service could not be set up and this commit was not made. Move or delete that file, then run the \
232 commit again. {NOTHING_CHANGED}"
233 ),
234 ClientFailure::WorkNotHandedOver(reason) => format!(
235 "Your commit is saved. Its backup push and upkeep could not be handed to the commit service: {reason}. \
236 Run `git push` when ready."
237 ),
238 ClientFailure::ContentPruned => format!(
239 "The files of this commit were removed from the repository while the commit was being made, so it was not made. \
240 Git removes content that no branch or index refers to yet when `git gc --prune=now` or `git prune` runs in the \
241 same repository, and Git's documentation says that `--prune=now` risks failures and corruption when another \
242 process writes to the repository at the same moment. Run the commit again once the cleanup has finished, and avoid `--prune=now` while commits are \
243 being made (the default `git gc` keeps new content for two weeks). If nothing like that was running, run \
244 `git fsck` to check the repository. {NOTHING_CHANGED}"
245 ),
246 ClientFailure::CouldNotStart(reason) => format!(
247 "The commit service could not be set up, so this commit was not made: {reason}. Fix that and run the commit \
248 again. {NOTHING_CHANGED}"
249 ),
250 ClientFailure::Unavailable => format!(
251 "The commit service could not finish this commit, so it was not made. Run the commit again; if it fails \
252 again, look at the service's log with `{LOG_COMMAND}`. {NOTHING_CHANGED}"
253 ),
254 };
255}
256
257pub fn platform_remedy(platform: Platform) -> String {
267 return match platform {
268 Platform::Windows => String::from(
269 "Committing through this wrapper is not supported on Windows yet. Use the Linux or macOS machine for commits \
270 to this repository.",
271 ),
272 Platform::Bsd => String::from(
273 "Committing through this wrapper is not supported on this system yet. Use a Linux or macOS machine for \
274 commits to this repository.",
275 ),
276 Platform::LinuxWithoutUserServices => format!(
277 "This account has no user service manager. Turn it on with `{LINGER_COMMAND}` (the system must run \
278 systemd), or commit from a system that does."
279 ),
280 Platform::Other => String::from(
281 "Committing through this wrapper is not supported on this system. Use a Linux or macOS machine for \
282 commits to this repository.",
283 ),
284 };
285}
286
287pub fn position_notice(number: usize, of: usize) -> String {
297 let ahead: usize = number.saturating_sub(1);
298 let _ = of;
299 return match ahead {
300 0 => String::from("This commit is next."),
301 1 => String::from("Waiting for 1 earlier commit."),
302 many => format!("Waiting for {many} earlier commits."),
303 };
304}
305
306pub const DENYLIST: [&str; 22] = [
309 "protocol", "journal", "request id", "daemon", "socket", "errno", "epipe", "econnrefused", "eagain", "flock",
310 "lock file", "peer", "frame", "handshake", "batch", "oid", "fast-import", "unix domain", "abstract", "pid",
311 "uid", "os error",
312];
313
314pub fn path_text(path: &Path) -> String {
316 return path.display().to_string();
317}
318
319#[cfg(test)]
321#[path = "service_messages_tests.rs"]
322mod tests;