ui: make status() return a dyn Write, add status_formatter()

When the caller needs a formatter, it's because they're doing
something non-trivial. When the user passed `--quiet` (see upcoming
patch), we should ideally skip doing related work for print the
formatting output. It helps if the `Ui` object doesn't even return a
`Formatter` then, so the caller is forced to handle the quiet case
differently.

Thanks to Yuya for the suggestion.
This commit is contained in:
Martin von Zweigbergk 2024-03-31 09:08:39 -07:00 committed by Martin von Zweigbergk
parent b940f1a092
commit e38e2904bb
12 changed files with 120 additions and 89 deletions

View file

@ -1147,16 +1147,17 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin
new_commit, new_commit,
)?; )?;
if Some(new_commit) != maybe_old_commit { if Some(new_commit) != maybe_old_commit {
let mut formatter = ui.status(); if let Some(mut formatter) = ui.status_formatter() {
let template = self.commit_summary_template(); let template = self.commit_summary_template();
write!(formatter, "Working copy now at: ")?; write!(formatter, "Working copy now at: ")?;
formatter.with_label("working_copy", |fmt| template.format(new_commit, fmt))?; formatter.with_label("working_copy", |fmt| template.format(new_commit, fmt))?;
writeln!(formatter)?;
for parent in new_commit.parents() {
// "Working copy now at: "
write!(formatter, "Parent commit : ")?;
template.format(&parent, formatter.as_mut())?;
writeln!(formatter)?; writeln!(formatter)?;
for parent in new_commit.parents() {
// "Working copy now at: "
write!(formatter, "Parent commit : ")?;
template.format(&parent, formatter.as_mut())?;
writeln!(formatter)?;
}
} }
} }
if let Some(stats) = stats { if let Some(stats) = stats {
@ -1236,6 +1237,9 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin
ui: &mut Ui, ui: &mut Ui,
old_repo: &Arc<ReadonlyRepo>, old_repo: &Arc<ReadonlyRepo>,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let Some(mut fmt) = ui.status_formatter() else {
return Ok(());
};
let old_view = old_repo.view(); let old_view = old_repo.view();
let new_repo = self.repo().as_ref(); let new_repo = self.repo().as_ref();
let new_view = new_repo.view(); let new_view = new_repo.view();
@ -1279,7 +1283,6 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin
.retain(|change_id, _commits| !removed_conflicts_by_change_id.contains_key(change_id)); .retain(|change_id, _commits| !removed_conflicts_by_change_id.contains_key(change_id));
// TODO: Also report new divergence and maybe resolved divergence // TODO: Also report new divergence and maybe resolved divergence
let mut fmt = ui.status();
let template = self.commit_summary_template(); let template = self.commit_summary_template();
if !resolved_conflicts_by_change_id.is_empty() { if !resolved_conflicts_by_change_id.is_empty() {
writeln!( writeln!(
@ -1642,12 +1645,12 @@ pub fn print_trackable_remote_branches(ui: &Ui, view: &View) -> io::Result<()> {
ui.hint_default(), ui.hint_default(),
"The following remote branches aren't associated with the existing local branches:" "The following remote branches aren't associated with the existing local branches:"
)?; )?;
let mut formatter = ui.status(); if let Some(mut formatter) = ui.status_formatter() {
for full_name in &remote_branch_names { for full_name in &remote_branch_names {
write!(formatter, " ")?; write!(formatter, " ")?;
writeln!(formatter.labeled("branch"), "{full_name}")?; writeln!(formatter.labeled("branch"), "{full_name}")?;
}
} }
drop(formatter);
writeln!( writeln!(
ui.hint_default(), ui.hint_default(),
"Run `jj branch track {names}` to keep local branches updated on future pulls.", "Run `jj branch track {names}` to keep local branches updated on future pulls.",

View file

@ -66,28 +66,29 @@ pub(crate) fn cmd_abandon(
} }
let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?; let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?;
if to_abandon.len() == 1 { if let Some(mut formatter) = ui.status_formatter() {
write!(ui.status(), "Abandoned commit ")?; if to_abandon.len() == 1 {
tx.base_workspace_helper() write!(formatter, "Abandoned commit ")?;
.write_commit_summary(ui.status().as_mut(), &to_abandon[0])?; tx.base_workspace_helper()
writeln!(ui.status())?; .write_commit_summary(formatter.as_mut(), &to_abandon[0])?;
} else if !args.summary { writeln!(ui.status())?;
let mut formatter = ui.status(); } else if !args.summary {
let template = tx.base_workspace_helper().commit_summary_template(); let template = tx.base_workspace_helper().commit_summary_template();
writeln!(formatter, "Abandoned the following commits:")?; writeln!(formatter, "Abandoned the following commits:")?;
for commit in &to_abandon { for commit in &to_abandon {
write!(formatter, " ")?; write!(formatter, " ")?;
template.format(commit, formatter.as_mut())?; template.format(commit, formatter.as_mut())?;
writeln!(formatter)?; writeln!(formatter)?;
}
} else {
writeln!(formatter, "Abandoned {} commits.", &to_abandon.len())?;
}
if num_rebased > 0 {
writeln!(
formatter,
"Rebased {num_rebased} descendant commits onto parents of abandoned commits"
)?;
} }
} else {
writeln!(ui.status(), "Abandoned {} commits.", &to_abandon.len())?;
}
if num_rebased > 0 {
writeln!(
ui.status(),
"Rebased {num_rebased} descendant commits onto parents of abandoned commits"
)?;
} }
let transaction_description = if to_abandon.len() == 1 { let transaction_description = if to_abandon.len() == 1 {
format!("abandon commit {}", to_abandon[0].id().hex()) format!("abandon commit {}", to_abandon[0].id().hex())

View file

@ -107,11 +107,13 @@ don't make any changes, then the operation will be aborted.",
// rebase_descendants early; otherwise `new_commit` would always have // rebase_descendants early; otherwise `new_commit` would always have
// a conflicted change id at this point. // a conflicted change id at this point.
let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?; let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?;
write!(ui.status(), "Created ")?; if let Some(mut formatter) = ui.status_formatter() {
tx.write_commit_summary(ui.status().as_mut(), &new_commit)?; write!(formatter, "Created ")?;
writeln!(ui.status())?; tx.write_commit_summary(formatter.as_mut(), &new_commit)?;
if num_rebased > 0 { writeln!(formatter)?;
writeln!(ui.status(), "Rebased {num_rebased} descendant commits")?; if num_rebased > 0 {
writeln!(formatter, "Rebased {num_rebased} descendant commits")?;
}
} }
tx.finish(ui, format!("edit commit {}", target_commit.id().hex()))?; tx.finish(ui, format!("edit commit {}", target_commit.id().hex()))?;
} }

View file

@ -77,10 +77,12 @@ pub(crate) fn cmd_duplicate(
duplicated_old_to_new.insert(original_commit_id, new_commit); duplicated_old_to_new.insert(original_commit_id, new_commit);
} }
for (old_id, new_commit) in &duplicated_old_to_new { if let Some(mut formatter) = ui.status_formatter() {
write!(ui.status(), "Duplicated {} as ", short_commit_hash(old_id))?; for (old_id, new_commit) in &duplicated_old_to_new {
tx.write_commit_summary(ui.status().as_mut(), new_commit)?; write!(formatter, "Duplicated {} as ", short_commit_hash(old_id))?;
writeln!(ui.status())?; tx.write_commit_summary(formatter.as_mut(), new_commit)?;
writeln!(formatter)?;
}
} }
tx.finish(ui, format!("duplicate {} commit(s)", to_duplicate.len()))?; tx.finish(ui, format!("duplicate {} commit(s)", to_duplicate.len()))?;
Ok(()) Ok(())

View file

@ -189,9 +189,11 @@ Please use `jj new 'all:x|y'` instead of `jj new --allow-large-revsets x y`.",
} }
num_rebased += tx.mut_repo().rebase_descendants(command.settings())?; num_rebased += tx.mut_repo().rebase_descendants(command.settings())?;
if args.no_edit { if args.no_edit {
write!(ui.status(), "Created new commit ")?; if let Some(mut formatter) = ui.status_formatter() {
tx.write_commit_summary(ui.status().as_mut(), &new_commit)?; write!(formatter, "Created new commit ")?;
writeln!(ui.status())?; tx.write_commit_summary(formatter.as_mut(), &new_commit)?;
writeln!(formatter)?;
}
} else { } else {
tx.edit(&new_commit).unwrap(); tx.edit(&new_commit).unwrap();
// The description of the new commit will be printed by tx.finish() // The description of the new commit will be printed by tx.finish()

View file

@ -33,6 +33,7 @@ use crate::cli_util::{
RevisionArg, WorkspaceCommandHelper, RevisionArg, WorkspaceCommandHelper,
}; };
use crate::command_error::{user_error, CommandError}; use crate::command_error::{user_error, CommandError};
use crate::formatter::Formatter;
use crate::ui::Ui; use crate::ui::Ui;
/// Move revisions to different parent(s) /// Move revisions to different parent(s)
@ -295,7 +296,9 @@ fn rebase_descendants(
.iter() .iter()
.partition::<Vec<_>, _>(|commit| commit.parents() == new_parents); .partition::<Vec<_>, _>(|commit| commit.parents() == new_parents);
if !skipped_commits.is_empty() { if !skipped_commits.is_empty() {
log_skipped_rebase_commits_message(ui, workspace_command, &skipped_commits)?; if let Some(mut fmt) = ui.status_formatter() {
log_skipped_rebase_commits_message(fmt.as_mut(), workspace_command, &skipped_commits)?;
}
} }
if old_commits.is_empty() { if old_commits.is_empty() {
return Ok(()); return Ok(());
@ -447,9 +450,11 @@ fn rebase_revision(
// longer have any children; they have all been rebased and the originals // longer have any children; they have all been rebased and the originals
// have been abandoned. // have been abandoned.
let skipped_commit_rebase = if old_commit.parents() == new_parents { let skipped_commit_rebase = if old_commit.parents() == new_parents {
write!(ui.status(), "Skipping rebase of commit ")?; if let Some(mut formatter) = ui.status_formatter() {
tx.write_commit_summary(ui.status().as_mut(), &old_commit)?; write!(formatter, "Skipping rebase of commit ")?;
writeln!(ui.status())?; tx.write_commit_summary(formatter.as_mut(), &old_commit)?;
writeln!(formatter)?;
}
true true
} else { } else {
rebase_commit(settings, tx.mut_repo(), &old_commit, &new_parents)?; rebase_commit(settings, tx.mut_repo(), &old_commit, &new_parents)?;
@ -496,21 +501,20 @@ fn check_rebase_destinations(
} }
fn log_skipped_rebase_commits_message( fn log_skipped_rebase_commits_message(
ui: &Ui, fmt: &mut dyn Formatter,
workspace_command: &WorkspaceCommandHelper, workspace_command: &WorkspaceCommandHelper,
commits: &[&Commit], commits: &[&Commit],
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let mut fmt = ui.status();
let template = workspace_command.commit_summary_template(); let template = workspace_command.commit_summary_template();
if commits.len() == 1 { if commits.len() == 1 {
write!(fmt, "Skipping rebase of commit ")?; write!(fmt, "Skipping rebase of commit ")?;
template.format(commits[0], fmt.as_mut())?; template.format(commits[0], fmt)?;
writeln!(fmt)?; writeln!(fmt)?;
} else { } else {
writeln!(fmt, "Skipping rebase of commits:")?; writeln!(fmt, "Skipping rebase of commits:")?;
for commit in commits { for commit in commits {
write!(fmt, " ")?; write!(fmt, " ")?;
template.format(commit, fmt.as_mut())?; template.format(commit, fmt)?;
writeln!(fmt)?; writeln!(fmt)?;
} }
} }

View file

@ -115,15 +115,18 @@ pub(crate) fn cmd_resolve(
format!("Resolve conflicts in commit {}", commit.id().hex()), format!("Resolve conflicts in commit {}", commit.id().hex()),
)?; )?;
// TODO: Delete local `--quiet`
if !args.quiet { if !args.quiet {
let new_tree = new_commit.tree()?; if let Some(mut formatter) = ui.status_formatter() {
let new_conflicts = new_tree.conflicts().collect_vec(); let new_tree = new_commit.tree()?;
if !new_conflicts.is_empty() { let new_conflicts = new_tree.conflicts().collect_vec();
writeln!( if !new_conflicts.is_empty() {
ui.status(), writeln!(
"After this operation, some files at this revision still have conflicts:" formatter,
)?; "After this operation, some files at this revision still have conflicts:"
print_conflicted_paths(&new_conflicts, ui.status().as_mut(), &workspace_command)?; )?;
print_conflicted_paths(&new_conflicts, formatter.as_mut(), &workspace_command)?;
}
} }
}; };
Ok(()) Ok(())

View file

@ -112,11 +112,13 @@ pub(crate) fn cmd_restore(
// rebase_descendants early; otherwise `new_commit` would always have // rebase_descendants early; otherwise `new_commit` would always have
// a conflicted change id at this point. // a conflicted change id at this point.
let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?; let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?;
write!(ui.status(), "Created ")?; if let Some(mut formatter) = ui.status_formatter() {
tx.write_commit_summary(ui.status().as_mut(), &new_commit)?; write!(formatter, "Created ")?;
writeln!(ui.status())?; tx.write_commit_summary(formatter.as_mut(), &new_commit)?;
if num_rebased > 0 { writeln!(formatter)?;
writeln!(ui.status(), "Rebased {num_rebased} descendant commits")?; if num_rebased > 0 {
writeln!(formatter, "Rebased {num_rebased} descendant commits")?;
}
} }
tx.finish(ui, format!("restore into commit {}", to_commit.id().hex()))?; tx.finish(ui, format!("restore into commit {}", to_commit.id().hex()))?;
} }

View file

@ -149,14 +149,16 @@ don't make any changes, then the operation will be aborted.
tx.mut_repo() tx.mut_repo()
.set_rewritten_commit(commit.id().clone(), second_commit.id().clone()); .set_rewritten_commit(commit.id().clone(), second_commit.id().clone());
let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?; let num_rebased = tx.mut_repo().rebase_descendants(command.settings())?;
if num_rebased > 0 { if let Some(mut formatter) = ui.status_formatter() {
writeln!(ui.status(), "Rebased {num_rebased} descendant commits")?; if num_rebased > 0 {
writeln!(formatter, "Rebased {num_rebased} descendant commits")?;
}
write!(formatter, "First part: ")?;
tx.write_commit_summary(formatter.as_mut(), &first_commit)?;
write!(formatter, "\nSecond part: ")?;
tx.write_commit_summary(formatter.as_mut(), &second_commit)?;
writeln!(formatter)?;
} }
write!(ui.status(), "First part: ")?;
tx.write_commit_summary(ui.status().as_mut(), &first_commit)?;
write!(ui.status(), "\nSecond part: ")?;
tx.write_commit_summary(ui.status().as_mut(), &second_commit)?;
writeln!(ui.status())?;
tx.finish(ui, format!("split commit {}", commit.id().hex()))?; tx.finish(ui, format!("split commit {}", commit.id().hex()))?;
Ok(()) Ok(())
} }

View file

@ -424,11 +424,13 @@ fn cmd_workspace_update_stale(
) )
})?; })?;
locked_ws.finish(repo.op_id().clone())?; locked_ws.finish(repo.op_id().clone())?;
write!(ui.status(), "Working copy now at: ")?; if let Some(mut formatter) = ui.status_formatter() {
ui.status().with_label("working_copy", |fmt| { write!(formatter, "Working copy now at: ")?;
workspace_command.write_commit_summary(fmt, &desired_wc_commit) formatter.with_label("working_copy", |fmt| {
})?; workspace_command.write_commit_summary(fmt, &desired_wc_commit)
writeln!(ui.status())?; })?;
writeln!(formatter)?;
}
print_checkout_stats(ui, stats, &desired_wc_commit)?; print_checkout_stats(ui, stats, &desired_wc_commit)?;
} }
} }

