blob: fe536c9265aeb83ec187a3c56667186bd1814710 [file] [log] [blame]
George Burgess IVec7e53c2021-03-25 19:32:43 -07001Cherry-pick of https://github.com/rust-lang/cargo/pull/8775, so that
2cargo works with git worktrees.
3
4diff --git a/src/tools/cargo/src/cargo/sources/path.rs b/src/tools/cargo/src/cargo/sources/path.rs
5index 64b0f77ed..f7dcfee7b 100644
6--- a/src/tools/cargo/src/cargo/sources/path.rs
7+++ b/src/tools/cargo/src/cargo/sources/path.rs
8@@ -191,12 +191,25 @@ impl<'cfg> PathSource<'cfg> {
9 let index = repo
10 .index()
11 .chain_err(|| format!("failed to open git index at {}", repo.path().display()))?;
12- let repo_root = repo.workdir().ok_or_else(|| {
13- anyhow::format_err!(
14+ let repo_root = if let Some(root) = repo.workdir() {
15+ root
16+ } else if !repo.is_bare() {
17+ // Sparse-checkouts (and possibly other git
18+ // configurations) make libgit2 confused but there's still
19+ // an actual non-bare repo here.
20+ if let Some(r) = repo.path().parent() {
21+ r
22+ } else {
23+ return Err(anyhow::format_err!(
24+ "repo path missing .git subfolder even when non-bare",
25+ ));
26+ }
27+ } else {
28+ return Err(anyhow::format_err!(
29 "did not expect repo at {} to be bare",
30 repo.path().display()
31- )
32- })?;
33+ ));
34+ };
35 let repo_relative_path = match paths::strip_prefix_canonical(root, repo_root) {
36 Ok(p) => p,
37 Err(e) => {
38@@ -225,9 +238,21 @@ impl<'cfg> PathSource<'cfg> {
39 ) -> CargoResult<Vec<PathBuf>> {
40 warn!("list_files_git {}", pkg.package_id());
41 let index = repo.index()?;
42- let root = repo
43- .workdir()
44- .ok_or_else(|| anyhow::format_err!("can't list files on a bare repository"))?;
45+ let root = if let Some(root) = repo.workdir() {
46+ root
47+ } else if !repo.is_bare() {
48+ // Sparse-checkouts (and possibly other git
49+ // configurations) make libgit2 confused but there's still
50+ // an actual non-bare repo here.
51+ if let Some(r) = repo.path().parent() {
52+ r
53+ } else {
54+ return Err(anyhow::format_err!("malformed non-bare repository root",));
55+ }
56+ } else {
57+ return Err(anyhow::format_err!("can't list files on a bare repository",));
58+ };
59+
60 let pkg_path = pkg.root();
61
62 let mut ret = Vec::<PathBuf>::new();