just playing with tangled
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cli: proxy write_commit_summary() of immutable context through workspace helper

I'm going to add 'format_commit_summary(commit) -> String' to replace
short_commit_description(), and it's fundamentally the same as
'write_commit_summary()' except where the output will go. The problem of
adding such method to WorkspaceCommandHelper is that it's super easy to
use wrong repo ref while transaction is in progress. This problem could
be avoided by passing repo ref explicitly, but other methods like
resolve_revset() have the same problem.

One idea to prevent such API misuse is to exclusively borrow the helper:
'start_transaction(&'a mut self) -> Wrapper<'a>'. That's doable, but I'm
not certain that it is the right way to go. Anyway, this isn't the problem
only applies to write_commit_summary(), so I decided to add a convenient
wrapper to WorkspaceCommandHelper.

+29 -53
+20
src/cli_util.rs
··· 726 726 } 727 727 } 728 728 729 + // TODO: Any methods that depend on self.repo aren't aware of a mutable repo 730 + // while transaction is in progress. That's fine if the caller expects to 731 + // operate on tx.base_repo(), but WorkspaceCommandHelper API doesn't clarify 732 + // that. 733 + 734 + /// Writes one-line summary of the given `commit`. 735 + pub fn write_commit_summary( 736 + &self, 737 + formatter: &mut dyn Formatter, 738 + commit: &Commit, 739 + ) -> std::io::Result<()> { 740 + write_commit_summary( 741 + formatter, 742 + self.repo.as_repo_ref(), 743 + self.workspace.workspace_id(), 744 + commit, 745 + &self.settings, 746 + ) 747 + } 748 + 729 749 pub fn check_rewriteable(&self, commit: &Commit) -> Result<(), CommandError> { 730 750 if commit.id() == self.repo.store().root_commit_id() { 731 751 return Err(user_error("Cannot rewrite the root commit"));
+9 -53
src/commands.rs
··· 1495 1495 let mut formatter = ui.stdout_formatter(); 1496 1496 let formatter = formatter.as_mut(); 1497 1497 if let Some(wc_commit) = &maybe_checkout { 1498 - let workspace_id = workspace_command.workspace_id(); 1499 1498 for parent in wc_commit.parents() { 1500 1499 formatter.write_str("Parent commit: ")?; 1501 - write_commit_summary( 1502 - formatter, 1503 - repo.as_repo_ref(), 1504 - &workspace_id, 1505 - &parent, 1506 - command.settings(), 1507 - )?; 1500 + workspace_command.write_commit_summary(formatter, &parent)?; 1508 1501 formatter.write_str("\n")?; 1509 1502 } 1510 1503 formatter.write_str("Working copy : ")?; 1511 - write_commit_summary( 1512 - formatter, 1513 - repo.as_repo_ref(), 1514 - &workspace_id, 1515 - wc_commit, 1516 - command.settings(), 1517 - )?; 1504 + workspace_command.write_commit_summary(formatter, wc_commit)?; 1518 1505 formatter.write_str("\n")?; 1519 1506 } else { 1520 1507 formatter.write_str("No working copy\n")?; ··· 3257 3244 3258 3245 fn list_branches( 3259 3246 ui: &mut Ui, 3260 - command: &CommandHelper, 3247 + _command: &CommandHelper, 3261 3248 workspace_command: &WorkspaceCommandHelper, 3262 3249 ) -> Result<(), CommandError> { 3263 3250 let repo = workspace_command.repo(); 3264 3251 3265 - let workspace_id = workspace_command.workspace_id(); 3266 3252 let print_branch_target = 3267 3253 |formatter: &mut dyn Formatter, target: Option<&RefTarget>| -> Result<(), CommandError> { 3268 3254 match target { 3269 3255 Some(RefTarget::Normal(id)) => { 3270 3256 write!(formatter, ": ")?; 3271 3257 let commit = repo.store().get_commit(id)?; 3272 - write_commit_summary( 3273 - formatter, 3274 - repo.as_repo_ref(), 3275 - &workspace_id, 3276 - &commit, 3277 - command.settings(), 3278 - )?; 3258 + workspace_command.write_commit_summary(formatter, &commit)?; 3279 3259 writeln!(formatter)?; 3280 3260 } 3281 3261 Some(RefTarget::Conflict { adds, removes }) => { ··· 3285 3265 for id in removes { 3286 3266 let commit = repo.store().get_commit(id)?; 3287 3267 write!(formatter, " - ")?; 3288 - write_commit_summary( 3289 - formatter, 3290 - repo.as_repo_ref(), 3291 - &workspace_id, 3292 - &commit, 3293 - command.settings(), 3294 - )?; 3268 + workspace_command.write_commit_summary(formatter, &commit)?; 3295 3269 writeln!(formatter)?; 3296 3270 } 3297 3271 for id in adds { 3298 3272 let commit = repo.store().get_commit(id)?; 3299 3273 write!(formatter, " + ")?; 3300 - write_commit_summary( 3301 - formatter, 3302 - repo.as_repo_ref(), 3303 - &workspace_id, 3304 - &commit, 3305 - command.settings(), 3306 - )?; 3274 + workspace_command.write_commit_summary(formatter, &commit)?; 3307 3275 writeln!(formatter)?; 3308 3276 } 3309 3277 } ··· 3734 3702 for (workspace_id, checkout_id) in repo.view().wc_commit_ids().iter().sorted() { 3735 3703 write!(ui, "{}: ", workspace_id.as_str())?; 3736 3704 let commit = repo.store().get_commit(checkout_id)?; 3737 - write_commit_summary( 3738 - ui.stdout_formatter().as_mut(), 3739 - repo.as_repo_ref(), 3740 - &workspace_command.workspace_id(), 3741 - &commit, 3742 - command.settings(), 3743 - )?; 3705 + workspace_command.write_commit_summary(ui.stdout_formatter().as_mut(), &commit)?; 3744 3706 writeln!(ui)?; 3745 3707 } 3746 3708 Ok(()) ··· 3754 3716 let workspace = command.load_workspace()?; 3755 3717 let mut workspace_command = command.resolve_operation(ui, workspace)?; 3756 3718 let repo = workspace_command.repo().clone(); 3757 - let workspace_id = workspace_command.workspace_id(); 3758 3719 let (mut locked_wc, desired_wc_commit) = 3759 3720 workspace_command.unsafe_start_working_copy_mutation()?; 3760 3721 match check_stale_working_copy(&locked_wc, &desired_wc_commit, repo.clone()) { ··· 3775 3736 })?; 3776 3737 locked_wc.finish(repo.op_id().clone()); 3777 3738 ui.write("Working copy now at: ")?; 3778 - write_commit_summary( 3779 - ui.stdout_formatter().as_mut(), 3780 - repo.as_repo_ref(), 3781 - &workspace_id, 3782 - &desired_wc_commit, 3783 - command.settings(), 3784 - )?; 3739 + workspace_command 3740 + .write_commit_summary(ui.stdout_formatter().as_mut(), &desired_wc_commit)?; 3785 3741 ui.write("\n")?; 3786 3742 print_checkout_stats(ui, stats)?; 3787 3743 }