Skip to content

Commit 022540d

Browse files
committed
feat: add Repository::upstream_branch_and_remote_name_for_tracking_branch()
It's a way to learn about the Remote and upstream branch which would match the given local tracking branch.
1 parent 288ba9c commit 022540d

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

gix/src/repository/config/branch.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use gix_ref::{FullName, FullNameRef};
55
use crate::bstr::BStr;
66
use crate::config::cache::util::ApplyLeniencyDefault;
77
use crate::config::tree::{Branch, Push};
8-
use crate::repository::{branch_remote_ref_name, branch_remote_tracking_ref_name};
8+
use crate::repository::{
9+
branch_remote_ref_name, branch_remote_tracking_ref_name, upstream_branch_and_remote_name_for_tracking_branch,
10+
};
911
use crate::{push, remote};
1012

1113
/// Query configuration related to branches.
@@ -20,19 +22,18 @@ impl crate::Repository {
2022
self.subsection_str_names_of("branch")
2123
}
2224

23-
/// Returns the validated reference on the remote associated with the given `name`,
25+
/// Returns the validated reference name of the upstream branch on the remote associated with the given `name`,
2426
/// which will be used when *merging*.
25-
/// The returned value corresponds to the `branch.<short_branch_name>.merge` configuration key.
27+
/// The returned value corresponds to the `branch.<short_branch_name>.merge` configuration key for [`remote::Direction::Fetch`].
28+
/// For the [push direction](`remote::Direction::Push`) the Git configuration is used for a variety of different outcomes,
29+
/// similar to what would happen when running `git push <name>`.
2630
///
27-
/// Returns `None` if there is no value at the given key, or if no remote or remote ref is configured.
28-
/// May return an error if the reference name to be returned is invalid.
31+
/// Returns `None` if there is nothing configured, or if no remote or remote ref is configured.
2932
///
3033
/// ### Note
3134
///
32-
/// This name refers to what Git calls upstream branch (as opposed to upstream *tracking* branch).
35+
/// The returned name refers to what Git calls upstream branch (as opposed to upstream *tracking* branch).
3336
/// The value is also fast to retrieve compared to its tracking branch.
34-
/// Also note that a [remote::Direction] isn't used here as Git only supports (and requires) configuring
35-
/// the remote to fetch from, not the one to push to.
3637
///
3738
/// See also [`Reference::remote_ref_name()`](crate::Reference::remote_ref_name()).
3839
#[doc(alias = "branch_upstream_name", alias = "git2")]
@@ -125,6 +126,26 @@ impl crate::Repository {
125126
.map(|res| res.map_err(Into::into))
126127
}
127128

129+
/// Given a local `tracking_branch` name, find the remote that maps to it along with the name of the branch on
130+
/// the side of the remote, also called upstream branch.
131+
///
132+
/// Return `Ok(None)` if there is no remote with fetch-refspecs that would match `tracking_branch` on the right-hand side,
133+
/// or `Err` if the matches were ambiguous.
134+
///
135+
/// ### Limitations
136+
///
137+
/// A single valid mapping is required as fine-grained matching isn't implemented yet. This means that
138+
pub fn upstream_branch_and_remote_name_for_tracking_branch(
139+
&self,
140+
tracking_branch: &FullNameRef,
141+
) -> Result<
142+
Option<(Cow<'_, FullNameRef>, remote::Name<'_>)>,
143+
upstream_branch_and_remote_name_for_tracking_branch::Error,
144+
> {
145+
for remote_name in self.remote_names() {}
146+
todo!()
147+
}
148+
128149
/// Returns the unvalidated name of the remote associated with the given `short_branch_name`,
129150
/// typically `main` instead of `refs/heads/main`.
130151
/// In some cases, the returned name will be an URL.

gix/src/repository/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,17 @@ pub mod branch_remote_tracking_ref_name {
360360
}
361361
}
362362

363+
///
364+
pub mod upstream_branch_and_remote_name_for_tracking_branch {
365+
/// The error returned by [Repository::upstream_branch_and_remote_name_for_tracking_branch()](crate::Repository::upstream_branch_and_remote_name_for_tracking_branch()).
366+
#[derive(Debug, thiserror::Error)]
367+
#[allow(missing_docs)]
368+
pub enum Error {
369+
#[error("The name of the tracking reference was invalid")]
370+
ValidateTrackingRef(#[from] gix_validate::reference::name::Error),
371+
}
372+
}
373+
363374
///
364375
#[cfg(feature = "attributes")]
365376
pub mod pathspec_defaults_ignore_case {

0 commit comments

Comments
 (0)