Skip to content

Commit

Permalink
WIP: Get commit history
Browse files Browse the repository at this point in the history
Refactor commit history to remove Arc and add handler

- Removed Arc from commit history return type
- Added handle_commit_history request handler
- Updated proto definitions for commit history requests and responses
  • Loading branch information
deepkhurana1999 committed Mar 9, 2025
1 parent baabf3f commit 35156ef
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crates/git/src/commit_history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::repository::CommitDetails;
use anyhow::{anyhow, Result};
use std::{path::Path, process::Stdio, sync::Arc};
use std::{path::Path, process::Stdio};

pub struct CommitHistory {}

Expand All @@ -10,7 +10,7 @@ impl CommitHistory {
working_directory: &Path,
skip: i32,
limit: i32,
) -> Result<Arc<Vec<CommitDetails>>> {
) -> Result<Vec<CommitDetails>> {
const COMMIT_WRAPPER_START: &str = "<COMMIT_START>";
const COMMIT_WRAPPER_END: &str = "<COMMIT_END>";
const DATA_MARKER: &str = "<DATA_MARKER>";
Expand Down
8 changes: 4 additions & 4 deletions crates/git/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub trait GitRepository: Send + Sync {
/// Returns the list of git statuses, sorted by path
fn status(&self, path_prefixes: &[RepoPath]) -> Result<GitStatus>;

fn commit_history(&self, skip: i32, limit: i32) -> Result<Arc<Vec<CommitDetails>>>;
fn commit_history(&self, skip: i32, limit: i32) -> Result<Vec<CommitDetails>>;

fn branches(&self) -> Result<Vec<Branch>>;
fn change_branch(&self, _: &str) -> Result<()>;
Expand Down Expand Up @@ -473,7 +473,7 @@ impl GitRepository for RealGitRepository {
GitStatus::new(&self.git_binary_path, &working_directory, path_prefixes)
}

fn commit_history(&self, skip: i32, limit: i32) -> Result<Arc<Vec<CommitDetails>>> {
fn commit_history(&self, skip: i32, limit: i32) -> Result<Vec<CommitDetails>> {
let working_directory = self
.repository
.lock()
Expand Down Expand Up @@ -1006,10 +1006,10 @@ impl GitRepository for FakeGitRepository {
})
}

fn commit_history(&self, skip: i32, limit: i32) -> Result<Arc<Vec<CommitDetails>>> {
fn commit_history(&self, skip: i32, limit: i32) -> Result<Vec<CommitDetails>> {
let _ = limit;
let _ = skip;
Ok(Arc::new(Vec::new()))
Ok(Vec::new())
}

fn branches(&self) -> Result<Vec<Branch>> {
Expand Down
75 changes: 75 additions & 0 deletions crates/project/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl GitStore {
client.add_entity_request_handler(Self::handle_askpass);
client.add_entity_request_handler(Self::handle_check_for_pushed_commits);
client.add_entity_request_handler(Self::handle_git_diff);
client.add_entity_request_handler(Self::handle_commit_history);
}

pub fn active_repository(&self) -> Option<Entity<Repository>> {
Expand Down Expand Up @@ -663,6 +664,37 @@ impl GitStore {
Ok(proto::Ack {})
}

async fn handle_commit_history(
this: Entity<Self>,
envelope: TypedEnvelope<proto::GitGetCommitHistory>,
mut cx: AsyncApp,
) -> Result<proto::GitCommitHistoryResponse> {
let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
let work_directory_id = ProjectEntryId::from_proto(envelope.payload.work_directory_id);
let repository_handle =
Self::repository_for_request(&this, worktree_id, work_directory_id, &mut cx)?;

let commits = repository_handle
.update(&mut cx, |repository_handle, _| {
repository_handle.get_commit_history(envelope.payload.skip, envelope.payload.limit)
})?
.await??;

Ok(proto::GitCommitHistoryResponse {
commits: commits
.clone()
.into_iter()
.map(|commit| proto::GitCommitDetails {
committer_name: commit.committer_name.into(),
commit_timestamp: commit.commit_timestamp.into(),
committer_email: commit.committer_email.into(),
sha: commit.sha.into(),
message: commit.message.into(),
})
.collect::<Vec<_>>(),
})
}

async fn handle_show(
this: Entity<Self>,
envelope: TypedEnvelope<proto::GitShow>,
Expand Down Expand Up @@ -1662,6 +1694,49 @@ impl Repository {
})
}

pub fn get_commit_history(
&self,
skip: i32,
limit: i32,
) -> oneshot::Receiver<Result<Vec<CommitDetails>>> {
self.send_job(move |repo| async move {
match repo {
GitRepo::Local(git_repository) => git_repository.commit_history(skip, limit),
GitRepo::Remote {
project_id,
client,
worktree_id,
work_directory_id,
} => {
let response = client
.request(proto::GitGetCommitHistory {
project_id: project_id.0,
worktree_id: worktree_id.to_proto(),
work_directory_id: work_directory_id.to_proto(),
skip: skip,
limit: limit,
})
.await?;

let commits = response
.commits
.clone()
.into_iter()
.map(|commit| CommitDetails {
committer_name: commit.committer_name.into(),
commit_timestamp: commit.commit_timestamp.into(),
committer_email: commit.committer_email.into(),
sha: commit.sha.into(),
message: commit.message.into(),
})
.collect();

Ok(commits)
}
}
})
}

pub fn diff(&self, diff_type: DiffType, _cx: &App) -> oneshot::Receiver<Result<String>> {
self.send_job(|repo| async move {
match repo {
Expand Down
16 changes: 15 additions & 1 deletion crates/proto/proto/zed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ message Envelope {
AskPassResponse ask_pass_response = 318;

GitDiff git_diff = 319;
GitDiffResponse git_diff_response = 320; // current max
GitDiffResponse git_diff_response = 320;
GitGetCommitHistory git_get_commit_history = 321;
GitCommitHistoryResponse git_commit_history_response = 322; // current max
}

reserved 87 to 88;
Expand Down Expand Up @@ -2709,6 +2711,10 @@ message GitBranchesResponse {
repeated Branch branches = 1;
}

message GitCommitHistoryResponse {
repeated GitCommitDetails commits = 1;
}

message UpdateGitBranch {
uint64 project_id = 1;
string branch_name = 2;
Expand Down Expand Up @@ -2937,3 +2943,11 @@ message GitDiff {
message GitDiffResponse {
string diff = 1;
}

message GitGetCommitHistory {
uint64 project_id = 1;
uint64 worktree_id = 2;
uint64 work_directory_id = 3;
int32 skip = 4;
int32 limit = 5;
}
4 changes: 4 additions & 0 deletions crates/proto/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ messages!(
(CheckForPushedCommitsResponse, Background),
(GitDiff, Background),
(GitDiffResponse, Background),
(GitGetCommitHistory, Background),
(GitCommitHistoryResponse, Background),
);

request_messages!(
Expand Down Expand Up @@ -607,6 +609,7 @@ request_messages!(
(GitChangeBranch, Ack),
(CheckForPushedCommits, CheckForPushedCommitsResponse),
(GitDiff, GitDiffResponse),
(GitGetCommitHistory, GitCommitHistoryResponse)
);

entity_messages!(
Expand Down Expand Up @@ -713,6 +716,7 @@ entity_messages!(
GitCreateBranch,
CheckForPushedCommits,
GitDiff,
GitGetCommitHistory
);

entity_messages!(
Expand Down

0 comments on commit 35156ef

Please sign in to comment.