@@ -4,25 +4,26 @@ use std::path::PathBuf;
44
55mod changes_from_objects;
66
7+ const NUM_CHANGES_SINCE_EVER : usize = 3516 ;
8+
79#[ test]
8- fn peek_changes ( ) {
9- let mut index = index_ro ( ) . unwrap ( ) ;
10+ fn peek_changes ( ) -> crate :: Result {
11+ let mut index = index_ro ( ) ? ;
1012 index. branch_name = "main" ;
1113 assert ! (
1214 index. last_seen_reference( ) . is_err( ) ,
1315 "marker ref doesn't exist"
1416 ) ;
15- let ( changes, last_seen_revision) = index. peek_changes ( ) . unwrap ( ) ;
17+ let ( changes, last_seen_revision) = index. peek_changes ( ) ? ;
1618 assert_eq ! (
1719 changes. len( ) ,
18- 3516 ,
20+ NUM_CHANGES_SINCE_EVER ,
1921 "all changes since the beginning of history"
2022 ) ;
2123
2224 let origin_main = index
2325 . repository ( )
24- . find_reference ( "refs/remotes/origin/main" )
25- . unwrap ( ) ;
26+ . find_reference ( "refs/remotes/origin/main" ) ?;
2627 assert_eq ! (
2728 last_seen_revision,
2829 origin_main. target( ) . expect( "direct ref" ) ,
@@ -32,26 +33,81 @@ fn peek_changes() {
3233 index. last_seen_reference( ) . is_err( ) ,
3334 "the last-seen reference has not been created"
3435 ) ;
36+ Ok ( ( ) )
3537}
3638
3739#[ test]
3840fn clone_if_needed ( ) {
3941 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 ( ) )
4543 . 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 ( ) )
4745 . expect ( "second instance re-uses existing clone" ) ;
4846}
4947
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+
5099fn index_ro ( ) -> crate :: Result < Index > {
51100 let dir = fixture_dir ( ) ?;
52101 Ok ( Index :: from_path_or_cloned ( dir. join ( "clone" ) ) ?)
53102}
54103
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+
55111fn fixture_dir ( ) -> crate :: Result < PathBuf > {
56112 Ok ( git_testtools:: scripted_fixture_repo_read_only_with_args (
57113 "make-index-from-parts.sh" ,
@@ -61,4 +117,9 @@ fn fixture_dir() -> crate::Result<PathBuf> {
61117 ) ?)
62118}
63119
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