History

refactor

Sebastian Thiel committed 1 year ago

Commit c331afc1 parent d2cd2ef4 files · +36 −24Browse files

Unified · Split

4 files
  1. gitoxide-core/src/repository/fetch.rs +1 −1
  2. gix/src/clone/fetch/mod.rs +12 −9
  3. gix/src/config/tree/sections/init.rs +1 −0
  4. gix/tests/gix/clone.rs +22 −14
gitoxide-core/src/repository/fetch.rs+1−1
@@ -320,7 +320,7 @@ pub(crate) mod function { · 319 unmodified lines
320 err,320 err,
321 "server sent {} tips, {} were filtered due to {} refspec(s).",321 "server sent {} tips, {} were filtered due to {} refspec(s).",
322 map.remote_refs.len(),322 map.remote_refs.len(),
323 map.remote_refs.len() - map.mappings.len(),323 map.remote_refs.len().saturating_sub(map.mappings.len()),
324 refspecs.len()324 refspecs.len()
325 )?;325 )?;
326 }326 }
gix/src/clone/fetch/mod.rs+12−9
@@ -2,6 +2,7 @@ use crate::{ · 1 unmodified line
2 bstr::{BString, ByteSlice},2 bstr::{BString, ByteSlice},
3 clone::PrepareFetch,3 clone::PrepareFetch,
4};4};
5use gix_ref::Category;
5 6
6/// The error returned by [`PrepareFetch::fetch_only()`].7/// The error returned by [`PrepareFetch::fetch_only()`].
7#[derive(Debug, thiserror::Error)]8#[derive(Debug, thiserror::Error)]
@@ -49,6 +50,8 @@ pub enum Error { · 41 unmodified lines
49 CommitterOrFallback(#[from] crate::config::time::Error),50 CommitterOrFallback(#[from] crate::config::time::Error),
50 #[error(transparent)]51 #[error(transparent)]
51 RefMap(#[from] crate::remote::ref_map::Error),52 RefMap(#[from] crate::remote::ref_map::Error),
53 #[error(transparent)]
54 ReferenceName(#[from] gix_validate::reference::name::Error),
52}55}
53 56
54/// Modification57/// Modification
@@ -107,14 +110,13 @@ impl PrepareFetch { · 52 unmodified lines
107 // For shallow clones without custom configuration, we'll use a single-branch refspec110 // For shallow clones without custom configuration, we'll use a single-branch refspec
108 // to match git's behavior (matching git's single-branch behavior for shallow clones).111 // to match git's behavior (matching git's single-branch behavior for shallow clones).
109 let use_single_branch_for_shallow = self.shallow != remote::fetch::Shallow::NoChange112 let use_single_branch_for_shallow = self.shallow != remote::fetch::Shallow::NoChange
110 && self.configure_remote.is_none()113 && remote.fetch_specs.is_empty()
111 && remote.fetch_specs.is_empty();114 && self.fetch_options.extra_refspecs.is_empty();
112 115
113 let target_ref = if use_single_branch_for_shallow {116 let target_ref = if use_single_branch_for_shallow {
114 // Determine target branch from user-specified ref_name or default branch117 // Determine target branch from user-specified ref_name or default branch
115 if let Some(ref_name) = &self.ref_name {118 if let Some(ref_name) = &self.ref_name {
116 // User specified a branch, use that119 Some(Category::LocalBranch.to_full_name(ref_name.as_ref().as_bstr())?)
117 Some(format!("refs/heads/{}", ref_name.as_ref().as_bstr()))
118 } else {120 } else {
119 // For shallow clones without a specified ref, we need to determine the default branch.121 // For shallow clones without a specified ref, we need to determine the default branch.
120 // We'll connect to get HEAD information. For Protocol V2, we need to explicitly list refs.122 // We'll connect to get HEAD information. For Protocol V2, we need to explicitly list refs.
@@ -129,7 +131,7 @@ impl PrepareFetch { · 8 unmodified lines
129 refs.iter().find_map(|r| match r {131 refs.iter().find_map(|r| match r {
130 gix_protocol::handshake::Ref::Symbolic {132 gix_protocol::handshake::Ref::Symbolic {
131 full_ref_name, target, ..133 full_ref_name, target, ..
132 } if full_ref_name == "HEAD" => Some(target.to_string()),134 } if full_ref_name == "HEAD" => gix_ref::FullName::try_from(target).ok(),
133 _ => None,135 _ => None,
134 })136 })
135 })137 })
@@ -143,9 +145,9 @@ impl PrepareFetch { · 7 unmodified lines
143 repo.config145 repo.config
144 .resolved146 .resolved
145 .string(crate::config::tree::Init::DEFAULT_BRANCH)147 .string(crate::config::tree::Init::DEFAULT_BRANCH)
146 .and_then(|name| name.to_str().ok().map(|s| format!("refs/heads/{}", s)))148 .and_then(|name| Category::LocalBranch.to_full_name(name.as_bstr()).ok())
147 })149 })
148 .unwrap_or_else(|| "refs/heads/main".to_string());150 .unwrap_or_else(|| gix_ref::FullName::try_from("refs/heads/main").expect("known to be valid"));
149 151
150 // Drop the connection explicitly to release the borrow on remote152 // Drop the connection explicitly to release the borrow on remote
151 drop(connection);153 drop(connection);
@@ -156,11 +158,12 @@ impl PrepareFetch { · 4 unmodified lines
156 None158 None
157 };159 };
158 160
159 // Set up refspec based on whether we're doing a single-branch shallow clone161 // Set up refspec based on whether we're doing a single-branch shallow clone,
162 // which requires a single ref to match Git unless it's overridden.
160 if remote.fetch_specs.is_empty() {163 if remote.fetch_specs.is_empty() {
161 if let Some(target_ref) = &target_ref {164 if let Some(target_ref) = &target_ref {
162 // Single-branch refspec for shallow clones165 // Single-branch refspec for shallow clones
163 let short_name = target_ref.strip_prefix("refs/heads/").unwrap_or(target_ref.as_str());166 let short_name = target_ref.shorten();
164 remote = remote167 remote = remote
165 .with_refspecs(168 .with_refspecs(
166 Some(format!("+{target_ref}:refs/remotes/{remote_name}/{short_name}").as_str()),169 Some(format!("+{target_ref}:refs/remotes/{remote_name}/{short_name}").as_str()),
gix/src/config/tree/sections/init.rs+1−0
@@ -5,6 +5,7 @@ use crate::{ · 4 unmodified lines
5 5
6impl Init {6impl Init {
7 /// The `init.defaultBranch` key.7 /// The `init.defaultBranch` key.
8 // TODO: review its usage for cases where this key is not set - sometimes it's 'master', sometimes it's 'main'.
8 pub const DEFAULT_BRANCH: keys::Any = keys::Any::new("defaultBranch", &config::Tree::INIT)9 pub const DEFAULT_BRANCH: keys::Any = keys::Any::new("defaultBranch", &config::Tree::INIT)
9 .with_deviation("If not set, we use `main` instead of `master`");10 .with_deviation("If not set, we use `main` instead of `master`");
10}11}
gix/tests/gix/clone.rs+22−14
@@ -4,6 +4,10 @@ use crate::{remote, util::restricted}; · 3 unmodified lines
4mod blocking_io {4mod blocking_io {
5 use std::{borrow::Cow, path::Path, sync::atomic::AtomicBool};5 use std::{borrow::Cow, path::Path, sync::atomic::AtomicBool};
6 6
7 use crate::{
8 remote,
9 util::{hex_to_id, restricted},
10 };
7 use gix::{11 use gix::{
8 bstr::BString,12 bstr::BString,
9 config::tree::{Clone, Core, Init, Key},13 config::tree::{Clone, Core, Init, Key},
@@ -14,11 +18,7 @@ mod blocking_io { · 4 unmodified lines
14 };18 };
15 use gix_object::bstr::ByteSlice;19 use gix_object::bstr::ByteSlice;
16 use gix_ref::TargetRef;20 use gix_ref::TargetRef;
17 21 use gix_refspec::parse::Operation;
18 use crate::{
19 remote,
20 util::{hex_to_id, restricted},
21 };
22 22
23 #[test]23 #[test]
24 fn fetch_shallow_no_checkout_then_unshallow() -> crate::Result {24 fn fetch_shallow_no_checkout_then_unshallow() -> crate::Result {
@@ -104,15 +104,14 @@ mod blocking_io { · 79 unmodified lines
104 104
105 // The refspec should be for a single branch (main), not a wildcard105 // The refspec should be for a single branch (main), not a wildcard
106 let refspec_str = refspecs[0].to_str().expect("valid utf8");106 let refspec_str = refspecs[0].to_str().expect("valid utf8");
107 assert!(107 assert_eq!(
108 !refspec_str.contains("*"),108 refspec_str,
109 "shallow clone refspec should not use wildcard: {}",109 if cfg!(windows) {
110 refspec_str110 "+refs/heads/master:refs/remotes/origin/master"
111 );111 } else {
112 assert!(112 "+refs/heads/main:refs/remotes/origin/main"
113 refspec_str.contains("refs/heads/main"),113 },
114 "shallow clone refspec should reference the main branch: {}",114 "shallow clone refspec should not use wildcard and should be the main branch: {refspec_str}"
115 refspec_str
116 );115 );
117 116
118 Ok(())117 Ok(())
@@ -238,7 +237,16 @@ mod blocking_io { · 119 unmodified lines
238 fn from_non_shallow_by_deepen_exclude_then_deepen_to_unshallow() -> crate::Result {237 fn from_non_shallow_by_deepen_exclude_then_deepen_to_unshallow() -> crate::Result {
239 let tmp = gix_testtools::tempfile::TempDir::new()?;238 let tmp = gix_testtools::tempfile::TempDir::new()?;
240 let excluded_leaf_refs = ["g", "h", "j"];239 let excluded_leaf_refs = ["g", "h", "j"];
240
241 let (repo, _change) = gix::prepare_clone_bare(remote::repo("base").path(), tmp.path())?241 let (repo, _change) = gix::prepare_clone_bare(remote::repo("base").path(), tmp.path())?
242 .with_fetch_options(gix::remote::ref_map::Options {
243 extra_refspecs: vec![gix::refspec::parse(
244 "refs/heads/*:refs/remotes/origin/*".into(),
245 Operation::Fetch,
246 )?
247 .into()],
248 ..Default::default()
249 })
242 .with_shallow(Shallow::Exclude {250 .with_shallow(Shallow::Exclude {
243 remote_refs: excluded_leaf_refs251 remote_refs: excluded_leaf_refs
244 .into_iter()252 .into_iter()