Skip to content

Commit

Permalink
Use rev_parse_single for PrepareCheckout
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernthedev committed Aug 2, 2024
1 parent e56f2c5 commit c670485
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 49 deletions.
43 changes: 1 addition & 42 deletions gix/src/clone/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,49 +61,8 @@ pub mod main_worktree {

/// Modification
impl PrepareCheckout {
/// Set the `name` of the reference to check out, instead of the remote `HEAD`.
/// If `None`, the `HEAD` will be used, which is the default.
///
/// Note that `name` should be a partial name like `main` or `feat/one`, but can be a full ref name.
/// If a branch on the remote matches, it will automatically be retrieved even without a refspec.
pub fn with_ref_name<'a, Name, E>(self, ref_name: Option<Name>) -> Result<PrepareCheckout, Error>
where
Name: TryInto<&'a gix_ref::PartialNameRef, Error = E>,
gix_ref::file::find::Error: std::convert::From<E>,
{
let repo = self
.repo
.as_ref()
.expect("BUG: this method may only be called until it is successful");

let reference = ref_name
.map(|ref_name| repo.try_find_reference(ref_name))
.transpose()
.map_err(|e| Error::FindHead(crate::reference::find::existing::Error::Find(e)))?
.flatten()
.map(|r| r.detach());

self.with_ref(reference)
}

/// Set the reference to checkout from the tree.
pub fn with_ref<'a>(mut self, ref_val: Option<gix_ref::Reference>) -> Result<PrepareCheckout, Error> {
let repo = self
.repo
.as_ref()
.expect("BUG: this method may only be called until it is successful");

self.checkout_object = ref_val
.map(|r| r.attach(repo))
.map(|r| r.into_fully_peeled_id())
.transpose()?
.map(|r| r.inner);

Ok(self)
}

/// Set the reference to checkout from the tree.
pub fn with_object_id(mut self, object_id: Option<crate::ObjectId>) -> PrepareCheckout {
pub fn with_rev_single(mut self, object_id: Option<gix_hash::ObjectId>) -> PrepareCheckout {
self.checkout_object = object_id;

self
Expand Down
23 changes: 16 additions & 7 deletions gix/src/clone/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Error {
},
#[error("Failed to update HEAD with values from remote")]
HeadUpdate(#[from] crate::reference::edit::Error),
#[error("Failed to parse revision")]
RefParseError(#[from] crate::revision::spec::parse::single::Error),
#[error("The remote didn't have any ref that matched '{}'", wanted.as_ref().as_bstr())]
RefNameMissing { wanted: gix_ref::PartialName },
#[error("The remote has {} refs for '{}', try to use a specific name: {}", candidates.len(), wanted.as_ref().as_bstr(), candidates.iter().filter_map(|n| n.to_str().ok()).collect::<Vec<_>>().join(", "))]
Expand Down Expand Up @@ -228,14 +230,21 @@ impl PrepareFetch {
P: crate::NestedProgress,
P::SubProgress: 'static,
{

let (repo, fetch_outcome) = self.fetch_only(progress, should_interrupt)?;
Ok((
crate::clone::PrepareCheckout {
repo: repo.into(),
checkout_object: None,
}.with_ref_name(self.ref_name.as_ref()).expect("BUG: Ref not found in repo after fetching!"),
fetch_outcome,
))

let mut checkout = crate::clone::PrepareCheckout {
repo: repo.into(),
checkout_object: None,
};

if let Some(ref_name) = self.ref_name.as_ref() {
let bstring: &gix_ref::PartialNameRef = ref_name.as_ref();
let rev = checkout.repo().rev_parse_single(bstring.as_bstr())?.detach();
checkout = checkout.with_rev_single(Some(rev));
}

Ok((checkout, fetch_outcome))
}
}

Expand Down

0 comments on commit c670485

Please sign in to comment.