Skip to content

Commit 3749220

Browse files
committed
refactor (#16)
1 parent 7cee17e commit 3749220

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed
File renamed without changes.

src/index/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ impl Index {
2929
&self.repo
3030
}
3131

32+
/// Return the crates.io repository, mutably.
33+
pub fn repository_mut(&mut self) -> &mut git::Repository {
34+
&mut self.repo
35+
}
36+
3237
/// Return the reference pointing to the state we have seen after calling `fetch_changes()`.
3338
pub fn last_seen_reference(
3439
&self,

tests/index/changes_between_commits.rs

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::index::index_ro;
22
use crates_index_diff::{Change, CrateVersion, Index};
33
use git_repository as git;
4-
use git_repository::prelude::ObjectIdExt;
54

65
#[test]
76
fn addition() -> crate::Result {
8-
let changes = changes(&index_ro()?, ":/initial commit")?;
7+
let changes = changes(index_ro()?, ":/initial commit")?;
98
assert_eq!(changes.len(), 3228);
109
assert!(matches!(
1110
changes
@@ -39,7 +38,7 @@ fn addition2() -> crate::Result {
3938

4039
#[test]
4140
fn deletion() -> crate::Result {
42-
let changes = changes(&index_ro()?, "@~326")?;
41+
let changes = changes(index_ro()?, "@~326")?;
4342
assert_eq!(changes.len(), 1);
4443
assert_eq!(changes.first().and_then(|c| c.deleted()), Some("girl"));
4544
Ok(())
@@ -55,7 +54,7 @@ fn deletion2() -> crate::Result {
5554

5655
#[test]
5756
fn new_version() -> crate::Result {
58-
let changes = changes(&index_ro()?, ":/Updating crate `git-repository#0.22.1`")?;
57+
let changes = changes(index_ro()?, ":/Updating crate `git-repository#0.22.1`")?;
5958
assert_eq!(changes.len(), 1);
6059
assert_eq!(
6160
changes
@@ -81,7 +80,7 @@ fn new_version2() -> crate::Result {
8180

8281
#[test]
8382
fn yanked() -> crate::Result {
84-
let changes = changes(&index_ro()?, ":/Yanking crate `github_release_rs#0.1.0`")?;
83+
let changes = changes(index_ro()?, ":/Yanking crate `github_release_rs#0.1.0`")?;
8584
assert_eq!(changes.len(), 1);
8685
assert_eq!(
8786
changes
@@ -107,7 +106,7 @@ fn yanked2() -> crate::Result {
107106

108107
#[test]
109108
fn unyanked_crates_recognized_as_added() -> crate::Result {
110-
let changes = changes(&index_ro()?, ":/Unyanking crate `git2mail#0.3.2`")?;
109+
let changes = changes(index_ro()?, ":/Unyanking crate `git2mail#0.3.2`")?;
111110
assert_eq!(changes.len(), 1);
112111
assert_eq!(
113112
changes
@@ -133,7 +132,7 @@ fn unyanked_crates_recognized_as_added2() -> crate::Result {
133132

134133
#[test]
135134
fn normalization() -> crate::Result {
136-
let changes = changes(&index_ro()?, ":/normalize")?;
135+
let changes = changes(index_ro()?, ":/normalize")?;
137136
assert_eq!(
138137
changes.len(),
139138
2356, // should be 0
@@ -153,27 +152,52 @@ fn normalization2() -> crate::Result {
153152
Ok(())
154153
}
155154

156-
fn changes(index: &Index, revspec: &str) -> crate::Result<Vec<Change>> {
157-
let repo = git::open(index.repository().path())?;
158-
let commit = repo.rev_parse(revspec)?.single().unwrap();
159-
let ancestor_tree = commit
160-
.object()?
161-
.into_commit()
162-
.parent_ids()
163-
.next()
164-
.and_then(|parent| parent.object().ok()?.into_commit().tree_id().ok())
165-
.unwrap_or_else(|| git::hash::ObjectId::empty_tree(repo.object_hash()).attach(&repo));
166-
Ok(index.changes_between_commits(ancestor_tree, commit)?)
155+
fn changes(mut index: Index, revspec: &str) -> crate::Result<Vec<Change>> {
156+
let (prev, current) = {
157+
let repo = index.repository_mut();
158+
repo.object_cache_size_if_unset(4 * 1024 * 1024);
159+
let commit = repo.rev_parse(revspec)?.single().unwrap();
160+
let ancestor_tree = commit
161+
.object()?
162+
.into_commit()
163+
.parent_ids()
164+
.next()
165+
.and_then(|parent| {
166+
parent
167+
.object()
168+
.ok()?
169+
.into_commit()
170+
.tree_id()
171+
.ok()
172+
.map(|id| id.detach())
173+
})
174+
.unwrap_or_else(|| git::hash::ObjectId::empty_tree(repo.object_hash()));
175+
(ancestor_tree, commit.detach())
176+
};
177+
Ok(index.changes_between_commits(prev, current)?)
167178
}
179+
168180
fn changes2(mut index: Index, revspec: &str) -> crate::Result<Vec<Change>> {
169-
let repo = git::open(index.repository().path())?;
170-
let commit = repo.rev_parse(revspec)?.single().unwrap();
171-
let ancestor_tree = commit
172-
.object()?
173-
.into_commit()
174-
.parent_ids()
175-
.next()
176-
.and_then(|parent| parent.object().ok()?.into_commit().tree_id().ok())
177-
.unwrap_or_else(|| git::hash::ObjectId::empty_tree(repo.object_hash()).attach(&repo));
178-
Ok(index.changes_between_commits2(ancestor_tree, commit)?)
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)?)
179203
}

0 commit comments

Comments
 (0)