Move git related things into specialized git crate
Co-Authored-By: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
parent
bf3b3da6ed
commit
d5fd531743
17 changed files with 151 additions and 100 deletions
22
crates/git/Cargo.toml
Normal file
22
crates/git/Cargo.toml
Normal file
|
@ -0,0 +1,22 @@
|
|||
[package]
|
||||
name = "git"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
path = "src/git.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.38"
|
||||
clock = { path = "../clock" }
|
||||
git2 = { version = "0.15", default-features = false }
|
||||
lazy_static = "1.4.0"
|
||||
sum_tree = { path = "../sum_tree" }
|
||||
text = { path = "../text" }
|
||||
util = { path = "../util" }
|
||||
log = { version = "0.4.16", features = ["kv_unstable_serde"] }
|
||||
smol = "1.2"
|
||||
parking_lot = "0.11.1"
|
||||
|
||||
[dev-dependencies]
|
||||
unindent = "0.1.7"
|
311
crates/git/src/diff.rs
Normal file
311
crates/git/src/diff.rs
Normal file
|
@ -0,0 +1,311 @@
|
|||
use std::ops::Range;
|
||||
|
||||
use sum_tree::SumTree;
|
||||
use text::{Anchor, BufferSnapshot, OffsetRangeExt, Point, ToPoint};
|
||||
|
||||
pub use git2 as libgit;
|
||||
use libgit::{DiffLineType as GitDiffLineType, DiffOptions as GitOptions, Patch as GitPatch};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum DiffHunkStatus {
|
||||
Added,
|
||||
Modified,
|
||||
Removed,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct DiffHunk<T> {
|
||||
pub buffer_range: Range<T>,
|
||||
pub head_byte_range: Range<usize>,
|
||||
}
|
||||
|
||||
impl DiffHunk<u32> {
|
||||
pub fn status(&self) -> DiffHunkStatus {
|
||||
if self.head_byte_range.is_empty() {
|
||||
DiffHunkStatus::Added
|
||||
} else if self.buffer_range.is_empty() {
|
||||
DiffHunkStatus::Removed
|
||||
} else {
|
||||
DiffHunkStatus::Modified
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl sum_tree::Item for DiffHunk<Anchor> {
|
||||
type Summary = DiffHunkSummary;
|
||||
|
||||
fn summary(&self) -> Self::Summary {
|
||||
DiffHunkSummary {
|
||||
buffer_range: self.buffer_range.clone(),
|
||||
head_range: self.head_byte_range.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct DiffHunkSummary {
|
||||
buffer_range: Range<Anchor>,
|
||||
head_range: Range<usize>,
|
||||
}
|
||||
|
||||
impl sum_tree::Summary for DiffHunkSummary {
|
||||
type Context = text::BufferSnapshot;
|
||||
|
||||
fn add_summary(&mut self, other: &Self, _: &Self::Context) {
|
||||
self.head_range.start = self.head_range.start.min(other.head_range.start);
|
||||
self.head_range.end = self.head_range.end.max(other.head_range.end);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
struct HunkHeadEnd(usize);
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, DiffHunkSummary> for HunkHeadEnd {
|
||||
fn add_summary(&mut self, summary: &'a DiffHunkSummary, _: &text::BufferSnapshot) {
|
||||
self.0 = summary.head_range.end;
|
||||
}
|
||||
|
||||
fn from_summary(summary: &'a DiffHunkSummary, _: &text::BufferSnapshot) -> Self {
|
||||
HunkHeadEnd(summary.head_range.end)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
struct HunkBufferStart(u32);
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, DiffHunkSummary> for HunkBufferStart {
|
||||
fn add_summary(&mut self, summary: &'a DiffHunkSummary, buffer: &text::BufferSnapshot) {
|
||||
self.0 = summary.buffer_range.start.to_point(buffer).row;
|
||||
}
|
||||
|
||||
fn from_summary(summary: &'a DiffHunkSummary, buffer: &text::BufferSnapshot) -> Self {
|
||||
HunkBufferStart(summary.buffer_range.start.to_point(buffer).row)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
struct HunkBufferEnd(u32);
|
||||
|
||||
impl<'a> sum_tree::Dimension<'a, DiffHunkSummary> for HunkBufferEnd {
|
||||
fn add_summary(&mut self, summary: &'a DiffHunkSummary, buffer: &text::BufferSnapshot) {
|
||||
self.0 = summary.buffer_range.end.to_point(buffer).row;
|
||||
}
|
||||
|
||||
fn from_summary(summary: &'a DiffHunkSummary, buffer: &text::BufferSnapshot) -> Self {
|
||||
HunkBufferEnd(summary.buffer_range.end.to_point(buffer).row)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BufferDiff {
|
||||
last_buffer_version: Option<clock::Global>,
|
||||
tree: SumTree<DiffHunk<Anchor>>,
|
||||
}
|
||||
|
||||
impl BufferDiff {
|
||||
pub fn new() -> BufferDiff {
|
||||
BufferDiff {
|
||||
last_buffer_version: None,
|
||||
tree: SumTree::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hunks_in_range<'a>(
|
||||
&'a self,
|
||||
query_row_range: Range<u32>,
|
||||
buffer: &'a BufferSnapshot,
|
||||
) -> impl 'a + Iterator<Item = DiffHunk<u32>> {
|
||||
self.tree.iter().filter_map(move |hunk| {
|
||||
let range = hunk.buffer_range.to_point(&buffer);
|
||||
|
||||
if range.start.row <= query_row_range.end && query_row_range.start <= range.end.row {
|
||||
let end_row = if range.end.column > 0 {
|
||||
range.end.row + 1
|
||||
} else {
|
||||
range.end.row
|
||||
};
|
||||
|
||||
Some(DiffHunk {
|
||||
buffer_range: range.start.row..end_row,
|
||||
head_byte_range: hunk.head_byte_range.clone(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn needs_update(&self, buffer: &text::BufferSnapshot) -> bool {
|
||||
match &self.last_buffer_version {
|
||||
Some(last) => buffer.version().changed_since(last),
|
||||
None => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update(&mut self, head_text: &str, buffer: &text::BufferSnapshot) {
|
||||
let mut tree = SumTree::new();
|
||||
|
||||
let buffer_text = buffer.as_rope().to_string();
|
||||
let patch = Self::diff(&head_text, &buffer_text);
|
||||
|
||||
if let Some(patch) = patch {
|
||||
let mut divergence = 0;
|
||||
for hunk_index in 0..patch.num_hunks() {
|
||||
let hunk = Self::process_patch_hunk(&patch, hunk_index, buffer, &mut divergence);
|
||||
tree.push(hunk, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
self.tree = tree;
|
||||
self.last_buffer_version = Some(buffer.version().clone());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn hunks<'a>(&'a self, text: &'a BufferSnapshot) -> impl 'a + Iterator<Item = DiffHunk<u32>> {
|
||||
self.hunks_in_range(0..u32::MAX, text)
|
||||
}
|
||||
|
||||
fn diff<'a>(head: &'a str, current: &'a str) -> Option<GitPatch<'a>> {
|
||||
let mut options = GitOptions::default();
|
||||
options.context_lines(0);
|
||||
|
||||
let patch = GitPatch::from_buffers(
|
||||
head.as_bytes(),
|
||||
None,
|
||||
current.as_bytes(),
|
||||
None,
|
||||
Some(&mut options),
|
||||
);
|
||||
|
||||
match patch {
|
||||
Ok(patch) => Some(patch),
|
||||
|
||||
Err(err) => {
|
||||
log::error!("`GitPatch::from_buffers` failed: {}", err);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn process_patch_hunk<'a>(
|
||||
patch: &GitPatch<'a>,
|
||||
hunk_index: usize,
|
||||
buffer: &text::BufferSnapshot,
|
||||
buffer_row_divergence: &mut i64,
|
||||
) -> DiffHunk<Anchor> {
|
||||
let line_item_count = patch.num_lines_in_hunk(hunk_index).unwrap();
|
||||
assert!(line_item_count > 0);
|
||||
|
||||
let mut first_deletion_buffer_row: Option<u32> = None;
|
||||
let mut buffer_row_range: Option<Range<u32>> = None;
|
||||
let mut head_byte_range: Option<Range<usize>> = None;
|
||||
|
||||
for line_index in 0..line_item_count {
|
||||
let line = patch.line_in_hunk(hunk_index, line_index).unwrap();
|
||||
let kind = line.origin_value();
|
||||
let content_offset = line.content_offset() as isize;
|
||||
let content_len = line.content().len() as isize;
|
||||
|
||||
if kind == GitDiffLineType::Addition {
|
||||
*buffer_row_divergence += 1;
|
||||
let row = line.new_lineno().unwrap().saturating_sub(1);
|
||||
|
||||
match &mut buffer_row_range {
|
||||
Some(buffer_row_range) => buffer_row_range.end = row + 1,
|
||||
None => buffer_row_range = Some(row..row + 1),
|
||||
}
|
||||
}
|
||||
|
||||
if kind == GitDiffLineType::Deletion {
|
||||
*buffer_row_divergence -= 1;
|
||||
let end = content_offset + content_len;
|
||||
|
||||
match &mut head_byte_range {
|
||||
Some(head_byte_range) => head_byte_range.end = end as usize,
|
||||
None => head_byte_range = Some(content_offset as usize..end as usize),
|
||||
}
|
||||
|
||||
if first_deletion_buffer_row.is_none() {
|
||||
let old_row = line.old_lineno().unwrap().saturating_sub(1);
|
||||
let row = old_row as i64 + *buffer_row_divergence;
|
||||
first_deletion_buffer_row = Some(row as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//unwrap_or deletion without addition
|
||||
let buffer_row_range = buffer_row_range.unwrap_or_else(|| {
|
||||
//we cannot have an addition-less hunk without deletion(s) or else there would be no hunk
|
||||
let row = first_deletion_buffer_row.unwrap();
|
||||
row..row
|
||||
});
|
||||
|
||||
//unwrap_or addition without deletion
|
||||
let head_byte_range = head_byte_range.unwrap_or(0..0);
|
||||
|
||||
let start = Point::new(buffer_row_range.start, 0);
|
||||
let end = Point::new(buffer_row_range.end, 0);
|
||||
let buffer_range = buffer.anchor_before(start)..buffer.anchor_before(end);
|
||||
DiffHunk {
|
||||
buffer_range,
|
||||
head_byte_range,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use text::Buffer;
|
||||
use unindent::Unindent as _;
|
||||
|
||||
#[test]
|
||||
fn test_buffer_diff_simple() {
|
||||
let head_text = "
|
||||
one
|
||||
two
|
||||
three
|
||||
"
|
||||
.unindent();
|
||||
|
||||
let buffer_text = "
|
||||
one
|
||||
hello
|
||||
three
|
||||
"
|
||||
.unindent();
|
||||
|
||||
let mut buffer = Buffer::new(0, 0, buffer_text);
|
||||
let mut diff = BufferDiff::new();
|
||||
smol::block_on(diff.update(&head_text, &buffer));
|
||||
assert_hunks(&diff, &buffer, &head_text, &[(1..2, "two\n")]);
|
||||
|
||||
buffer.edit([(0..0, "point five\n")]);
|
||||
assert_hunks(&diff, &buffer, &head_text, &[(2..3, "two\n")]);
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn assert_hunks(
|
||||
diff: &BufferDiff,
|
||||
buffer: &BufferSnapshot,
|
||||
head_text: &str,
|
||||
expected_hunks: &[(Range<u32>, &str)],
|
||||
) {
|
||||
let hunks = diff.hunks(buffer).collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
hunks.len(),
|
||||
expected_hunks.len(),
|
||||
"actual hunks are {hunks:#?}"
|
||||
);
|
||||
|
||||
let diff_iter = hunks.iter().enumerate();
|
||||
for ((index, hunk), (expected_range, expected_str)) in diff_iter.zip(expected_hunks) {
|
||||
assert_eq!(&hunk.buffer_range, expected_range, "for hunk {index}");
|
||||
assert_eq!(
|
||||
&head_text[hunk.head_byte_range.clone()],
|
||||
*expected_str,
|
||||
"for hunk {index}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
12
crates/git/src/git.rs
Normal file
12
crates/git/src/git.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
use std::ffi::OsStr;
|
||||
|
||||
pub use git2 as libgit;
|
||||
pub use lazy_static::lazy_static;
|
||||
|
||||
pub mod diff;
|
||||
pub mod repository;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref DOT_GIT: &'static OsStr = OsStr::new(".git");
|
||||
pub static ref GITIGNORE: &'static OsStr = OsStr::new(".gitignore");
|
||||
}
|
95
crates/git/src/repository.rs
Normal file
95
crates/git/src/repository.rs
Normal file
|
@ -0,0 +1,95 @@
|
|||
use anyhow::Result;
|
||||
use git2::Repository as LibGitRepository;
|
||||
use parking_lot::Mutex;
|
||||
use std::{path::Path, sync::Arc};
|
||||
use util::ResultExt;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GitRepository {
|
||||
// Path to folder containing the .git file or directory
|
||||
content_path: Arc<Path>,
|
||||
// Path to the actual .git folder.
|
||||
// Note: if .git is a file, this points to the folder indicated by the .git file
|
||||
git_dir_path: Arc<Path>,
|
||||
scan_id: usize,
|
||||
libgit_repository: Arc<Mutex<LibGitRepository>>,
|
||||
}
|
||||
|
||||
impl GitRepository {
|
||||
pub fn open(dotgit_path: &Path) -> Option<GitRepository> {
|
||||
LibGitRepository::open(&dotgit_path)
|
||||
.log_err()
|
||||
.and_then(|libgit_repository| {
|
||||
Some(Self {
|
||||
content_path: libgit_repository.workdir()?.into(),
|
||||
git_dir_path: dotgit_path.canonicalize().log_err()?.into(),
|
||||
scan_id: 0,
|
||||
libgit_repository: Arc::new(parking_lot::Mutex::new(libgit_repository)),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn manages(&self, path: &Path) -> bool {
|
||||
path.canonicalize()
|
||||
.map(|path| path.starts_with(&self.content_path))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn in_dot_git(&self, path: &Path) -> bool {
|
||||
path.canonicalize()
|
||||
.map(|path| path.starts_with(&self.git_dir_path))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn content_path(&self) -> &Path {
|
||||
self.content_path.as_ref()
|
||||
}
|
||||
|
||||
pub fn git_dir_path(&self) -> &Path {
|
||||
self.git_dir_path.as_ref()
|
||||
}
|
||||
|
||||
pub fn scan_id(&self) -> usize {
|
||||
self.scan_id
|
||||
}
|
||||
|
||||
pub fn set_scan_id(&mut self, scan_id: usize) {
|
||||
println!("setting scan id");
|
||||
self.scan_id = scan_id;
|
||||
}
|
||||
|
||||
pub async fn load_head_text(&self, relative_file_path: &Path) -> Option<String> {
|
||||
fn logic(repo: &LibGitRepository, relative_file_path: &Path) -> Result<Option<String>> {
|
||||
let object = repo
|
||||
.head()?
|
||||
.peel_to_tree()?
|
||||
.get_path(relative_file_path)?
|
||||
.to_object(&repo)?;
|
||||
|
||||
let content = match object.as_blob() {
|
||||
Some(blob) => blob.content().to_owned(),
|
||||
None => return Ok(None),
|
||||
};
|
||||
|
||||
let head_text = String::from_utf8(content.to_owned())?;
|
||||
Ok(Some(head_text))
|
||||
}
|
||||
|
||||
match logic(&self.libgit_repository.as_ref().lock(), relative_file_path) {
|
||||
Ok(value) => return value,
|
||||
Err(err) => log::error!("Error loading head text: {:?}", err),
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for GitRepository {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("GitRepository")
|
||||
.field("content_path", &self.content_path)
|
||||
.field("git_dir_path", &self.git_dir_path)
|
||||
.field("scan_id", &self.scan_id)
|
||||
.field("libgit_repository", &"LibGitRepository")
|
||||
.finish()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue