Skip to content

Commit 371b512

Browse files
committed
remove git2 verion of the diff algorithm (#16)
1 parent 3749220 commit 371b512

File tree

3 files changed

+4
-180
lines changed

3 files changed

+4
-180
lines changed

src/index/diff/mod.rs

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use crate::{Change, CrateVersion, Index};
1+
use crate::{Change, Index};
22
use git_repository as git;
33
use git_repository::prelude::{FindExt, ObjectIdExt, TreeIterExt};
44
use git_repository::refs::transaction::PreviousValue;
55
use std::convert::TryFrom;
66

7-
static LINE_ADDED_INDICATOR: char = '+';
8-
97
mod delegate;
108
use delegate::Delegate;
119

@@ -90,82 +88,6 @@ impl Index {
9088
from: impl Into<git::hash::ObjectId>,
9189
to: impl Into<git::hash::ObjectId>,
9290
) -> Result<Vec<Change>, Error> {
93-
let repo = git2::Repository::open(self.repo.git_dir())?;
94-
let from = git2::Oid::from_bytes(from.into().as_slice())?;
95-
let to = git2::Oid::from_bytes(to.into().as_slice())?;
96-
fn into_tree<'a>(
97-
repo: &'a git2::Repository,
98-
obj: &git2::Object<'_>,
99-
) -> Result<git2::Tree<'a>, git2::Error> {
100-
repo.find_tree(match obj.kind() {
101-
Some(git2::ObjectType::Commit) => obj
102-
.as_commit()
103-
.expect("object of kind commit yields commit")
104-
.tree_id(),
105-
_ =>
106-
/* let it possibly fail later */
107-
{
108-
obj.id()
109-
}
110-
})
111-
}
112-
let from = repo.find_object(from, None)?;
113-
let to = repo.find_object(to, None)?;
114-
let diff = repo.diff_tree_to_tree(
115-
Some(&into_tree(&repo, &from)?),
116-
Some(&into_tree(&repo, &to)?),
117-
None,
118-
)?;
119-
let mut changes: Vec<Change> = Vec::new();
120-
let mut deletes: Vec<String> = Vec::new();
121-
diff.foreach(
122-
&mut |delta, _| {
123-
if delta.status() == git2::Delta::Deleted {
124-
if let Some(path) = delta.new_file().path() {
125-
if let Some(file_name) = path.file_name() {
126-
deletes.push(file_name.to_string_lossy().to_string());
127-
}
128-
}
129-
}
130-
true
131-
},
132-
None,
133-
None,
134-
Some(&mut |delta, _hunk, diffline| {
135-
if diffline.origin() != LINE_ADDED_INDICATOR {
136-
return true;
137-
}
138-
if !matches!(delta.status(), git2::Delta::Added | git2::Delta::Modified) {
139-
return true;
140-
}
141-
142-
if let Ok(crate_version) =
143-
serde_json::from_slice::<CrateVersion>(diffline.content())
144-
{
145-
if crate_version.yanked {
146-
changes.push(Change::Yanked(crate_version));
147-
} else {
148-
changes.push(Change::Added(crate_version));
149-
}
150-
}
151-
true
152-
}),
153-
)?;
154-
155-
changes.extend(deletes.iter().map(|krate| Change::Deleted {
156-
name: krate.clone(),
157-
}));
158-
Ok(changes)
159-
}
160-
161-
/// Similar to `changes()`, but requires `from` and `to` objects to be provided. They may point
162-
/// to either `Commit`s or `Tree`s.
163-
pub fn changes_between_commits2(
164-
&mut self,
165-
from: impl Into<git::hash::ObjectId>,
166-
to: impl Into<git::hash::ObjectId>,
167-
) -> Result<Vec<Change>, Error> {
168-
self.repo.object_cache_size_if_unset(4 * 1024 * 1024);
16991
let into_tree = |id: git::hash::ObjectId| -> Result<git::Tree<'_>, Error> {
17092
Ok(id
17193
.attach(&self.repo)

src/index/init.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ impl Index {
8686
}
8787
})?;
8888

89+
let mut repo = git::open(repo.path())?.apply_environment();
90+
repo.object_cache_size_if_unset(4 * 1024 * 1024);
8991
Ok(Index {
90-
repo: git::open(repo.path())?,
92+
repo,
9193
branch_name: "master",
9294
seen_ref_name: LAST_SEEN_REFNAME,
9395
})

tests/index/changes_between_commits.rs

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,6 @@ fn addition() -> crate::Result {
1919
Ok(())
2020
}
2121

