From 574914f2b93e677a0d2f805a27e5056f1428ee96 Mon Sep 17 00:00:00 2001 From: Essien Ita Essien <34972+essiene@users.noreply.github.com> Date: Mon, 16 Sep 2024 13:39:32 +0100 Subject: [PATCH] cli: Warn if specified remote branch not found for `jj git fetch` * First fetch from remote. * Then check tx.{base_repo(),repo}.view().remote_bookmarks_matching(, ). This has to happen after the fetch has been done so the tx.repo() is updated. * Warn if a branch is not found in any of the remotes used in the fetch. Note that the remotes used in the fetch can be a subset of the remotes configured for the repo, so the language of the warning tries to point that out. Fixes: #4293 --- CHANGELOG.md | 3 ++ cli/src/commands/git/fetch.rs | 38 +++++++++++++++ cli/tests/test_git_fetch.rs | 87 +++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51f6f2fdc6..0ce68525b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed bugs + * `jj git fetch -b ` will now warn if the branch(es) + can not be found in any of the specified/configured remotes. + * Fixed panic when parsing invalid conflict markers of a particular form. ([#2611](https://github.com/martinvonz/jj/pull/2611)) diff --git a/cli/src/commands/git/fetch.rs b/cli/src/commands/git/fetch.rs index a127332f35..c869237ca1 100644 --- a/cli/src/commands/git/fetch.rs +++ b/cli/src/commands/git/fetch.rs @@ -21,6 +21,7 @@ use jj_lib::settings::UserSettings; use jj_lib::str_util::StringPattern; use crate::cli_util::CommandHelper; +use crate::cli_util::WorkspaceCommandTransaction; use crate::command_error::user_error; use crate::command_error::user_error_with_hint; use crate::command_error::CommandError; @@ -100,6 +101,12 @@ pub fn cmd_git_fetch( })?; print_git_import_stats(ui, tx.repo(), &stats.import_stats, true)?; } + warn_if_branches_not_found( + ui, + &tx, + &args.branch[..], + &remotes.iter().map(StringPattern::exact).collect_vec()[..], + )?; tx.finish( ui, format!("fetch from git remote(s) {}", remotes.iter().join(",")), @@ -140,3 +147,34 @@ fn get_all_remotes(git_repo: &git2::Repository) -> Result, CommandEr .filter_map(|x| x.map(ToOwned::to_owned)) .collect()) } + +fn warn_if_branches_not_found( + ui: &mut Ui, + tx: &WorkspaceCommandTransaction, + branches: &[StringPattern], + remotes: &[StringPattern], +) -> Result<(), CommandError> { + for branch in branches { + let matches = remotes.iter().any(|remote| { + tx.repo() + .view() + .remote_bookmarks_matching(branch, remote) + .next() + .is_some() + || tx + .base_repo() + .view() + .remote_bookmarks_matching(branch, remote) + .next() + .is_some() + }); + if !matches { + writeln!( + ui.warning_default(), + "No branch matching `{branch}` found on any specified/configured remote", + )?; + } + } + + Ok(()) +} diff --git a/cli/tests/test_git_fetch.rs b/cli/tests/test_git_fetch.rs index 6311444c95..e9bd59aa2d 100644 --- a/cli/tests/test_git_fetch.rs +++ b/cli/tests/test_git_fetch.rs @@ -792,6 +792,92 @@ fn test_git_fetch_some_of_many_bookmarks() { "###); } +#[test] +fn test_git_fetch_bookmarks_some_missing() { + let test_env = TestEnvironment::default(); + test_env.add_config("git.auto-local-branch = true"); + test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]); + let repo_path = test_env.env_root().join("repo"); + add_git_remote(&test_env, &repo_path, "origin"); + add_git_remote(&test_env, &repo_path, "rem1"); + add_git_remote(&test_env, &repo_path, "rem2"); + + // single missing bookmark, implicit remotes (@origin) + let (_stdout, stderr) = + test_env.jj_cmd_ok(&repo_path, &["git", "fetch", "--branch", "noexist"]); + insta::assert_snapshot!(stderr, @r###" + Warning: No branch matching `noexist` found on any specified/configured remote + Nothing changed. + "###); + insta::assert_snapshot!(get_bookmark_output(&test_env, &repo_path), @""); + + // multiple missing bookmarks, implicit remotes (@origin) + let (_stdout, stderr) = test_env.jj_cmd_ok( + &repo_path, + &[ + "git", "fetch", "--branch", "noexist1", "--branch", "noexist2", + ], + ); + insta::assert_snapshot!(stderr, @r###" + Warning: No branch matching `noexist1` found on any specified/configured remote + Warning: No branch matching `noexist2` found on any specified/configured remote + Nothing changed. + "###); + insta::assert_snapshot!(get_bookmark_output(&test_env, &repo_path), @""); + + // single existing bookmark, implicit remotes (@origin) + let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["git", "fetch", "--branch", "origin"]); + insta::assert_snapshot!(stderr, @r###" + bookmark: origin@origin [new] tracked + "###); + insta::assert_snapshot!(get_bookmark_output(&test_env, &repo_path), @r###" + origin: oputwtnw ffecd2d6 message + @origin: oputwtnw ffecd2d6 message + "###); + + // multiple existing bookmark, explicit remotes, each bookmark is only in one + // remote. + let (_stdout, stderr) = test_env.jj_cmd_ok( + &repo_path, + &[ + "git", "fetch", "--branch", "rem1", "--branch", "rem2", "--remote", "rem1", "--remote", + "rem2", + ], + ); + insta::assert_snapshot!(stderr, @r###" + bookmark: rem1@rem1 [new] tracked + bookmark: rem2@rem2 [new] tracked + "###); + insta::assert_snapshot!(get_bookmark_output(&test_env, &repo_path), @r###" + origin: oputwtnw ffecd2d6 message + @origin: oputwtnw ffecd2d6 message + rem1: qxosxrvv 6a211027 message + @rem1: qxosxrvv 6a211027 message + rem2: yszkquru 2497a8a0 message + @rem2: yszkquru 2497a8a0 message + "###); + + // multiple bookmarks, one exists, one doesn't + let (_stdout, stderr) = test_env.jj_cmd_ok( + &repo_path, + &[ + "git", "fetch", "--branch", "rem1", "--branch", "notexist", "--remote", "rem1", + ], + ); + insta::assert_snapshot!(stderr, @r###" + Warning: No branch matching `notexist` found on any specified/configured remote + Nothing changed. + "###); + insta::assert_snapshot!(get_bookmark_output(&test_env, &repo_path), @r###" + origin: oputwtnw ffecd2d6 message + @origin: oputwtnw ffecd2d6 message + rem1: qxosxrvv 6a211027 message + @rem1: qxosxrvv 6a211027 message + rem2: yszkquru 2497a8a0 message + @rem2: yszkquru 2497a8a0 message + "###); +} + // See `test_undo_restore_commands.rs` for fetch-undo-push and fetch-undo-fetch // of the same bookmarks for various kinds of undo. #[test] @@ -1209,6 +1295,7 @@ fn test_git_fetch_removed_parent_bookmark() { bookmark: a1@origin [deleted] untracked bookmark: trunk1@origin [deleted] untracked Abandoned 1 commits that are no longer reachable. + Warning: No branch matching `master` found on any specified/configured remote "###); insta::assert_snapshot!(get_log_output(&test_env, &target_jj_repo_path), @r###" ○ c7d4bdcbc215 descr_for_b b