Skip to content

Commit

Permalink
Add method to allow replacing a Remote's refspecs entirely. (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 18, 2022
1 parent 6f60a79 commit d8f1608
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions git-repository/src/remote/access.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use git_refspec::RefSpec;

use crate::bstr::BStr;
use crate::{remote, Remote};

/// Access
Expand Down Expand Up @@ -60,4 +61,36 @@ impl Remote<'_> {
url_err.or(push_url_err).map(Err::<&mut Self, _>).transpose()?;
Ok(self)
}

/// Replace all currently set refspecs, typically from configuration, with the given `specs` for `direction`,
/// or `None` if one of the input specs could not be parsed.
pub fn replace_refspecs<Spec>(
&mut self,
specs: impl IntoIterator<Item = Spec>,
direction: remote::Direction,
) -> Result<(), git_refspec::parse::Error>
where
Spec: AsRef<BStr>,
{
use remote::Direction::*;
let specs: Vec<_> = specs
.into_iter()
.map(|spec| {
git_refspec::parse(
spec.as_ref(),
match direction {
Push => git_refspec::parse::Operation::Push,
Fetch => git_refspec::parse::Operation::Fetch,
},
)
.map(|url| url.to_owned())
})
.collect::<Result<_, _>>()?;
let dst = match direction {
Push => &mut self.push_specs,
Fetch => &mut self.fetch_specs,
};
*dst = specs;
Ok(())
}
}

0 comments on commit d8f1608

Please sign in to comment.