4 use crate::util
::Binding
;
5 use crate::{raw, signature, Error, Oid, Repository, Signature}
;
7 /// A structure representing a [note][note] in git.
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
,
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
16 _marker
: marker
::PhantomData
<&'repo Repository
>,
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
>,
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)) }
31 /// Get the note committer
32 pub fn committer(&self) -> Signature
<'_
> {
33 unsafe { signature::from_raw_const(self, raw::git_note_committer(&*self.raw)) }
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() }
41 /// Get the note message as a string, returning `None` if it is not UTF-8.
42 pub fn message(&self) -> Option
<&str> {
43 str::from_utf8(self.message_bytes()).ok()
46 /// Get the note object's id
47 pub fn id(&self) -> Oid
{
48 unsafe { Binding::from_raw(raw::git_note_id(&*self.raw)) }
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
> {
57 _marker
: marker
::PhantomData
,
60 fn raw(&self) -> *mut raw
::git_note
{
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()
71 impl<'repo
> Drop
for Note
<'repo
> {
74 raw
::git_note_free(self.raw
);
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
> {
84 _marker
: marker
::PhantomData
,
87 fn raw(&self) -> *mut raw
::git_note_iterator
{
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
= raw
::git_oid
{
96 id
: [0; raw
::GIT_OID_RAWSZ
],
98 let mut annotated_id
= note_id
;
100 try_call_iter
!(raw
::git_note_next(
106 Binding
::from_raw(¬e_id
as *const _
),
107 Binding
::from_raw(&annotated_id
as *const _
),
113 impl<'repo
> Drop
for Notes
<'repo
> {
116 raw
::git_note_iterator_free(self.raw
);
125 let (_td
, repo
) = crate::test
::repo_init();
126 assert
!(repo
.notes(None
).is_err());
128 let sig
= repo
.signature().unwrap();
129 let head
= repo
.head().unwrap().target().unwrap();
130 let note
= repo
.note(&sig
, &sig
, None
, head
, "foo", false).unwrap();
131 assert_eq
!(repo
.notes(None
).unwrap().count(), 1);
133 let note_obj
= repo
.find_note(None
, head
).unwrap();
134 assert_eq
!(note_obj
.id(), note
);
135 assert_eq
!(note_obj
.message(), Some("foo"));
137 let (a
, b
) = repo
.notes(None
).unwrap().next().unwrap().unwrap();
141 assert_eq
!(repo
.note_default_ref().unwrap(), "refs/notes/commits");
143 assert_eq
!(sig
.name(), note_obj
.author().name());
144 assert_eq
!(sig
.name(), note_obj
.committer().name());
145 assert
!(sig
.when() == note_obj
.committer().when());