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,86 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::{GetCommitDiffRequest, GetDiffRequest, GetDiffResponse};
|
||||
|
||||
impl GitBare {
|
||||
pub fn get_commit_diff(&self, request: GetCommitDiffRequest) -> GitResult<GetDiffResponse> {
|
||||
let commit = 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 => "HEAD".into(),
|
||||
};
|
||||
let base = self.first_parent_or_empty_tree(&commit)?;
|
||||
self.get_diff(GetDiffRequest {
|
||||
repository: request.repository,
|
||||
base: Some(crate::pb::ObjectSelector {
|
||||
selector: Some(crate::pb::object_selector::Selector::Revision(
|
||||
crate::pb::ObjectName { revision: base },
|
||||
)),
|
||||
}),
|
||||
head: Some(crate::pb::ObjectSelector {
|
||||
selector: Some(crate::pb::object_selector::Selector::Revision(
|
||||
crate::pb::ObjectName { revision: commit },
|
||||
)),
|
||||
}),
|
||||
options: request.options,
|
||||
pagination: request.pagination,
|
||||
})
|
||||
}
|
||||
|
||||
fn first_parent_or_empty_tree(&self, commit: &str) -> GitResult<String> {
|
||||
let result = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"rev-list",
|
||||
"--parents",
|
||||
"-n",
|
||||
"1",
|
||||
commit,
|
||||
],
|
||||
)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !result.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: result.status.code(),
|
||||
stderr: String::from_utf8_lossy(&result.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
let output = String::from_utf8_lossy(&result.stdout);
|
||||
let parts = output.split_whitespace().collect::<Vec<_>>();
|
||||
if let Some(parent) = parts.get(1) {
|
||||
return Ok((*parent).to_string());
|
||||
}
|
||||
|
||||
let empty_tree = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"hash-object",
|
||||
"-t",
|
||||
"tree",
|
||||
"-w",
|
||||
"--stdin",
|
||||
],
|
||||
)
|
||||
.stdin_bytes(Vec::<u8>::new())
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !empty_tree.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: empty_tree.status.code(),
|
||||
stderr: String::from_utf8_lossy(&empty_tree.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
Ok(String::from_utf8_lossy(&empty_tree.stdout)
|
||||
.trim()
|
||||
.to_string())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::diff::get_diff_stats::{diff_stats_for_range, push_diff_options};
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::paginate;
|
||||
use crate::pb::diff_file::ChangeType;
|
||||
use crate::pb::{DiffFile, GetDiffRequest, GetDiffResponse};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct NameStatusEntry {
|
||||
status: char,
|
||||
old_path: String,
|
||||
new_path: String,
|
||||
similarity: f64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
struct TreeMeta {
|
||||
oid_hex: String,
|
||||
mode: u32,
|
||||
}
|
||||
|
||||
impl GitBare {
|
||||
pub fn get_diff(&self, request: GetDiffRequest) -> GitResult<GetDiffResponse> {
|
||||
let base = match request.base.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 => "HEAD".into(),
|
||||
};
|
||||
let head = match request.head.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 => "HEAD".into(),
|
||||
};
|
||||
|
||||
let options = request.options.as_ref();
|
||||
let entries = self.diff_name_status(&base, &head, options)?;
|
||||
let max_files = options.and_then(|o| (o.max_files > 0).then_some(o.max_files as usize));
|
||||
let overflow = max_files.is_some_and(|max| entries.len() > max);
|
||||
let entries_to_build =
|
||||
max_files.map_or(entries.as_slice(), |max| &entries[..entries.len().min(max)]);
|
||||
|
||||
let mut files = Vec::with_capacity(entries_to_build.len());
|
||||
for entry in entries_to_build {
|
||||
let old_meta = if !entry.old_path.is_empty() {
|
||||
self.tree_meta(&base, &entry.old_path).ok().flatten()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let new_meta = if !entry.new_path.is_empty() {
|
||||
self.tree_meta(&head, &entry.new_path).ok().flatten()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let (additions, deletions, binary) = self.path_numstat(&base, &head, entry)?;
|
||||
let (patch, too_large) = self.path_patch(&base, &head, entry, options)?;
|
||||
|
||||
files.push(DiffFile {
|
||||
old_path: entry.old_path.clone(),
|
||||
new_path: entry.new_path.clone(),
|
||||
old_oid: old_meta.as_ref().map(|m| self.oid_to_pb(&m.oid_hex)),
|
||||
new_oid: new_meta.as_ref().map(|m| self.oid_to_pb(&m.oid_hex)),
|
||||
old_mode: old_meta.as_ref().map(|m| m.mode).unwrap_or(0),
|
||||
new_mode: new_meta.as_ref().map(|m| m.mode).unwrap_or(0),
|
||||
change_type: change_type(entry.status) as i32,
|
||||
binary,
|
||||
too_large,
|
||||
additions,
|
||||
deletions,
|
||||
hunks: Vec::new(),
|
||||
patch,
|
||||
similarity: entry.similarity,
|
||||
});
|
||||
}
|
||||
|
||||
let stats = diff_stats_for_range(self, &base, &head, options)?;
|
||||
let (files, page_info) = paginate::paginate(&files, request.pagination.as_ref());
|
||||
|
||||
Ok(GetDiffResponse {
|
||||
files,
|
||||
stats: Some(stats),
|
||||
page_info: Some(page_info),
|
||||
overflow,
|
||||
})
|
||||
}
|
||||
|
||||
fn diff_name_status(
|
||||
&self,
|
||||
base: &str,
|
||||
head: &str,
|
||||
options: Option<&crate::pb::DiffOptions>,
|
||||
) -> GitResult<Vec<NameStatusEntry>> {
|
||||
let mut args = vec![
|
||||
"--git-dir".to_string(),
|
||||
self.bare_dir.to_string_lossy().into_owned(),
|
||||
"diff".into(),
|
||||
"--name-status".into(),
|
||||
"-z".into(),
|
||||
];
|
||||
push_diff_options(&mut args, options);
|
||||
args.push(base.to_string());
|
||||
args.push(head.to_string());
|
||||
if let Some(options) = options
|
||||
&& !options.pathspec.is_empty()
|
||||
{
|
||||
args.push("--".into());
|
||||
args.extend(options.pathspec.iter().cloned());
|
||||
}
|
||||
|
||||
let result = duct::cmd("git", &args)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !result.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: result.status.code(),
|
||||
stderr: String::from_utf8_lossy(&result.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
|
||||
let parts = result
|
||||
.stdout
|
||||
.split(|b| *b == 0)
|
||||
.filter(|part| !part.is_empty())
|
||||
.map(|part| String::from_utf8_lossy(part).into_owned())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut entries = Vec::new();
|
||||
let mut idx = 0;
|
||||
while idx < parts.len() {
|
||||
let status_token = &parts[idx];
|
||||
idx += 1;
|
||||
let status = status_token.chars().next().unwrap_or('M');
|
||||
let similarity = status_token
|
||||
.get(1..)
|
||||
.and_then(|s| s.parse::<f64>().ok())
|
||||
.unwrap_or(0.0);
|
||||
|
||||
if matches!(status, 'R' | 'C') {
|
||||
if idx + 1 >= parts.len() {
|
||||
break;
|
||||
}
|
||||
let old_path = parts[idx].clone();
|
||||
let new_path = parts[idx + 1].clone();
|
||||
idx += 2;
|
||||
entries.push(NameStatusEntry {
|
||||
status,
|
||||
old_path,
|
||||
new_path,
|
||||
similarity,
|
||||
});
|
||||
} else {
|
||||
if idx >= parts.len() {
|
||||
break;
|
||||
}
|
||||
let path = parts[idx].clone();
|
||||
idx += 1;
|
||||
let (old_path, new_path) = match status {
|
||||
'A' => (String::new(), path),
|
||||
'D' => (path, String::new()),
|
||||
_ => (path.clone(), path),
|
||||
};
|
||||
entries.push(NameStatusEntry {
|
||||
status,
|
||||
old_path,
|
||||
new_path,
|
||||
similarity,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
fn tree_meta(&self, revision: &str, path: &str) -> GitResult<Option<TreeMeta>> {
|
||||
let result = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"ls-tree",
|
||||
"-z",
|
||||
"-l",
|
||||
revision,
|
||||
"--",
|
||||
path,
|
||||
],
|
||||
)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !result.status.success() || result.stdout.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let record = result
|
||||
.stdout
|
||||
.split(|b| *b == 0)
|
||||
.find(|part| !part.is_empty())
|
||||
.map(|part| String::from_utf8_lossy(part).into_owned());
|
||||
let Some(record) = record else {
|
||||
return Ok(None);
|
||||
};
|
||||
let Some((meta, _path)) = record.split_once('\t') else {
|
||||
return Ok(None);
|
||||
};
|
||||
let parts = meta.split_whitespace().collect::<Vec<_>>();
|
||||
if parts.len() < 3 {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some(TreeMeta {
|
||||
mode: u32::from_str_radix(parts[0], 8).unwrap_or(0),
|
||||
oid_hex: parts[2].to_string(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn path_numstat(
|
||||
&self,
|
||||
base: &str,
|
||||
head: &str,
|
||||
entry: &NameStatusEntry,
|
||||
) -> GitResult<(u32, u32, bool)> {
|
||||
let path = if entry.new_path.is_empty() {
|
||||
&entry.old_path
|
||||
} else {
|
||||
&entry.new_path
|
||||
};
|
||||
let result = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"diff",
|
||||
"--numstat",
|
||||
base,
|
||||
head,
|
||||
"--",
|
||||
path,
|
||||
],
|
||||
)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !result.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: result.status.code(),
|
||||
stderr: String::from_utf8_lossy(&result.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
let line = String::from_utf8_lossy(&result.stdout)
|
||||
.lines()
|
||||
.next()
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
let mut parts = line.split('\t');
|
||||
let add = parts.next().unwrap_or_default();
|
||||
let del = parts.next().unwrap_or_default();
|
||||
let binary = add == "-" || del == "-";
|
||||
Ok((add.parse().unwrap_or(0), del.parse().unwrap_or(0), binary))
|
||||
}
|
||||
|
||||
fn path_patch(
|
||||
&self,
|
||||
base: &str,
|
||||
head: &str,
|
||||
entry: &NameStatusEntry,
|
||||
options: Option<&crate::pb::DiffOptions>,
|
||||
) -> GitResult<(Vec<u8>, bool)> {
|
||||
let Some(options) = options else {
|
||||
return Ok((Vec::new(), false));
|
||||
};
|
||||
if !options.include_patch {
|
||||
return Ok((Vec::new(), false));
|
||||
}
|
||||
|
||||
let path = if entry.new_path.is_empty() {
|
||||
&entry.old_path
|
||||
} else {
|
||||
&entry.new_path
|
||||
};
|
||||
let context = options.context_lines.to_string();
|
||||
let mut args = vec![
|
||||
"--git-dir".to_string(),
|
||||
self.bare_dir.to_string_lossy().into_owned(),
|
||||
"diff".into(),
|
||||
"--patch".into(),
|
||||
format!("--unified={context}"),
|
||||
];
|
||||
if options.include_binary {
|
||||
args.push("--binary".into());
|
||||
}
|
||||
push_diff_options(&mut args, Some(options));
|
||||
args.push(base.to_string());
|
||||
args.push(head.to_string());
|
||||
args.push("--".into());
|
||||
args.push(path.to_string());
|
||||
|
||||
let result = duct::cmd("git", &args)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !result.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: result.status.code(),
|
||||
stderr: String::from_utf8_lossy(&result.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
|
||||
let mut patch = result.stdout;
|
||||
let too_large = options.max_bytes > 0 && patch.len() > options.max_bytes as usize;
|
||||
if too_large {
|
||||
patch.truncate(options.max_bytes as usize);
|
||||
}
|
||||
Ok((patch, too_large))
|
||||
}
|
||||
}
|
||||
|
||||
fn change_type(status: char) -> ChangeType {
|
||||
match status {
|
||||
'A' => ChangeType::DiffFileChangeTypeAdded,
|
||||
'D' => ChangeType::DiffFileChangeTypeDeleted,
|
||||
'R' => ChangeType::DiffFileChangeTypeRenamed,
|
||||
'C' => ChangeType::DiffFileChangeTypeCopied,
|
||||
'T' => ChangeType::DiffFileChangeTypeTypeChanged,
|
||||
'U' => ChangeType::DiffFileChangeTypeUnmerged,
|
||||
_ => ChangeType::DiffFileChangeTypeModified,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::GetDiffStatsRequest;
|
||||
|
||||
impl GitBare {
|
||||
pub fn get_diff_stats(&self, request: GetDiffStatsRequest) -> GitResult<crate::pb::DiffStats> {
|
||||
let base = match request.base.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 => "HEAD".into(),
|
||||
};
|
||||
let head = match request.head.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 => "HEAD".into(),
|
||||
};
|
||||
diff_stats_for_range(self, &base, &head, request.options.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn diff_stats_for_range(
|
||||
repo: &GitBare,
|
||||
base: &str,
|
||||
head: &str,
|
||||
options: Option<&crate::pb::DiffOptions>,
|
||||
) -> GitResult<crate::pb::DiffStats> {
|
||||
let mut args = vec![
|
||||
"--git-dir".to_string(),
|
||||
repo.bare_dir.to_string_lossy().into_owned(),
|
||||
"diff".into(),
|
||||
"--shortstat".into(),
|
||||
];
|
||||
push_diff_options(&mut args, options);
|
||||
args.push(base.to_string());
|
||||
args.push(head.to_string());
|
||||
if let Some(options) = options
|
||||
&& !options.pathspec.is_empty()
|
||||
{
|
||||
args.push("--".into());
|
||||
args.extend(options.pathspec.iter().cloned());
|
||||
}
|
||||
|
||||
let result = duct::cmd("git", &args)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !result.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: result.status.code(),
|
||||
stderr: String::from_utf8_lossy(&result.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(parse_shortstat(&String::from_utf8_lossy(&result.stdout)))
|
||||
}
|
||||
|
||||
pub(crate) fn parse_shortstat(output: &str) -> crate::pb::DiffStats {
|
||||
let mut stats = crate::pb::DiffStats::default();
|
||||
for part in output.trim().split(',') {
|
||||
let part = part.trim();
|
||||
if let Some(n) = part
|
||||
.strip_suffix(" insertion(+)")
|
||||
.or_else(|| part.strip_suffix(" insertions(+)"))
|
||||
{
|
||||
stats.additions = n.trim().parse().unwrap_or(0);
|
||||
} else if let Some(n) = part
|
||||
.strip_suffix(" deletion(-)")
|
||||
.or_else(|| part.strip_suffix(" deletions(-)"))
|
||||
{
|
||||
stats.deletions = n.trim().parse().unwrap_or(0);
|
||||
} else if let Some(n) = part
|
||||
.strip_suffix(" file changed")
|
||||
.or_else(|| part.strip_suffix(" files changed"))
|
||||
{
|
||||
stats.changed_files = n.trim().parse().unwrap_or(0);
|
||||
}
|
||||
}
|
||||
stats
|
||||
}
|
||||
|
||||
pub(crate) fn push_diff_options(args: &mut Vec<String>, options: Option<&crate::pb::DiffOptions>) {
|
||||
let Some(options) = options else {
|
||||
return;
|
||||
};
|
||||
|
||||
if options.rename_detection {
|
||||
args.push("-M".into());
|
||||
}
|
||||
if options.copy_detection {
|
||||
args.push("-C".into());
|
||||
}
|
||||
|
||||
match crate::pb::diff_options::WhitespaceMode::try_from(options.whitespace_mode)
|
||||
.unwrap_or(crate::pb::diff_options::WhitespaceMode::DiffWhitespaceModeDefault)
|
||||
{
|
||||
crate::pb::diff_options::WhitespaceMode::DiffWhitespaceModeIgnoreAll => {
|
||||
args.push("-w".into())
|
||||
}
|
||||
crate::pb::diff_options::WhitespaceMode::DiffWhitespaceModeIgnoreChange => {
|
||||
args.push("-b".into())
|
||||
}
|
||||
crate::pb::diff_options::WhitespaceMode::DiffWhitespaceModeIgnoreEol => {
|
||||
args.push("--ignore-space-at-eol".into());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
use crate::bare::GitBare;
|
||||
use crate::error::{GitError, GitResult};
|
||||
use crate::pb::{GetPatchRequest, GetPatchResponse};
|
||||
|
||||
impl GitBare {
|
||||
pub fn get_patch(&self, request: GetPatchRequest) -> GitResult<Vec<GetPatchResponse>> {
|
||||
let base = match request.base.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 => "HEAD".into(),
|
||||
};
|
||||
let head = match request.head.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 => "HEAD".into(),
|
||||
};
|
||||
let result = duct::cmd(
|
||||
"git",
|
||||
[
|
||||
"--git-dir",
|
||||
self.bare_dir.to_string_lossy().as_ref(),
|
||||
"diff",
|
||||
"--patch",
|
||||
&base,
|
||||
&head,
|
||||
],
|
||||
)
|
||||
.stdout_capture()
|
||||
.stderr_capture()
|
||||
.unchecked()
|
||||
.run()?;
|
||||
if !result.status.success() {
|
||||
return Err(GitError::CommandFailed {
|
||||
status_code: result.status.code(),
|
||||
stderr: String::from_utf8_lossy(&result.stderr).into_owned(),
|
||||
});
|
||||
}
|
||||
Ok(vec![GetPatchResponse {
|
||||
data: result.stdout,
|
||||
}])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
pub mod get_commit_diff;
|
||||
pub mod get_diff;
|
||||
pub mod get_diff_stats;
|
||||
pub mod get_patch;
|
||||
Reference in New Issue
Block a user