mirror of
https://github.com/gitui-org/gitui
synced 2026-05-24 01:18:21 +00:00
use new custom logwalker
This commit is contained in:
parent
e8c2dc25d4
commit
6006e7aebd
4 changed files with 63 additions and 85 deletions
|
|
@ -151,7 +151,7 @@ impl AsyncLog {
|
|||
) -> Result<()> {
|
||||
let mut entries = Vec::with_capacity(LIMIT_COUNT);
|
||||
let r = repo(CWD)?;
|
||||
let mut walker = LogWalker::new(&r, LIMIT_COUNT);
|
||||
let mut walker = LogWalker::new(&r, LIMIT_COUNT)?;
|
||||
loop {
|
||||
entries.clear();
|
||||
let res_is_err = walker.read(&mut entries).is_err();
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ mod tests {
|
|||
|
||||
fn count_commits(repo: &Repository, max: usize) -> usize {
|
||||
let mut items = Vec::new();
|
||||
let mut walk = LogWalker::new(&repo, max);
|
||||
let mut walk = LogWalker::new(&repo, max).unwrap();
|
||||
walk.read(&mut items).unwrap();
|
||||
items.len()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,51 +1,82 @@
|
|||
#![allow(clippy::missing_panics_doc)]
|
||||
|
||||
use super::CommitId;
|
||||
use crate::error::Result;
|
||||
use git2::{Repository, Revwalk};
|
||||
use git2::{Commit, Oid, Repository};
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
collections::{BinaryHeap, HashSet},
|
||||
};
|
||||
|
||||
struct TimeOrderedCommit<'a>(Commit<'a>);
|
||||
|
||||
impl<'a> Eq for TimeOrderedCommit<'a> {}
|
||||
|
||||
impl<'a> PartialEq for TimeOrderedCommit<'a> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0.time().eq(&other.0.time())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PartialOrd for TimeOrderedCommit<'a> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
self.0.time().partial_cmp(&other.0.time())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Ord for TimeOrderedCommit<'a> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.0.time().cmp(&other.0.time())
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
pub struct LogWalker<'a> {
|
||||
repo: &'a Repository,
|
||||
revwalk: Option<Revwalk<'a>>,
|
||||
commits: BinaryHeap<TimeOrderedCommit<'a>>,
|
||||
visited: HashSet<Oid>,
|
||||
limit: usize,
|
||||
}
|
||||
|
||||
impl<'a> LogWalker<'a> {
|
||||
///
|
||||
pub const fn new(repo: &'a Repository, limit: usize) -> Self {
|
||||
Self {
|
||||
repo,
|
||||
revwalk: None,
|
||||
pub fn new(repo: &'a Repository, limit: usize) -> Result<Self> {
|
||||
let c = repo.head()?.peel_to_commit()?;
|
||||
|
||||
let mut commits = BinaryHeap::with_capacity(10);
|
||||
commits.push(TimeOrderedCommit(c));
|
||||
|
||||
Ok(Self {
|
||||
commits,
|
||||
limit,
|
||||
}
|
||||
visited: HashSet::with_capacity(1000),
|
||||
})
|
||||
}
|
||||
|
||||
///
|
||||
pub fn read(&mut self, out: &mut Vec<CommitId>) -> Result<usize> {
|
||||
let mut count = 0_usize;
|
||||
|
||||
if self.revwalk.is_none() {
|
||||
let mut walk = self.repo.revwalk()?;
|
||||
while let Some(c) = self.commits.pop() {
|
||||
for p in c.0.parents() {
|
||||
self.visit(p);
|
||||
}
|
||||
|
||||
walk.push_head()?;
|
||||
out.push(c.0.id().into());
|
||||
|
||||
self.revwalk = Some(walk);
|
||||
}
|
||||
|
||||
if let Some(ref mut walk) = self.revwalk {
|
||||
for id in walk.into_iter().flatten() {
|
||||
out.push(id.into());
|
||||
count += 1;
|
||||
|
||||
if count == self.limit {
|
||||
break;
|
||||
}
|
||||
count += 1;
|
||||
if count == self.limit {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
//
|
||||
fn visit(&mut self, c: Commit<'a>) {
|
||||
if !self.visited.contains(&c.id()) {
|
||||
self.visited.insert(c.id());
|
||||
self.commits.push(TimeOrderedCommit(c));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -73,7 +104,7 @@ mod tests {
|
|||
let oid2 = commit(repo_path, "commit2").unwrap();
|
||||
|
||||
let mut items = Vec::new();
|
||||
let mut walk = LogWalker::new(&repo, 1);
|
||||
let mut walk = LogWalker::new(&repo, 1)?;
|
||||
walk.read(&mut items).unwrap();
|
||||
|
||||
assert_eq!(items.len(), 1);
|
||||
|
|
@ -97,7 +128,7 @@ mod tests {
|
|||
let oid2 = commit(repo_path, "commit2").unwrap();
|
||||
|
||||
let mut items = Vec::new();
|
||||
let mut walk = LogWalker::new(&repo, 100);
|
||||
let mut walk = LogWalker::new(&repo, 100)?;
|
||||
walk.read(&mut items).unwrap();
|
||||
|
||||
let info = get_commits_info(repo_path, &items, 50).unwrap();
|
||||
|
|
@ -113,60 +144,4 @@ mod tests {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// fn walk_all_commits(repo: &Repository) -> Vec<CommitId> {
|
||||
// let mut items = Vec::new();
|
||||
// let mut walk = LogWalker::new(&repo).mode(Mode::AllRefs);
|
||||
// walk.read(&mut items, 10).unwrap();
|
||||
// items
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn test_multiple_branches() {
|
||||
// let (td, repo) = repo_init_empty().unwrap();
|
||||
// let repo_path = td.path().to_string_lossy();
|
||||
|
||||
// let c1 = write_commit_file_at(
|
||||
// &repo,
|
||||
// "test.txt",
|
||||
// "",
|
||||
// "c1",
|
||||
// Time::new(1, 0),
|
||||
// );
|
||||
|
||||
// let items = walk_all_commits(&repo);
|
||||
|
||||
// assert_eq!(items, vec![c1]);
|
||||
|
||||
// let b1 = create_branch(&repo_path, "b1").unwrap();
|
||||
|
||||
// let c2 = write_commit_file_at(
|
||||
// &repo,
|
||||
// "test2.txt",
|
||||
// "",
|
||||
// "c2",
|
||||
// Time::new(2, 0),
|
||||
// );
|
||||
|
||||
// let items = walk_all_commits(&repo);
|
||||
// assert_eq!(items, vec![c2, c1]);
|
||||
|
||||
// let _b2 = create_branch(&repo_path, "b2").unwrap();
|
||||
|
||||
// let c3 = write_commit_file_at(
|
||||
// &repo,
|
||||
// "test3.txt",
|
||||
// "",
|
||||
// "c3",
|
||||
// Time::new(3, 0),
|
||||
// );
|
||||
|
||||
// let items = walk_all_commits(&repo);
|
||||
// assert_eq!(items, vec![c2, c3, c1]);
|
||||
|
||||
// checkout_branch(&repo_path, &b1).unwrap();
|
||||
|
||||
// let items = walk_all_commits(&repo);
|
||||
// assert_eq!(items, vec![c2, c3, c1]);
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -274,7 +274,10 @@ mod tests {
|
|||
max_count: usize,
|
||||
) -> Vec<CommitId> {
|
||||
let mut commit_ids = Vec::<CommitId>::new();
|
||||
LogWalker::new(r, max_count).read(&mut commit_ids).unwrap();
|
||||
LogWalker::new(r, max_count)
|
||||
.unwrap()
|
||||
.read(&mut commit_ids)
|
||||
.unwrap();
|
||||
|
||||
commit_ids
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue