Skip to content
Open
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
1 change: 1 addition & 0 deletions src/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ log_menu.log_other = ["o"]
log_menu.quit = ["q", "esc"]
log_menu.-n = ["-n"]
log_menu.--grep = ["-F"]
log_menu.-G = ["-G"]

root.merge_menu = ["m"]
merge_menu.--ff-only = ["-f"]
Expand Down
39 changes: 39 additions & 0 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::highlight;
use crate::item_data::ItemData;
use crate::item_data::Ref;
use crate::item_data::SectionHeader;
use git2::DiffOptions;
use git2::Oid;
use git2::Repository;
use ratatui::text::Line;
Expand Down Expand Up @@ -300,6 +301,7 @@ pub(crate) fn log(
limit: usize,
rev: Option<Oid>,
msg_regex: Option<Regex>,
changes_regex: Option<Regex>,
) -> Res<Vec<Item>> {
let mut revwalk = repo.revwalk().map_err(Error::ReadLog)?;
if let Some(r) = rev {
Expand Down Expand Up @@ -350,6 +352,43 @@ pub(crate) fn log(
return Ok(None);
}

if let Some(re) = &changes_regex {
let parent = commit.parent(0).ok();
let parent_tree = parent
.as_ref()
.map(|p| p.tree())
.transpose()
.map_err(Error::ReadLog)?;
let current_tree = commit.tree().map_err(Error::ReadLog)?;

let mut opts = DiffOptions::new();
let diff = repo
.diff_tree_to_tree(parent_tree.as_ref(), Some(&current_tree), Some(&mut opts))
.map_err(Error::ReadLog)?;

let mut found = false;

diff.print(git2::DiffFormat::Patch, |_delta, _hunk, line| {
let content = std::str::from_utf8(line.content()).unwrap_or("");

match line.origin() {
'+' | '-' => {
if re.is_match(content) {
found = true;
return false;
}
}
_ => {}
}
true
})
.ok();

if !found {
return Ok(None);
}
}

let associated_references: Vec<_> = references
.iter()
.filter(|(commit, _)| commit.id() == oid)
Expand Down
10 changes: 10 additions & 0 deletions src/ops/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) fn init_args() -> Vec<Arg> {
positive_number,
),
Arg::new_arg("--grep", "Search messages", None, any_regex),
Arg::new_arg("-G", "Search changes", None, any_regex),
// Arg::new_str("-S", "Search occurrences"), // TODO: Implement search
]
}
Expand Down Expand Up @@ -98,6 +99,14 @@ fn goto_log_screen(app: &mut App, rev: Option<Oid>) {

let msg_regex = msg_regex_menu.and_then(|arg| arg.value_as::<Regex>().cloned());

let changes_regex_menu = app
.state
.pending_menu
.as_ref()
.and_then(|m| m.args.get("-G"));

let changes_regex = changes_regex_menu.and_then(|arg| arg.value_as::<Regex>().cloned());

app.state.screens.push(
screen::log::create(
Arc::clone(&app.state.config),
Expand All @@ -106,6 +115,7 @@ fn goto_log_screen(app: &mut App, rev: Option<Oid>) {
limit as usize,
rev,
msg_regex,
changes_regex,
)
.expect("Couldn't create screen"),
);
Expand Down
3 changes: 2 additions & 1 deletion src/screen/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ pub(crate) fn create(
limit: usize,
rev: Option<Oid>,
msg_regex: Option<Regex>,
changes_regex: Option<Regex>,
) -> Res<Screen> {
Screen::new(
Arc::clone(&config),
size,
Box::new(move || log(&repo, limit, rev, msg_regex.clone())),
Box::new(move || log(&repo, limit, rev, msg_regex.clone(), changes_regex.clone())),
)
}
2 changes: 1 addition & 1 deletion src/screen/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,5 @@ fn create_log_section_items<'a>(
},
]
.into_iter()
.chain(items::log(repo, limit, None, None).unwrap())
.chain(items::log(repo, limit, None, None, None).unwrap())
}
33 changes: 33 additions & 0 deletions src/tests/log_grep_changes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use super::*;

fn setup(ctx: TestContext) -> TestContext {
commit(&ctx.dir, "first", "third");
commit(&ctx.dir, "first", "second");
commit(&ctx.dir, "first", "first");
ctx
}

#[test]
fn grep_changes_prompt() {
snapshot!(setup(setup_clone!()), "l-G");
}

#[test]
fn grep_changes_set_example() {
snapshot!(setup(setup_clone!()), "l-Gexample<enter>");
}

