use crate::commit_transaction::{CommitInvocation, TransactionStop, failed};
use crate::service_commit_args::{FixupKind, ServiceCommitArgs, cleanup_message};
use crate::transaction_git::{GitContext, GitRequest, run_git};
use std::ffi::OsString;
use std::path::Path;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Built {
pub message: String,
pub reused_author: Option<String>,
}
pub fn identity(context: &GitContext, cwd: &Path, variable: &str, overrides: &[(&str, String)]) -> Result<Option<String>, TransactionStop> {
let mut request: GitRequest = GitRequest::new(cwd, &["var", variable]);
for (name, value) in overrides {
request.environment.push((OsString::from(name), OsString::from(value)));
}
let output = match run_git(context, &request) {
Ok(finished) => finished,
Err(error) => return Err(failed(format!("reading the commit identity failed: {error:?}"))),
};
if output.code != Some(0) {
return Ok(None);
}
return Ok(Some(String::from_utf8_lossy(output.stdout.as_slice()).trim().to_string()));
}
pub fn author_overrides(parsed: &ServiceCommitArgs) -> Vec<(&'static str, String)> {
let mut overrides: Vec<(&'static str, String)> = Vec::new();
if let Some(author) = parsed.author.as_deref() {
let (name, rest): (&str, &str) = author.split_once('<').unwrap_or((author, ""));
overrides.push(("GIT_AUTHOR_NAME", name.trim().to_string()));
overrides.push(("GIT_AUTHOR_EMAIL", rest.trim_end().trim_end_matches('>').to_string()));
}
if let Some(date) = parsed.date.as_deref() {
overrides.push(("GIT_AUTHOR_DATE", date.to_string()));
}
return overrides;
}
fn query(context: &GitContext, cwd: &Path, arguments: &[&str]) -> Result<Option<Vec<u8>>, TransactionStop> {
let output = match run_git(context, &GitRequest::new(cwd, arguments)) {
Ok(finished) => finished,
Err(error) => return Err(failed(format!("reading the repository failed: {error:?}"))),
};
return Ok(if output.code == Some(0) { Some(output.stdout) } else { None });
}
fn reused_commit(context: &GitContext, cwd: &Path, revision: &str) -> Result<Option<(String, String)>, TransactionStop> {
if revision.starts_with('-') {
return Ok(None);
}
let Some(oid) = query(context, cwd, &["rev-parse", "--verify", "--quiet", format!("{revision}^{{commit}}").as_str()])? else { return Ok(None) };
let Some(raw) = query(context, cwd, &["cat-file", "commit", String::from_utf8_lossy(oid.as_slice()).trim()])? else { return Ok(None) };
let Ok(text) = String::from_utf8(raw) else { return Ok(None) };
let Some((header, message)) = text.split_once("\n\n") else { return Ok(None) };
if header.lines().any(is_encoding_header) {
return Ok(None);
}
let Some(author) = header.lines().find_map(author_of_header_line) else { return Ok(None) };
return Ok(Some((author.to_string(), message.to_string())));
}
fn subject_of(context: &GitContext, cwd: &Path, revision: &str) -> Result<Option<String>, TransactionStop> {
if revision.starts_with('-') {
return Ok(None);
}
let Some(bytes) = query(context, cwd, &["log", "-1", "--format=%s", revision, "--"])? else { return Ok(None) };
return Ok(String::from_utf8(bytes).ok().map(without_final_line_breaks));
}
fn is_encoding_header(line: &str) -> bool {
return line.starts_with("encoding ");
}
fn author_of_header_line(line: &str) -> Option<&str> {
return line.strip_prefix("author ");
}
fn without_final_line_breaks(text: String) -> String {
return text.trim_end_matches('\n').to_string();
}
fn is_not_comment_line(line: &&str) -> bool {
return !line.starts_with('#');
}
fn with_final_line_break(text: &String) -> String {
return if text.ends_with('\n') { text.clone() } else { format!("{text}\n") };
}
pub fn strip_comment_lines(text: &str) -> String {
return text.split('\n').filter(is_not_comment_line).collect::<Vec<&str>>().join("\n");
}
pub fn build_message(
invocation: &CommitInvocation<'_>,
parsed: &ServiceCommitArgs,
committer: &str,
previous_message: Option<&str>,
) -> Result<Option<Built>, TransactionStop> {
let context: &GitContext = invocation.context;
let cwd: &Path = invocation.cwd;
let mut reused_author: Option<String> = None;
let sources: Vec<String> = if let Some(file) = parsed.file.as_ref() {
let Ok(bytes) = std::fs::read(cwd.join(file)) else { return Ok(None) };
let Ok(text) = String::from_utf8(bytes) else { return Ok(None) };
vec![text]
} else if let Some(revision) = parsed.reuse.as_deref() {
let Some((author, message)) = reused_commit(context, cwd, revision)? else { return Ok(None) };
reused_author = Some(author);
vec![message]
} else if let Some((kind, revision)) = parsed.fixup.as_ref() {
let Some(subject) = subject_of(context, cwd, revision.as_str())? else { return Ok(None) };
let prefix: &str = match kind {
FixupKind::Fixup => "fixup!",
};
vec![format!("{prefix} {subject}")]
} else if parsed.messages.is_empty() && parsed.amend && let Some(previous) = previous_message {
vec![previous.to_string()]
} else {
parsed.messages.clone()
};
let texts: Vec<String> = if parsed.strip { sources.iter().map(String::as_str).map(strip_comment_lines).collect() } else { sources };
let cleaned: Option<String> = if parsed.verbatim {
Some(texts.iter().map(with_final_line_break).collect::<Vec<String>>().join("\n"))
} else {
cleanup_message(texts.as_slice())
};
let message: String = match cleaned {
Some(text) if !text.trim().is_empty() || parsed.verbatim => text,
_ if parsed.allow_empty_message => String::new(),
_ => return Ok(None),
};
let mut trailers: Vec<String> = Vec::new();
if parsed.signoff {
let who: &str = match committer.split_once('>') {
Some((name_and_email, _time)) => &committer[..name_and_email.len() + 1],
None => committer,
};
trailers.push(format!("Signed-off-by: {who}"));
}
trailers.extend(parsed.trailers.iter().cloned());
if trailers.is_empty() {
return Ok(Some(Built { message, reused_author }));
}
let mut arguments: Vec<String> = vec![String::from("interpret-trailers")];
for trailer in &trailers {
arguments.push(String::from("--trailer"));
arguments.push(trailer.clone());
}
let mut request: GitRequest = GitRequest::new(cwd, arguments.as_slice());
request.input = Some(message.into_bytes());
let output = match run_git(context, &request) {
Ok(finished) => finished,
Err(error) => return Err(failed(format!("preparing the commit message failed: {error:?}"))),
};
if output.code != Some(0) {
return Ok(None);
}
return Ok(match String::from_utf8(output.stdout) {
Ok(trailed) => Some(Built { message: trailed, reused_author }),
Err(_not_text) => None,
});
}