@@ -56,13 +56,16 @@ pub(crate) fn hash_files(
56
56
Ok ( hashes)
57
57
}
58
58
59
- pub ( crate ) fn get_package_file_hashes_from_processing_gitignore < S : AsRef < str > > (
59
+ pub ( crate ) fn get_package_file_hashes_without_git < S : AsRef < str > > (
60
60
turbo_root : & AbsoluteSystemPath ,
61
61
package_path : & AnchoredSystemPath ,
62
62
inputs : & [ S ] ,
63
+ include_default_files : bool ,
63
64
) -> Result < GitHashes , Error > {
64
65
let full_package_path = turbo_root. resolve ( package_path) ;
65
66
let mut hashes = GitHashes :: new ( ) ;
67
+ let mut default_file_hashes = GitHashes :: new ( ) ;
68
+ let mut excluded_file_hashes = GitHashes :: new ( ) ;
66
69
67
70
let mut walker_builder = WalkBuilder :: new ( & full_package_path) ;
68
71
let mut includes = Vec :: new ( ) ;
@@ -102,6 +105,7 @@ pub(crate) fn get_package_file_hashes_from_processing_gitignore<S: AsRef<str>>(
102
105
} else {
103
106
Some ( any ( excludes) ?)
104
107
} ;
108
+
105
109
let walker = walker_builder
106
110
. follow_links ( false )
107
111
// if inputs have been provided manually, we shouldn't skip ignored files to mimic the
@@ -110,6 +114,7 @@ pub(crate) fn get_package_file_hashes_from_processing_gitignore<S: AsRef<str>>(
110
114
. require_git ( false )
111
115
. hidden ( false ) // this results in yielding hidden files (e.g. .gitignore)
112
116
. build ( ) ;
117
+
113
118
for dirent in walker {
114
119
let dirent = dirent?;
115
120
let metadata = dirent. metadata ( ) ?;
@@ -118,26 +123,80 @@ pub(crate) fn get_package_file_hashes_from_processing_gitignore<S: AsRef<str>>(
118
123
if metadata. is_dir ( ) {
119
124
continue ;
120
125
}
126
+
121
127
let path = AbsoluteSystemPath :: from_std_path ( dirent. path ( ) ) ?;
122
128
let relative_path = full_package_path. anchor ( path) ?;
123
129
let relative_path = relative_path. to_unix ( ) ;
130
+
131
+ // if we have includes, and this path doesn't match any of them, skip it
124
132
if let Some ( include_pattern) = include_pattern. as_ref ( ) {
125
133
if !include_pattern. is_match ( relative_path. as_str ( ) ) {
126
134
continue ;
127
135
}
128
136
}
137
+
138
+ // if we have excludes, and this path matches one of them, skip it
129
139
if let Some ( exclude_pattern) = exclude_pattern. as_ref ( ) {
130
140
if exclude_pattern. is_match ( relative_path. as_str ( ) ) {
131
141
continue ;
132
142
}
133
143
}
144
+
134
145
// FIXME: we don't hash symlinks...
135
146
if metadata. is_symlink ( ) {
136
147
continue ;
137
148
}
138
149
let hash = git_like_hash_file ( path) ?;
139
150
hashes. insert ( relative_path, hash) ;
140
151
}
152
+
153
+ // If we're including default files, we need to walk again, but this time with
154
+ // git_ignore enabled
155
+ if include_default_files {
156
+ let walker = walker_builder
157
+ . follow_links ( false )
158
+ . git_ignore ( true )
159
+ . require_git ( false )
160
+ . hidden ( false ) // this results in yielding hidden files (e.g. .gitignore)
161
+ . build ( ) ;
162
+
163
+ for dirent in walker {
164
+ let dirent = dirent?;
165
+ let metadata = dirent. metadata ( ) ?;
166
+ // We need to do this here, rather than as a filter, because the root
167
+ // directory is always yielded and not subject to the supplied filter.
168
+ if metadata. is_dir ( ) {
169
+ continue ;
170
+ }
171
+
172
+ let path = AbsoluteSystemPath :: from_std_path ( dirent. path ( ) ) ?;
173
+ let relative_path = full_package_path. anchor ( path) ?;
174
+ let relative_path = relative_path. to_unix ( ) ;
175
+
176
+ if let Some ( exclude_pattern) = exclude_pattern. as_ref ( ) {
177
+ if exclude_pattern. is_match ( relative_path. as_str ( ) ) {
178
+ // track excludes so we can exclude them to the hash map later
179
+ if !metadata. is_symlink ( ) {
180
+ let hash = git_like_hash_file ( path) ?;
181
+ excluded_file_hashes. insert ( relative_path. clone ( ) , hash) ;
182
+ }
183
+ }
184
+ }
185
+
186
+ // FIXME: we don't hash symlinks...
187
+ if metadata. is_symlink ( ) {
188
+ continue ;
189
+ }
190
+ let hash = git_like_hash_file ( path) ?;
191
+ default_file_hashes. insert ( relative_path, hash) ;
192
+ }
193
+ }
194
+
195
+ // merge default with all hashes
196
+ hashes. extend ( default_file_hashes) ;
197
+ // remove excluded files
198
+ hashes. retain ( |key, _| !excluded_file_hashes. contains_key ( key) ) ;
199
+
141
200
Ok ( hashes)
142
201
}
143
202
@@ -353,7 +412,7 @@ mod tests {
353
412
) ;
354
413
355
414
let hashes =
356
- get_package_file_hashes_from_processing_gitignore :: < & str > ( & turbo_root, & pkg_path, & [ ] )
415
+ get_package_file_hashes_without_git :: < & str > ( & turbo_root, & pkg_path, & [ ] , false )
357
416
. unwrap ( ) ;
358
417
assert_eq ! ( hashes, expected) ;
359
418
@@ -384,10 +443,11 @@ mod tests {
384
443
}
385
444
}
386
445
387
- let hashes = get_package_file_hashes_from_processing_gitignore (
446
+ let hashes = get_package_file_hashes_without_git (
388
447
& turbo_root,
389
448
& pkg_path,
390
449
& [ "**/*file" , "!some-dir/excluded-file" ] ,
450
+ false ,
391
451
)
392
452
. unwrap ( ) ;
393
453
0 commit comments