Skip to content

Commit 50a24ff

Browse files
committed
Check if symlinks are directories
Fixes #2748. Uses @ehuss's suggested fix. See #6817 (comment)
1 parent 77cfcee commit 50a24ff

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/cargo/sources/path.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,17 @@ impl<'cfg> PathSource<'cfg> {
219219
// the untracked files are often part of a build and may become relevant
220220
// as part of a future commit.
221221
let index_files = index.iter().map(|entry| {
222-
use libgit2_sys::GIT_FILEMODE_COMMIT;
223-
let is_dir = entry.mode == GIT_FILEMODE_COMMIT as u32;
224-
(join(root, &entry.path), Some(is_dir))
222+
use libgit2_sys::{GIT_FILEMODE_COMMIT, GIT_FILEMODE_LINK};
223+
// ``is_dir`` is an optimization to avoid calling
224+
// ``fs::metadata`` on every file.
225+
let is_dir = if entry.mode == GIT_FILEMODE_LINK as u32 {
226+
// Let the code below figure out if this symbolic link points
227+
// to a directory or not.
228+
None
229+
} else {
230+
Some(entry.mode == GIT_FILEMODE_COMMIT as u32)
231+
};
232+
(join(root, &entry.path), is_dir)
225233
});
226234
let mut opts = git2::StatusOptions::new();
227235
opts.include_untracked(true);

tests/testsuite/package.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,14 @@ fn package_git_submodule() {
506506

507507
#[cargo_test]
508508
fn package_symlink_to_submodule() {
509+
#[cfg(unix)]
510+
use std::os::unix::fs::symlink as symlink;
511+
#[cfg(windows)]
512+
use std::os::unix::fs::symlink_dir as symlink;
513+
509514
let project = git::new("foo", |project| {
510515
project
511516
.file("src/lib.rs", "pub fn foo() {}")
512-
.symlink("submodule", "submodule-link")
513517
}).unwrap();
514518

515519
let library = git::new("submodule", |library| {
@@ -519,6 +523,8 @@ fn package_symlink_to_submodule() {
519523
let repository = git2::Repository::open(&project.root()).unwrap();
520524
let url = path2url(library.root()).to_string();
521525
git::add_submodule(&repository, &url, Path::new("submodule"));
526+
t!(symlink(&project.root().join("submodule"), &project.root().join("submodule-link")));
527+
git::add(&repository);
522528
git::commit(&repository);
523529

524530
let repository = git2::Repository::open(&project.root().join("submodule")).unwrap();

0 commit comments

Comments
 (0)