|
1 | 1 | //! Helpers to gather the VCS information for `cargo package`.
|
2 | 2 |
|
3 | 3 | use std::collections::HashSet;
|
4 |
| -use std::path::Path; |
5 | 4 | use std::path::PathBuf;
|
6 | 5 |
|
7 | 6 | use anyhow::Context as _;
|
@@ -139,8 +138,7 @@ fn git(
|
139 | 138 | .iter()
|
140 | 139 | .filter(|src_file| dirty_files.iter().any(|path| src_file.starts_with(path)))
|
141 | 140 | .map(|p| p.as_ref())
|
142 |
| - .chain(dirty_metadata_paths(pkg, repo)?.iter()) |
143 |
| - .chain(dirty_symlinks(pkg, repo, src_files)?.iter()) |
| 141 | + .chain(dirty_files_outside_pkg_root(pkg, repo, src_files)?.iter()) |
144 | 142 | .map(|path| {
|
145 | 143 | pathdiff::diff_paths(path, cwd)
|
146 | 144 | .as_ref()
|
@@ -173,54 +171,39 @@ fn git(
|
173 | 171 | }
|
174 | 172 | }
|
175 | 173 |
|
176 |
| -/// Checks whether files at paths specified in `package.readme` and |
177 |
| -/// `package.license-file` have been modified. |
| 174 | +/// Checks whether "included" source files outside package root have been modified. |
178 | 175 | ///
|
179 |
| -/// This is required because those paths may link to a file outside the |
180 |
| -/// current package root, but still under the git workdir, affecting the |
181 |
| -/// final packaged `.crate` file. |
182 |
| -fn dirty_metadata_paths(pkg: &Package, repo: &git2::Repository) -> CargoResult<Vec<PathBuf>> { |
183 |
| - let mut dirty_files = Vec::new(); |
184 |
| - let workdir = repo.workdir().unwrap(); |
185 |
| - let root = pkg.root(); |
186 |
| - let meta = pkg.manifest().metadata(); |
187 |
| - for path in [&meta.license_file, &meta.readme] { |
188 |
| - let Some(path) = path.as_deref().map(Path::new) else { |
189 |
| - continue; |
190 |
| - }; |
191 |
| - let abs_path = paths::normalize_path(&root.join(path)); |
192 |
| - if paths::strip_prefix_canonical(&abs_path, root).is_ok() { |
193 |
| - // Inside package root. Don't bother checking git status. |
194 |
| - continue; |
195 |
| - } |
196 |
| - if let Ok(rel_path) = paths::strip_prefix_canonical(&abs_path, workdir) { |
197 |
| - // Outside package root but under git workdir, |
198 |
| - if repo.status_file(&rel_path)? != git2::Status::CURRENT { |
199 |
| - dirty_files.push(workdir.join(rel_path)) |
200 |
| - } |
201 |
| - } |
202 |
| - } |
203 |
| - Ok(dirty_files) |
204 |
| -} |
205 |
| - |
206 |
| -/// Checks whether source files are symlinks and have been modified. |
| 176 | +/// This currently looks at |
| 177 | +/// |
| 178 | +/// * `package.readme` and `package.license-file` pointing to paths outside package root |
| 179 | +/// * symlinks targets reside outside package root |
207 | 180 | ///
|
208 | 181 | /// This is required because those paths may link to a file outside the
|
209 | 182 | /// current package root, but still under the git workdir, affecting the
|
210 | 183 | /// final packaged `.crate` file.
|
211 |
| -fn dirty_symlinks( |
| 184 | +fn dirty_files_outside_pkg_root( |
212 | 185 | pkg: &Package,
|
213 | 186 | repo: &git2::Repository,
|
214 | 187 | src_files: &[PathEntry],
|
215 | 188 | ) -> CargoResult<HashSet<PathBuf>> {
|
| 189 | + let pkg_root = pkg.root(); |
216 | 190 | let workdir = repo.workdir().unwrap();
|
| 191 | + |
| 192 | + let meta = pkg.manifest().metadata(); |
| 193 | + let metadata_paths: Vec<_> = [&meta.license_file, &meta.readme] |
| 194 | + .into_iter() |
| 195 | + .filter_map(|p| p.as_deref()) |
| 196 | + .map(|path| paths::normalize_path(&pkg_root.join(path))) |
| 197 | + .collect(); |
| 198 | + |
217 | 199 | let mut dirty_symlinks = HashSet::new();
|
218 | 200 | for rel_path in src_files
|
219 | 201 | .iter()
|
220 | 202 | .filter(|p| p.is_symlink_or_under_symlink())
|
221 |
| - .map(|p| p.as_ref().as_path()) |
| 203 | + .map(|p| p.as_ref()) |
| 204 | + .chain(metadata_paths.iter()) |
222 | 205 | // If inside package root. Don't bother checking git status.
|
223 |
| - .filter(|p| paths::strip_prefix_canonical(p, pkg.root()).is_err()) |
| 206 | + .filter(|p| paths::strip_prefix_canonical(p, pkg_root).is_err()) |
224 | 207 | // Handle files outside package root but under git workdir,
|
225 | 208 | .filter_map(|p| paths::strip_prefix_canonical(p, workdir).ok())
|
226 | 209 | {
|
|
0 commit comments