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
* Add new field `unchanged_remote_refs` to GitImportStats.
* Check both `stats.changed_remote_refs` and `stats.unchanged_remote_refs`
  after running `git fetch` and warn if any specified branches is
  not found in either.

Fixes: #4293
  • Loading branch information
essiene committed Sep 18, 2024
1 parent 47323c8 commit 0cf9737
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
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 remotes.

* Fixed panic when parsing invalid conflict markers of a particular form.
([#2611](https://github.com/martinvonz/jj/pull/2611))

Expand Down
58 changes: 57 additions & 1 deletion cli/src/commands/git/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashSet;

use itertools::Itertools;
use jj_lib::git;
use jj_lib::git::GitFetchError;
use jj_lib::git::GitImportStats;
use jj_lib::git::RefName;
use jj_lib::repo::Repo;
use jj_lib::settings::ConfigResultExt as _;
use jj_lib::settings::UserSettings;
use jj_lib::str_util::StringPattern;
use jj_lib::view::View;

use crate::cli_util::CommandHelper;
use crate::command_error::user_error;
Expand Down Expand Up @@ -59,14 +64,17 @@ pub fn cmd_git_fetch(
args: &GitFetchArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let git_repo = get_git_repo(workspace_command.repo().store())?;
let repo = workspace_command.repo();
let view = repo.view().clone();
let git_repo = get_git_repo(repo.store())?;
let remotes = if args.all_remotes {
get_all_remotes(&git_repo)?
} else if args.remotes.is_empty() {
get_default_fetch_remotes(ui, command.settings(), &git_repo)?
} else {
args.remotes.clone()
};
let mut found_branches: HashSet<&str> = HashSet::from([]);
let mut tx = workspace_command.start_transaction();
for remote in &remotes {
let stats = with_remote_git_callbacks(ui, None, |cb| {
Expand Down Expand Up @@ -98,8 +106,24 @@ pub fn cmd_git_fetch(
GitFetchError::InternalGitError(err) => map_git_error(err),
_ => user_error(err),
})?;
for pattern in &args.branch {
if branch_in_remote(&view, remote, pattern, &stats.import_stats) {
found_branches.insert(pattern.as_str());
continue;
}
}
print_git_import_stats(ui, tx.repo(), &stats.import_stats, true)?;
}
for branch in args
.branch
.iter()
.filter(|b| !found_branches.contains(b.as_str()))
{
writeln!(
ui.warning_default(),
"Branch `{branch}` not found on any remote",
)?;
}
tx.finish(
ui,
format!("fetch from git remote(s) {}", remotes.iter().join(",")),
Expand Down Expand Up @@ -140,3 +164,35 @@ fn get_all_remotes(git_repo: &git2::Repository) -> Result<Vec<String>, CommandEr
.filter_map(|x| x.map(ToOwned::to_owned))
.collect())
}

fn matches_remote_branch(
pattern: &StringPattern,
in_remote: &str,
remote_branch: &RefName,
) -> bool {
match remote_branch {
RefName::RemoteBranch { branch, remote } => pattern.matches(branch) && remote == in_remote,
RefName::LocalBranch(..) | RefName::Tag(..) => false,
}
}

fn branch_in_remote(
view: &View,
remote: &str,
pattern: &StringPattern,
stats: &GitImportStats,
) -> bool {
for ref_name in stats.changed_remote_refs.keys() {
if matches_remote_branch(pattern, remote, ref_name) {
return true;
}
}

for (branch, _) in view.remote_bookmarks(remote) {
if pattern.matches(branch) {
return true;
}
}

false
}
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: Branch `noexist` not found on any 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: Branch `noexist1` not found on any remote
Warning: Branch `noexist2` not found on any 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: Branch `notexist` not found on any 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: Branch `master` not found on any 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 0cf9737

Please sign in to comment.