Skip to content

Commit d096a86

Browse files
committed
Auto merge of #7448 - ehuss:gitignore-lockfile, r=alexcrichton
Allow gitignore of Cargo.lock with explicit `include`. If a package has an `include` list, but `Cargo.lock` is in `.gitignore`, then Cargo would complain that `Cargo.lock` is "dirty". This changes it so that ignored `Cargo.lock` is allowed, even though it is still packaged. This is under the presumption that `Cargo.lock` is machine generated, so it is not critical. This was also an unexpected regression. If you don't have an `include` list, then there is no complaint about `Cargo.lock` being dirty because Cargo uses git to deduce the file list, and `Cargo.lock` would be skipped for the dirty check (but still included in the package). Closes #7319
2 parents d5621be + 36f01e6 commit d096a86

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,16 @@ fn check_repo_state(
290290
.filter(|file| {
291291
let relative = file.strip_prefix(workdir).unwrap();
292292
if let Ok(status) = repo.status_file(relative) {
293-
status != git2::Status::CURRENT
293+
if status == git2::Status::CURRENT {
294+
false
295+
} else {
296+
if relative.to_str().unwrap_or("") == "Cargo.lock" {
297+
// It is OK to include this file even if it is ignored.
298+
status != git2::Status::IGNORED
299+
} else {
300+
true
301+
}
302+
}
294303
} else {
295304
submodule_dirty(file)
296305
}

tests/testsuite/publish_lockfile.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,58 @@ dependencies = [
396396
)
397397
.run();
398398
}
399+
400+
#[cargo_test]
401+
fn ignore_lockfile() {
402+
// With an explicit `include` list, but Cargo.lock in .gitignore, don't
403+
// complain about `Cargo.lock` being ignored. Note that it is still
404+
// included in the packaged regardless.
405+
let (p, _r) = git::new_repo("foo", |p| {
406+
p.file(
407+
"Cargo.toml",
408+
r#"
409+
[package]
410+
name = "foo"
411+
version = "0.0.1"
412+
authors = []
413+
license = "MIT"
414+
description = "foo"
415+
documentation = "foo"
416+
homepage = "foo"
417+
repository = "foo"
418+
419+
include = [
420+
"src/main.rs"
421+
]
422+
"#,
423+
)
424+
.file("src/main.rs", "fn main() {}")
425+
.file(".gitignore", "Cargo.lock")
426+
});
427+
p.cargo("package -l")
428+
.with_stdout(
429+
"\
430+
.cargo_vcs_info.json
431+
Cargo.lock
432+
Cargo.toml
433+
src/main.rs
434+
",
435+
)
436+
.run();
437+
p.cargo("generate-lockfile").run();
438+
p.cargo("package -v")
439+
.with_stderr(
440+
"\
441+
[PACKAGING] foo v0.0.1 ([..])
442+
[ARCHIVING] Cargo.toml
443+
[ARCHIVING] src/main.rs
444+
[ARCHIVING] .cargo_vcs_info.json
445+
[ARCHIVING] Cargo.lock
446+
[VERIFYING] foo v0.0.1 ([..])
447+
[COMPILING] foo v0.0.1 ([..])
448+
[RUNNING] `rustc --crate-name foo src/main.rs [..]
449+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
450+
",
451+
)
452+
.run();
453+
}

0 commit comments

Comments
 (0)