Skip to content

Commit

Permalink
feat(sync): when --pull is passed, fetch only from the main branch …
Browse files Browse the repository at this point in the history
…remote, instead of all remotes
  • Loading branch information
arxanas committed Jul 6, 2024
1 parent 96af439 commit 7023260
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
13 changes: 13 additions & 0 deletions git-branchless-lib/src/git/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>> {
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`.
Expand Down
21 changes: 16 additions & 5 deletions git-branchless/src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -109,7 +108,6 @@ pub fn sync(
&execute_options,
&thread_pool,
&repo_pool,
&head_info,
)?);
}

Expand Down Expand Up @@ -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,
Expand All @@ -157,7 +168,7 @@ fn pull_main_branch(
execute_options,
thread_pool,
repo_pool,
head_info,
&head_info,
)?);

Ok(Ok(()))
Expand Down
18 changes: 9 additions & 9 deletions git-branchless/tests/test_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: <git-executable> fetch --all
branchless: running command: <git-executable> fetch origin
Fast-forwarding branch master to f81d55c create test5.txt
Attempting rebase in-memory...
[1/1] Committed as: 2831fb5 create test6.txt
Expand All @@ -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: <git-executable> fetch --all
branchless: running command: <git-executable> fetch origin
Not updating branch master at f81d55c create test5.txt
Not moving up-to-date stack at 2831fb5 create test6.txt
"###);
Expand Down Expand Up @@ -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: <git-executable> fetch --all
branchless: running command: <git-executable> fetch origin
Syncing branch master
Attempting rebase in-memory...
[1/1] Committed as: f81d55c create test5.txt
Expand Down Expand Up @@ -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: <git-executable> fetch --all
branchless: running command: <git-executable> fetch origin
Syncing branch master
branchless: running command: <git-executable> diff --quiet
Calling Git for on-disk rebase...
Expand Down Expand Up @@ -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: <git-executable> fetch --all
Fast-forwarding branch master to 96d1c37 create test2.txt
branchless: running command: <git-executable> rebase 96d1c37a3d4363611c49f7e52186e189a04c531f
"###);
branchless: running command: <git-executable> fetch origin
Fast-forwarding branch master to 96d1c37 create test2.txt
branchless: running command: <git-executable> rebase 96d1c37a3d4363611c49f7e52186e189a04c531f
"###);
}

{
Expand Down Expand Up @@ -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: <git-executable> fetch --all
branchless: running command: <git-executable> fetch origin
Not updating branch master at 62fc20d create test1.txt
branchless: running command: <git-executable> rebase 62fc20d2a290daea0d52bdc2ed2ad4be6491010e
"###);
Expand Down

0 comments on commit 7023260

Please sign in to comment.