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

Add support for recursive blame #2285

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.26.3] - 2024-06-02

### Added
* add recursive git blame (do `SHIFT+B` in the revlog to go forward, and `b` to go back).

## [0.26.3] - 2024-06-02

### Breaking Changes

#### Theme file format
Expand Down
2 changes: 2 additions & 0 deletions src/keys/key_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct KeysList {
pub shift_down: GituiKeyEvent,
pub enter: GituiKeyEvent,
pub blame: GituiKeyEvent,
pub blame_back: GituiKeyEvent,
pub file_history: GituiKeyEvent,
pub edit_file: GituiKeyEvent,
pub status_stage_all: GituiKeyEvent,
Expand Down Expand Up @@ -160,6 +161,7 @@ impl Default for KeysList {
shift_down: GituiKeyEvent::new(KeyCode::Down, KeyModifiers::SHIFT),
enter: GituiKeyEvent::new(KeyCode::Enter, KeyModifiers::empty()),
blame: GituiKeyEvent::new(KeyCode::Char('B'), KeyModifiers::SHIFT),
blame_back: GituiKeyEvent::new(KeyCode::Char('b'), KeyModifiers::empty()),
file_history: GituiKeyEvent::new(KeyCode::Char('H'), KeyModifiers::SHIFT),
edit_file: GituiKeyEvent::new(KeyCode::Char('e'), KeyModifiers::empty()),
status_stage_all: GituiKeyEvent::new(KeyCode::Char('a'), KeyModifiers::empty()),
Expand Down
161 changes: 114 additions & 47 deletions src/popups/blame_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub struct BlameFilePopup {
table_state: std::cell::Cell<TableState>,
key_config: SharedKeyConfig,
current_height: std::cell::Cell<usize>,
blame: Option<BlameProcess>,
blame_stack: Vec<BlameProcess>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like us to not keep this stack around. @cruessler and I agreed on working out an asynchronous blaming that will deliver results iteratively so the worst case scenarios are not as slow anymore and this stack will save a lot of data when going only in one direction through the blame history and only optimize for going back fast and not going back and forth.

Furthermore I have a gut feeling that it might lead to weird stacks when jumping to a file history from a blame and then to another blame again which will not properly reset the stack

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense. So, to clarify, do you want to a) have somewhat of a non-popping stack, b) just nix the back functionality altogether, or c) did you have something else in mind?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a friendly ping :)
@extrawurst

app_sender: Sender<AsyncAppNotification>,
git_sender: Sender<AsyncGitNotification>,
repo: RepoPathRef,
Expand Down Expand Up @@ -123,16 +123,17 @@ impl DrawableComponent for BlameFilePopup {
];

let number_of_rows: usize = rows.len();
let syntax_highlight_progress = match self.blame {
Some(BlameProcess::SyntaxHighlighting {
ref job,
..
}) => job
.progress()
.map(|p| format!(" ({}%)", p.progress))
.unwrap_or_default(),
_ => String::new(),
};
let syntax_highlight_progress =
match self.blame_stack.last() {
Some(BlameProcess::SyntaxHighlighting {
ref job,
..
}) => job
.progress()
.map(|p| format!(" ({}%)", p.progress))
.unwrap_or_default(),
_ => String::new(),
};
let title_with_highlight_progress =
format!("{title}{syntax_highlight_progress}");

