use crate::bstr::{ByteSlice, ByteVec}; use crate::{remote, Reference}; use std::borrow::Cow; /// Remotes impl<'repo> Reference<'repo> { /// Find the name of our remote for `direction` as configured in `branch..remote|pushRemote` respectively. /// If `Some()` it can be used in [`Repository::find_remote(…)`][crate::Repository::find_remote()], or if `None` then /// [Repository::remote_default_name()][crate::Repository::remote_default_name()] could be used in its place. /// /// Return `None` if no remote is configured. /// /// # Note /// /// - it's recommended to use the [`remote(…)`][Self::remote()] method as it will configure the remote with additional /// information. /// - `branch..pushRemote` falls back to `branch..remote`. pub fn remote_name(&self, direction: remote::Direction) -> Option> { let name = self.name().shorten().to_str().ok()?; let config = &self.repo.config.resolved; (direction == remote::Direction::Push) .then(|| { config .string("branch", Some(name), "pushRemote") .or_else(|| config.string("remote", None, "pushDefault")) }) .flatten() .or_else(|| config.string("branch", Some(name), "remote")) .and_then(|name| match name { Cow::Borrowed(n) => n.to_str().ok().map(Cow::Borrowed), Cow::Owned(n) => Vec::from(n).into_string().ok().map(Cow::Owned), }) } /// Like [`remote_name(…)`][Self::remote_name()], but configures the returned `Remote` with additional information like /// /// - `branch..merge` to know which branch on the remote side corresponds to this one for merging when pulling. pub fn remote( &self, direction: remote::Direction, ) -> Option, remote::find::existing::Error>> { let name = self.remote_name(direction)?; // TODO: use `branch..merge` self.repo.find_remote(name.as_ref()).into() } }