Skip to content

Commit

Permalink
cli: preserve RevisionArg type as much as possible
Browse files Browse the repository at this point in the history
Just for a bit of type safety.
  • Loading branch information
yuja committed Apr 3, 2024
1 parent 3afb51b commit 8b73d7a
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn cmd_branch_create(
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let target_commit =
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"))?;
workspace_command.resolve_single_rev(args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
let view = workspace_command.repo().view();
let branch_names = &args.names;
if let Some(branch_name) = branch_names
Expand Down Expand Up @@ -341,7 +341,7 @@ fn cmd_branch_set(
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let target_commit =
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"))?;
workspace_command.resolve_single_rev(args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
let repo = workspace_command.repo().as_ref();
let is_fast_forward = |old_target: &RefTarget| {
// Strictly speaking, "all" old targets should be ancestors, but we allow
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ fn cmd_debug_tree(
let tree = store.get_tree(&dir, &tree_id)?;
MergedTree::resolved(tree)
} else {
let commit =
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"))?;
let commit = workspace_command
.resolve_single_rev(args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
commit.tree()?
};
let matcher = workspace_command.matcher_from_values(&args.paths)?;
Expand Down
10 changes: 6 additions & 4 deletions cli/src/commands/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ pub(crate) fn cmd_diff(
let from_tree;
let to_tree;
if args.from.is_some() || args.to.is_some() {
let from = workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"))?;
let from =
workspace_command.resolve_single_rev(args.from.as_ref().unwrap_or(&RevisionArg::AT))?;
from_tree = from.tree()?;
let to = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"))?;
let to =
workspace_command.resolve_single_rev(args.to.as_ref().unwrap_or(&RevisionArg::AT))?;
to_tree = to.tree()?;
} else {
let commit =
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"))?;
let commit = workspace_command
.resolve_single_rev(args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
let parents = commit.parents();
from_tree = merge_commit_trees(workspace_command.repo().as_ref(), &parents)?;
to_tree = commit.tree()?
Expand Down
10 changes: 6 additions & 4 deletions cli/src/commands/diffedit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ pub(crate) fn cmd_diffedit(

let (target_commit, base_commits, diff_description);
if args.from.is_some() || args.to.is_some() {
target_commit = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"))?;
target_commit =
workspace_command.resolve_single_rev(args.to.as_ref().unwrap_or(&RevisionArg::AT))?;
base_commits =
vec![workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"))?];
vec![workspace_command
.resolve_single_rev(args.from.as_ref().unwrap_or(&RevisionArg::AT))?];
diff_description = format!(
"The diff initially shows the commit's changes relative to:\n{}",
workspace_command.format_commit_summary(&base_commits[0])
);
} else {
target_commit =
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"))?;
target_commit = workspace_command
.resolve_single_rev(args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
base_commits = target_commit.parents();
diff_description = "The diff initially shows the commit's changes.".to_string();
};
Expand Down
5 changes: 3 additions & 2 deletions cli/src/commands/interdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ pub(crate) fn cmd_interdiff(
args: &InterdiffArgs,
) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?;
let from = workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"))?;
let to = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"))?;
let from =
workspace_command.resolve_single_rev(args.from.as_ref().unwrap_or(&RevisionArg::AT))?;
let to = workspace_command.resolve_single_rev(args.to.as_ref().unwrap_or(&RevisionArg::AT))?;

let from_tree = rebase_to_dest_parent(workspace_command.repo().as_ref(), &from, &to)?;
let to_tree = to.tree()?;
Expand Down
6 changes: 4 additions & 2 deletions cli/src/commands/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ pub(crate) fn cmd_move(
"`jj move` will be removed in a future version, and this will be a hard error"
)?;
let mut workspace_command = command.workspace_helper(ui)?;
let source = workspace_command.resolve_single_rev(args.from.as_deref().unwrap_or("@"))?;
let destination = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"))?;
let source =
workspace_command.resolve_single_rev(args.from.as_ref().unwrap_or(&RevisionArg::AT))?;
let destination =
workspace_command.resolve_single_rev(args.to.as_ref().unwrap_or(&RevisionArg::AT))?;
if source.id() == destination.id() {
return Err(user_error("Source and destination cannot be the same."));
}
Expand Down
10 changes: 5 additions & 5 deletions cli/src/commands/rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Please use `jj rebase -d 'all:x|y'` instead of `jj rebase --allow-large-revsets
.resolve_some_revsets_default_single(&args.destination)?
.into_iter()
.collect_vec();
if let Some(rev_str) = &args.revision {
if let Some(rev_arg) = &args.revision {
assert_eq!(
// In principle, `-r --skip-empty` could mean to abandon the `-r`
// commit if it becomes empty. This seems internally consistent with
Expand All @@ -218,7 +218,7 @@ Please use `jj rebase -d 'all:x|y'` instead of `jj rebase --allow-large-revsets
command.settings(),
&mut workspace_command,
&new_parents,
rev_str,
rev_arg,
)?;
} else if !args.source.is_empty() {
let source_commits = workspace_command.resolve_some_revsets_default_single(&args.source)?;
Expand All @@ -232,7 +232,7 @@ Please use `jj rebase -d 'all:x|y'` instead of `jj rebase --allow-large-revsets
)?;
} else {
let branch_commits = if args.branch.is_empty() {
IndexSet::from([workspace_command.resolve_single_rev("@")?])
IndexSet::from([workspace_command.resolve_single_rev(&RevisionArg::AT)?])
} else {
workspace_command.resolve_some_revsets_default_single(&args.branch)?
};
Expand Down Expand Up @@ -350,9 +350,9 @@ fn rebase_revision(
settings: &UserSettings,
workspace_command: &mut WorkspaceCommandHelper,
new_parents: &[Commit],
rev_str: &str,
rev_arg: &RevisionArg,
) -> Result<(), CommandError> {
let old_commit = workspace_command.resolve_single_rev(rev_str)?;
let old_commit = workspace_command.resolve_single_rev(rev_arg)?;
workspace_command.check_rewritable([&old_commit])?;
if new_parents.contains(&old_commit) {
return Err(user_error(format!(
Expand Down
9 changes: 5 additions & 4 deletions cli/src/commands/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ pub(crate) fn cmd_restore(
));
}
if args.from.is_some() || args.to.is_some() {
to_commit = workspace_command.resolve_single_rev(args.to.as_deref().unwrap_or("@"))?;
to_commit =
workspace_command.resolve_single_rev(args.to.as_ref().unwrap_or(&RevisionArg::AT))?;
from_tree = workspace_command
.resolve_single_rev(args.from.as_deref().unwrap_or("@"))?
.resolve_single_rev(args.from.as_ref().unwrap_or(&RevisionArg::AT))?
.tree()?;
} else {
to_commit =
workspace_command.resolve_single_rev(args.changes_in.as_deref().unwrap_or("@"))?;
to_commit = workspace_command
.resolve_single_rev(args.changes_in.as_ref().unwrap_or(&RevisionArg::AT))?;
from_tree = merge_commit_trees(workspace_command.repo().as_ref(), &to_commit.parents())?;
}
workspace_command.check_rewritable([&to_commit])?;
Expand Down
9 changes: 5 additions & 4 deletions cli/src/commands/squash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ pub(crate) fn cmd_squash(
let destination;
if args.from.is_some() || args.into.is_some() {
sources = workspace_command
.parse_revset(args.from.as_deref().unwrap_or("@"))?
.parse_revset(args.from.as_ref().unwrap_or(&RevisionArg::AT))?
.evaluate_to_commits()?
.try_collect()?;
destination = workspace_command.resolve_single_rev(args.into.as_deref().unwrap_or("@"))?;
destination =
workspace_command.resolve_single_rev(args.into.as_ref().unwrap_or(&RevisionArg::AT))?;
if sources.iter().any(|source| source.id() == destination.id()) {
return Err(user_error("Source and destination cannot be the same"));
}
Expand All @@ -97,8 +98,8 @@ pub(crate) fn cmd_squash(
// a little faster.
sources.reverse();
} else {
let source =
workspace_command.resolve_single_rev(args.revision.as_deref().unwrap_or("@"))?;
let source = workspace_command
.resolve_single_rev(args.revision.as_ref().unwrap_or(&RevisionArg::AT))?;
let mut parents = source.parents();
if parents.len() != 1 {
return Err(user_error("Cannot squash merge commits"));
Expand Down

0 comments on commit 8b73d7a

Please sign in to comment.