Update jj edit <commit> to add commit into view heads if not already

`jj new <commit>` automatically adds the checked out commits into the view head ids. However,
`jj edit` does not.

To reproduce:
```
jj git init test
cd test
jj commit -m "my commit"
jj log -r @- -T commit_id # Save the id
jj abandon -r @-
jj edit <saved_id>

jj log -r :: # Does not show the currently editing commit 
```
This commit is contained in:
Kevin Liao 2024-09-09 12:14:48 -07:00
parent d04ff1213f
commit 69edc7f2df
3 changed files with 25 additions and 0 deletions

View file

@ -41,6 +41,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
* Fixed panic when parsing invalid conflict markers of a particular form.
([#2611](https://github.com/martinvonz/jj/pull/2611))
* Editing a hidden commit now makes it visible.
## [0.21.0] - 2024-09-04
### Breaking changes

View file

@ -1375,6 +1375,7 @@ impl MutableRepo {
commit: &Commit,
) -> Result<(), EditCommitError> {
self.maybe_abandon_wc_commit(&workspace_id)?;
self.add_head(commit)?;
self.set_wc_commit(workspace_id, commit.id().clone())
.map_err(|RewriteRootCommit| EditCommitError::RewriteRootCommit)
}

View file

@ -318,6 +318,28 @@ fn test_edit_initial() {
);
}
#[test]
fn test_edit_hidden_commit() {
// Test that MutableRepo::edit() edits a hidden commit and updates
// the view head ids.
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let repo = &test_repo.repo;
let mut tx = repo.start_transaction(&settings);
let wc_commit = write_random_commit(tx.repo_mut(), &settings);
// Intentionally not doing tx.commit, so the commit id is not tracked
// in the view head ids.
let mut tx = repo.start_transaction(&settings);
let ws_id = WorkspaceId::default();
tx.repo_mut().edit(ws_id.clone(), &wc_commit).unwrap();
let repo = tx.commit("test");
assert_eq!(repo.view().get_wc_commit_id(&ws_id), Some(wc_commit.id()));
assert_eq!(*repo.view().heads(), hashset! {wc_commit.id().clone()});
}
#[test]
fn test_add_head_success() {
// Test that MutableRepo::add_head() adds the head, and that it's still there