Skip to content

Commit

Permalink
git_fetch: when removing a remote branch, remove git ref as well
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Feb 26, 2023
1 parent 1056cfa commit b515d14
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fixed a bug that could get partially resolved conflicts to be interpreted
incorrectly.

* `jj git fetch`: when re-adding a remote repository that had been previously
removed, in some situations the remote branches were not recreated.

## [0.7.0] - 2023-02-16

### Breaking changes
Expand Down
12 changes: 11 additions & 1 deletion src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,22 @@ fn cmd_git_remote_remove(
branches_to_delete.push(branch.clone());
}
}
if !branches_to_delete.is_empty() {
let prefix = format!("refs/remotes/{}/", args.remote);
let git_refs_to_delete = repo
.view()
.git_refs()
.keys()
.filter_map(|r| r.starts_with(&prefix).then(|| r.clone()))
.collect_vec();
if !branches_to_delete.is_empty() || !git_refs_to_delete.is_empty() {
let mut tx =
workspace_command.start_transaction(&format!("remove git remote {}", &args.remote));
for branch in branches_to_delete {
tx.mut_repo().remove_remote_branch(&branch, &args.remote);
}
for git_ref in git_refs_to_delete {
tx.mut_repo().remove_git_ref(&git_ref);
}
tx.finish(ui)?;
}
Ok(())
Expand Down
40 changes: 40 additions & 0 deletions tests/test_git_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,43 @@ fn test_git_fetch_undo() {
o 000000000000
"###);
}

#[test]
fn test_git_fetch_remove_fetch() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
add_git_remote(&test_env, &repo_path, "origin");

test_env.jj_cmd_success(&repo_path, &["branch", "set", "origin"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
origin: 230dd059e1b0 (no description set)
"###);

test_env.jj_cmd_success(&repo_path, &["git", "fetch"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
origin (conflicted):
+ 230dd059e1b0 (no description set)
+ ffecd2d67827 message
@origin (behind by 1 commits): ffecd2d67827 message
"###);

test_env.jj_cmd_success(&repo_path, &["git", "remote", "remove", "origin"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
origin (conflicted):
+ 230dd059e1b0 (no description set)
+ ffecd2d67827 message
"###);

test_env.jj_cmd_success(&repo_path, &["git", "remote", "add", "origin", "../origin"]);

// Check that origin@origin is properly recreated
let stdout = test_env.jj_cmd_success(&repo_path, &["git", "fetch"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
origin (conflicted):
+ 230dd059e1b0 (no description set)
+ ffecd2d67827 message
@origin (behind by 1 commits): ffecd2d67827 message
"###);
}

0 comments on commit b515d14

Please sign in to comment.