A simple CPU rendered GUI IDE experience.
Diffstat (limited to 'src/git.rs')
-rw-r--r--src/git.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/git.rs b/src/git.rs
index 8b13789..c47cd2b 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -1 +1,31 @@
+use std::path::Path;
+use git2::Repository;
+use imara_diff::InternedInput;
+use ropey::Rope;
+
+pub fn load(p: &Path, ws: &Path) -> Result<Vec<u8>, git2::Error> {
+ let r = Repository::open(ws).unwrap();
+ let o =
+ r.head()?.peel_to_commit()?.tree()?.get_path(p)?.to_object(&r)?;
+ let blob = o
+ .as_blob()
+ .ok_or(git2::Error::from_str("failure to blob"))?
+ .content()
+ .to_vec();
+ Ok(blob)
+}
+
+pub fn diff(
+ p: &Path,
+ ws: &Path,
+ now: &Rope,
+) -> Result<imara_diff::Diff, anyhow::Error> {
+ let f = String::from_utf8(load(p, ws)?)?;
+ let now = now.to_string();
+ let i = InternedInput::new(&*f, &now);
+ let mut diff =
+ imara_diff::Diff::compute(imara_diff::Algorithm::Myers, &i);
+ diff.postprocess_lines(&i);
+ Ok(diff)
+}