Skip to content

Commit 8524379

Browse files
committed
Amended IndexConflict to make it sensitive to the possibility that git_index_conflict_iterator returns a NULL pointer for one of the trio of IndexEntries. Also added Reference::find_reference_dwim to make it a little easier to find refs.
1 parent 718799c commit 8524379

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

libgit2-sys/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,9 @@ extern {
18861886
pub fn git_reference_lookup(out: *mut *mut git_reference,
18871887
repo: *mut git_repository,
18881888
name: *const c_char) -> c_int;
1889+
pub fn git_reference_dwim(out: *mut *mut git_reference,
1890+
repo: *mut git_repository,
1891+
refname: *const c_char) -> c_int;
18891892
pub fn git_reference_name(r: *const git_reference) -> *const c_char;
18901893
pub fn git_reference_name_to_id(out: *mut git_oid,
18911894
repo: *mut git_repository,

src/index.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ pub struct IndexConflicts<'index> {
3333
/// A structure to represent the information returned when a conflict is detected in an index entry
3434
pub struct IndexConflict {
3535
/// The ancestor index entry of the two conflicting index entries
36-
pub ancestor: IndexEntry,
36+
pub ancestor: Option<IndexEntry>,
3737
/// The index entry originating from the user's copy of the repository.
3838
/// Its contents conflict with 'their' index entry
39-
pub our: IndexEntry,
39+
pub our: Option<IndexEntry>,
4040
/// The index entry originating from the external repository.
4141
/// Its contents conflict with 'our' index entry
42-
pub their: IndexEntry,
42+
pub their: Option<IndexEntry>,
4343
}
4444

4545
/// A callback function to filter index matches.
@@ -588,9 +588,18 @@ impl<'index> Iterator for IndexConflicts<'index> {
588588
self.conflict_iter
589589
));
590590
Some(Ok(IndexConflict {
591-
ancestor: IndexEntry::from_raw(*ancestor),
592-
our: IndexEntry::from_raw(*our),
593-
their: IndexEntry::from_raw(*their),
591+
ancestor: match ancestor.is_null() {
592+
false => Some(IndexEntry::from_raw(*ancestor)),
593+
true => None,
594+
},
595+
our: match our.is_null() {
596+
false => Some(IndexEntry::from_raw(*our)),
597+
true => None,
598+
},
599+
their: match their.is_null() {
600+
false => Some(IndexEntry::from_raw(*their)),
601+
true => None,
602+
},
594603
}))
595604
}
596605
}

src/repo.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,19 @@ impl Repository {
12181218
}
12191219
}
12201220

1221+
/// Lookup a reference to one of the objects in a repository.
1222+
/// `Repository::find_reference` with teeth; give the method your reference in
1223+
/// human-readable format e.g. 'master' instead of 'refs/heads/master', and it
1224+
/// will do-what-you-mean, returning the `Reference`.
1225+
pub fn find_reference_dwim(&self, refname: &str) -> Result<Reference, Error> {
1226+
let refname = try!(CString::new(refname));
1227+
let mut raw = ptr::null_mut();
1228+
unsafe {
1229+
try_call!(raw::git_reference_dwim(&mut raw, self.raw(), refname));
1230+
Ok(Binding::from_raw(raw))
1231+
}
1232+
}
1233+
12211234
/// Lookup a reference by name and resolve immediately to OID.
12221235
///
12231236
/// This function provides a quick way to resolve a reference name straight

0 commit comments

Comments
 (0)