Skip to content

Commit 8181839

Browse files
committed
feat(sync): when --pull is passed, fetch only from the main branch remote, instead of all remotes
1 parent cd6e38f commit 8181839

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

git-branchless-lib/src/git/reference.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,19 @@ impl<'repo> Branch<'repo> {
338338
Ok(Some(upstream_branch_name_without_remote.to_owned()))
339339
}
340340

341+
/// Get the associated remote to pull from for this branch. If there is no
342+
/// associated remote, returns `None`.
343+
#[instrument]
344+
pub fn get_pull_remote_name(&self) -> eyre::Result<Option<String>> {
345+
let branch_name = self
346+
.inner
347+
.name()?
348+
.ok_or_else(|| eyre::eyre!("Branch name was not UTF-8: {self:?}"))?;
349+
let config = self.repo.get_readonly_config()?;
350+
let pull_remote_name = config.get(format!("branch.{branch_name}.remote"))?;
351+
Ok(pull_remote_name)
352+
}
353+
341354
/// Get the associated remote to push to for this branch. If there is no
342355
/// associated remote, returns `None`. Note that this never reads the value
343356
/// of `push.remoteDefault`.

git-branchless-submit/tests/test_github_forge.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ fn test_github_forge_mock_client_closes_pull_requests() -> eyre::Result<()> {
324324
{
325325
let (stdout, _stderr) = local_repo.branchless("sync", &["--pull"])?;
326326
let stdout = remove_rebase_lines(stdout);
327-
insta::assert_snapshot!(stdout, @r###"
328-
branchless: running command: <git-executable> fetch --all
327+
insta::assert_snapshot!(stdout, @r"
328+
branchless: running command: <git-executable> fetch origin
329329
Fast-forwarding branch master to 047b7ad create test1.txt
330330
Attempting rebase in-memory...
331331
[1/2] Skipped commit (was already applied upstream): 62fc20d create test1.txt
@@ -335,7 +335,7 @@ fn test_github_forge_mock_client_closes_pull_requests() -> eyre::Result<()> {
335335
and have 2 and 2 different commits each, respectively.
336336
In-memory rebase succeeded.
337337
Synced 62fc20d create test1.txt
338-
"###);
338+
");
339339
}
340340
{
341341
let stdout = local_repo.smartlog()?;

git-branchless/src/commands/sync.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ pub fn sync(
9696
let repo_pool = RepoResource::new_pool(&repo)?;
9797

9898
if pull {
99-
let head_info = repo.get_head_info()?;
10099
try_exit_code!(pull_main_branch(
101100
effects,
102101
git_run_info,
@@ -109,7 +108,6 @@ pub fn sync(
109108
&execute_options,
110109
&thread_pool,
111110
&repo_pool,
112-
&head_info,
113111
)?);
114112
}
115113

@@ -144,9 +142,22 @@ fn pull_main_branch(
144142
execute_options: &ExecuteRebasePlanOptions,
145143
thread_pool: &ThreadPool,
146144
repo_pool: &RepoPool,
147-
head_info: &ResolvedReferenceInfo,
148145
) -> EyreExitOr<()> {
149-
try_exit_code!(git_run_info.run(effects, Some(event_tx_id), &["fetch", "--all"])?);
146+
let head_info = repo.get_head_info()?;
147+
let main_branch = repo.get_main_branch()?;
148+
149+
match main_branch.get_pull_remote_name()? {
150+
Some(main_branch_pull_remote) => {
151+
try_exit_code!(git_run_info.run(
152+
effects,
153+
Some(event_tx_id),
154+
&["fetch", &main_branch_pull_remote]
155+
)?);
156+
}
157+
None => {
158+
tracing::warn!(?main_branch, "Main branch has no remote; not fetching it");
159+
}
160+
}
150161

151162
try_exit_code!(execute_main_branch_sync_plan(
152163
effects,
@@ -157,7 +168,7 @@ fn pull_main_branch(
157168
execute_options,
158169
thread_pool,
159170
repo_pool,
160-
head_info,
171+
&head_info,
161172
)?);
162173

163174
Ok(Ok(()))

git-branchless/tests/test_sync.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn test_sync_pull() -> eyre::Result<()> {
158158
let (stdout, _stderr) = cloned_repo.branchless("sync", &["-p"])?;
159159
let stdout: String = remove_nondeterministic_lines(stdout);
160160
insta::assert_snapshot!(stdout, @r###"
161-
branchless: running command: <git-executable> fetch --all
161+
branchless: running command: <git-executable> fetch origin
162162
Fast-forwarding branch master to f81d55c create test5.txt
163163
Attempting rebase in-memory...
164164
[1/1] Committed as: 2831fb5 create test6.txt
@@ -183,7 +183,7 @@ fn test_sync_pull() -> eyre::Result<()> {
183183
let (stdout, _stderr) = cloned_repo.branchless("sync", &["-p"])?;
184184
let stdout: String = remove_nondeterministic_lines(stdout);
185185
insta::assert_snapshot!(stdout, @r###"
186-
branchless: running command: <git-executable> fetch --all
186+
branchless: running command: <git-executable> fetch origin
187187
Not updating branch master at f81d55c create test5.txt
188188
Not moving up-to-date stack at 2831fb5 create test6.txt
189189
"###);
@@ -297,7 +297,7 @@ fn test_sync_divergent_main_branch() -> eyre::Result<()> {
297297
let (stdout, _stderr) = cloned_repo.branchless("sync", &["-p"])?;
298298
let stdout = remove_nondeterministic_lines(stdout);
299299
insta::assert_snapshot!(stdout, @r###"
300-
branchless: running command: <git-executable> fetch --all
300+
branchless: running command: <git-executable> fetch origin
301301
Syncing branch master
302302
Attempting rebase in-memory...
303303
[1/1] Committed as: f81d55c create test5.txt
@@ -371,7 +371,7 @@ fn test_sync_no_delete_main_branch() -> eyre::Result<()> {
371371
Successfully rebased and updated detached HEAD.
372372
"###);
373373
insta::assert_snapshot!(stdout, @r###"
374-
branchless: running command: <git-executable> fetch --all
374+
branchless: running command: <git-executable> fetch origin
375375
Syncing branch master
376376
branchless: running command: <git-executable> diff --quiet
377377
Calling Git for on-disk rebase...
@@ -469,10 +469,10 @@ fn test_sync_checked_out_main_branch() -> eyre::Result<()> {
469469
let (stdout, _stderr) = cloned_repo.branchless("sync", &["--pull"])?;
470470
let stdout: String = remove_nondeterministic_lines(stdout);
471471
insta::assert_snapshot!(stdout, @r###"
472-
branchless: running command: <git-executable> fetch --all
473-
Fast-forwarding branch master to 96d1c37 create test2.txt
474-
branchless: running command: <git-executable> rebase 96d1c37a3d4363611c49f7e52186e189a04c531f
475-
"###);
472+
branchless: running command: <git-executable> fetch origin
473+
Fast-forwarding branch master to 96d1c37 create test2.txt
474+
branchless: running command: <git-executable> rebase 96d1c37a3d4363611c49f7e52186e189a04c531f
475+
"###);
476476
}
477477

478478
{
@@ -524,7 +524,7 @@ fn test_sync_checked_out_main_with_dirty_working_copy() -> eyre::Result<()> {
524524
error: Please commit or stash them.
525525
"###);
526526
insta::assert_snapshot!(stdout, @r###"
527-
branchless: running command: <git-executable> fetch --all
527+
branchless: running command: <git-executable> fetch origin
528528
Not updating branch master at 62fc20d create test1.txt
529529
branchless: running command: <git-executable> rebase 62fc20d2a290daea0d52bdc2ed2ad4be6491010e
530530
"###);

0 commit comments

Comments
 (0)