Skip to content

Commit 3a5af29

Browse files
committed
Swap Result/Option for discover_git_and_list_files.
1 parent 1232ad3 commit 3a5af29

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

src/cargo/sources/path.rs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ impl<'cfg> PathSource<'cfg> {
160160

161161
// Attempt Git-prepopulate only if no `include` (see rust-lang/cargo#4135).
162162
if no_include_option {
163-
if let Some(result) = self.discover_git_and_list_files(pkg, root, &mut filter) {
164-
return result;
163+
if let Some(result) = self.discover_git_and_list_files(pkg, root, &mut filter)? {
164+
return Ok(result);
165165
}
166166
// no include option and not git repo discovered (see rust-lang/cargo#7183).
167167
return self.list_files_walk_except_dot_files_and_dirs(pkg, &mut filter);
@@ -176,51 +176,44 @@ impl<'cfg> PathSource<'cfg> {
176176
pkg: &Package,
177177
root: &Path,
178178
filter: &mut dyn FnMut(&Path, bool) -> CargoResult<bool>,
179-
) -> Option<CargoResult<Vec<PathBuf>>> {
179+
) -> CargoResult<Option<Vec<PathBuf>>> {
180180
let repo = match git2::Repository::discover(root) {
181181
Ok(repo) => repo,
182-
Err(_) => return None,
183-
};
184-
let index = match repo.index() {
185-
Ok(index) => index,
186-
Err(err) => {
187-
let e = anyhow::Error::new(err).context(format!(
188-
"failed to open git index at {}",
189-
repo.path().display()
190-
));
191-
return Some(Err(e));
192-
}
193-
};
194-
let repo_root = match repo.workdir() {
195-
Some(dir) => dir,
196-
None => {
197-
return Some(Err(anyhow::format_err!(
198-
"did not expect repo at {} to be bare",
199-
repo.path().display()
200-
)))
201-
}
202-
};
203-
let repo_relative_path = match root.strip_prefix(repo_root) {
204-
Ok(path) => path,
205-
Err(err) => {
206-
let e = anyhow::Error::new(err).context(format!(
207-
"expected git repo {} to be parent of package {}",
208-
repo.path().display(),
209-
root.display()
210-
));
211-
return Some(Err(e));
182+
Err(e) => {
183+
log::debug!(
184+
"could not discover git repo at or above {}: {}",
185+
root.display(),
186+
e
187+
);
188+
return Ok(None);
212189
}
213190
};
191+
let index = repo
192+
.index()
193+
.chain_err(|| format!("failed to open git index at {}", repo.path().display()))?;
194+
let repo_root = repo.workdir().ok_or_else(|| {
195+
anyhow::format_err!(
196+
"did not expect repo at {} to be bare",
197+
repo.path().display()
198+
)
199+
})?;
200+
let repo_relative_path = root.strip_prefix(repo_root).chain_err(|| {
201+
format!(
202+
"expected git repo {} to be parent of package {}",
203+
repo.path().display(),
204+
root.display()
205+
)
206+
})?;
214207
// Git requires forward-slashes.
215208
let repo_safe_path = repo_relative_path
216209
.join("Cargo.toml")
217210
.to_string_lossy()
218211
.replace('\\', "/");
219212
if index.get_path(Path::new(&repo_safe_path), 0).is_some() {
220-
return Some(self.list_files_git(pkg, &repo, filter));
213+
return Ok(Some(self.list_files_git(pkg, &repo, filter)?));
221214
}
222215
// Package Cargo.toml is not in git, don't use git to guide our selection.
223-
None
216+
Ok(None)
224217
}
225218

226219
fn list_files_git(

0 commit comments

Comments
 (0)