git: absence of globs is None rather than &[]

In `git_fetch()`, any glob present in `globs` is an "allow" mark. Using
`&[]` to represent an "allow-all" may be misleading, as it could
indicate that no branch (only the git HEAD) should be fetched.

By using an `Option<&[&str]>`, it is clearer that `None` means that
all branches are fetched.
This commit is contained in:
Samuel Tardieu 2023-02-26 15:23:04 +01:00
parent 6fd65cca30
commit 0ca4e2dad2
3 changed files with 25 additions and 21 deletions

View file

@ -342,7 +342,7 @@ pub fn fetch(
mut_repo: &mut MutableRepo,
git_repo: &git2::Repository,
remote_name: &str,
globs: &[&str],
branch_name_globs: Option<&[&str]>,
callbacks: RemoteCallbacks<'_>,
git_settings: &GitSettings,
) -> Result<Option<String>, GitFetchError> {
@ -364,15 +364,19 @@ pub fn fetch(
fetch_options.proxy_options(proxy_options);
let callbacks = callbacks.into_git();
fetch_options.remote_callbacks(callbacks);
if globs.iter().any(|g| g.contains(|c| ":^".contains(c))) {
return Err(GitFetchError::InvalidGlob);
}
// At this point, we are only updating Git's remote tracking branches, not the
// local branches.
let refspecs = globs
.iter()
.map(|glob| format!("+refs/heads/{glob}:refs/remotes/{remote_name}/{glob}"))
.collect_vec();
let refspecs = if let Some(globs) = branch_name_globs {
if globs.iter().any(|g| g.contains(|c| ":^".contains(c))) {
return Err(GitFetchError::InvalidGlob);
}
// At this point, we are only updating Git's remote tracking branches, not the
// local branches.
globs
.iter()
.map(|glob| format!("+refs/heads/{glob}:refs/remotes/{remote_name}/{glob}"))
.collect_vec()
} else {
vec![]
};
tracing::debug!("remote.download");
remote.download(&refspecs, Some(&mut fetch_options))?;
tracing::debug!("remote.prune");

View file

@ -957,7 +957,7 @@ fn test_fetch_empty_repo() {
tx.mut_repo(),
&test_data.git_repo,
"origin",
&[],
None,
git::RemoteCallbacks::default(),
&git_settings,
)
@ -981,7 +981,7 @@ fn test_fetch_initial_commit() {
tx.mut_repo(),
&test_data.git_repo,
"origin",
&[],
None,
git::RemoteCallbacks::default(),
&git_settings,
)
@ -1023,7 +1023,7 @@ fn test_fetch_success() {
tx.mut_repo(),
&test_data.git_repo,
"origin",
&[],
None,
git::RemoteCallbacks::default(),
&git_settings,
)
@ -1044,7 +1044,7 @@ fn test_fetch_success() {
tx.mut_repo(),
&test_data.git_repo,
"origin",
&[],
None,
git::RemoteCallbacks::default(),
&git_settings,
)
@ -1086,7 +1086,7 @@ fn test_fetch_prune_deleted_ref() {
tx.mut_repo(),
&test_data.git_repo,
"origin",
&[],
None,
git::RemoteCallbacks::default(),
&git_settings,
)
@ -1105,7 +1105,7 @@ fn test_fetch_prune_deleted_ref() {
tx.mut_repo(),
&test_data.git_repo,
"origin",
&[],
None,
git::RemoteCallbacks::default(),
&git_settings,
)
@ -1126,7 +1126,7 @@ fn test_fetch_no_default_branch() {
tx.mut_repo(),
&test_data.git_repo,
"origin",
&[],
None,
git::RemoteCallbacks::default(),
&git_settings,
)
@ -1149,7 +1149,7 @@ fn test_fetch_no_default_branch() {
tx.mut_repo(),
&test_data.git_repo,
"origin",
&[],
None,
git::RemoteCallbacks::default(),
&git_settings,
)
@ -1169,7 +1169,7 @@ fn test_fetch_no_such_remote() {
tx.mut_repo(),
&test_data.git_repo,
"invalid-remote",
&[],
None,
git::RemoteCallbacks::default(),
&git_settings,
);

View file

@ -302,7 +302,7 @@ fn cmd_git_fetch(
tx.mut_repo(),
&git_repo,
&remote,
&branches,
(!branches.is_empty()).then(|| &*branches),
cb,
&command.settings().git_settings(),
)
@ -430,7 +430,7 @@ fn do_git_clone(
fetch_tx.mut_repo(),
&git_repo,
remote_name,
&[],
None,
cb,
&command.settings().git_settings(),
)