Skip to content

Commit

Permalink
git fetch: Refactor a reusable git_fetch function from the command.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
essiene committed Nov 4, 2024
1 parent 068336f commit 7a5633c
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions cli/src/commands/git/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('*')))
{
Expand All @@ -104,15 +126,10 @@ pub fn cmd_git_fetch(
}
warn_if_branches_not_found(
ui,
&tx,
&args.branch,
tx,
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";
Expand Down

0 comments on commit 7a5633c

Please sign in to comment.