#[test]
fn grep_changes_second() {
snapshot!(setup(setup_clone!()), "l-Gsecond<enter>l");
}

#[test]
fn grep_changes_no_match() {
snapshot!(setup(setup_clone!()), "l-Gdoesntexist<enter>l");
}

#[test]
fn grep_changes_second_other() {
snapshot!(setup(setup_clone!()), "l-Gsecond<enter>omain<enter>");
}
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod discard;
mod editor;
mod fetch;
mod log;
mod log_grep_changes;
mod merge;
mod pull;
mod push;
Expand Down
6 changes: 3 additions & 3 deletions src/tests/snapshots/gitu__tests__log__grep_prompt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ expression: ctx.redact_buffer()
────────────────────────────────────────────────────────────────────────────────|
Log Arguments |
l current -F Search messages (--grep) |
o other -n Limit number of commits (-n=256) |
q/esc Quit/Close |
o other -G Search changes (-G) |
q/esc Quit/Close -n Limit number of commits (-n=256) |
────────────────────────────────────────────────────────────────────────────────|
? Search messages: › █ |
styles_hash: 30915effaeaeb2e3
styles_hash: 68173f9f5398311b
6 changes: 3 additions & 3 deletions src/tests/snapshots/gitu__tests__log__grep_set_example.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ expression: ctx.redact_buffer()
────────────────────────────────────────────────────────────────────────────────|
Log Arguments |
l current -F Search messages (--grep=example) |
o other -n Limit number of commits (-n=256) |
q/esc Quit/Close |
styles_hash: 5877b9adb59fe24d
o other -G Search changes (-G) |
q/esc Quit/Close -n Limit number of commits (-n=256) |
styles_hash: 30b4594236e60aa2
6 changes: 3 additions & 3 deletions src/tests/snapshots/gitu__tests__log__limit_prompt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ expression: ctx.redact_buffer()
────────────────────────────────────────────────────────────────────────────────|
Log Arguments |
l current -F Search messages (--grep) |
o other -n Limit number of commits (-n) |
q/esc Quit/Close |
o other -G Search changes (-G) |
q/esc Quit/Close -n Limit number of commits (-n) |
────────────────────────────────────────────────────────────────────────────────|
? Limit number of commits (default 256): › █ |
styles_hash: 99ca458c7c196a27
styles_hash: f0682b6afe30adf9
6 changes: 3 additions & 3 deletions src/tests/snapshots/gitu__tests__log__limit_set_10.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ expression: ctx.redact_buffer()
────────────────────────────────────────────────────────────────────────────────|
Log Arguments |
l current -F Search messages (--grep) |
o other -n Limit number of commits (-n=10) |
q/esc Quit/Close |
styles_hash: 195bb974698466ed
o other -G Search changes (-G) |
q/esc Quit/Close -n Limit number of commits (-n=10) |
styles_hash: 6b49349f1757c2bd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/log_grep_changes.rs
expression: ctx.redact_buffer()
---
▌No commits found |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
styles_hash: 90ecdf643519e051
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/log_grep_changes.rs
expression: ctx.redact_buffer()
---
▌On branch main |
▌Your branch is ahead of 'origin/main' by 3 commit(s). |
|
Recent commits |
a7fb92e main modify first |
eb42c24 modify first |
a086849 add first |
b66a0bf origin/main add initial-file |
|
|
|
|
|
────────────────────────────────────────────────────────────────────────────────|
Log Arguments |
l current -F Search messages (--grep) |
o other -G Search changes (-G) |
q/esc Quit/Close -n Limit number of commits (-n=256) |
────────────────────────────────────────────────────────────────────────────────|
? Search changes: › █ |
styles_hash: f31db5867a2f6c5d
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/log_grep_changes.rs
expression: ctx.redact_buffer()
---
▌a7fb92e main modify first |
eb42c24 modify first |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
styles_hash: a908290abddd40c9
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/log_grep_changes.rs
expression: ctx.redact_buffer()
---
▌a7fb92e main modify first |
eb42c24 modify first |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
styles_hash: a908290abddd40c9
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/log_grep_changes.rs
expression: ctx.redact_buffer()
---
▌On branch main |
▌Your branch is ahead of 'origin/main' by 3 commit(s). |
|
Recent commits |
a7fb92e main modify first |
eb42c24 modify first |
a086849 add first |
b66a0bf origin/main add initial-file |
|
|
|
|
|
|
|
────────────────────────────────────────────────────────────────────────────────|
Log Arguments |
l current -F Search messages (--grep) |
o other -G Search changes (-G=example) |
q/esc Quit/Close -n Limit number of commits (-n=256) |
styles_hash: 835226a0c290291d