Skip to content

Commit 05e8e4a

Browse files
committed
[tree-diff] be independent on commit hashes
This should help with addressing commits on windows. Trees and files will probbaly still be an issue, but lets see
1 parent 1bfa9da commit 05e8e4a

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

git-diff/tests/visit/mod.rs

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,10 @@ mod changes {
22
mod to_obtain_tree {
33
use crate::hex_to_id;
44
use git_diff::visit::recorder;
5-
use git_object::tree::EntryMode;
5+
use git_hash::{oid, ObjectId};
6+
use git_object::{bstr::ByteSlice, tree::EntryMode};
67
use git_odb::{linked, pack, Locate};
78

8-
const COMMIT_1: &str = "055df97e18cd537da3cb16bcbdf1733fdcdfb430";
9-
const COMMIT_2: &str = "a5ebf9ee3b1cac5daf3dc9056026ee848be52da2";
10-
const COMMIT_3: &str = "65cd7e777303b4b3a2d41e81303b5c2dd15041fa";
11-
const COMMIT_5: &str = "69bbebb6608472d98be684f4e6ef1faaac2a03bc";
12-
const COMMIT_6: &str = "9bd749db486b2af4a0d4df2de1972db2f198903d";
13-
const COMMIT_9: &str = "ac0a340c76810b53b23e6dc44cf1445ebbd52201";
14-
const COMMIT_11: &str = "76a3f837e9b4aad1840df6be5ca413d696eabc9d";
15-
const COMMIT_13: &str = "05533d594489fae72d4e7422fbdf061c1b70bc22";
16-
const COMMIT_14: &str = "ac7c4c37c3939b820f3ff9003a7ed11d6143dc2b";
17-
const COMMIT_15: &str = "6112ecdac98a18bcbdbd83f0b180b3e1df12e293";
18-
const COMMIT_16: &str = "0ca25edc0c0b38fd6b6a0f6e4797dc08bf0c55c2";
19-
const COMMIT_17: &str = "0b93c2b59feb6c9a4efa1c78a4b4b17fd1c78508";
20-
const COMMIT_18: &str = "53e18fb0d3296990f05382f9c67f8bd256126c4c";
21-
229
fn db() -> crate::Result<linked::Db> {
2310
linked::Db::at(
2411
test_tools::scripted_fixture_repo_read_only("make_diff_repo.sh")?
@@ -28,8 +15,7 @@ mod changes {
2815
.map_err(Into::into)
2916
}
3017

31-
fn diff_with_previous_commit_from(db: &linked::Db, commit_id: &str) -> crate::Result<recorder::Changes> {
32-
let commit_id = git_hash::ObjectId::from_hex(commit_id.as_bytes())?;
18+
fn diff_with_previous_commit_from(db: &linked::Db, commit_id: &oid) -> crate::Result<recorder::Changes> {
3319
let mut buf = Vec::new();
3420
let (main_tree_id, parent_commit_id) = {
3521
let commit = db
@@ -75,11 +61,41 @@ mod changes {
7561
Ok(recorder.records)
7662
}
7763

64+
fn head_of(db: &linked::Db) -> ObjectId {
65+
ObjectId::from_hex(
66+
&std::fs::read(
67+
db.dbs[0]
68+
.loose
69+
.path
70+
.parent()
71+
.unwrap()
72+
.join("refs")
73+
.join("heads")
74+
.join("main"),
75+
)
76+
.expect("head ref")
77+
.as_bstr()
78+
.trim(),
79+
)
80+
.expect("valid hex id")
81+
}
82+
83+
fn all_commits(db: &linked::Db) -> Vec<ObjectId> {
84+
let head = head_of(db);
85+
git_odb::traverse::Ancestors::new(db, Some(head), &mut pack::cache::Never)
86+
.collect::<Vec<_>>()
87+
.into_iter()
88+
.rev()
89+
.collect::<Result<Vec<_>, _>>()
90+
.expect("valid iteration")
91+
}
92+
7893
#[test]
7994
fn many_different_states() -> crate::Result {
8095
let db = db()?;
96+
let all_commits = all_commits(&db);
8197
assert_eq!(
82-
diff_with_previous_commit_from(&db, COMMIT_1)?,
98+
diff_with_previous_commit_from(&db, &all_commits[0])?,
8399
vec![recorder::Change::Addition {
84100
entry_mode: EntryMode::Blob,
85101
oid: hex_to_id("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"),
@@ -88,7 +104,7 @@ mod changes {
88104
, ":000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A f");
89105

90106
assert_eq!(
91-
diff_with_previous_commit_from(&db, COMMIT_2)?,
107+
diff_with_previous_commit_from(&db, &all_commits[1])?,
92108
vec![recorder::Change::Modification {
93109
previous_entry_mode: EntryMode::Blob,
94110
previous_oid: hex_to_id("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"),
@@ -99,7 +115,7 @@ mod changes {
99115
, ":100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 28ce6a8b26aa170e1de65536fe8abe1832bd3242 M f");
100116

101117
assert_eq!(
102-
diff_with_previous_commit_from(&db, COMMIT_3)?,
118+
diff_with_previous_commit_from(&db, &all_commits[2])?,
103119
vec![recorder::Change::Deletion {
104120
entry_mode: EntryMode::Blob,
105121
oid: hex_to_id("28ce6a8b26aa170e1de65536fe8abe1832bd3242"),
@@ -110,7 +126,7 @@ mod changes {
110126
);
111127

112128
assert_eq!(
113-
diff_with_previous_commit_from(&db, COMMIT_5)?,
129+
diff_with_previous_commit_from(&db, &all_commits[4])?,
114130
vec![recorder::Change::Deletion {
115131
entry_mode: EntryMode::Blob,
116132
oid: hex_to_id("28ce6a8b26aa170e1de65536fe8abe1832bd3242"),
@@ -130,7 +146,7 @@ mod changes {
130146
:000000 100644 0000000000000000000000000000000000000000 28ce6a8b26aa170e1de65536fe8abe1832bd3242 A f/f");
131147

132148
assert_eq!(
133-
diff_with_previous_commit_from(&db, COMMIT_6)?,
149+
diff_with_previous_commit_from(&db, &all_commits[5])?,
134150
vec![
135151
recorder::Change::Modification {
136152
previous_entry_mode: EntryMode::Tree,
@@ -151,7 +167,7 @@ mod changes {
151167
);
152168

153169
assert_eq!(
154-
diff_with_previous_commit_from(&db, COMMIT_9)?,
170+
diff_with_previous_commit_from(&db, &all_commits[8])?,
155171
vec![
156172
recorder::Change::Modification {
157173
previous_entry_mode: EntryMode::Tree,
@@ -172,7 +188,7 @@ mod changes {
172188
);
173189

174190
assert_eq!(
175-
diff_with_previous_commit_from(&db, COMMIT_11)?,
191+
diff_with_previous_commit_from(&db, &all_commits[10])?,
176192
vec![
177193
recorder::Change::Deletion {
178194
entry_mode: EntryMode::Tree,
@@ -206,7 +222,7 @@ mod changes {
206222
:120000 000000 2e65efe2a145dda7ee51d1741299f848e5bf752e 0000000000000000000000000000000000000000 D f/f"
207223
);
208224
assert_eq!(
209-
diff_with_previous_commit_from(&db, COMMIT_13)?,
225+
diff_with_previous_commit_from(&db, &all_commits[12])?,
210226
vec![
211227
recorder::Change::Deletion {
212228
entry_mode: EntryMode::Tree,
@@ -222,7 +238,7 @@ mod changes {
222238
":100644 000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 D d/f"
223239
);
224240
assert_eq!(
225-
diff_with_previous_commit_from(&db, COMMIT_14)?,
241+
diff_with_previous_commit_from(&db, &all_commits[13])?,
226242
vec![
227243
recorder::Change::Addition {
228244
entry_mode: EntryMode::Blob,
@@ -245,7 +261,7 @@ mod changes {
245261
:000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A e"
246262
);
247263
assert_eq!(
248-
diff_with_previous_commit_from(&db, COMMIT_15)?,
264+
diff_with_previous_commit_from(&db, &all_commits[14])?,
249265
vec![
250266
recorder::Change::Addition {
251267
entry_mode: EntryMode::Tree,
@@ -261,7 +277,7 @@ mod changes {
261277
":000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A g/a"
262278
);
263279
assert_eq!(
264-
diff_with_previous_commit_from(&db, COMMIT_16)?,
280+
diff_with_previous_commit_from(&db, &all_commits[15])?,
265281
vec![
266282
recorder::Change::Deletion {
267283
entry_mode: EntryMode::Blob,
@@ -284,7 +300,7 @@ mod changes {
284300
:100644 000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 D e"
285301
);
286302
assert_eq!(
287-
diff_with_previous_commit_from(&db, COMMIT_17)?,
303+
diff_with_previous_commit_from(&db, &all_commits[16])?,
288304
vec![
289305
recorder::Change::Deletion {
290306
entry_mode: EntryMode::Blob,
@@ -301,7 +317,7 @@ mod changes {
301317
:000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A ff"
302318
);
303319
assert_eq!(
304-
diff_with_previous_commit_from(&db, COMMIT_18)?,
320+
diff_with_previous_commit_from(&db, &all_commits[17])?,
305321
vec![
306322
recorder::Change::Modification {
307323
previous_entry_mode: EntryMode::Tree,

0 commit comments

Comments
 (0)