Skip to content

Commit b82b58a

Browse files
committed
show single file
1 parent 2f1d296 commit b82b58a

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

src/git.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ansi_to_tui::IntoText;
2-
use git2::{DiffLineType, Oid, Repository};
2+
use git2::{DiffLineType, DiffOptions, Oid, Repository};
33
use std::{
44
error,
55
path::{Path, PathBuf},
@@ -92,12 +92,12 @@ fn format_line_num_and_code(line_num: i32, line: &str) -> Vec<Span<'static>> {
9292
]
9393
}
9494

95-
pub fn show(repo: &Repository, commit_id: Oid) -> Text<'static> {
95+
pub fn show(repo: &Repository, commit_id: Oid, path: PathBuf) -> Text<'static> {
9696
let commit = match repo.find_commit(commit_id) {
9797
Ok(commit) => commit,
9898
Err(e) => return Text::raw(e.to_string()),
9999
};
100-
let diff = match diff_for_commit(repo, &commit) {
100+
let diff = match diff_for_commit(repo, &commit, path) {
101101
Ok(diff) => diff,
102102
Err(e) => return Text::raw(e.to_string()),
103103
};
@@ -148,9 +148,18 @@ pub fn show(repo: &Repository, commit_id: Oid) -> Text<'static> {
148148
Text::from(lines)
149149
}
150150

151-
fn diff_for_commit<'a>(repo: &'a Repository, commit: &git2::Commit<'a>) -> Result<git2::Diff<'a>, git2::Error> {
151+
fn diff_for_commit<'a>(
152+
repo: &'a Repository,
153+
commit: &git2::Commit<'a>,
154+
path: PathBuf,
155+
) -> Result<git2::Diff<'a>, git2::Error> {
152156
let parent_tree = commit.parent(0).and_then(|parent| parent.tree()).ok();
153-
return repo.diff_tree_to_tree(parent_tree.as_ref(), Some(&commit.tree()?), None);
157+
let mut options = DiffOptions::new();
158+
return repo.diff_tree_to_tree(
159+
parent_tree.as_ref(),
160+
Some(&commit.tree()?),
161+
Some(options.pathspec(path)),
162+
);
154163
}
155164

156165
fn push_lines(lines: &mut Vec<Line>, s: &str, color: Color) {

src/terminal.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,12 @@ fn handle_input(key: &KeyEvent, app: &mut App, term_size: &Rect) -> Result<bool,
239239
code: KeyCode::Enter, ..
240240
} => {
241241
if let Some(index) = app.blame_state.selected() {
242-
app.right_panel = Some(git::show(app.repo, app.blame[index].commit));
242+
let blame = &app.blame[index];
243+
let line_path = match blame.path.to_owned() {
244+
Some(p) => p,
245+
None => app.commit_stack.last().unwrap().path.to_owned(),
246+
};
247+
app.right_panel = Some(git::show(app.repo, blame.commit, line_path));
243248
}
244249
}
245250
KeyEvent { code: Char('w'), .. } => {

0 commit comments

Comments
 (0)