@@ -4,25 +4,26 @@ use std::path::PathBuf;
4
4
5
5
mod changes_from_objects;
6
6
7
+ const NUM_CHANGES_SINCE_EVER : usize = 3516 ;
8
+
7
9
#[ test]
8
- fn peek_changes ( ) {
9
- let mut index = index_ro ( ) . unwrap ( ) ;
10
+ fn peek_changes ( ) -> crate :: Result {
11
+ let mut index = index_ro ( ) ? ;
10
12
index. branch_name = "main" ;
11
13
assert ! (
12
14
index. last_seen_reference( ) . is_err( ) ,
13
15
"marker ref doesn't exist"
14
16
) ;
15
- let ( changes, last_seen_revision) = index. peek_changes ( ) . unwrap ( ) ;
17
+ let ( changes, last_seen_revision) = index. peek_changes ( ) ? ;
16
18
assert_eq ! (
17
19
changes. len( ) ,
18
- 3516 ,
20
+ NUM_CHANGES_SINCE_EVER ,
19
21
"all changes since the beginning of history"
20
22
) ;
21
23
22
24
let origin_main = index
23
25
. repository ( )
24
- . find_reference ( "refs/remotes/origin/main" )
25
- . unwrap ( ) ;
26
+ . find_reference ( "refs/remotes/origin/main" ) ?;
26
27
assert_eq ! (
27
28
last_seen_revision,
28
29
origin_main. target( ) . expect( "direct ref" ) ,
@@ -32,26 +33,81 @@ fn peek_changes() {
32
33
index. last_seen_reference( ) . is_err( ) ,
33
34
"the last-seen reference has not been created"
34
35
) ;
36
+ Ok ( ( ) )
35
37
}
36
38
37
39
#[ test]
38
40
fn clone_if_needed ( ) {
39
41
let tmp = TempDir :: new ( ) . unwrap ( ) ;
40
- let options = || crates_index_diff:: index:: CloneOptions {
41
- repository_url : fixture_dir ( ) . unwrap ( ) . join ( "base" ) . display ( ) . to_string ( ) ,
42
- fetch_options : None ,
43
- } ;
44
- Index :: from_path_or_cloned_with_options ( tmp. path ( ) , options ( ) )
42
+ Index :: from_path_or_cloned_with_options ( tmp. path ( ) , clone_options ( ) )
45
43
. expect ( "successful clone to be created" ) ;
46
- Index :: from_path_or_cloned_with_options ( tmp. path ( ) , options ( ) )
44
+ Index :: from_path_or_cloned_with_options ( tmp. path ( ) , clone_options ( ) )
47
45
. expect ( "second instance re-uses existing clone" ) ;
48
46
}
49
47
48
+ #[ test]
49
+ fn quick_changes_since_last_fetch ( ) {
50
+ let ( index, _tmp) = index_rw ( ) . unwrap ( ) ;
51
+ assert ! ( index. last_seen_reference( ) . is_err( ) , "no marker exists" ) ;
52
+ let num_changes_since_first_commit = index. fetch_changes ( ) . unwrap ( ) . len ( ) ;
53
+ assert_eq ! (
54
+ num_changes_since_first_commit, NUM_CHANGES_SINCE_EVER ,
55
+ "all changes since ever"
56
+ ) ;
57
+ let mut marker = index
58
+ . last_seen_reference ( )
59
+ . expect ( "must be created/update now" ) ;
60
+ let remote_main = index
61
+ . repository ( )
62
+ . find_reference ( "refs/remotes/origin/main" )
63
+ . unwrap ( ) ;
64
+ assert_eq ! (
65
+ marker. target( ) ,
66
+ remote_main. target( ) ,
67
+ "we are updated to the most recent known version of the remote"
68
+ ) ;
69
+
70
+ // reset to previous one
71
+ marker
72
+ . set_target (
73
+ index
74
+ . repository ( )
75
+ . revparse_single ( & format ! ( "{}~2" , index. seen_ref_name) )
76
+ . unwrap ( )
77
+ . id ( ) ,
78
+ "resetting to previous commit" ,
79
+ )
80
+ . expect ( "reset success" ) ;
81
+ let num_seen_after_reset = index. fetch_changes ( ) . unwrap ( ) . len ( ) ;
82
+ assert_eq ! (
83
+ marker. target( ) ,
84
+ remote_main. target( ) ,
85
+ "seen branch was updated again"
86
+ ) ;
87
+ assert_eq ! (
88
+ num_seen_after_reset, 2357 ,
89
+ "normalization has no changes, but the commit before has one"
90
+ ) ;
91
+
92
+ assert_eq ! (
93
+ index. fetch_changes( ) . unwrap( ) . len( ) ,
94
+ 0 ,
95
+ "nothing if there was no change"
96
+ ) ;
97
+ }
98
+
50
99
fn index_ro ( ) -> crate :: Result < Index > {
51
100
let dir = fixture_dir ( ) ?;
52
101
Ok ( Index :: from_path_or_cloned ( dir. join ( "clone" ) ) ?)
53
102
}
54
103
104
+ fn index_rw ( ) -> crate :: Result < ( Index , TempDir ) > {
105
+ let tmp = TempDir :: new ( ) . unwrap ( ) ;
106
+ let mut index = Index :: from_path_or_cloned_with_options ( tmp. path ( ) , clone_options ( ) ) ?;
107
+ index. branch_name = "main" ;
108
+ Ok ( ( index, tmp) )
109
+ }
110
+
55
111
fn fixture_dir ( ) -> crate :: Result < PathBuf > {
56
112
Ok ( git_testtools:: scripted_fixture_repo_read_only_with_args (
57
113
"make-index-from-parts.sh" ,
@@ -61,4 +117,9 @@ fn fixture_dir() -> crate::Result<PathBuf> {
61
117
) ?)
62
118
}
63
119
64
- mod old;
120
+ fn clone_options ( ) -> crates_index_diff:: index:: CloneOptions < ' static > {
121
+ crates_index_diff:: index:: CloneOptions {
122
+ repository_url : fixture_dir ( ) . unwrap ( ) . join ( "base" ) . display ( ) . to_string ( ) ,
123
+ fetch_options : None ,
124
+ }
125
+ }
0 commit comments