Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindings for git_remote_default_branch (#572) #578

Merged
merged 1 commit into from Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions libgit2-sys/lib.rs
Expand Up @@ -2049,6 +2049,7 @@ extern "C" {
remote: *mut git_remote,
callbacks: *const git_remote_callbacks,
) -> c_int;
pub fn git_remote_default_branch(out: *mut git_buf, remote: *mut git_remote) -> c_int;

// refspec
pub fn git_refspec_direction(spec: *const git_refspec) -> git_direction;
Expand Down
24 changes: 23 additions & 1 deletion src/remote.rs
Expand Up @@ -9,7 +9,7 @@ use std::str;

use crate::string_array::StringArray;
use crate::util::Binding;
use crate::{raw, Direction, Error, FetchPrune, Oid, ProxyOptions, Refspec};
use crate::{raw, Buf, Direction, Error, FetchPrune, Oid, ProxyOptions, Refspec};
use crate::{AutotagOption, Progress, RemoteCallbacks, Repository};

/// A structure representing a [remote][1] of a git repository.
Expand Down Expand Up @@ -112,6 +112,20 @@ impl<'repo> Remote<'repo> {
unsafe { crate::opt_bytes(self, raw::git_remote_pushurl(&*self.raw)) }
}

/// Get the remote's default branch.
///
/// The remote (or more exactly its transport) must have connected to the
/// remote repository. This default branch is available as soon as the
/// connection to the remote is initiated and it remains available after
/// disconnecting.
pub fn default_branch(&self) -> Result<Buf, Error> {
unsafe {
let buf = Buf::new();
try_call!(raw::git_remote_default_branch(buf.raw(), self.raw));
Ok(buf)
}
}

/// Open a connection to a remote.
pub fn connect(&mut self, dir: Direction) -> Result<(), Error> {
// TODO: can callbacks be exposed safely?
Expand Down Expand Up @@ -576,6 +590,14 @@ impl<'repo, 'connection, 'cb> RemoteConnection<'repo, 'connection, 'cb> {
pub fn list(&self) -> Result<&[RemoteHead<'_>], Error> {
self.remote.list()
}

/// Get the remote's default branch.
///
/// This default branch is available as soon as the connection to the remote
/// is initiated and it remains available after disconnecting.
pub fn default_branch(&self) -> Result<Buf, Error> {
self.remote.default_branch()
}
}

impl<'repo, 'connection, 'cb> Drop for RemoteConnection<'repo, 'connection, 'cb> {
Expand Down