Skip to content

Commit

Permalink
jj-lib: abstract a lower-level api for fetching from git.
Browse files Browse the repository at this point in the history
* This allows more control over the stages of the fetch.
* Implement `git::fetch_and_get_default_branch` in terms of the new api.
* Update the cli/src/commands/Git/clone.rs to use new `git::fetch_and_get_default_branch`.
* Update tests to explicitly use `git::fetch_and_get_default_branch` where relevant.
* Update tests to reflect that `git::fetch` doesn't return the default branch.

Issue: #4923
  • Loading branch information
essiene committed Nov 30, 2024
1 parent 0ca6f00 commit c35a6f7
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 85 deletions.
3 changes: 1 addition & 2 deletions cli/src/commands/git/clone.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Copyright 2020-2023 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -222,7 +221,7 @@ fn do_git_clone(
let mut fetch_tx = workspace_command.start_transaction();

let stats = with_remote_git_callbacks(ui, None, |cb| {
git::fetch(
git::fetch_and_get_default_branch(
fetch_tx.repo_mut(),
&git_repo,
remote_name,
Expand Down
249 changes: 173 additions & 76 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,146 @@ pub enum GitFetchError {
InternalGitError(#[from] git2::Error),
}

fn git_fetch_options(
callbacks: RemoteCallbacks<'_>,
depth: Option<NonZeroU32>,
) -> git2::FetchOptions<'_> {
let mut proxy_options = git2::ProxyOptions::new();
proxy_options.auto();

let mut fetch_options = git2::FetchOptions::new();
fetch_options.proxy_options(proxy_options);
fetch_options.remote_callbacks(callbacks.into_git());
if let Some(depth) = depth {
fetch_options.depth(depth.get().try_into().unwrap_or(i32::MAX));
}

fetch_options
}

struct FetchedBranches {
branches: Vec<StringPattern>,
remote: String,
}

struct GitFetch<'a> {
mut_repo: &'a mut MutableRepo,
git_repo: &'a git2::Repository,
git_settings: &'a GitSettings,
fetch_options: git2::FetchOptions<'a>,
fetched: Vec<FetchedBranches>,
}

impl<'a> GitFetch<'a> {
fn new(
mut_repo: &'a mut MutableRepo,
git_repo: &'a git2::Repository,
git_settings: &'a GitSettings,
fetch_options: git2::FetchOptions<'a>,
) -> Self {
GitFetch {
mut_repo,
git_repo,
git_settings,
fetch_options,
fetched: vec![],
}
}

fn fetch(
&mut self,
branch_names: &[StringPattern],
remote_name: &str,
) -> Result<Option<String>, GitFetchError> {
// Perform a `git fetch` on the local git repo, updating the remote-tracking
// branches in the git repo.
let mut remote = self.git_repo.find_remote(remote_name).map_err(|err| {
if is_remote_not_found_err(&err) {
GitFetchError::NoSuchRemote(remote_name.to_string())
} else {
GitFetchError::InternalGitError(err)
}
})?;
// At this point, we are only updating Git's remote tracking branches, not the
// local branches.
let refspecs: Vec<_> = branch_names
.iter()
.map(|pattern| {
pattern
.to_glob()
.filter(
/* This triggered by non-glob `*`s in addition to INVALID_REFSPEC_CHARS
* because `to_glob()` escapes such `*`s as `[*]`. */
|glob| !glob.contains(INVALID_REFSPEC_CHARS),
)
.map(|glob| format!("+refs/heads/{glob}:refs/remotes/{remote_name}/{glob}"))
})
.collect::<Option<_>>()
.ok_or(GitFetchError::InvalidBranchPattern)?;
if refspecs.is_empty() {
// Don't fall back to the base refspecs.
return Ok(None);
}

tracing::debug!("remote.download");
remote.download(&refspecs, Some(&mut self.fetch_options))?;
tracing::debug!("remote.prune");
remote.prune(None)?;
tracing::debug!("remote.update_tips");
remote.update_tips(
None,
git2::RemoteUpdateFlags::empty(),
git2::AutotagOption::Unspecified,
None,
)?;

self.fetched.push(FetchedBranches {
branches: branch_names.to_vec(),
remote: remote_name.to_string(),
});

let mut default_branch = None;
if let Ok(default_ref_buf) = remote.default_branch() {
if let Some(default_ref) = default_ref_buf.as_str() {
// LocalBranch here is the local branch on the remote, so it's really the remote
// branch
if let Some(RefName::LocalBranch(branch_name)) = parse_git_ref(default_ref) {
tracing::debug!(default_branch = branch_name);
default_branch = Some(branch_name);
}
}
}
tracing::debug!("remote.disconnect");
remote.disconnect()?;
Ok(default_branch)
}

pub fn import_refs(&mut self) -> Result<GitImportStats, GitImportError> {
// Import the remote-tracking branches into the jj repo and update jj's
// local branches. We also import local tags since remote tags should have
// been merged by Git.
tracing::debug!("import_refs");
let import_stats = import_some_refs(self.mut_repo, self.git_settings, |ref_name| {
self.fetched
.iter()
.filter_map(|fetched| {
to_remote_branch(ref_name, &fetched.remote).map(|branch| {
fetched
.branches
.iter()
.any(|pattern| pattern.matches(branch))
})
})
.next()
.unwrap_or(matches!(ref_name, RefName::Tag(_)))
})?;

self.fetched = vec![];

Ok(import_stats)
}
}

/// Describes successful `fetch()` result.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct GitFetchStats {
Expand All @@ -1239,7 +1379,7 @@ pub struct GitFetchStats {
}

#[tracing::instrument(skip(mut_repo, git_repo, callbacks))]
pub fn fetch(
pub fn fetch_and_get_default_branch(
mut_repo: &mut MutableRepo,
git_repo: &git2::Repository,
remote_name: &str,
Expand All @@ -1248,88 +1388,45 @@ pub fn fetch(
git_settings: &GitSettings,
depth: Option<NonZeroU32>,
) -> Result<GitFetchStats, GitFetchError> {
// Perform a `git fetch` on the local git repo, updating the remote-tracking
// branches in the git repo.
let mut remote = git_repo.find_remote(remote_name).map_err(|err| {
if is_remote_not_found_err(&err) {
GitFetchError::NoSuchRemote(remote_name.to_string())
} else {
GitFetchError::InternalGitError(err)
}
})?;
let mut fetch_options = git2::FetchOptions::new();
let mut proxy_options = git2::ProxyOptions::new();
proxy_options.auto();
fetch_options.proxy_options(proxy_options);
let callbacks = callbacks.into_git();
fetch_options.remote_callbacks(callbacks);
if let Some(depth) = depth {
fetch_options.depth(depth.get().try_into().unwrap_or(i32::MAX));
}
// At this point, we are only updating Git's remote tracking branches, not the
// local branches.
let refspecs: Vec<_> = branch_names
.iter()
.map(|pattern| {
pattern
.to_glob()
.filter(
/* This triggered by non-glob `*`s in addition to INVALID_REFSPEC_CHARS
* because `to_glob()` escapes such `*`s as `[*]`. */
|glob| !glob.contains(INVALID_REFSPEC_CHARS),
)
.map(|glob| format!("+refs/heads/{glob}:refs/remotes/{remote_name}/{glob}"))
})
.collect::<Option<_>>()
.ok_or(GitFetchError::InvalidBranchPattern)?;
if refspecs.is_empty() {
// Don't fall back to the base refspecs.
let stats = GitFetchStats::default();
return Ok(stats);
}
tracing::debug!("remote.download");
remote.download(&refspecs, Some(&mut fetch_options))?;
tracing::debug!("remote.prune");
remote.prune(None)?;
tracing::debug!("remote.update_tips");
remote.update_tips(
None,
git2::RemoteUpdateFlags::empty(),
git2::AutotagOption::Unspecified,
None,
)?;
// TODO: We could make it optional to get the default branch since we only care
// about it on clone.
let mut default_branch = None;
if let Ok(default_ref_buf) = remote.default_branch() {
if let Some(default_ref) = default_ref_buf.as_str() {
// LocalBranch here is the local branch on the remote, so it's really the remote
// branch
if let Some(RefName::LocalBranch(branch_name)) = parse_git_ref(default_ref) {
tracing::debug!(default_branch = branch_name);
default_branch = Some(branch_name);
}
}
}
tracing::debug!("remote.disconnect");
remote.disconnect()?;

// Import the remote-tracking branches into the jj repo and update jj's
// local branches. We also import local tags since remote tags should have
// been merged by Git.
tracing::debug!("import_refs");
let import_stats = import_some_refs(mut_repo, git_settings, |ref_name| {
to_remote_branch(ref_name, remote_name)
.map(|branch| branch_names.iter().any(|pattern| pattern.matches(branch)))
.unwrap_or_else(|| matches!(ref_name, RefName::Tag(_)))
})?;
let mut git_fetch = GitFetch::new(
mut_repo,
git_repo,
git_settings,
git_fetch_options(callbacks, depth),
);
let default_branch = git_fetch.fetch(branch_names, remote_name)?;
let import_stats = git_fetch.import_refs()?;
let stats = GitFetchStats {
default_branch,
import_stats,
};
Ok(stats)
}

pub fn fetch(
mut_repo: &mut MutableRepo,
git_repo: &git2::Repository,
remote_name: &str,
branch_names: &[StringPattern],
callbacks: RemoteCallbacks<'_>,
git_settings: &GitSettings,
depth: Option<NonZeroU32>,
) -> Result<GitFetchStats, GitFetchError> {
let mut git_fetch = GitFetch::new(
mut_repo,
git_repo,
git_settings,
git_fetch_options(callbacks, depth),
);
git_fetch.fetch(branch_names, remote_name)?;
let import_stats = git_fetch.import_refs()?;
let stats = GitFetchStats {
default_branch: None,
import_stats,
};
Ok(stats)
}

#[derive(Error, Debug, PartialEq)]
pub enum GitPushError {
#[error("No git remote named '{0}'")]
Expand Down
Loading

0 comments on commit c35a6f7

Please sign in to comment.