| 1 |
use libc::size_t; |
| 2 |
use std::iter::FusedIterator; |
| 3 |
use std::marker; |
| 4 |
use std::ops::Range; |
| 5 |
use std::str; |
| 6 |
|
| 7 |
use crate::util::Binding; |
| 8 |
use crate::{raw, signature, Error, Oid, Signature}; |
| 9 |
|
| 10 |
/// A reference log of a git repository. |
| 11 |
pub struct Reflog { |
| 12 |
raw: *mut raw::git_reflog, |
| 13 |
} |
| 14 |
|
| 15 |
/// An entry inside the reflog of a repository |
| 16 |
pub struct ReflogEntry<'reflog> { |
| 17 |
raw: *const raw::git_reflog_entry, |
| 18 |
_marker: marker::PhantomData<&'reflog Reflog>, |
| 19 |
} |
| 20 |
|
| 21 |
/// An iterator over the entries inside of a reflog. |
| 22 |
pub struct ReflogIter<'reflog> { |
| 23 |
range: Range<usize>, |
| 24 |
reflog: &'reflog Reflog, |
| 25 |
} |
| 26 |
|
| 27 |
impl Reflog { |
| 28 |
/// Add a new entry to the in-memory reflog. |
| 29 |
pub fn append( |
| 30 |
&mut self, |
| 31 |
new_oid: Oid, |
| 32 |
committer: &Signature<'_>, |
| 33 |
msg: Option<&str>, |
| 34 |
) -> Result<(), Error> { |
| 35 |
let msg = crate::opt_cstr(msg)?; |
| 36 |
unsafe { |
| 37 |
try_call!(raw::git_reflog_append( |
| 38 |
self.raw, |
| 39 |
new_oid.raw(), |
| 40 |
committer.raw(), |
| 41 |
msg |
| 42 |
)); |
| 43 |
} |
| 44 |
Ok(()) |
| 45 |
} |
| 46 |
|
| 47 |
/// Remove an entry from the reflog by its index |
| 48 |
/// |
| 49 |
/// To ensure there's no gap in the log history, set rewrite_previous_entry |
| 50 |
/// param value to `true`. When deleting entry n, member old_oid of entry |
| 51 |
/// n-1 (if any) will be updated with the value of member new_oid of entry |
| 52 |
/// n+1. |
| 53 |
pub fn remove(&mut self, i: usize, rewrite_previous_entry: bool) -> Result<(), Error> { |
| 54 |
unsafe { |
| 55 |
try_call!(raw::git_reflog_drop( |
| 56 |
self.raw, |
| 57 |
i as size_t, |
| 58 |
rewrite_previous_entry |
| 59 |
)); |
| 60 |
} |
| 61 |
Ok(()) |
| 62 |
} |
| 63 |
|
| 64 |
/// Lookup an entry by its index |
| 65 |
/// |
| 66 |
/// Requesting the reflog entry with an index of 0 (zero) will return the |
| 67 |
/// most recently created entry. |
| 68 |
pub fn get(&self, i: usize) -> Option<ReflogEntry<'_>> { |
| 69 |
unsafe { |
| 70 |
let ptr = raw::git_reflog_entry_byindex(self.raw, i as size_t); |
| 71 |
Binding::from_raw_opt(ptr) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/// Get the number of log entries in a reflog |
| 76 |
pub fn len(&self) -> usize { |
| 77 |
unsafe { raw::git_reflog_entrycount(self.raw) as usize } |
| 78 |
} |
| 79 |
|
| 80 |
/// Return `true ` is there is no log entry in a reflog |
| 81 |
pub fn is_empty(&self) -> bool { |
| 82 |
self.len() == 0 |
| 83 |
} |
| 84 |
|
| 85 |
/// Get an iterator to all entries inside of this reflog |
| 86 |
pub fn iter(&self) -> ReflogIter<'_> { |
| 87 |
ReflogIter { |
| 88 |
range: 0..self.len(), |
| 89 |
reflog: self, |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/// Write an existing in-memory reflog object back to disk using an atomic |
| 94 |
/// file lock. |
| 95 |
pub fn write(&mut self) -> Result<(), Error> { |
| 96 |
unsafe { |
| 97 |
try_call!(raw::git_reflog_write(self.raw)); |
| 98 |
} |
| 99 |
Ok(()) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
impl Binding for Reflog { |
| 104 |
type Raw = *mut raw::git_reflog; |
| 105 |
|
| 106 |
unsafe fn from_raw(raw: *mut raw::git_reflog) -> Reflog { |
| 107 |
Reflog { raw } |
| 108 |
} |
| 109 |
fn raw(&self) -> *mut raw::git_reflog { |
| 110 |
self.raw |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
impl Drop for Reflog { |
| 115 |
fn drop(&mut self) { |
| 116 |
unsafe { raw::git_reflog_free(self.raw) } |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
impl<'reflog> ReflogEntry<'reflog> { |
| 121 |
/// Get the committer of this entry |
| 122 |
pub fn committer(&self) -> Signature<'_> { |
| 123 |
unsafe { |
| 124 |
let ptr = raw::git_reflog_entry_committer(self.raw); |
| 125 |
signature::from_raw_const(self, ptr) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/// Get the new oid |
| 130 |
pub fn id_new(&self) -> Oid { |
| 131 |
unsafe { Binding::from_raw(raw::git_reflog_entry_id_new(self.raw)) } |
| 132 |
} |
| 133 |
|
| 134 |
/// Get the old oid |
| 135 |
pub fn id_old(&self) -> Oid { |
| 136 |
unsafe { Binding::from_raw(raw::git_reflog_entry_id_old(self.raw)) } |
| 137 |
} |
| 138 |
|
| 139 |
/// Get the log message. |
| 140 |
pub fn message(&self) -> Result<Option<&str>, Error> { |
| 141 |
match self.message_bytes() { |
| 142 |
Some(mb) => str::from_utf8(mb).map(|s| Some(s)).map_err(|e| e.into()), |
| 143 |
None => Ok(None), |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/// Get the log message as a byte array. |
| 148 |
pub fn message_bytes(&self) -> Option<&[u8]> { |
| 149 |
unsafe { crate::opt_bytes(self, raw::git_reflog_entry_message(self.raw)) } |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl<'reflog> Binding for ReflogEntry<'reflog> { |
| 154 |
type Raw = *const raw::git_reflog_entry; |
| 155 |
|
| 156 |
unsafe fn from_raw(raw: *const raw::git_reflog_entry) -> ReflogEntry<'reflog> { |
| 157 |
ReflogEntry { |
| 158 |
raw, |
| 159 |
_marker: marker::PhantomData, |
| 160 |
} |
| 161 |
} |
| 162 |
fn raw(&self) -> *const raw::git_reflog_entry { |
| 163 |
self.raw |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
impl<'reflog> Iterator for ReflogIter<'reflog> { |
| 168 |
type Item = ReflogEntry<'reflog>; |
| 169 |
fn next(&mut self) -> Option<ReflogEntry<'reflog>> { |
| 170 |
self.range.next().and_then(|i| self.reflog.get(i)) |
| 171 |
} |
| 172 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 173 |
self.range.size_hint() |
| 174 |
} |
| 175 |
} |
| 176 |
impl<'reflog> DoubleEndedIterator for ReflogIter<'reflog> { |
| 177 |
fn next_back(&mut self) -> Option<ReflogEntry<'reflog>> { |
| 178 |
self.range.next_back().and_then(|i| self.reflog.get(i)) |
| 179 |
} |
| 180 |
} |
| 181 |
impl<'reflog> FusedIterator for ReflogIter<'reflog> {} |
| 182 |
impl<'reflog> ExactSizeIterator for ReflogIter<'reflog> {} |
| 183 |
|
| 184 |
#[cfg(test)] |
| 185 |
mod tests { |
| 186 |
#[test] |
| 187 |
fn smoke() { |
| 188 |
let (_td, repo) = crate::test::repo_init(); |
| 189 |
let mut reflog = repo.reflog("HEAD").unwrap(); |
| 190 |
assert_eq!(reflog.iter().len(), 1); |
| 191 |
reflog.write().unwrap(); |
| 192 |
|
| 193 |
let entry = reflog.iter().next().unwrap(); |
| 194 |
assert!(entry.message().expect("Should be ok").is_some()); |
| 195 |
|
| 196 |
repo.reflog_rename("HEAD", "refs/heads/foo").unwrap(); |
| 197 |
repo.reflog_delete("refs/heads/foo").unwrap(); |
| 198 |
} |
| 199 |
} |