From 7fc53d7558d67c05a2960eb494d86bb1424dc64f Mon Sep 17 00:00:00 2001 From: Essien Ita Essien <34972+essiene@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:04:02 +0000 Subject: [PATCH] git fetch: Refactor a reusable `git_fetch` function from the command. We will need this in the `jj git sync` command to perform a fetch that behaves the same as the `jj git fetch` command. Fetch extract the function in this commit, so diffs are easier to review. A later commit will move this function (and associated functions) out to cli/src/git_util.rs Part of: #1039 --- cli/src/commands/git/fetch.rs | 43 ++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/cli/src/commands/git/fetch.rs b/cli/src/commands/git/fetch.rs index 8413895072..8dc9efff17 100644 --- a/cli/src/commands/git/fetch.rs +++ b/cli/src/commands/git/fetch.rs @@ -69,22 +69,44 @@ pub fn cmd_git_fetch( args.remotes.clone() }; let mut tx = workspace_command.start_transaction(); - for remote in &remotes { + git_fetch( + ui, + &mut tx, + &command.settings(), + &git_repo, + &remotes, + &args.branch, + )?; + tx.finish( + ui, + format!("fetch from git remote(s) {}", remotes.iter().join(",")), + )?; + Ok(()) +} + +fn git_fetch( + ui: &mut Ui, + tx: &mut WorkspaceCommandTransaction, + settings: &UserSettings, + git_repo: &git2::Repository, + remotes: &[String], + branch: &[StringPattern], +) -> Result<(), CommandError> { + for remote in remotes { let stats = with_remote_git_callbacks(ui, None, |cb| { git::fetch( tx.repo_mut(), - &git_repo, + git_repo, remote, - &args.branch, + branch, cb, - &command.settings().git_settings(), + &settings.git_settings(), None, ) }) .map_err(|err| match err { GitFetchError::InvalidBranchPattern => { - if args - .branch + if branch .iter() .any(|pattern| pattern.as_exact().map_or(false, |s| s.contains('*'))) { @@ -105,14 +127,9 @@ pub fn cmd_git_fetch( warn_if_branches_not_found( ui, &tx, - &args.branch, + branch, &remotes.iter().map(StringPattern::exact).collect_vec(), - )?; - tx.finish( - ui, - format!("fetch from git remote(s) {}", remotes.iter().join(",")), - )?; - Ok(()) + ) } const DEFAULT_REMOTE: &str = "origin";