Skip to content

Added some low- and high-level bindings for creating branches, settin… #543

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

Merged
merged 3 commits into from
Apr 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,10 @@ extern "C" {
repo: *mut git_repository,
commitish: *const git_oid,
) -> c_int;
pub fn git_repository_set_head_detached_from_annotated(
repo: *mut git_repository,
commitish: *const git_annotated_commit,
) -> c_int;
pub fn git_repository_set_bare(repo: *mut git_repository) -> c_int;
pub fn git_repository_is_worktree(repo: *const git_repository) -> c_int;
pub fn git_repository_is_bare(repo: *const git_repository) -> c_int;
Expand Down Expand Up @@ -2528,6 +2532,13 @@ extern "C" {
target: *const git_commit,
force: c_int,
) -> c_int;
pub fn git_branch_create_from_annotated(
ref_out: *mut *mut git_reference,
repository: *mut git_repository,
branch_name: *const c_char,
commit: *const git_annotated_commit,
force: c_int,
) -> c_int;
pub fn git_branch_delete(branch: *mut git_reference) -> c_int;
pub fn git_branch_is_head(branch: *const git_reference) -> c_int;
pub fn git_branch_iterator_free(iter: *mut git_branch_iterator);
Expand Down Expand Up @@ -2891,6 +2902,7 @@ extern "C" {

// merge
pub fn git_annotated_commit_id(commit: *const git_annotated_commit) -> *const git_oid;
pub fn git_annotated_commit_ref(commit: *const git_annotated_commit) -> *const c_char;
pub fn git_annotated_commit_from_ref(
out: *mut *mut git_annotated_commit,
repo: *mut git_repository,
Expand Down
13 changes: 13 additions & 0 deletions src/merge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::c_uint;
use std::marker;
use std::mem;
use std::str;

use crate::call::Convert;
use crate::util::Binding;
Expand All @@ -26,6 +27,18 @@ impl<'repo> AnnotatedCommit<'repo> {
pub fn id(&self) -> Oid {
unsafe { Binding::from_raw(raw::git_annotated_commit_id(self.raw)) }
}

/// Get the refname that the given git_annotated_commit refers to
///
/// Returns None if it is not valid utf8
pub fn refname(&self) -> Option<&str> {
str::from_utf8(self.refname_bytes()).ok()
}

/// Get the refname that the given git_annotated_commit refers to.
pub fn refname_bytes(&self) -> &[u8] {
unsafe { crate::opt_bytes(self, raw::git_annotated_commit_ref(&*self.raw)).unwrap() }
}
}

impl Default for MergeOptions {
Expand Down
49 changes: 49 additions & 0 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,27 @@ impl Repository {
Ok(())
}

/// Make the repository HEAD directly point to the commit.
///
/// If the provided committish cannot be found in the repository, the HEAD
/// is unaltered and an error is returned.
/// If the provided commitish cannot be peeled into a commit, the HEAD is
/// unaltered and an error is returned.
/// Otherwise, the HEAD will eventually be detached and will directly point
/// to the peeled commit.
pub fn set_head_detached_from_annotated(
&self,
commitish: AnnotatedCommit<'_>,
) -> Result<(), Error> {
unsafe {
try_call!(raw::git_repository_set_head_detached_from_annotated(
self.raw,
commitish.raw()
));
}
Ok(())
}

/// Create an iterator for the repo's references
pub fn references(&self) -> Result<References<'_>, Error> {
let mut ret = ptr::null_mut();
Expand Down Expand Up @@ -1053,6 +1074,34 @@ impl Repository {
}
}

/// Create a new branch pointing at a target commit
///
/// This behaves like `Repository::branch()` but takes
/// an annotated commit, which lets you specify which
/// extended sha syntax string was specified by a user,
/// allowing for more exact reflog messages.
///
/// See the documentation for `Repository::branch()`
pub fn branch_from_annotated_commit(
&self,
branch_name: &str,
target: &AnnotatedCommit<'_>,
force: bool,
) -> Result<Branch<'_>, Error> {
let branch_name = CString::new(branch_name)?;
let mut raw = ptr::null_mut();
unsafe {
try_call!(raw::git_branch_create_from_annotated(
&mut raw,
self.raw(),
branch_name,
target.raw(),
force
));
Ok(Branch::wrap(Binding::from_raw(raw)))
}
}

/// Lookup a branch by its name in a repository.
pub fn find_branch(&self, name: &str, branch_type: BranchType) -> Result<Branch<'_>, Error> {
let name = CString::new(name)?;
Expand Down