Expand All @@ -151,6 +152,14 @@ impl DrawableComponent for BlameFilePopup {

let mut table_state = self.table_state.take();

if table_state
.selected()
.is_some_and(|s| s > number_of_rows)
{
table_state.select(Some(number_of_rows - 1));
table_state = table_state.with_offset(0);
}

f.render_widget(Clear, area);
f.render_stateful_widget(table, area, &mut table_state);

Expand Down Expand Up @@ -194,9 +203,9 @@ impl Component for BlameFilePopup {
force_all: bool,
) -> CommandBlocking {
let has_result = self
.blame
.as_ref()
.is_some_and(|blame| blame.result().is_some());
.blame_stack
.last()
.is_some_and(|last| last.result().is_some());
if self.is_visible() || force_all {
out.push(
CommandInfo::new(
Expand Down Expand Up @@ -234,11 +243,30 @@ impl Component for BlameFilePopup {
)
.order(1),
);
out.push(
CommandInfo::new(
strings::commands::blame_file(&self.key_config),
true,
has_result,
)
.order(1),
);
out.push(
CommandInfo::new(
strings::commands::blame_file_back(
&self.key_config,
),
true,
true,
)
.order(1),
);
}

visibility_blocking(self)
}

#[allow(too_many_lines)]
fn event(
&mut self,
event: &crossterm::event::Event,
Expand Down Expand Up @@ -307,6 +335,42 @@ impl Component for BlameFilePopup {
),
));
}
} else if key_match(key, self.key_config.keys.blame) {
if let Some(file_path) = self
.params
.as_ref()
.map(|p| p.file_path.clone())
{
// Avoid loading the same view.
if self.selected_commit().is_some_and(|c| {
self.blame_stack
.last()
.and_then(|last| last.result())
.map_or(false, |r| {
r.file_blame.commit_id != c
})
}) {
let _ = self.open(BlameFileOpen {
file_path,
commit_id: self.selected_commit(),
selection: self.get_selection(),
});
}
}
} else if key_match(
key,
self.key_config.keys.blame_back,
) {
match self.blame_stack.len() {
0 => self.hide_stacked(false),
1 => {
self.blame_stack.pop();
self.hide_stacked(false);
}
_ => {
self.blame_stack.pop();
}
}
}

return Ok(EventState::Consumed);
Expand Down Expand Up @@ -342,7 +406,7 @@ impl BlameFilePopup {
current_height: std::cell::Cell::new(0),
app_sender: env.sender_app.clone(),
git_sender: env.sender_git.clone(),
blame: None,
blame_stack: vec![],
repo: env.repo.clone(),
}
}
Expand Down Expand Up @@ -371,11 +435,12 @@ impl BlameFilePopup {
file_path: open.file_path,
commit_id: open.commit_id,
});
self.blame =
Some(BlameProcess::GettingBlame(AsyncBlame::new(
self.blame_stack.push(BlameProcess::GettingBlame(
AsyncBlame::new(
self.repo.borrow().clone(),
&self.git_sender,
)));
),
));
self.table_state.get_mut().select(Some(0));
self.visible = true;
self.update()?;
Expand All @@ -384,9 +449,10 @@ impl BlameFilePopup {
}

