| 1 |
use std::marker; |
| 2 |
use std::str; |
| 3 |
|
| 4 |
use crate::util::Binding; |
| 5 |
use crate::{raw, signature, Error, Oid, Repository, Signature}; |
| 6 |
|
| 7 |
/// A structure representing a [note][note] in git. |
| 8 |
/// |
| 9 |
/// [note]: http://alblue.bandlem.com/2011/11/git-tip-of-week-git-notes.html |
| 10 |
pub struct Note<'repo> { |
| 11 |
raw: *mut raw::git_note, |
| 12 |
|
| 13 |
// Hmm, the current libgit2 version does not have this inside of it, but |
| 14 |
// perhaps it's a good idea to keep it around? Can always remove it later I |
| 15 |
// suppose... |
| 16 |
_marker: marker::PhantomData<&'repo Repository>, |
| 17 |
} |
| 18 |
|
| 19 |
/// An iterator over all of the notes within a repository. |
| 20 |
pub struct Notes<'repo> { |
| 21 |
raw: *mut raw::git_note_iterator, |
| 22 |
_marker: marker::PhantomData<&'repo Repository>, |
| 23 |
} |
| 24 |
|
| 25 |
impl<'repo> Note<'repo> { |
| 26 |
/// Get the note author |
| 27 |
pub fn author(&self) -> Signature<'_> { |
| 28 |
unsafe { signature::from_raw_const(self, raw::git_note_author(&*self.raw)) } |
| 29 |
} |
| 30 |
|
| 31 |
/// Get the note committer |
| 32 |
pub fn committer(&self) -> Signature<'_> { |
| 33 |
unsafe { signature::from_raw_const(self, raw::git_note_committer(&*self.raw)) } |
| 34 |
} |
| 35 |
|
| 36 |
/// Get the note message, in bytes. |
| 37 |
pub fn message_bytes(&self) -> &[u8] { |
| 38 |
unsafe { crate::opt_bytes(self, raw::git_note_message(&*self.raw)).unwrap() } |
| 39 |
} |
| 40 |
|
| 41 |
/// Get the note message as a string. |
| 42 |
pub fn message(&self) -> Result<&str, Error> { |
| 43 |
str::from_utf8(self.message_bytes()).map_err(|e| e.into()) |
| 44 |
} |
| 45 |
|
| 46 |
/// Get the note object's id |
| 47 |
pub fn id(&self) -> Oid { |
| 48 |
unsafe { Binding::from_raw(raw::git_note_id(&*self.raw)) } |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl<'repo> Binding for Note<'repo> { |
| 53 |
type Raw = *mut raw::git_note; |
| 54 |
unsafe fn from_raw(raw: *mut raw::git_note) -> Note<'repo> { |
| 55 |
Note { |
| 56 |
raw, |
| 57 |
_marker: marker::PhantomData, |
| 58 |
} |
| 59 |
} |
| 60 |
fn raw(&self) -> *mut raw::git_note { |
| 61 |
self.raw |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl<'repo> std::fmt::Debug for Note<'repo> { |
| 66 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { |
| 67 |
f.debug_struct("Note").field("id", &self.id()).finish() |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
impl<'repo> Drop for Note<'repo> { |
| 72 |
fn drop(&mut self) { |
| 73 |
unsafe { |
| 74 |
raw::git_note_free(self.raw); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl<'repo> Binding for Notes<'repo> { |
| 80 |
type Raw = *mut raw::git_note_iterator; |
| 81 |
unsafe fn from_raw(raw: *mut raw::git_note_iterator) -> Notes<'repo> { |
| 82 |
Notes { |
| 83 |
raw, |
| 84 |
_marker: marker::PhantomData, |
| 85 |
} |
| 86 |
} |
| 87 |
fn raw(&self) -> *mut raw::git_note_iterator { |
| 88 |
self.raw |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
impl<'repo> Iterator for Notes<'repo> { |
| 93 |
type Item = Result<(Oid, Oid), Error>; |
| 94 |
fn next(&mut self) -> Option<Result<(Oid, Oid), Error>> { |
| 95 |
let mut note_id = crate::util::zeroed_raw_oid(); |
| 96 |
let mut annotated_id = note_id; |
| 97 |
unsafe { |
| 98 |
try_call_iter!(raw::git_note_next( |
| 99 |
&mut note_id, |
| 100 |
&mut annotated_id, |
| 101 |
self.raw |
| 102 |
)); |
| 103 |
Some(Ok(( |
| 104 |
Binding::from_raw(¬e_id as *const _), |
| 105 |
Binding::from_raw(&annotated_id as *const _), |
| 106 |
))) |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
impl<'repo> Drop for Notes<'repo> { |
| 112 |
fn drop(&mut self) { |
| 113 |
unsafe { |
| 114 |
raw::git_note_iterator_free(self.raw); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
#[cfg(test)] |
| 120 |
mod tests { |
| 121 |
#[test] |
| 122 |
fn smoke() { |
| 123 |
let (_td, repo) = crate::test::repo_init(); |
| 124 |
assert!(repo.notes(None).is_err()); |
| 125 |
|
| 126 |
let sig = repo.signature().unwrap(); |
| 127 |
let head = repo.head().unwrap().target().unwrap(); |
| 128 |
let note = repo.note(&sig, &sig, None, head, "foo", false).unwrap(); |
| 129 |
assert_eq!(repo.notes(None).unwrap().count(), 1); |
| 130 |
|
| 131 |
let note_obj = repo.find_note(None, head).unwrap(); |
| 132 |
assert_eq!(note_obj.id(), note); |
| 133 |
assert_eq!(note_obj.message(), Ok("foo")); |
| 134 |
|
| 135 |
let (a, b) = repo.notes(None).unwrap().next().unwrap().unwrap(); |
| 136 |
assert_eq!(a, note); |
| 137 |
assert_eq!(b, head); |
| 138 |
|
| 139 |
assert_eq!(repo.note_default_ref().unwrap(), "refs/notes/commits"); |
| 140 |
|
| 141 |
assert_eq!(sig.name(), note_obj.author().name()); |
| 142 |
assert_eq!(sig.name(), note_obj.committer().name()); |
| 143 |
assert!(sig.when() == note_obj.committer().when()); |
| 144 |
} |
| 145 |
} |