Skip to content

Commit

Permalink
cli: status: when current change has conflicts, display instructions …
Browse files Browse the repository at this point in the history
…to resolve them
  • Loading branch information
poliorcetics committed Mar 2, 2024
1 parent f76830a commit 97dae12
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 27 deletions.
74 changes: 48 additions & 26 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,38 +1254,60 @@ See https://github.com/martinvonz/jj/blob/main/docs/working-copy.md#stale-workin
.iter()
.commits(new_repo.store())
.try_collect()?;
if !root_conflict_commits.is_empty() {
fmt.push_label("hint")?;
if added_conflict_commits.len() == 1 {
writeln!(fmt, "To resolve the conflicts, start by updating to it:",)?;
} else if root_conflict_commits.len() == 1 {
writeln!(
fmt,
"To resolve the conflicts, start by updating to the first one:",
)?;
} else {
writeln!(
fmt,
"To resolve the conflicts, start by updating to one of the first ones:",
)?;
}
for commit in root_conflict_commits {
writeln!(fmt, " jj new {}", short_change_hash(commit.change_id()))?;
}
writeln!(
fmt,
r#"Then use `jj resolve`, or edit the conflict markers in the file directly.
Once the conflicts are resolved, you may want inspect the result with `jj diff`.
Then run `jj squash` to move the resolution into the conflicted commit."#,
)?;
fmt.pop_label()?;
}

let hint_start = if added_conflict_commits.len() == 1 {
"To resolve the conflicts, start by updating to it:"
} else if root_conflict_commits.len() == 1 {
"To resolve the conflicts, start by updating to the first one:"
} else {
"To resolve the conflicts, start by updating to one of the first ones:"
};
print_conflict_resolution_hint(fmt.as_mut(), hint_start, &root_conflict_commits)?;
}

Ok(())
}
}

/// Prints conflict resolution hints when apprioriate.
///
/// `hint_start` is used to adapt the hint to the situation (for example, adapt
/// the hint to conflicts introduced after a rebase or to being in the middle of
/// a rebase where the focus will be the current change).
pub(crate) fn print_conflict_resolution_hint<C>(
fmt: &mut (dyn Formatter + '_),
hint_start: &str,
conflict_commits: &[C],
) -> Result<(), CommandError>
where
C: AsRef<Commit>,
{
if conflict_commits.is_empty() {
return Ok(());
}

fmt.push_label("hint")?;
writeln!(fmt, "{hint_start}")?;

for commit in conflict_commits {
writeln!(
fmt,
" jj new {}",
short_change_hash(commit.as_ref().change_id())
)?;
}

writeln!(
fmt,
r#"Then use `jj resolve`, or edit the conflict markers in the file directly.
Once the conflicts are resolved, you may want inspect the result with `jj diff`.
Then run `jj squash` to move the resolution into the conflicted commit."#,
)?;

fmt.pop_label()?;
Ok(())
}

#[must_use]
pub struct WorkspaceCommandTransaction<'a> {
helper: &'a mut WorkspaceCommandHelper,
Expand Down
8 changes: 7 additions & 1 deletion cli/src/commands/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ pub(crate) fn cmd_status(
formatter.labeled("conflict"),
"There are unresolved conflicts at these paths:"
)?;
resolve::print_conflicted_paths(&conflicts, formatter, &workspace_command)?
resolve::print_conflicted_paths(&conflicts, formatter, &workspace_command)?;

crate::cli_util::print_conflict_resolution_hint(
formatter,
"To resolve the conflicts, start by updating the current change:",
&[wc_commit],
)?;
}

formatter.write_str("Working copy : ")?;
Expand Down
51 changes: 51 additions & 0 deletions cli/tests/test_status_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,54 @@ fn test_status_ignored_gitignore() {
Parent commit: zzzzzzzz 00000000 (empty) (no description set)
"###);
}

// See <https://github.com/martinvonz/jj/issues/3108>
#[test]
fn test_status_display_rebase_instructions() {
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");
let conflicted_path = repo_path.join("conflicted.txt");

// PARENT: Write the initial file
std::fs::write(&conflicted_path, "initial contents").unwrap();
test_env.jj_cmd_ok(&repo_path, &["describe", "--message", "Initial contents"]);

// CHILD1: New commit on top of <PARENT>
test_env.jj_cmd_ok(
&repo_path,
&["new", "--message", "First part of conflicting change"],
);
std::fs::write(&conflicted_path, "Child 1").unwrap();

// CHILD2: New commit also on top of <PARENT>
test_env.jj_cmd_ok(
&repo_path,
&[
"new",
"--message",
"Second part of conflicting change",
"@-",
],
);
std::fs::write(&conflicted_path, "Child 2").unwrap();

// CONFLICT: New commit that is conflicted by merging <CHILD1> and <CHILD2>
test_env.jj_cmd_ok(&repo_path, &["new", "--message", "boom", "all:(@-)+"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["status"]);

insta::assert_snapshot!(stdout, @r###"
The working copy is clean
There are unresolved conflicts at these paths:
conflicted.txt 2-sided conflict
To resolve the conflicts, start by updating the current change:
jj new mzvwutvlkqwt
Then use `jj resolve`, or edit the conflict markers in the file directly.
Once the conflicts are resolved, you may want inspect the result with `jj diff`.
Then run `jj squash` to move the resolution into the conflicted commit.
Working copy : mzvwutvl 5c0db5c6 (conflict) (empty) boom
Parent commit: zsuskuln ba5f8773 Second part of conflicting change
Parent commit: kkmpptxz 55ce6709 First part of conflicting change
"###);
}
6 changes: 6 additions & 0 deletions lib/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ impl Hash for Commit {
}
}

impl AsRef<Self> for Commit {
fn as_ref(&self) -> &Self {
self
}
}

impl Commit {
pub fn new(store: Arc<Store>, id: CommitId, data: Arc<backend::Commit>) -> Self {
Commit { store, id, data }
Expand Down

0 comments on commit 97dae12

Please sign in to comment.