feat(core): implement Git repository operations with gRPC services
- Add advertise_refs functionality for Git protocol communication - Implement archive service with TAR/ZIP format support and streaming - Create blame service for Git file annotation with line tracking - Add branch management including create, delete, rename and compare operations - Implement merge checking with conflict detection and fast-forward handling - Add cherry-pick functionality for applying commits between branches - Integrate gix library for Git repository operations and object handling - Add comprehensive test suite covering all Git operations - Implement proper error handling and repository validation - Add pagination support for large result sets - Create protobuf definitions for all Git operations and data structures - Add build system for gRPC code generation and dependency management
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::commit::create_commit::command_ok;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::{CherryPickCommitRequest, CreateCommitResponse, GetCommitRequest};
|
||||
|
||||
impl GitBare {
|
||||
pub fn cherry_pick_commit(
|
||||
&self,
|
||||
request: CherryPickCommitRequest,
|
||||
) -> GitResult<CreateCommitResponse> {
|
||||
let target_branch = request.branch.clone();
|
||||
let cp_revision = match request.commit.and_then(|s| s.selector) {
|
||||
Some(crate::pb::object_selector::Selector::Oid(oid)) => oid.hex,
|
||||
Some(crate::pb::object_selector::Selector::Revision(name)) => name.revision,
|
||||
None => return Err(GitError::InvalidArgument("commit is required".into())),
|
||||
};
|
||||
|
||||
let repo = self.gix_repo()?;
|
||||
|
||||
let branch_ref = format!("refs/heads/{}", target_branch);
|
||||
let branch_tip = repo
|
||||
.find_reference(branch_ref.as_str())
|
||||
.ok()
|
||||
.and_then(|mut r| r.peel_to_id().ok())
|
||||
.map(|id| id.to_string())
|
||||
.ok_or_else(|| GitError::RefNotFound(target_branch.clone()))?;
|
||||
|
||||
let cp_id = repo.rev_parse_single(cp_revision.as_str())?;
|
||||
let cp_obj = cp_id
|
||||
.object()?
|
||||
.try_into_commit()
|
||||
.map_err(|e| GitError::Gix(e.to_string()))?;
|
||||
let parent_id = cp_obj.parent_ids().next().map(|p| p.to_string());
|
||||
|
||||
let tmp_index = tempfile::Builder::new()
|
||||
.prefix("gitks-cp-")
|
||||
.tempfile_in(&self.bare_dir)?;
|
||||
let idx_path = tmp_index.path().to_string_lossy().into_owned();
|
||||
let bare = self.bare_dir.to_string_lossy().into_owned();
|
||||
|
||||
let read_tree = duct::cmd(
|
||||
"git",
|
||||
["--git-dir", bare.as_str(), "read-tree", branch_tip.as_str()],
|
||||
)
|
||||
.env("GIT_INDEX_FILE", &idx_path)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
command_ok(read_tree)?;
|
||||
|
||||
let mut format_patch_args = vec![
|
||||
"--git-dir".to_string(),
|
||||
bare.clone(),
|
||||
"format-patch".to_string(),
|
||||
"--stdout".to_string(),
|
||||
"--full-index".to_string(),
|
||||
"--binary".to_string(),
|
||||
"-1".to_string(),
|
||||
];
|
||||
if parent_id.is_none() {
|
||||
format_patch_args.push("--root".to_string());
|
||||
}
|
||||
format_patch_args.push(cp_revision.clone());
|
||||
|
||||
let diff = duct::cmd("git", &format_patch_args)
|
||||
.env("GIT_INDEX_FILE", &idx_path)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
let patch_data = command_ok(diff)?;
|
||||
|
||||
let apply = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
bare.as_str(),
|
||||
"apply",
|
||||
"--cached",
|
||||
"--allow-empty",
|
||||
"-",
|
||||
],
|
||||
)
|
||||
.env("GIT_INDEX_FILE", &idx_path)
|
||||
.stdin_bytes(patch_data.as_bytes())
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !apply.status.success() {
|
||||
return Err(GitError::Internal(format!(
|
||||
"cherry-pick apply failed: {}",
|
||||
String::from_utf8_lossy(&apply.stderr)
|
||||
)));
|
||||
}
|
||||
|
||||
let write_tree = duct::cmd("git", ["--git-dir", bare.as_str(), "write-tree"])
|
||||
.env("GIT_INDEX_FILE", &idx_path)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
let tree_id = command_ok(write_tree)?.trim().to_string();
|
||||
|
||||
let message = cp_obj.message_raw()?.to_string();
|
||||
|
||||
let parents = vec![branch_tip.clone()];
|
||||
let commit_id = self.commit_tree(
|
||||
&tree_id,
|
||||
&parents,
|
||||
&message,
|
||||
request.committer.as_ref(),
|
||||
request.committer.as_ref(),
|
||||
)?;
|
||||
|
||||
self.update_branch_ref(&target_branch, &commit_id, Some(&branch_tip), false)?;
|
||||
|
||||
Ok(CreateCommitResponse {
|
||||
commit: Some(self.get_commit(GetCommitRequest {
|
||||
repository: request.repository,
|
||||
revision: Some(crate::pb::ObjectSelector {
|
||||
selector: Some(crate::pb::object_selector::Selector::Revision(
|
||||
crate::pb::ObjectName {
|
||||
revision: commit_id,
|
||||
},
|
||||
)),
|
||||
}),
|
||||
include_stats: false,
|
||||
include_raw: false,
|
||||
})?),
|
||||
branch: target_branch,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn update_branch_ref(
|
||||
&self,
|
||||
branch: &str,
|
||||
commit_id: &str,
|
||||
old_value: Option<&str>,
|
||||
force: bool,
|
||||
) -> GitResult<()> {
|
||||
let refname = format!("refs/heads/{}", branch);
|
||||
let mut args = vec![
|
||||
"--git-dir".to_string(),
|
||||
self.bare_dir.to_string_lossy().into_owned(),
|
||||
"update-ref".into(),
|
||||
refname,
|
||||
commit_id.to_string(),
|
||||
];
|
||||
if !force {
|
||||
args.push(old_value.unwrap_or(crate::oid::ZERO_OID).to_string());
|
||||
}
|
||||
let update = duct::cmd("git", &args)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
command_ok(update).map(|_| ())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user