Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commit log filtering #1800

Merged
merged 3 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

**search commits**

![commit-search](assets/log-search.gif)

**visualize empty lines in diff better**

![diff-empty-line](assets/diff-empty-line.png)
Expand All @@ -20,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Future additions of colors etc. will not break existing themes anymore

### Added
* search commits by files in diff or commit message ([#1791](https://github.com/extrawurst/gitui/issues/1791))
* support 'n'/'p' key to move to the next/prev hunk in diff component [[@hamflx](https://github.com/hamflx)] ([#1523](https://github.com/extrawurst/gitui/issues/1523))
* simplify theme overrides [[@cruessler](https://github.com/cruessler)] ([#1367](https://github.com/extrawurst/gitui/issues/1367))
* support for sign-off of commits [[@domtac](https://github.com/domtac)]([#1757](https://github.com/extrawurst/gitui/issues/1757))
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ For a [RustBerlin meetup presentation](https://youtu.be/rpilJV-eIVw?t=5334) ([sl

These are the high level goals before calling out `1.0`:

* log search (commit, author, sha) ([#1791](https://github.com/extrawurst/gitui/issues/1791))
* visualize branching structure in log tab ([#81](https://github.com/extrawurst/gitui/issues/81))
* interactive rebase ([#32](https://github.com/extrawurst/gitui/issues/32))

Expand Down
Binary file added assets/log-search.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions asyncgit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ categories = ["concurrency", "asynchronous"]
keywords = ["git"]

[dependencies]
bitflags = "1"
crossbeam-channel = "0.5"
easy-cast = "0.5"
fuzzy-matcher = "0.3"
git2 = "0.17"
log = "0.4"
# git2 = { path = "../../extern/git2-rs", features = ["vendored-openssl"]}
Expand Down
42 changes: 33 additions & 9 deletions asyncgit/src/revlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
Arc, Mutex,
},
thread,
time::Duration,
time::{Duration, Instant},
};

///
Expand All @@ -25,9 +25,16 @@ pub enum FetchStatus {
Started,
}

///
pub struct AsyncLogResult {
///
pub commits: Vec<CommitId>,
///
pub duration: Duration,
}
///
pub struct AsyncLog {
current: Arc<Mutex<Vec<CommitId>>>,
current: Arc<Mutex<AsyncLogResult>>,
current_head: Arc<Mutex<Option<CommitId>>>,
sender: Sender<AsyncGitNotification>,
pending: Arc<AtomicBool>,
Expand All @@ -49,7 +56,10 @@ impl AsyncLog {
) -> Self {
Self {
repo,
current: Arc::new(Mutex::new(Vec::new())),
current: Arc::new(Mutex::new(AsyncLogResult {
commits: Vec::new(),
duration: Duration::default(),
})),
current_head: Arc::new(Mutex::new(None)),
sender: sender.clone(),
pending: Arc::new(AtomicBool::new(false)),
Expand All @@ -60,7 +70,7 @@ impl AsyncLog {

///
pub fn count(&self) -> Result<usize> {
Ok(self.current.lock()?.len())
Ok(self.current.lock()?.commits.len())
}

///
Expand All @@ -69,17 +79,28 @@ impl AsyncLog {
start_index: usize,
amount: usize,
) -> Result<Vec<CommitId>> {
let list = self.current.lock()?;
let list = &self.current.lock()?.commits;
let list_len = list.len();
let min = start_index.min(list_len);
let max = min + amount;
let max = max.min(list_len);
Ok(list[min..max].to_vec())
}

///
pub fn get_items(&self) -> Result<Vec<CommitId>> {
let list = &self.current.lock()?.commits;
Ok(list.clone())
}

///
pub fn get_last_duration(&self) -> Result<Duration> {
Ok(self.current.lock()?.duration)
}

///
pub fn position(&self, id: CommitId) -> Result<Option<usize>> {
let list = self.current.lock()?;
let list = &self.current.lock()?.commits;
let position = list.iter().position(|&x| x == id);

Ok(position)
Expand Down Expand Up @@ -160,11 +181,13 @@ impl AsyncLog {

fn fetch_helper(
repo_path: &RepoPath,
arc_current: &Arc<Mutex<Vec<CommitId>>>,
arc_current: &Arc<Mutex<AsyncLogResult>>,
arc_background: &Arc<AtomicBool>,
sender: &Sender<AsyncGitNotification>,
filter: Option<LogWalkerFilter>,
) -> Result<()> {
let start_time = Instant::now();

let mut entries = Vec::with_capacity(LIMIT_COUNT);
let r = repo(repo_path)?;
let mut walker =
Expand All @@ -175,7 +198,8 @@ impl AsyncLog {

if !res_is_err {
let mut current = arc_current.lock()?;
current.extend(entries.iter());
current.commits.extend(entries.iter());
current.duration = start_time.elapsed();
}

if res_is_err || entries.len() <= 1 {
Expand All @@ -196,7 +220,7 @@ impl AsyncLog {
}

fn clear(&mut self) -> Result<()> {
self.current.lock()?.clear();
self.current.lock()?.commits.clear();
*self.current_head.lock()? = None;
Ok(())
}
Expand Down
210 changes: 209 additions & 1 deletion asyncgit/src/sync/logwalker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(dead_code)]
use super::CommitId;
use crate::{error::Result, sync::commit_files::get_commit_diff};
use git2::{Commit, Oid, Repository};
use bitflags::bitflags;
use fuzzy_matcher::FuzzyMatcher;
use git2::{Commit, Diff, Oid, Repository};
use std::{
cmp::Ordering,
collections::{BinaryHeap, HashSet},
Expand Down Expand Up @@ -55,6 +58,163 @@ pub fn diff_contains_file(file_path: String) -> LogWalkerFilter {
))
}

bitflags! {
///
pub struct SearchFields: u32 {
///
const MESSAGE = 0b0000_0001;
///
const FILENAMES = 0b0000_0010;
//TODO:
// const COMMIT_HASHES = 0b0000_0100;
// ///
// const DATES = 0b0000_1000;
// ///
// const AUTHORS = 0b0001_0000;
// ///
// const DIFFS = 0b0010_0000;
}
}

impl Default for SearchFields {
fn default() -> Self {
Self::MESSAGE
}
}

bitflags! {
///
pub struct SearchOptions: u32 {
///
const CASE_SENSITIVE = 0b0000_0001;
///
const FUZZY_SEARCH = 0b0000_0010;
}
}

impl Default for SearchOptions {
fn default() -> Self {
Self::empty()
}
}

///
#[derive(Default, Debug, Clone)]
pub struct LogFilterSearchOptions {
///
pub search_pattern: String,
///
pub fields: SearchFields,
///
pub options: SearchOptions,
}

///
#[derive(Default)]
pub struct LogFilterSearch {
///
pub matcher: fuzzy_matcher::skim::SkimMatcherV2,
///
pub options: LogFilterSearchOptions,
}

impl LogFilterSearch {
///
pub fn new(options: LogFilterSearchOptions) -> Self {
let mut options = options;
if !options.options.contains(SearchOptions::CASE_SENSITIVE) {
options.search_pattern =
options.search_pattern.to_lowercase();
}
Self {
matcher: fuzzy_matcher::skim::SkimMatcherV2::default(),
options,
}
}

fn match_diff(&self, diff: &Diff<'_>) -> bool {
diff.deltas().any(|delta| {
if delta
.new_file()
.path()
.and_then(|file| file.as_os_str().to_str())
.map(|file| self.match_text(file))
.unwrap_or_default()
{
return true;
}

delta
.old_file()
.path()
.and_then(|file| file.as_os_str().to_str())
.map(|file| self.match_text(file))
.unwrap_or_default()
})
}

///
pub fn match_text(&self, text: &str) -> bool {
if self.options.options.contains(SearchOptions::FUZZY_SEARCH)
{
self.matcher
.fuzzy_match(
text,
self.options.search_pattern.as_str(),
)
.is_some()
} else if self
.options
.options
.contains(SearchOptions::CASE_SENSITIVE)
{
text.contains(self.options.search_pattern.as_str())
} else {
text.to_lowercase()
.contains(self.options.search_pattern.as_str())
}
}
}

///
pub fn filter_commit_by_search(
filter: LogFilterSearch,
) -> LogWalkerFilter {
Arc::new(Box::new(
move |repo: &Repository,
commit_id: &CommitId|
-> Result<bool> {
let commit = repo.find_commit((*commit_id).into())?;

let msg_match = filter
.options
.fields
.contains(SearchFields::MESSAGE)
.then(|| {
commit.message().map(|msg| filter.match_text(msg))
})
.flatten()
.unwrap_or_default();

let file_match = filter
.options
.fields
.contains(SearchFields::FILENAMES)
.then(|| {
get_commit_diff(
repo, *commit_id, None, None, None,
)
.ok()
})
.flatten()
.map(|diff| filter.match_diff(&diff))
.unwrap_or_default();

Ok(msg_match || file_match)
},
))
}

///
pub struct LogWalker<'a> {
commits: BinaryHeap<TimeOrderedCommit<'a>>,
Expand Down Expand Up @@ -130,6 +290,7 @@ impl<'a> LogWalker<'a> {
mod tests {
use super::*;
use crate::error::Result;
use crate::sync::tests::write_commit_file;
use crate::sync::RepoPath;
use crate::sync::{
commit, get_commits_info, stage_add_file,
Expand Down Expand Up @@ -246,4 +407,51 @@ mod tests {

Ok(())
}

#[test]
fn test_logwalker_with_filter_search() {
let (_td, repo) = repo_init_empty().unwrap();

write_commit_file(&repo, "foo", "a", "commit1");
let second_commit_id = write_commit_file(
&repo,
"baz",
"a",
"my commit msg (#2)",
);
write_commit_file(&repo, "foo", "b", "commit3");

let log_filter = filter_commit_by_search(
LogFilterSearch::new(LogFilterSearchOptions {
fields: SearchFields::MESSAGE,
options: SearchOptions::FUZZY_SEARCH,
search_pattern: String::from("my msg"),
}),
);

let mut items = Vec::new();
let mut walker = LogWalker::new(&repo, 100)
.unwrap()
.filter(Some(log_filter));
walker.read(&mut items).unwrap();

assert_eq!(items.len(), 1);
assert_eq!(items[0], second_commit_id);

let log_filter = filter_commit_by_search(
LogFilterSearch::new(LogFilterSearchOptions {
fields: SearchFields::FILENAMES,
options: SearchOptions::FUZZY_SEARCH,
search_pattern: String::from("fo"),
}),
);

let mut items = Vec::new();
let mut walker = LogWalker::new(&repo, 100)
.unwrap()
.filter(Some(log_filter));
walker.read(&mut items).unwrap();

assert_eq!(items.len(), 2);
}
}
Loading
Loading