Skip to content

Commit ae3f971

Browse files
committed
first stab at normalization can reduce 245 version, but… (#16)
…that's unexpectedly low. What's the matter here?
1 parent 68ff142 commit ae3f971

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

src/index/diff/delegate.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use crate::{Change, CrateVersion};
33
use git_repository as git;
44
use git_repository::diff::tree::visit::Action;
55
use similar::ChangeTag;
6+
use std::collections::BTreeSet;
67

78
pub(crate) struct Delegate<'repo> {
89
changes: Vec<Change>,
9-
deletes: Vec<CrateVersion>,
10+
delete_version_ids: BTreeSet<u64>,
1011
file_name: git::bstr::BString,
1112
err: Option<Error>,
1213
repo: &'repo git::Repository,
@@ -16,7 +17,7 @@ impl<'repo> Delegate<'repo> {
1617
pub fn from_repo(repo: &'repo git::Repository) -> Self {
1718
Delegate {
1819
changes: Vec::new(),
19-
deletes: Vec::new(),
20+
delete_version_ids: BTreeSet::new(),
2021
err: None,
2122
file_name: Default::default(),
2223
repo,
@@ -74,7 +75,7 @@ impl<'repo> Delegate<'repo> {
7475
Change::Added(version)
7576
});
7677
} else {
77-
self.deletes.push(version);
78+
self.delete_version_ids.insert(version.id());
7879
}
7980
}
8081
ChangeTag::Equal => {}
@@ -85,15 +86,21 @@ impl<'repo> Delegate<'repo> {
8586
}
8687
Ok(())
8788
}
88-
pub fn into_result(self) -> Result<Vec<Change>, Error> {
89-
// assert_eq!(
90-
// self.deletes.len(),
91-
// 0,
92-
// "TODO: handle apparent version deletions"
93-
// );
89+
pub fn into_result(mut self) -> Result<Vec<Change>, Error> {
9490
match self.err {
9591
Some(err) => Err(err),
96-
None => Ok(self.changes),
92+
None => {
93+
if !self.delete_version_ids.is_empty() {
94+
let deleted_version_ids = &self.delete_version_ids;
95+
self.changes.retain(|change| match change {
96+
Change::Added(v) | Change::Yanked(v) => {
97+
!deleted_version_ids.contains(&v.id())
98+
}
99+
Change::Deleted { .. } => true,
100+
})
101+
}
102+
Ok(self.changes)
103+
}
97104
}
98105
}
99106
}

src/types.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::HashMap;
22

33
use git_repository as git;
44
use std::fmt;
5+
use std::hash::{Hash, Hasher};
56

67
/// A wrapper for a repository of the crates.io index.
78
pub struct Index {
@@ -88,8 +89,22 @@ pub struct CrateVersion {
8889
pub dependencies: Vec<Dependency>,
8990
}
9091

92+
impl CrateVersion {
93+
pub(crate) fn id(&self) -> u64 {
94+
let mut s = std::collections::hash_map::DefaultHasher::new();
95+
self.name.hash(&mut s);
96+
self.yanked.hash(&mut s);
97+
self.version.hash(&mut s);
98+
self.checksum.hash(&mut s);
99+
self.dependencies.hash(&mut s);
100+
s.finish()
101+
}
102+
}
103+
91104
/// A single dependency of a specific crate version
92-
#[derive(Clone, serde::Serialize, serde::Deserialize, Ord, PartialOrd, Eq, PartialEq, Debug)]
105+
#[derive(
106+
Clone, serde::Serialize, serde::Deserialize, Ord, PartialOrd, Eq, PartialEq, Debug, Hash,
107+
)]
93108
pub struct Dependency {
94109
/// The crate name
95110
pub name: String,

tests/index/changes_between_commits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn normalization() -> crate::Result {
7171
let changes = changes(index_ro()?, ":/normalize")?;
7272
assert_eq!(
7373
changes.len(),
74-
2356, // should be 0
74+
2111, // should be 0
7575
"normalization changes the representation, but the data itself stays the same, BUT we can't do it yet"
7676
);
7777
Ok(())

tests/index/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn quick_changes_since_last_fetch() -> crate::Result {
8484
"seen branch was updated again"
8585
);
8686
assert_eq!(
87-
num_seen_after_reset, 2357,
87+
num_seen_after_reset, 2112,
8888
"normalization has no changes, but the commit before has one"
8989
);
9090

0 commit comments

Comments
 (0)