///
pub const fn any_work_pending(&self) -> bool {
self.blame.is_some()
&& !matches!(self.blame, Some(BlameProcess::Result(_)))
pub fn any_work_pending(&self) -> bool {
self.blame_stack.last().is_some_and(|last| {
!matches!(last, &BlameProcess::Result(_))
})
}

pub fn update_async(
Expand Down Expand Up @@ -416,7 +482,7 @@ impl BlameFilePopup {
if self.is_visible() {
if let Some(BlameProcess::GettingBlame(
ref mut async_blame,
)) = self.blame
)) = self.blame_stack.last_mut()
{
if let Some(params) = &self.params {
if let Some((
Expand All @@ -425,7 +491,7 @@ impl BlameFilePopup {
)) = async_blame.last()?
{
if previous_blame_params == *params {
self.blame = Some(
*self.blame_stack.last_mut().expect("Last will always exist here, as we just got it.") =
BlameProcess::SyntaxHighlighting {
unstyled_file_blame:
SyntaxFileBlame {
Expand All @@ -436,8 +502,7 @@ impl BlameFilePopup {
job: AsyncSingleJob::new(
self.app_sender.clone(),
),
},
);
};
self.set_open_selection();
self.highlight_blame_lines();

Expand All @@ -457,7 +522,7 @@ impl BlameFilePopup {
let Some(BlameProcess::SyntaxHighlighting {
ref unstyled_file_blame,
ref job,
}) = self.blame
}) = self.blame_stack.last_mut()
else {
return;
};
Expand All @@ -474,16 +539,18 @@ impl BlameFilePopup {
== Path::new(
unstyled_file_blame.path(),
) {
self.blame =
Some(BlameProcess::Result(
SyntaxFileBlame {
file_blame:
unstyled_file_blame
.file_blame
.clone(),
styled_text: Some(syntax),
},
));
*self
.blame_stack
.last_mut()
.expect("Last will always exist here, as we just got it.") = BlameProcess::Result(
SyntaxFileBlame {
file_blame:
unstyled_file_blame
.file_blame
.clone(),
styled_text: Some(syntax),
},
);
}
}
}
Expand All @@ -498,7 +565,7 @@ impl BlameFilePopup {
match (
self.any_work_pending(),
self.params.as_ref(),
self.blame.as_ref().and_then(|blame| blame.result()),
self.blame_stack.last().and_then(|blame| blame.result()),
) {
(true, Some(params), _) => {
format!(
Expand Down Expand Up @@ -526,8 +593,8 @@ impl BlameFilePopup {

///
fn get_rows(&self, width: usize) -> Vec<Row> {
self.blame
.as_ref()
self.blame_stack
.last()
.and_then(|blame| blame.result())
.map(|file_blame| {
let styled_text: Option<Text<'_>> = file_blame
Expand Down Expand Up @@ -556,7 +623,7 @@ impl BlameFilePopup {
let Some(BlameProcess::SyntaxHighlighting {
ref unstyled_file_blame,
ref mut job,
}) = self.blame
}) = self.blame_stack.last_mut()
else {
return;
};
Expand Down Expand Up @@ -654,7 +721,7 @@ impl BlameFilePopup {
});

let file_blame =
self.blame.as_ref().and_then(|blame| blame.result());
self.blame_stack.last().and_then(|blame| blame.result());
let is_blamed_commit = file_blame
.and_then(|file_blame| {
blame_hunk.map(|hunk| {
Expand All @@ -673,8 +740,8 @@ impl BlameFilePopup {
}

fn get_max_line_number(&self) -> usize {
self.blame
.as_ref()
self.blame_stack
.last()
.and_then(|blame| blame.result())
.map_or(0, |file_blame| file_blame.lines().len() - 1)
}
Expand Down Expand Up @@ -727,8 +794,8 @@ impl BlameFilePopup {
}

fn get_selection(&self) -> Option<usize> {
self.blame
.as_ref()
self.blame_stack
.last()
.and_then(|blame| blame.result())
.and_then(|_| {
let table_state = self.table_state.take();
Expand All @@ -742,8 +809,8 @@ impl BlameFilePopup {
}

fn selected_commit(&self) -> Option<CommitId> {
self.blame
.as_ref()
self.blame_stack
.last()
.and_then(|blame| blame.result())
.and_then(|file_blame| {
let table_state = self.table_state.take();
Expand Down
8 changes: 8 additions & 0 deletions src/popups/file_revlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,14 @@ impl Component for FileRevlogPopup {
)
.order(1),
);
out.push(
CommandInfo::new(
strings::commands::blame_file_back(&self.key_config),
true,
self.selected_commit().is_some(),
)
.order(1),
);

out.push(CommandInfo::new(
strings::commands::diff_focus_right(&self.key_config),
Expand Down
12 changes: 12 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,18 @@ pub mod commands {
CMD_GROUP_GENERAL,
)
}
pub fn blame_file_back(
key_config: &SharedKeyConfig,
) -> CommandText {
CommandText::new(
format!(
"Back [{}]",
key_config.get_hint(key_config.keys.blame_back),
),
"go to previous blame",
CMD_GROUP_GENERAL,
)
}
pub fn open_file_history(
key_config: &SharedKeyConfig,
) -> CommandText {
Expand Down
Loading