Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli_util: short-prefixes for commit summary in transaction #4046

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,25 +999,28 @@ impl WorkspaceCommandHelper {
)
}

fn new_id_prefix_context(&self) -> Result<IdPrefixContext, CommandError> {
let mut context: IdPrefixContext = IdPrefixContext::new(self.revset_extensions.clone());
let revset_string: String = self
.settings
.config()
.get_string("revsets.short-prefixes")
.unwrap_or_else(|_| self.settings.default_revset());
if !revset_string.is_empty() {
let (expression, modifier) =
revset::parse_with_modifier(&revset_string, &self.revset_parse_context()).map_err(
|err| config_error_with_message("Invalid `revsets.short-prefixes`", err),
)?;
let (None | Some(RevsetModifier::All)) = modifier;
context = context.disambiguate_within(revset::optimize(expression));
}
Ok(context)
}

pub fn id_prefix_context(&self) -> Result<&IdPrefixContext, CommandError> {
self.user_repo.id_prefix_context.get_or_try_init(|| {
let mut context: IdPrefixContext = IdPrefixContext::new(self.revset_extensions.clone());
let revset_string: String = self
.settings
.config()
.get_string("revsets.short-prefixes")
.unwrap_or_else(|_| self.settings.default_revset());
if !revset_string.is_empty() {
let (expression, modifier) =
revset::parse_with_modifier(&revset_string, &self.revset_parse_context())
.map_err(|err| {
config_error_with_message("Invalid `revsets.short-prefixes`", err)
})?;
let (None | Some(RevsetModifier::All)) = modifier;
context = context.disambiguate_within(revset::optimize(expression));
}
Ok(context)
})
self.user_repo
.id_prefix_context
.get_or_try_init(|| self.new_id_prefix_context())
}

pub fn template_aliases_map(&self) -> &TemplateAliasesMap {
Expand Down Expand Up @@ -1642,8 +1645,10 @@ impl WorkspaceCommandTransaction<'_> {
formatter: &mut dyn Formatter,
commit: &Commit,
) -> std::io::Result<()> {
// TODO: Use the disambiguation revset
let id_prefix_context = IdPrefixContext::new(self.helper.revset_extensions.clone());
let id_prefix_context = self
.helper
.new_id_prefix_context()
.expect("parse error should be confined by WorkspaceCommandHelper::new()");
let language = CommitTemplateLanguage::new(
self.tx.repo(),
self.helper.workspace_id(),
Expand Down
67 changes: 67 additions & 0 deletions cli/tests/test_commit_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,3 +870,70 @@ fn test_log_contained_in() {
Hint: Did you mean "main"?
"###);
}

#[test]
fn test_short_prefix_in_transaction() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

test_env.add_config(r#"
[revsets]
log = '::description(test)'

[templates]
log = 'summary ++ "\n"'
commit_summary = 'summary'

[template-aliases]
'format_id(id)' = 'id.shortest(12).prefix() ++ "[" ++ id.shortest(12).rest() ++ "]"'
'summary' = 'separate(" ", format_id(change_id), format_id(commit_id), description.first_line())'
"#);

std::fs::write(repo_path.join("file"), "original file\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "initial"]);

// Create a chain of 5 commits
for i in 0..5 {
test_env.jj_cmd_ok(&repo_path, &["new", "-m", &format!("commit{i}")]);
std::fs::write(repo_path.join("file"), format!("file {i}\n")).unwrap();
}
// Create 2^4 duplicates of the chain
for _ in 0..4 {
test_env.jj_cmd_ok(&repo_path, &["duplicate", "description(commit)"]);
}

// Short prefix should be used for commit summary inside the transaction
let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["new", "--no-edit", "-m", "test"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Created new commit km[kuslswpqwq] 7[4ac55dd119b] test
"###);

// Should match log's short prefixes
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "--no-graph"]);
insta::assert_snapshot!(stdout, @r###"
km[kuslswpqwq] 7[4ac55dd119b] test
y[qosqzytrlsw] 5[8731db5875e] commit4
r[oyxmykxtrkr] 9[95cc897bca7] commit3
m[zvwutvlkqwt] 3[74534c54448] commit2
zs[uskulnrvyr] d[e304c281bed] commit1
kk[mpptxzrspx] 05[2755155952] commit0
q[pvuntsmwlqt] e[0e22b9fae75] initial
zz[zzzzzzzzzz] 00[0000000000]
"###);

test_env.add_config(r#"revsets.short-prefixes = """#);

let stdout = test_env.jj_cmd_success(&repo_path, &["log", "--no-graph"]);
insta::assert_snapshot!(stdout, @r###"
kmk[uslswpqwq] 74ac[55dd119b] test
yq[osqzytrlsw] 587[31db5875e] commit4
ro[yxmykxtrkr] 99[5cc897bca7] commit3
mz[vwutvlkqwt] 374[534c54448] commit2
zs[uskulnrvyr] de[304c281bed] commit1
kk[mpptxzrspx] 052[755155952] commit0
qp[vuntsmwlqt] e0[e22b9fae75] initial
zz[zzzzzzzzzz] 00[0000000000]
"###);
}