From b515d14f18cf81700421313baa967978bf361cb4 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Fri, 24 Feb 2023 18:24:48 +0100 Subject: [PATCH] git_fetch: when removing a remote branch, remove git ref as well --- CHANGELOG.md | 3 +++ src/commands/git.rs | 12 +++++++++++- tests/test_git_fetch.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e6caf087..e0173bdad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/commands/git.rs b/src/commands/git.rs index 8779fb777..c906d3851 100644 --- a/src/commands/git.rs +++ b/src/commands/git.rs @@ -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(()) diff --git a/tests/test_git_fetch.rs b/tests/test_git_fetch.rs index 0638b2846..cb4e8da57 100644 --- a/tests/test_git_fetch.rs +++ b/tests/test_git_fetch.rs @@ -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 + "###); +}