pub(super) const AUTO_MERGING: &str = "Auto-merging ";
pub(super) const MERGE_CONFLICT_IN: &str = "Merge conflict in ";
pub(super) fn shown(line: &str) -> &str {
line.rsplit('\r')
.map(|part| part.trim_start_matches("\u{1b}[K"))
.find(|part| !part.trim().is_empty())
.unwrap_or_default()
}
pub(super) fn squash(line: &str) -> String {
line.split_whitespace().collect::<Vec<_>>().join(" ")
}
pub fn headline(output: &str) -> &str {
let said = || {
output
.lines()
.map(shown)
.filter(|line| !line.trim().is_empty())
};
said()
.find(|line| !line.starts_with(AUTO_MERGING))
.or_else(|| said().next())
.unwrap_or_default()
.trim_end()
}
pub fn summary_line(output: &str) -> String {
const NOISE: [&str; 6] = [
"remote:",
"hint:",
"To ",
"On branch ",
"Your branch ",
AUTO_MERGING,
];
let lines = || {
output
.lines()
.map(|l| shown(l).trim())
.filter(|l| !l.is_empty() && !NOISE.iter().any(|n| l.starts_with(n)))
};
let Some(line) = lines()
.find(|l| l.contains(" -> "))
.or_else(|| lines().next())
else {
return String::new();
};
let mut words = line.split_whitespace().peekable();
if words
.peek()
.is_some_and(|w| matches!(*w, "*" | "+" | "-" | "!" | "="))
{
words.next();
}
words.collect::<Vec<_>>().join(" ")
}
pub fn result_line(output: &str) -> String {
const NOISE: [&str; 18] = [
"From ",
"To ",
"remote:",
"hint:",
"Updating ",
"Fetching ",
AUTO_MERGING,
"warning:",
"error:",
"fatal:",
"On branch ",
"Your branch ",
"Changes not staged for commit:",
"Changes to be committed:",
"Untracked files:",
"Unmerged paths:",
"no changes added to commit",
"nothing added to commit",
];
let file_row = |line: &str| {
let bytes = line.as_bytes();
bytes.len() > 1 && bytes[0].is_ascii_uppercase() && bytes[1] == b'\t'
};
let result = output.lines().map(shown).find(|line| {
!line.trim().is_empty()
&& !line.starts_with(char::is_whitespace)
&& !file_row(line)
&& !NOISE.iter().any(|n| line.starts_with(n))
});
match result {
Some(line) => line.trim_end().to_string(),
None if output.contains(" -> ") => summary_line(output),
None => String::new(),
}
}
pub fn conflict_summary(output: &str) -> Option<String> {
const NAMED: usize = 3;
let conflicts: Vec<&str> = output
.lines()
.map(|l| shown(l).trim())
.filter(|l| l.starts_with("CONFLICT ("))
.collect();
match conflicts.as_slice() {
[] => return None,
[only] if !only.contains(MERGE_CONFLICT_IN) => return Some(squash(only)),
_ => {}
}
let mut files: Vec<&str> = Vec::new();
let mut unnamed = 0;
for line in &conflicts {
match line.split_once(MERGE_CONFLICT_IN) {
Some((_, file)) if !files.contains(&file.trim()) => files.push(file.trim()),
Some(_) => {}
None => unnamed += 1,
}
}
let count = files.len() + unnamed;
let noun = if count == 1 { "conflict" } else { "conflicts" };
if files.is_empty() {
return Some(format!("{count} {noun}"));
}
let mut names = files[..files.len().min(NAMED)].join(", ");
if count > NAMED.min(files.len()) {
names.push_str(", \u{2026}");
}
Some(format!("{count} {noun} ({names})"))
}
pub fn error_line(err: &str) -> String {
if let Some(conflicts) = conflict_summary(err) {
return conflicts;
}
let lines = || err.lines().map(|l| shown(l).trim());
if let Some(rejected) = lines().find_map(|l| l.strip_prefix('!').filter(|_| l.contains(" -> ")))
{
return squash(rejected);
}
if let Some(at) = lines().position(|l| l.starts_with("error:") || l.starts_with("fatal:")) {
let line = lines().nth(at).map(squash).unwrap_or_default();
return with_listed_files(line, err.lines().skip(at + 1));
}
let line = summary_line(err);
if line.is_empty() {
headline(err).to_string()
} else {
line
}
}
pub(super) fn with_listed_files<'a>(
mut line: String,
after: impl Iterator<Item = &'a str>,
) -> String {
const NAMED: usize = 3;
if !line.ends_with(':') {
return line;
}
let files: Vec<&str> = after
.map(shown)
.take_while(|l| l.starts_with(char::is_whitespace) && !l.trim().is_empty())
.map(str::trim)
.collect();
if files.is_empty() {
return line;
}
line.push(' ');
line.push_str(&files[..files.len().min(NAMED)].join(", "));
if files.len() > NAMED {
line.push_str(", \u{2026}");
}
line
}
pub fn local_changes_block_switch(err: &str) -> bool {
err.contains("commit your changes or stash them") && !err.contains("move or remove them")
}
pub fn failure_line(noun: &str, err: &str) -> String {
match conflict_summary(err) {
Some(conflicts) => format!("{noun}: {conflicts}"),
None => format!("{noun} failed: {}", error_line(err)),
}
}