Skip to content

Commit 9e1d8af

Browse files
author
Stephan Dilly
committed
request commit based diff async
1 parent 6a6fae9 commit 9e1d8af

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

asyncgit/src/diff.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,27 @@ use std::{
99
Arc, Mutex,
1010
},
1111
};
12+
use sync::CommitId;
1213

1314
///
14-
#[derive(Default, Hash, Clone, PartialEq)]
15-
pub struct DiffParams(pub String, pub bool);
15+
#[derive(Hash, Clone, PartialEq)]
16+
pub enum DiffType {
17+
/// diff in a given commit
18+
Commit(CommitId),
19+
/// diff against staged file
20+
Stage,
21+
/// diff against file in workdir
22+
WorkDir,
23+
}
24+
25+
///
26+
#[derive(Hash, Clone, PartialEq)]
27+
pub struct DiffParams {
28+
/// path to the file to diff
29+
pub path: String,
30+
/// what kind of diff
31+
pub diff_type: DiffType,
32+
}
1633

1734
struct Request<R, A>(R, Option<A>);
1835

@@ -121,8 +138,19 @@ impl AsyncDiff {
121138
arc_current: Arc<Mutex<Request<u64, FileDiff>>>,
122139
hash: u64,
123140
) -> Result<bool> {
124-
let res =
125-
sync::diff::get_diff(CWD, params.0.clone(), params.1)?;
141+
let res = match params.diff_type {
142+
DiffType::Stage => {
143+
sync::diff::get_diff(CWD, params.path.clone(), true)?
144+
}
145+
DiffType::WorkDir => {
146+
sync::diff::get_diff(CWD, params.path.clone(), false)?
147+
}
148+
DiffType::Commit(id) => sync::diff::get_diff_commit(
149+
CWD,
150+
id,
151+
params.path.clone(),
152+
)?,
153+
};
126154

127155
let mut notify = false;
128156
{

asyncgit/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod sync;
1515

1616
pub use crate::{
1717
commit_files::AsyncCommitFiles,
18-
diff::{AsyncDiff, DiffParams},
18+
diff::{AsyncDiff, DiffParams, DiffType},
1919
revlog::{AsyncLog, FetchStatus},
2020
status::{AsyncStatus, StatusParams},
2121
sync::{

src/components/inspect_commit.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use crate::{
77
accessors, keys, queue::Queue, strings, ui::style::Theme,
88
};
99
use anyhow::Result;
10-
use asyncgit::{sync, AsyncNotification, CWD};
10+
use asyncgit::{
11+
sync, AsyncDiff, AsyncNotification, DiffParams, DiffType,
12+
};
1113
use crossbeam_channel::Sender;
1214
use crossterm::event::Event;
1315
use strings::commands;
@@ -23,6 +25,7 @@ pub struct InspectCommitComponent {
2325
commit_id: Option<CommitId>,
2426
diff: DiffComponent,
2527
details: CommitDetailsComponent,
28+
git_diff: AsyncDiff,
2629
visible: bool,
2730
}
2831

@@ -159,6 +162,7 @@ impl InspectCommitComponent {
159162
),
160163
diff: DiffComponent::new(None, theme),
161164
commit_id: None,
165+
git_diff: AsyncDiff::new(sender.clone()),
162166
visible: false,
163167
}
164168
}
@@ -173,7 +177,7 @@ impl InspectCommitComponent {
173177

174178
///
175179
pub fn any_work_pending(&self) -> bool {
176-
self.details.any_work_pending()
180+
self.git_diff.is_pending() || self.details.any_work_pending()
177181
}
178182

179183
///
@@ -185,7 +189,7 @@ impl InspectCommitComponent {
185189
if let AsyncNotification::CommitFiles = ev {
186190
self.update()?
187191
} else if let AsyncNotification::Diff = ev {
188-
self.update()?
192+
self.update_diff()?
189193
}
190194
}
191195

@@ -198,13 +202,21 @@ impl InspectCommitComponent {
198202
if let Some(id) = self.commit_id {
199203
if let Some(f) = self.details.files().selection_file()
200204
{
201-
self.diff.update(
202-
f.path.clone(),
203-
false,
204-
sync::get_diff_commit(CWD, id, f.path)?,
205-
)?;
205+
let diff_params = DiffParams {
206+
path: f.path.clone(),
207+
diff_type: DiffType::Commit(id),
208+
};
209+
210+
if let Some((params, last)) =
211+
self.git_diff.last()?
212+
{
213+
if params == diff_params {
214+
self.diff.update(f.path, false, last)?;
215+
return Ok(());
216+
}
217+
}
206218

207-
return Ok(());
219+
self.git_diff.request(diff_params)?;
208220
}
209221
}
210222

src/tabs/status.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
use anyhow::Result;
1414
use asyncgit::{
1515
sync::{self, status::StatusType},
16-
AsyncDiff, AsyncNotification, AsyncStatus, DiffParams,
16+
AsyncDiff, AsyncNotification, AsyncStatus, DiffParams, DiffType,
1717
StatusParams, CWD,
1818
};
1919
use components::{command_pump, visibility_blocking};
@@ -243,7 +243,16 @@ impl Status {
243243
///
244244
pub fn update_diff(&mut self) -> Result<()> {
245245
if let Some((path, is_stage)) = self.selected_path() {
246-
let diff_params = DiffParams(path.clone(), is_stage);
246+
let diff_type = if is_stage {
247+
DiffType::Stage
248+
} else {
249+
DiffType::WorkDir
250+
};
251+
252+
let diff_params = DiffParams {
253+
path: path.clone(),
254+
diff_type,
255+
};
247256

248257
if self.diff.current() == (path.clone(), is_stage) {
249258
// we are already showing a diff of the right file

0 commit comments

Comments
 (0)