Skip to content

Commit

Permalink
git: test git fetch with more than two remotes and branches
Browse files Browse the repository at this point in the history
In some cases, there are non trivial codepaths for fetching multiple
branches explicitly. In particular, it might be the case that fetching
works for n = 2 but not n = 3.

This commit changes a cli test to have 3 remotes and 3 branches, and a
lib test with 3 branches, only one of them succeds.
  • Loading branch information
bsdinis committed Jan 23, 2025
1 parent 717ae02 commit c773c02
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
22 changes: 14 additions & 8 deletions cli/tests/test_git_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ fn test_git_fetch_bookmarks_some_missing() {
add_git_remote(&test_env, &repo_path, "origin");
add_git_remote(&test_env, &repo_path, "rem1");
add_git_remote(&test_env, &repo_path, "rem2");
add_git_remote(&test_env, &repo_path, "rem3");

// single missing bookmark, implicit remotes (@origin)
let (_stdout, stderr) =
Expand Down Expand Up @@ -838,22 +839,25 @@ fn test_git_fetch_bookmarks_some_missing() {
let (_stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&[
"git", "fetch", "--branch", "rem1", "--branch", "rem2", "--remote", "rem1", "--remote",
"rem2",
"git", "fetch", "--branch", "rem1", "--branch", "rem2", "--branch", "rem3", "--remote",
"rem1", "--remote", "rem2", "--remote", "rem3",
],
);
insta::assert_snapshot!(stderr, @r###"
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###"
bookmark: rem3@rem3 [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
"###);
rem3: lvsrtwwm 4ffdff2b message
@rem3: lvsrtwwm 4ffdff2b message
");

// multiple bookmarks, one exists, one doesn't
let (_stdout, stderr) = test_env.jj_cmd_ok(
Expand All @@ -866,14 +870,16 @@ fn test_git_fetch_bookmarks_some_missing() {
Warning: No branch matching `notexist` found on any specified/configured remote
Nothing changed.
"###);
insta::assert_snapshot!(get_bookmark_output(&test_env, &repo_path), @r###"
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
"###);
rem3: lvsrtwwm 4ffdff2b message
@rem3: lvsrtwwm 4ffdff2b message
");
}

// See `test_undo_restore_commands.rs` for fetch-undo-push and fetch-undo-fetch
Expand Down
38 changes: 38 additions & 0 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2866,6 +2866,44 @@ fn test_fetch_no_such_remote() {
assert!(matches!(result, Err(GitFetchError::NoSuchRemote(_))));
}

#[test]
fn test_fetch_multiple_branches() {
let test_data = GitRepoData::create();
let _initial_git_commit = empty_git_commit(&test_data.origin_repo, "refs/heads/main", &[]);
let git_settings = GitSettings {
auto_local_bookmark: true,
..Default::default()
};

let mut tx = test_data.repo.start_transaction();
let fetch_stats = git::fetch(
tx.repo_mut(),
&test_data.git_repo,
"origin",
&[
StringPattern::Exact("main".to_string()),
StringPattern::Exact("noexist1".to_string()),
StringPattern::Exact("noexist2".to_string()),
],
git::RemoteCallbacks::default(),
&git_settings,
None,
)
.unwrap();

assert_eq!(
fetch_stats
.import_stats
.changed_remote_refs
.keys()
.collect_vec(),
vec![&RefName::RemoteBranch {
branch: "main".to_string(),
remote: "origin".to_string()
}]
);
}

struct PushTestSetup {
source_repo_dir: PathBuf,
jj_repo: Arc<ReadonlyRepo>,
Expand Down

0 comments on commit c773c02

Please sign in to comment.