View file

@ -256,6 +256,9 @@ pub fn print_git_import_stats(
stats: &GitImportStats, stats: &GitImportStats,
show_ref_stats: bool, show_ref_stats: bool,
) -> Result<(), CommandError> { ) -> Result<(), CommandError> {
let Some(mut formatter) = ui.status_formatter() else {
return Ok(());
};
if show_ref_stats { if show_ref_stats {
let refs_stats = stats let refs_stats = stats
.changed_remote_refs .changed_remote_refs
@ -274,7 +277,6 @@ pub fn print_git_import_stats(
let max_width = refs_stats.iter().map(|x| x.ref_name.width()).max(); let max_width = refs_stats.iter().map(|x| x.ref_name.width()).max();
if let Some(max_width) = max_width { if let Some(max_width) = max_width {
let mut formatter = ui.status();
for status in refs_stats { for status in refs_stats {
status.output(max_width, has_both_ref_kinds, &mut *formatter)?; status.output(max_width, has_both_ref_kinds, &mut *formatter)?;
} }
@ -283,7 +285,7 @@ pub fn print_git_import_stats(
if !stats.abandoned_commits.is_empty() { if !stats.abandoned_commits.is_empty() {
writeln!( writeln!(
ui.status(), formatter,
"Abandoned {} commits that are no longer reachable.", "Abandoned {} commits that are no longer reachable.",
stats.abandoned_commits.len() stats.abandoned_commits.len()
)?; )?;

View file

@ -373,10 +373,16 @@ impl Ui {
}) })
} }
/// Writes a message that's a status update not part of the command's main /// Writer to print an update that's not part of the command's main output.
/// output. pub fn status(&self) -> Box<dyn Write + '_> {
pub fn status(&self) -> Box<dyn Formatter + '_> { Box::new(self.stderr())
self.stderr_formatter() }
/// A formatter to print an update that's not part of the command's main
/// output. Returns `None` if `--quiet` was requested.
// TODO: Actually support `--quiet`
pub fn status_formatter(&self) -> Option<Box<dyn Formatter + '_>> {
Some(self.stderr_formatter())
} }
/// Writer to print hint with the default "Hint: " heading. /// Writer to print hint with the default "Hint: " heading.
@ -388,7 +394,7 @@ impl Ui {
/// Writer to print hint without the "Hint: " heading. /// Writer to print hint without the "Hint: " heading.
pub fn hint_no_heading(&self) -> LabeledWriter<Box<dyn Formatter + '_>, &'static str> { pub fn hint_no_heading(&self) -> LabeledWriter<Box<dyn Formatter + '_>, &'static str> {
LabeledWriter::new(self.status(), "hint") LabeledWriter::new(self.stderr_formatter(), "hint")
} }
/// Writer to print hint with the given heading. /// Writer to print hint with the given heading.
@ -396,7 +402,7 @@ impl Ui {
&self, &self,
heading: H, heading: H,
) -> HeadingLabeledWriter<Box<dyn Formatter + '_>, &'static str, H> { ) -> HeadingLabeledWriter<Box<dyn Formatter + '_>, &'static str, H> {
HeadingLabeledWriter::new(self.status(), "hint", heading) HeadingLabeledWriter::new(self.stderr_formatter(), "hint", heading)
} }
/// Writer to print warning with the default "Warning: " heading. /// Writer to print warning with the default "Warning: " heading.