#[cfg(test)]
#[must_use]
pub fn prompt_generate_commit_message_with_diff(diff: &str) -> String {
use crate::workspace::WorkspaceFs;
use std::env;
let workspace = WorkspaceFs::new(env::current_dir().unwrap());
let diff_content = diff.trim();
let has_changes = !diff_content.is_empty();
if !has_changes {
return "ERROR: Empty diff provided. This indicates a bug in the caller - \
meaningful changes should be checked before requesting a commit message."
.to_string();
}
let template_content = include_str!("../templates/commit_message_xml.txt");
let template = Template::new(template_content);
let partials = get_shared_partials();
let variables = HashMap::from([
("DIFF", diff_content.to_string()),
(
"COMMIT_MESSAGE_XML_PATH",
workspace.absolute_str(".agent/tmp/commit_message.xml"),
),
(
"COMMIT_MESSAGE_XSD_PATH",
workspace.absolute_str(".agent/tmp/commit_message.xsd"),
),
]);
template
.render_with_partials(&variables, &partials)
.unwrap_or_else(|_e| {
format!(
"Generate a conventional commit message for this diff:\n\n{diff_content}\n\n\
Output format: <ralph-commit><ralph-subject>type: description</ralph-subject></ralph-commit>"
)
})
}
pub fn prompt_generate_commit_message_with_diff_with_log(
context: &TemplateContext,
diff: &str,
workspace: &dyn Workspace,
template_name: &str,
) -> RenderedTemplate {
let tmp_dir = std::path::Path::new(".agent/tmp");
let _ = workspace.create_dir_all(tmp_dir);
let _ = workspace.write(
&tmp_dir.join("commit_message.xsd"),
COMMIT_MESSAGE_XSD_SCHEMA,
);
let diff_content = diff.trim();
let has_changes = !diff_content.is_empty();
if !has_changes {
let prompt_content = "ERROR: Empty diff provided. This indicates a bug in the caller - \
meaningful changes should be checked before requesting a commit message."
.to_string();
return RenderedTemplate {
content: prompt_content,
log: SubstitutionLog {
template_name: template_name.to_string(),
substituted: vec![],
unsubstituted: vec![],
},
};
}
let template_content = context
.registry()
.get_template("commit_message_xml")
.unwrap_or_else(|_| include_str!("../templates/commit_message_xml.txt").to_string());
let template = Template::new(&template_content);
let partials = get_shared_partials();
let variables = HashMap::from([
("DIFF", diff_content.to_string()),
(
"COMMIT_MESSAGE_XML_PATH",
workspace.absolute_str(".agent/tmp/commit_message.xml"),
),
(
"COMMIT_MESSAGE_XSD_PATH",
workspace.absolute_str(".agent/tmp/commit_message.xsd"),
),
]);
match template.render_with_log(template_name, &variables, &partials) {
Ok(rendered) => rendered,
Err(_e) => {
let prompt_content = format!(
"Generate a conventional commit message for this diff:\n\n{diff_content}\n\n\
Output format: <ralph-commit><ralph-subject>type: description</ralph-subject></ralph-commit>"
);
RenderedTemplate {
content: prompt_content,
log: SubstitutionLog {
template_name: template_name.to_string(),
substituted: vec![SubstitutionEntry {
name: "DIFF".to_string(),
source: SubstitutionSource::Value,
}],
unsubstituted: vec![],
},
}
}
}
}
pub fn prompt_generate_commit_message_with_diff_with_context(
context: &TemplateContext,
diff: &str,
workspace: &dyn Workspace,
) -> String {
let tmp_dir = std::path::Path::new(".agent/tmp");
let _ = workspace.create_dir_all(tmp_dir);
let _ = workspace.write(
&tmp_dir.join("commit_message.xsd"),
COMMIT_MESSAGE_XSD_SCHEMA,
);
let diff_content = diff.trim();
let has_changes = !diff_content.is_empty();
if !has_changes {
return "ERROR: Empty diff provided. This indicates a bug in the caller - \
meaningful changes should be checked before requesting a commit message."
.to_string();
}
let template_content = context
.registry()
.get_template("commit_message_xml")
.unwrap_or_else(|_| include_str!("../templates/commit_message_xml.txt").to_string());
let template = Template::new(&template_content);
let partials = get_shared_partials();
let variables = HashMap::from([
("DIFF", diff_content.to_string()),
(
"COMMIT_MESSAGE_XML_PATH",
workspace.absolute_str(".agent/tmp/commit_message.xml"),
),
(
"COMMIT_MESSAGE_XSD_PATH",
workspace.absolute_str(".agent/tmp/commit_message.xsd"),
),
]);
template
.render_with_partials(&variables, &partials)
.unwrap_or_else(|_e| {
format!(
"Generate a conventional commit message for this diff:\n\n{diff_content}\n\n\
Output format: <ralph-commit><ralph-subject>type: description</ralph-subject></ralph-commit>"
)
})
}