Skip to content

Commit

Permalink
cli: Warn if specified remote branch not found for jj git fetch
Browse files Browse the repository at this point in the history
* First fetch from remote.
* Then check tx.{base_repo(),repo}.view().remote_bookmarks_matching(<branch>, <remote>).
  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
  • Loading branch information
essiene committed Sep 21, 2024
1 parent 47323c8 commit 574914f
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed bugs

* `jj git fetch -b <remote-git-branch-name>` 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))

Expand Down
38 changes: 38 additions & 0 deletions cli/src/commands/git/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(",")),
Expand Down Expand Up @@ -140,3 +147,34 @@ fn get_all_remotes(git_repo: &git2::Repository) -> Result<Vec<String>, 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(())
}
87 changes: 87 additions & 0 deletions cli/tests/test_git_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 574914f

Please sign in to comment.