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 CHECK_COMMANDS: [&str; 2] = ["git log -1", "git status"];
25
26pub const STARTING_NOTICE: &str = "Starting the commit service...";
28
29pub const UPDATING_NOTICE: &str = "Updating the commit service...";
31
32#[derive(Clone, Copy, Debug, Eq, PartialEq)]
42pub enum Platform {
43 Windows,
45 Bsd,
47 LinuxWithoutUserServices,
49 Other,
51}
52
53#[derive(Clone, Debug, Eq, PartialEq)]
63pub enum ClientFailure {
64 DidNotStart,
66 NoActivation(Platform),
68 OtherAccount,
70 MayNotHaveLanded,
72 NoCommonVersion,
74 RepositoryFull(String),
76 RepositoryReadOnly(String),
78 BranchKeepsChanging { branch: String, times: usize },
80 WaitedTooLong { minutes: u64 },
82 Refused,
84 IndexInUse(String),
86 Unavailable,
88 ProgramMissing(String),
90 CouldNotStart(String),
92 UnitInTheWay(String),
94 WorkNotHandedOver(String),
96}
97
98const NOTHING_CHANGED: &str = "Nothing in the repository changed.";
100
101pub fn render(failure: &ClientFailure) -> String {
111 return match failure {
112 ClientFailure::DidNotStart => format!(
113 "The commit service did not start, so this commit was not made. Its log shows why: `{LOG_COMMAND}`. \
114 Run the commit again after fixing that. {NOTHING_CHANGED}"
115 ),
116 ClientFailure::NoActivation(platform) => format!(
117 "This computer cannot start the commit service on demand, so this commit was not made. {} {NOTHING_CHANGED}",
118 platform_remedy(*platform)
119 ),
120 ClientFailure::OtherAccount => format!(
121 "Another account runs the commit service here, so this commit was not made. Use your own account, or ask \
122 the other account to stop its service, then run the commit again. {NOTHING_CHANGED}"
123 ),
124 ClientFailure::MayNotHaveLanded => format!(
125 "The commit service stopped before it confirmed this commit. Run `{}` to see whether it was made, and `{}` \
126 to see what is still staged. If it was not made, run the commit again.",
127 CHECK_COMMANDS[0], CHECK_COMMANDS[1]
128 ),
129 ClientFailure::NoCommonVersion => format!(
130 "This git is older than the installed commit service and cannot work with it, so this commit was not made. \
131 Update the git wrapper with the tool that installed it, and run the commit again. {NOTHING_CHANGED}"
132 ),
133 ClientFailure::RepositoryFull(path) => format!(
134 "There is no space left for {path}, so this commit was not made. Free some space and run the commit again. \
135 {NOTHING_CHANGED}"
136 ),
137 ClientFailure::RepositoryReadOnly(path) => format!(
138 "{path} cannot be written to, so this commit was not made. Make the repository writable (check its \
139 permissions and whether the disk is mounted read-only) and run the commit again. {NOTHING_CHANGED}"
140 ),
141 ClientFailure::BranchKeepsChanging { branch, times } => format!(
142 "The branch {branch} changed {times} times while this commit was being prepared, so it was not made. Run \
143 the commit again; if other people commit to the same branch at the same moment, it will succeed once they \
144 pause. {NOTHING_CHANGED}"
145 ),
146 ClientFailure::WaitedTooLong { minutes } => format!(
147 "This commit waited {minutes} minutes behind other commits and was not made. Run it again later, or raise \
148 the waiting limit in the settings for the wrapper. {NOTHING_CHANGED}"
149 ),
150 ClientFailure::Refused => format!(
151 "The commit service refused this commit because it names a path or a value that cannot be committed, so it \
152 was not made. Check the paths you passed to the commit. {NOTHING_CHANGED}"
153 ),
154 ClientFailure::IndexInUse(holder) => format!(
155 "{holder} is using this repository's index, so this commit was not made. Wait for it to finish, or close \
156 it, and run the commit again. {NOTHING_CHANGED}"
157 ),
158 ClientFailure::ProgramMissing(path) => format!(
159 "The commit service program was not found at {path}, so this commit was not made. Reinstall the git wrapper \
160 with the tool that installed it, so that the program sits next to it, then run the commit again. \
161 {NOTHING_CHANGED}"
162 ),
163 ClientFailure::UnitInTheWay(path) => format!(
164 "The file `{path}` has the name of a settings file of the commit service but was not written by the git wrapper, \
165 so the commit service could not be set up and this commit was not made. Move or delete that file, then run the \
166 commit again. {NOTHING_CHANGED}"
167 ),
168 ClientFailure::WorkNotHandedOver(reason) => format!(
169 "Your commit is saved. Its backup push and upkeep could not be handed to the commit service: {reason}. \
170 Run `git push` when ready."
171 ),
172 ClientFailure::CouldNotStart(reason) => format!(
173 "The commit service could not be set up, so this commit was not made: {reason}. Fix that and run the commit \
174 again. {NOTHING_CHANGED}"
175 ),
176 ClientFailure::Unavailable => format!(
177 "The commit service could not finish this commit, so it was not made. Run the commit again; if it fails \
178 again, look at the service's log with `{LOG_COMMAND}`. {NOTHING_CHANGED}"
179 ),
180 };
181}
182
183pub fn platform_remedy(platform: Platform) -> String {
193 return match platform {
194 Platform::Windows => String::from(
195 "Committing through this wrapper is not supported on Windows yet. Use the Linux or macOS machine for commits \
196 to this repository.",
197 ),
198 Platform::Bsd => String::from(
199 "Committing through this wrapper is not supported on this system yet. Use a Linux or macOS machine for \
200 commits to this repository.",
201 ),
202 Platform::LinuxWithoutUserServices => format!(
203 "This account has no user service manager. Turn it on with `{LINGER_COMMAND}` (the system must run \
204 systemd), or commit from a system that does."
205 ),
206 Platform::Other => String::from(
207 "Committing through this wrapper is not supported on this system. Use a Linux or macOS machine for \
208 commits to this repository.",
209 ),
210 };
211}
212
213pub fn position_notice(number: usize, of: usize) -> String {
223 let ahead: usize = number.saturating_sub(1);
224 let _ = of;
225 return match ahead {
226 0 => String::from("This commit is next."),
227 1 => String::from("Waiting for 1 earlier commit."),
228 many => format!("Waiting for {many} earlier commits."),
229 };
230}
231
232pub fn push_failed_notice(commit: &str, said: &str) -> String {
243 let short: &str = commit.get(..7).unwrap_or(commit);
244 let detail: String = if said.is_empty() { String::from("Git gave no reason.") } else { format!("Git said: {said}") };
245 return format!(
246 "The backup push of your commit {short} failed, so it exists only on this computer for now. {detail} The commit \
247 service runs with its own settings, not your terminal's, so a push that works in your terminal can fail here \
248 when it needs a variable your terminal has. Run `git push` when ready."
249 );
250}
251
252pub const DENYLIST: [&str; 22] = [
255 "protocol", "journal", "request id", "daemon", "socket", "errno", "epipe", "econnrefused", "eagain", "flock",
256 "lock file", "peer", "frame", "handshake", "batch", "oid", "fast-import", "unix domain", "abstract", "pid",
257 "uid", "os error",
258];
259
260pub fn path_text(path: &Path) -> String {
262 return path.display().to_string();
263}
264
265#[cfg(test)]
267#[path = "service_messages_tests.rs"]
268mod tests;