|
| 1 | +use git2::{Error, Oid, Repository, Revwalk}; |
| 2 | + |
| 3 | +/// |
| 4 | +pub struct LogWalker<'a> { |
| 5 | + repo: &'a Repository, |
| 6 | + revwalk: Option<Revwalk<'a>>, |
| 7 | +} |
| 8 | + |
| 9 | +impl<'a> LogWalker<'a> { |
| 10 | + /// |
| 11 | + pub fn new(repo: &'a Repository) -> Self { |
| 12 | + Self { |
| 13 | + repo, |
| 14 | + revwalk: None, |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + /// |
| 19 | + pub fn read( |
| 20 | + &mut self, |
| 21 | + out: &mut Vec<Oid>, |
| 22 | + limit: usize, |
| 23 | + ) -> Result<usize, Error> { |
| 24 | + let mut count = 0_usize; |
| 25 | + |
| 26 | + if self.revwalk.is_none() { |
| 27 | + let mut walk = self.repo.revwalk()?; |
| 28 | + walk.push_head()?; |
| 29 | + self.revwalk = Some(walk); |
| 30 | + } |
| 31 | + |
| 32 | + if let Some(ref mut walk) = self.revwalk { |
| 33 | + for id in walk { |
| 34 | + if let Ok(id) = id { |
| 35 | + out.push(id); |
| 36 | + count += 1; |
| 37 | + |
| 38 | + if count == limit { |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + Ok(count) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(test)] |
| 50 | +mod tests { |
| 51 | + use super::*; |
| 52 | + use crate::sync::{ |
| 53 | + commit, get_commits_info, stage_add_file, |
| 54 | + tests::repo_init_empty, |
| 55 | + }; |
| 56 | + use std::{ |
| 57 | + fs::File, |
| 58 | + io::{Error, Write}, |
| 59 | + path::Path, |
| 60 | + }; |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn test_limit() -> Result<(), Error> { |
| 64 | + let file_path = Path::new("foo"); |
| 65 | + let (_td, repo) = repo_init_empty(); |
| 66 | + let root = repo.path().parent().unwrap(); |
| 67 | + let repo_path = root.as_os_str().to_str().unwrap(); |
| 68 | + |
| 69 | + File::create(&root.join(file_path))?.write_all(b"a")?; |
| 70 | + stage_add_file(repo_path, file_path); |
| 71 | + commit(repo_path, "commit1"); |
| 72 | + File::create(&root.join(file_path))?.write_all(b"a")?; |
| 73 | + stage_add_file(repo_path, file_path); |
| 74 | + let oid2 = commit(repo_path, "commit2"); |
| 75 | + |
| 76 | + let mut items = Vec::new(); |
| 77 | + let mut walk = LogWalker::new(&repo); |
| 78 | + walk.read(&mut items, 1).unwrap(); |
| 79 | + |
| 80 | + assert_eq!(items.len(), 1); |
| 81 | + assert_eq!(items[0], oid2); |
| 82 | + |
| 83 | + Ok(()) |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_logwalker() -> Result<(), Error> { |
| 88 | + let file_path = Path::new("foo"); |
| 89 | + let (_td, repo) = repo_init_empty(); |
| 90 | + let root = repo.path().parent().unwrap(); |
| 91 | + let repo_path = root.as_os_str().to_str().unwrap(); |
| 92 | + |
| 93 | + File::create(&root.join(file_path))?.write_all(b"a")?; |
| 94 | + stage_add_file(repo_path, file_path); |
| 95 | + commit(repo_path, "commit1"); |
| 96 | + File::create(&root.join(file_path))?.write_all(b"a")?; |
| 97 | + stage_add_file(repo_path, file_path); |
| 98 | + let oid2 = commit(repo_path, "commit2"); |
| 99 | + |
| 100 | + let mut items = Vec::new(); |
| 101 | + let mut walk = LogWalker::new(&repo); |
| 102 | + walk.read(&mut items, 100).unwrap(); |
| 103 | + |
| 104 | + let info = get_commits_info(repo_path, &items).unwrap(); |
| 105 | + dbg!(&info); |
| 106 | + |
| 107 | + assert_eq!(items.len(), 2); |
| 108 | + assert_eq!(items[0], oid2); |
| 109 | + |
| 110 | + let mut items = Vec::new(); |
| 111 | + walk.read(&mut items, 100).unwrap(); |
| 112 | + |
| 113 | + assert_eq!(items.len(), 0); |
| 114 | + |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | +} |
0 commit comments