git2/
note.rs

1use std::marker;
2use std::str;
3
4use crate::util::Binding;
5use 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
10pub 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.
20pub struct Notes<'repo> {
21    raw: *mut raw::git_note_iterator,
22    _marker: marker::PhantomData<&'repo Repository>,
23}
24
25impl<'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, returning `None` if it is not UTF-8.
42    pub fn message(&self) -> Option<&str> {
43        str::from_utf8(self.message_bytes()).ok()
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
52impl<'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
65impl<'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
71impl<'repo> Drop for Note<'repo> {
72    fn drop(&mut self) {
73        unsafe {
74            raw::git_note_free(self.raw);
75        }
76    }
77}
78
79impl<'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
92impl<'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],
97        };
98        let mut annotated_id = note_id;
99        unsafe {
100            try_call_iter!(raw::git_note_next(
101                &mut note_id,
102                &mut annotated_id,
103                self.raw
104            ));
105            Some(Ok((
106                Binding::from_raw(&note_id as *const _),
107                Binding::from_raw(&annotated_id as *const _),
108            )))
109        }
110    }
111}
112
113impl<'repo> Drop for Notes<'repo> {
114    fn drop(&mut self) {
115        unsafe {
116            raw::git_note_iterator_free(self.raw);
117        }
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    #[test]
124    fn smoke() {
125        let (_td, repo) = crate::test::repo_init();
126        assert!(repo.notes(None).is_err());
127
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);
132
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"));
136
137        let (a, b) = repo.notes(None).unwrap().next().unwrap().unwrap();
138        assert_eq!(a, note);
139        assert_eq!(b, head);
140
141        assert_eq!(repo.note_default_ref().unwrap(), "refs/notes/commits");
142
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());
146    }
147}