Skip to content

Commit

Permalink
cli: reuse commit summary template and formatter in a loop
Browse files Browse the repository at this point in the history
The performance wouldn't matter, but the new code doesn't look bad either.
  • Loading branch information
yuja committed Mar 5, 2024
1 parent 749ef45 commit 59a61a2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
31 changes: 19 additions & 12 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,12 +906,16 @@ Set which revision the branch points to with `jj branch set {branch_name} -r <RE
self.parse_template(&language, template_text)
}

fn commit_summary_template(&self) -> Box<dyn Template<Commit> + '_> {
/// Template for one-line summary of a commit.
pub fn commit_summary_template(&self) -> Box<dyn Template<Commit> + '_> {
self.parse_commit_template(&self.commit_summary_template_text)
.expect("parse error should be confined by WorkspaceCommandHelper::new()")
}

/// Returns one-line summary of the given `commit`.
///
/// Use `write_commit_summary()` to get colorized output. Use
/// `commit_summary_template()` if you have many commits to process.
pub fn format_commit_summary(&self, commit: &Commit) -> String {
let mut output = Vec::new();
self.write_commit_summary(&mut PlainTextFormatter::new(&mut output), commit)
Expand All @@ -920,6 +924,8 @@ Set which revision the branch points to with `jj branch set {branch_name} -r <RE
}

/// Writes one-line summary of the given `commit`.
///
/// Use `commit_summary_template()` if you have many commits to process.
#[instrument(skip_all)]
pub fn write_commit_summary(
&self,
Expand Down Expand Up @@ -1098,16 +1104,16 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin
new_commit,
)?;
if Some(new_commit) != maybe_old_commit {
write!(ui.stderr(), "Working copy now at: ")?;
ui.stderr_formatter().with_label("working_copy", |fmt| {
self.write_commit_summary(fmt, new_commit)
})?;
writeln!(ui.stderr())?;
let mut formatter = ui.stderr_formatter();
let template = self.commit_summary_template();
write!(formatter, "Working copy now at: ")?;
formatter.with_label("working_copy", |fmt| template.format(new_commit, fmt))?;
writeln!(formatter)?;
for parent in new_commit.parents() {
// "Working copy now at: "
write!(ui.stderr(), "Parent commit : ")?;
self.write_commit_summary(ui.stderr_formatter().as_mut(), &parent)?;
writeln!(ui.stderr())?;
// "Working copy now at: "
write!(formatter, "Parent commit : ")?;
template.format(&parent, formatter.as_mut())?;
writeln!(formatter)?;
}
}
if let Some(stats) = stats {
Expand Down Expand Up @@ -1231,6 +1237,7 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin

// TODO: Also report new divergence and maybe resolved divergence
let mut fmt = ui.stderr_formatter();
let template = self.commit_summary_template();
if !resolved_conflicts_by_change_id.is_empty() {
writeln!(
fmt,
Expand All @@ -1242,7 +1249,7 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin
// repo, which isn't currently supported by Google's revset engine.
for commit in old_commits {
write!(fmt, " ")?;
self.write_commit_summary(fmt.as_mut(), commit)?;
template.format(commit, fmt.as_mut())?;
writeln!(fmt)?;
}
}
Expand All @@ -1252,7 +1259,7 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin
for (_, new_commits) in &new_conflicts_by_change_id {
for commit in new_commits {
write!(fmt, " ")?;
self.write_commit_summary(fmt.as_mut(), commit)?;
template.format(commit, fmt.as_mut())?;
writeln!(fmt)?;
}
}
Expand Down
11 changes: 6 additions & 5 deletions cli/src/commands/abandon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ pub(crate) fn cmd_abandon(
.write_commit_summary(ui.stderr_formatter().as_mut(), &to_abandon[0])?;
writeln!(ui.stderr())?;
} else if !args.summary {
writeln!(ui.stderr(), "Abandoned the following commits:")?;
let mut formatter = ui.stderr_formatter();
let template = tx.base_workspace_helper().commit_summary_template();
writeln!(formatter, "Abandoned the following commits:")?;
for commit in &to_abandon {
write!(ui.stderr(), " ")?;
tx.base_workspace_helper()
.write_commit_summary(ui.stderr_formatter().as_mut(), commit)?;
writeln!(ui.stderr())?;
write!(formatter, " ")?;
template.format(commit, formatter.as_mut())?;
writeln!(formatter)?;
}
} else {
writeln!(ui.stderr(), "Abandoned {} commits.", &to_abandon.len())?;
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ pub fn choose_commit<'a>(
) -> Result<&'a Commit, CommandError> {
writeln!(ui.stdout(), "ambiguous {cmd} commit, choose one to target:")?;
let mut formatter = ui.stdout_formatter();
let template = workspace_command.commit_summary_template();
let mut choices: Vec<String> = Default::default();
for (i, commit) in commits.iter().enumerate() {
write!(formatter, "{}: ", i + 1)?;
workspace_command.write_commit_summary(formatter.as_mut(), commit)?;
template.format(commit, formatter.as_mut())?;
writeln!(formatter)?;
choices.push(format!("{}", i + 1));
}
Expand Down
7 changes: 3 additions & 4 deletions cli/src/commands/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ pub(crate) fn cmd_status(
resolve::print_conflicted_paths(&conflicts, formatter, &workspace_command)?
}

let template = workspace_command.commit_summary_template();
formatter.write_str("Working copy : ")?;
formatter.with_label("working_copy", |fmt| {
workspace_command.write_commit_summary(fmt, wc_commit)
})?;
formatter.with_label("working_copy", |fmt| template.format(wc_commit, fmt))?;
formatter.write_str("\n")?;
for parent in wc_commit.parents() {
formatter.write_str("Parent commit: ")?;
workspace_command.write_commit_summary(formatter, &parent)?;
template.format(&parent, formatter)?;
formatter.write_str("\n")?;
}
} else {
Expand Down
8 changes: 5 additions & 3 deletions cli/src/commands/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,13 @@ fn cmd_workspace_list(
) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
let mut formatter = ui.stdout_formatter();
let template = workspace_command.commit_summary_template();
for (workspace_id, wc_commit_id) in repo.view().wc_commit_ids().iter().sorted() {
write!(ui.stdout(), "{}: ", workspace_id.as_str())?;
write!(formatter, "{}: ", workspace_id.as_str())?;
let commit = repo.store().get_commit(wc_commit_id)?;
workspace_command.write_commit_summary(ui.stdout_formatter().as_mut(), &commit)?;
writeln!(ui.stdout())?;
template.format(&commit, formatter.as_mut())?;
writeln!(formatter)?;
}
Ok(())
}
Expand Down

0 comments on commit 59a61a2

Please sign in to comment.