22-
#[test]
23-
fn addition2() -> crate::Result {
24-
let changes = changes2(index_ro()?, ":/initial commit")?;
25-
assert_eq!(changes.len(), 3228);
26-
assert!(matches!(
27-
changes
28-
.first()
29-
.and_then(|c| c.added().map(|v| v.name.as_str())),
30-
Some("gi-get-artifact")
31-
));
32-
assert!(matches!(
33-
changes.last().expect("present"),
34-
Change::Added(CrateVersion {name, ..}) if name == "gizmo"
35-
));
36-
Ok(())
37-
}
38-
3922
#[test]
4023
fn deletion() -> crate::Result {
4124
let changes = changes(index_ro()?, "@~326")?;
@@ -44,14 +27,6 @@ fn deletion() -> crate::Result {
4427
Ok(())
4528
}
4629

47-
#[test]
48-
fn deletion2() -> crate::Result {
49-
let changes = changes2(index_ro()?, "@~326")?;
50-
assert_eq!(changes.len(), 1);
51-
assert_eq!(changes.first().and_then(|c| c.deleted()), Some("girl"));
52-
Ok(())
53-
}
54-
5530
#[test]
5631
fn new_version() -> crate::Result {
5732
let changes = changes(index_ro()?, ":/Updating crate `git-repository#0.22.1`")?;
@@ -65,19 +40,6 @@ fn new_version() -> crate::Result {
6540
Ok(())
6641
}
6742

68-
#[test]
69-
fn new_version2() -> crate::Result {
70-
let changes = changes2(index_ro()?, ":/Updating crate `git-repository#0.22.1`")?;
71-
assert_eq!(changes.len(), 1);
72-
assert_eq!(
73-
changes
74-
.first()
75-
.and_then(|c| c.added().map(|v| v.name.as_str())),
76-
Some("git-repository")
77-
);
78-
Ok(())
79-
}
80-
8143
#[test]
8244
fn yanked() -> crate::Result {
8345
let changes = changes(index_ro()?, ":/Yanking crate `github_release_rs#0.1.0`")?;
@@ -91,19 +53,6 @@ fn yanked() -> crate::Result {
9153
Ok(())
9254
}
9355

94-
#[test]
95-
fn yanked2() -> crate::Result {
96-
let changes = changes2(index_ro()?, ":/Yanking crate `github_release_rs#0.1.0`")?;
97-
assert_eq!(changes.len(), 1);
98-
assert_eq!(
99-
changes
100-
.first()
101-
.and_then(|c| c.yanked().map(|v| v.name.as_str())),
102-
Some("github_release_rs")
103-
);
104-
Ok(())
105-
}
106-
10756
#[test]
10857
fn unyanked_crates_recognized_as_added() -> crate::Result {
10958
let changes = changes(index_ro()?, ":/Unyanking crate `git2mail#0.3.2`")?;
@@ -117,19 +66,6 @@ fn unyanked_crates_recognized_as_added() -> crate::Result {
11766
Ok(())
11867
}
11968

120-
#[test]
121-
fn unyanked_crates_recognized_as_added2() -> crate::Result {
122-
let changes = changes2(index_ro()?, ":/Unyanking crate `git2mail#0.3.2`")?;
123-
assert_eq!(changes.len(), 1);
124-
assert_eq!(
125-
changes
126-
.first()
127-
.and_then(|c| c.added().map(|v| v.name.as_str())),
128-
Some("git2mail")
129-
);
130-
Ok(())
131-
}
132-
13369
#[test]
13470
fn normalization() -> crate::Result {
13571
let changes = changes(index_ro()?, ":/normalize")?;
@@ -141,17 +77,6 @@ fn normalization() -> crate::Result {
14177
Ok(())
14278
}
14379

144-
#[test]
145-
fn normalization2() -> crate::Result {
146-
let changes = changes2(index_ro()?, ":/normalize")?;
147-
assert_eq!(
148-
changes.len(),
149-
2356, // should be 0
150-
"normalization changes the representation, but the data itself stays the same, BUT we can't do it yet"
151-
);
152-
Ok(())
153-
}
154-
15580
fn changes(mut index: Index, revspec: &str) -> crate::Result<Vec<Change>> {
15681
let (prev, current) = {
15782
let repo = index.repository_mut();
@@ -176,28 +101,3 @@ fn changes(mut index: Index, revspec: &str) -> crate::Result<Vec<Change>> {
176101
};
177102
Ok(index.changes_between_commits(prev, current)?)
178103
}
179-
180-
fn changes2(mut index: Index, revspec: &str) -> crate::Result<Vec<Change>> {
181-
let (prev, current) = {
182-
let repo = index.repository_mut();
183-
repo.object_cache_size_if_unset(4 * 1024 * 1024);
184-
let commit = repo.rev_parse(revspec)?.single().unwrap();
185-
let ancestor_tree = commit
186-
.object()?
187-
.into_commit()
188-
.parent_ids()
189-
.next()
190-
.and_then(|parent| {
191-
parent
192-
.object()
193-
.ok()?
194-
.into_commit()
195-
.tree_id()
196-
.ok()
197-
.map(|id| id.detach())
198-
})
199-
.unwrap_or_else(|| git::hash::ObjectId::empty_tree(repo.object_hash()));
200-
(ancestor_tree, commit.detach())
201-
};
202-
Ok(index.changes_between_commits2(prev, current)?)
203-
}

0 commit comments

Comments
 (0)