diff --git a/git-branchless-lib/src/git/reference.rs b/git-branchless-lib/src/git/reference.rs index 9f6688019..b68c95bd6 100644 --- a/git-branchless-lib/src/git/reference.rs +++ b/git-branchless-lib/src/git/reference.rs @@ -337,6 +337,19 @@ impl<'repo> Branch<'repo> { Ok(Some(upstream_branch_name_without_remote.to_owned())) } + /// Get the associated remote to pull from for this branch. If there is no + /// associated remote, returns `None`. + #[instrument] + pub fn get_pull_remote_name(&self) -> eyre::Result> { + let branch_name = self + .inner + .name()? + .ok_or_else(|| eyre::eyre!("Branch name was not UTF-8: {self:?}"))?; + let config = self.repo.get_readonly_config()?; + let pull_remote_name = config.get(format!("branch.{branch_name}.remote"))?; + Ok(pull_remote_name) + } + /// Get the associated remote to push to for this branch. If there is no /// associated remote, returns `None`. Note that this never reads the value /// of `push.remoteDefault`. diff --git a/git-branchless/src/commands/sync.rs b/git-branchless/src/commands/sync.rs index 9afcb8c7c..ecbbb9af6 100644 --- a/git-branchless/src/commands/sync.rs +++ b/git-branchless/src/commands/sync.rs @@ -96,7 +96,6 @@ pub fn sync( let repo_pool = RepoResource::new_pool(&repo)?; if pull { - let head_info = repo.get_head_info()?; try_exit_code!(pull_main_branch( effects, git_run_info, @@ -109,7 +108,6 @@ pub fn sync( &execute_options, &thread_pool, &repo_pool, - &head_info, )?); } @@ -144,9 +142,22 @@ fn pull_main_branch( execute_options: &ExecuteRebasePlanOptions, thread_pool: &ThreadPool, repo_pool: &RepoPool, - head_info: &ResolvedReferenceInfo, ) -> EyreExitOr<()> { - try_exit_code!(git_run_info.run(effects, Some(event_tx_id), &["fetch", "--all"])?); + let head_info = repo.get_head_info()?; + let main_branch = repo.get_main_branch()?; + + match main_branch.get_pull_remote_name()? { + Some(main_branch_pull_remote) => { + try_exit_code!(git_run_info.run( + effects, + Some(event_tx_id), + &["fetch", &main_branch_pull_remote] + )?); + } + None => { + tracing::warn!(?main_branch, "Main branch has no remote; not fetching it"); + } + } try_exit_code!(execute_main_branch_sync_plan( effects, @@ -157,7 +168,7 @@ fn pull_main_branch( execute_options, thread_pool, repo_pool, - head_info, + &head_info, )?); Ok(Ok(())) diff --git a/git-branchless/tests/test_sync.rs b/git-branchless/tests/test_sync.rs index 50b2b7d01..31108ebbf 100644 --- a/git-branchless/tests/test_sync.rs +++ b/git-branchless/tests/test_sync.rs @@ -158,7 +158,7 @@ fn test_sync_pull() -> eyre::Result<()> { let (stdout, _stderr) = cloned_repo.run(&["sync", "-p"])?; let stdout: String = remove_nondeterministic_lines(stdout); insta::assert_snapshot!(stdout, @r###" - branchless: running command: fetch --all + branchless: running command: fetch origin Fast-forwarding branch master to f81d55c create test5.txt Attempting rebase in-memory... [1/1] Committed as: 2831fb5 create test6.txt @@ -183,7 +183,7 @@ fn test_sync_pull() -> eyre::Result<()> { let (stdout, _stderr) = cloned_repo.run(&["sync", "-p"])?; let stdout: String = remove_nondeterministic_lines(stdout); insta::assert_snapshot!(stdout, @r###" - branchless: running command: fetch --all + branchless: running command: fetch origin Not updating branch master at f81d55c create test5.txt Not moving up-to-date stack at 2831fb5 create test6.txt "###); @@ -297,7 +297,7 @@ fn test_sync_divergent_main_branch() -> eyre::Result<()> { let (stdout, _stderr) = cloned_repo.run(&["sync", "-p"])?; let stdout = remove_nondeterministic_lines(stdout); insta::assert_snapshot!(stdout, @r###" - branchless: running command: fetch --all + branchless: running command: fetch origin Syncing branch master Attempting rebase in-memory... [1/1] Committed as: f81d55c create test5.txt @@ -371,7 +371,7 @@ fn test_sync_no_delete_main_branch() -> eyre::Result<()> { Successfully rebased and updated detached HEAD. "###); insta::assert_snapshot!(stdout, @r###" - branchless: running command: fetch --all + branchless: running command: fetch origin Syncing branch master branchless: running command: diff --quiet Calling Git for on-disk rebase... @@ -469,10 +469,10 @@ fn test_sync_checked_out_main_branch() -> eyre::Result<()> { let (stdout, _stderr) = cloned_repo.branchless("sync", &["--pull"])?; let stdout: String = remove_nondeterministic_lines(stdout); insta::assert_snapshot!(stdout, @r###" - branchless: running command: fetch --all - Fast-forwarding branch master to 96d1c37 create test2.txt - branchless: running command: rebase 96d1c37a3d4363611c49f7e52186e189a04c531f - "###); + branchless: running command: fetch origin + Fast-forwarding branch master to 96d1c37 create test2.txt + branchless: running command: rebase 96d1c37a3d4363611c49f7e52186e189a04c531f + "###); } { @@ -524,7 +524,7 @@ fn test_sync_checked_out_main_with_dirty_working_copy() -> eyre::Result<()> { error: Please commit or stash them. "###); insta::assert_snapshot!(stdout, @r###" - branchless: running command: fetch --all + branchless: running command: fetch origin Not updating branch master at 62fc20d create test1.txt branchless: running command: rebase 62fc20d2a290daea0d52bdc2ed2ad4be6491010e "###);