-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,014 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate::{ | ||
config, | ||
config::tree::{keys, Key, Push, Section}, | ||
}; | ||
|
||
impl Push { | ||
/// The `push.default` key | ||
pub const DEFAULT: Default = Default::new_with_validate("default", &config::Tree::PUSH, validate::Default); | ||
} | ||
|
||
impl Section for Push { | ||
fn name(&self) -> &str { | ||
"push" | ||
} | ||
|
||
fn keys(&self) -> &[&dyn Key] { | ||
&[&Self::DEFAULT] | ||
} | ||
} | ||
|
||
/// The `remote.<name>.tagOpt` key type. | ||
pub type Default = keys::Any<validate::Default>; | ||
|
||
mod default { | ||
use std::borrow::Cow; | ||
|
||
use crate::{ | ||
bstr::{BStr, ByteSlice}, | ||
config, | ||
config::tree::push::Default, | ||
push, | ||
}; | ||
|
||
impl Default { | ||
/// Try to interpret `value` as `push.default`. | ||
pub fn try_into_default( | ||
&'static self, | ||
value: Cow<'_, BStr>, | ||
) -> Result<push::Default, config::key::GenericErrorWithValue> { | ||
Ok(match value.as_ref().as_bytes() { | ||
b"nothing" => push::Default::Nothing, | ||
b"current" => push::Default::Current, | ||
b"upstream" | b"tracking" => push::Default::Upstream, | ||
b"simple" => push::Default::Simple, | ||
b"matching" => push::Default::Matching, | ||
_ => return Err(config::key::GenericErrorWithValue::from_value(self, value.into_owned())), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
mod validate { | ||
pub struct Default; | ||
use std::{borrow::Cow, error::Error}; | ||
|
||
use crate::{bstr::BStr, config::tree::keys::Validate}; | ||
|
||
impl Validate for Default { | ||
fn validate(&self, value: &BStr) -> Result<(), Box<dyn Error + Send + Sync + 'static>> { | ||
super::Push::DEFAULT.try_into_default(Cow::Borrowed(value))?; | ||
Ok(()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,6 +187,8 @@ pub mod tag; | |
|
||
/// | ||
pub mod progress; | ||
/// | ||
pub mod push; | ||
|
||
/// | ||
pub mod diff; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// All possible values of `push.default`. | ||
#[derive(Default, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash, Debug)] | ||
pub enum Default { | ||
/// Do not push anything unless a refspec is provided explicitly. | ||
/// | ||
/// This is for safety. | ||
Nothing, | ||
/// Push the current branch to update a remote branch with the same name. | ||
Current, | ||
/// Push the current branch to the branch it would fetch from and merge with, | ||
/// i.e. what is configured in `branch.<name>.merge`, retrievable with | ||
/// the `@{upstream}` refspec. | ||
Upstream, | ||
/// Push the current branch with the same name to the remote. | ||
/// This is the same as [`Current`](Default::Current), but fails if | ||
/// `branch.<name>.merge` is set to a branch that is named differently. | ||
#[default] | ||
Simple, | ||
/// Push *all* branches to their similarly named counterpart on the remote. | ||
Matching, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,47 @@ | ||
use crate::{config, config::tree::Branch, remote, Reference}; | ||
use crate::repository::{branch_remote_ref_name, branch_remote_tracking_ref_name}; | ||
use crate::{remote, Reference}; | ||
use gix_ref::FullNameRef; | ||
use std::borrow::Cow; | ||
|
||
/// Remotes | ||
impl<'repo> Reference<'repo> { | ||
/// Find the unvalidated name of our remote for `direction` as configured in `branch.<name>.remote|pushRemote` respectively. | ||
/// If `Some(<name>)` 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. | ||
/// | ||
/// Find the name of our remote for `direction` as configured in `branch.<name>.remote|pushRemote` respectively. | ||
/// 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.<name>.pushRemote` falls back to `branch.<name>.remote`. | ||
/// See also [`Repository::branch_remote_name()`](crate::Repository::branch_remote_name()) for more details. | ||
pub fn remote_name(&self, direction: remote::Direction) -> Option<remote::Name<'repo>> { | ||
let name = self.name().shorten(); | ||
let config = &self.repo.config.resolved; | ||
(direction == remote::Direction::Push) | ||
.then(|| { | ||
config | ||
.string("branch", Some(name), Branch::PUSH_REMOTE.name) | ||
.or_else(|| config.string("remote", None, config::tree::Remote::PUSH_DEFAULT.name)) | ||
}) | ||
.flatten() | ||
.or_else(|| config.string("branch", Some(name), Branch::REMOTE.name)) | ||
.and_then(|name| name.try_into().ok()) | ||
self.repo.branch_remote_name(self.name().shorten(), direction) | ||
} | ||
|
||
/// Like [`remote_name(…)`][Self::remote_name()], but configures the returned `Remote` with additional information like | ||
/// Find the remote along with all configuration associated with it suitable for handling this reference. | ||
/// | ||
/// - `branch.<name>.merge` to know which branch on the remote side corresponds to this one for merging when pulling. | ||
/// | ||
/// It also handles if the remote is a configured URL, which has no name. | ||
/// See also [`Repository::branch_remote()`](crate::Repository::branch_remote()) for more details. | ||
pub fn remote( | ||
&self, | ||
direction: remote::Direction, | ||
) -> Option<Result<crate::Remote<'repo>, remote::find::existing::Error>> { | ||
// TODO: use `branch.<name>.merge` | ||
self.remote_name(direction).map(|name| match name { | ||
remote::Name::Symbol(name) => self.repo.find_remote(name.as_ref()).map_err(Into::into), | ||
remote::Name::Url(url) => gix_url::parse(url.as_ref()).map_err(Into::into).and_then(|url| { | ||
self.repo | ||
.remote_at(url) | ||
.map_err(|err| remote::find::existing::Error::Find(remote::find::Error::Init(err))) | ||
}), | ||
}) | ||
self.repo.branch_remote(self.name().shorten(), direction) | ||
} | ||
|
||
/// Return the name of this reference on the remote side. | ||
/// | ||
/// See [`Repository::branch_remote_ref_name()`](crate::Repository::branch_remote_ref_name()) for details. | ||
#[doc(alias = "upstream", alias = "git2")] | ||
pub fn remote_ref_name( | ||
&self, | ||
direction: remote::Direction, | ||
) -> Option<Result<Cow<'_, FullNameRef>, branch_remote_ref_name::Error>> { | ||
self.repo.branch_remote_ref_name(self.name(), direction) | ||
} | ||
|
||
/// Return the name of the reference that tracks this reference on the remote side. | ||
/// | ||
/// See [`Repository::branch_remote_tracking_ref_name()`](crate::Repository::branch_remote_tracking_ref_name()) for details. | ||
#[doc(alias = "upstream", alias = "git2")] | ||
pub fn remote_tracking_ref_name( | ||
&self, | ||
direction: remote::Direction, | ||
) -> Option<Result<Cow<'_, FullNameRef>, branch_remote_tracking_ref_name::Error>> { | ||
self.repo.branch_remote_tracking_ref_name(self.name(), direction) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.