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
73 changes: 73 additions & 0 deletions asyncgit/src/branch_compare.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::{
asyncjob::{AsyncJob, RunParams},
error::Result,
sync::{branch::branch_compare_upstream, BranchCompare, RepoPath},
AsyncGitNotification,
};
use std::sync::{Arc, Mutex};

enum JobState {
Request {
repo: RepoPath,
branch: String,
},
Response(Result<BranchCompare>),
}

///
#[derive(Clone, Default)]
pub struct AsyncBranchCompareJob {
state: Arc<Mutex<Option<JobState>>>,
}

impl AsyncBranchCompareJob {
///
pub fn new(repo: RepoPath, branch: String) -> Self {
Self {
state: Arc::new(Mutex::new(Some(JobState::Request {
repo,
branch,
}))),
}
}

///
pub fn result(&self) -> Option<Result<BranchCompare>> {
if let Ok(mut state) = self.state.lock() {
if let Some(state) = state.take() {
return match state {
JobState::Request { .. } => None,
JobState::Response(result) => Some(result),
};
}
}

None
}
}

impl AsyncJob for AsyncBranchCompareJob {
type Notification = AsyncGitNotification;
type Progress = ();

fn run(
&mut self,
_params: RunParams<Self::Notification, Self::Progress>,
) -> Result<Self::Notification> {
if let Ok(mut state) = self.state.lock() {
*state = state.take().map(|state| match state {
JobState::Request { repo, branch } => {
let compare =
branch_compare_upstream(&repo, &branch);

JobState::Response(compare)
}
JobState::Response(result) => {
JobState::Response(result)
}
});
}

Ok(AsyncGitNotification::BranchCompare)
}
}
4 changes: 4 additions & 0 deletions asyncgit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ It wraps libraries like git2 and gix.

pub mod asyncjob;
mod blame;
mod branch_compare;
mod branches;
pub mod cached;
mod commit_files;
Expand All @@ -66,6 +67,7 @@ mod treefiles;

pub use crate::{
blame::{AsyncBlame, BlameParams},
branch_compare::AsyncBranchCompareJob,
branches::AsyncBranchesJob,
commit_files::{AsyncCommitFiles, CommitFilesParams},
diff::{AsyncDiff, DiffParams, DiffType},
Expand Down Expand Up @@ -125,6 +127,8 @@ pub enum AsyncGitNotification {
///
Branches,
///
BranchCompare,
///
TreeFiles,
///
CommitFilter,
Expand Down