From 91a6a055b8fabfceadfe3ea5816618c02554fe23 Mon Sep 17 00:00:00 2001 From: Sebastian Wiesner Date: Sun, 3 Nov 2019 11:40:23 +0100 Subject: [PATCH] Add support for remotes of branches Wrap git_branch_upstream_name and git_branch_upstream_remote which retrieve the configured upstream remote and branch, but do not attempt to resolve them, i.e. these functions don't fail if the configured upstream doesn't exist. Closes GH-469 --- libgit2-sys/lib.rs | 10 ++++++++++ src/repo.rs | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/libgit2-sys/lib.rs b/libgit2-sys/lib.rs index 234ec3003a..0f1b942497 100644 --- a/libgit2-sys/lib.rs +++ b/libgit2-sys/lib.rs @@ -2424,6 +2424,16 @@ extern "C" { ) -> c_int; pub fn git_branch_upstream(out: *mut *mut git_reference, branch: *const git_reference) -> c_int; + pub fn git_branch_upstream_name( + out: *mut git_buf, + repo: *mut git_repository, + refname: *const c_char, + ) -> c_int; + pub fn git_branch_upstream_remote( + out: *mut git_buf, + repo: *mut git_repository, + refname: *const c_char, + ) -> c_int; // index pub fn git_index_add(index: *mut git_index, entry: *const git_index_entry) -> c_int; diff --git a/src/repo.rs b/src/repo.rs index 02e660d9d2..451f912ad1 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -2433,6 +2433,31 @@ impl Repository { Ok(()) } } + + /// Retrieves the name of the reference supporting the remote tracking branch, + /// given the name of a local branch reference. + pub fn branch_upstream_name(&self, refname: &str) -> Result { + let refname = CString::new(refname)?; + unsafe { + let buf = Buf::new(); + try_call!(raw::git_branch_upstream_name(buf.raw(), self.raw, refname)); + Ok(buf) + } + } + + /// Retrieve the name of the upstream remote of a local branch. + pub fn branch_upstream_remote(&self, refname: &str) -> Result { + let refname = CString::new(refname)?; + unsafe { + let buf = Buf::new(); + try_call!(raw::git_branch_upstream_remote( + buf.raw(), + self.raw, + refname + )); + Ok(buf) + } + } } impl Binding for Repository {