From 232f4feacebb5c9ea7072238ae3e565c2c292b70 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Thu, 7 Mar 2024 14:33:43 +0100 Subject: [PATCH] Merge pull request #9324 from 9999years/fix-8854-take-2 Don't attempt to `git add` ignored files (cherry picked from commit 359990dfdc713c80aabd7ea6f7e4528628fbe108) === also added a regression test that isn't upstream to be sure we're actually fixing the bug. Change-Id: I8267a3d0ece9909d8008b7435b90e7b3eee366f6 --- src/libfetchers/git.cc | 25 +++++++++---- tests/functional/flakes/develop-r8854.sh | 45 ++++++++++++++++++++++++ tests/functional/local.mk | 1 + 3 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 tests/functional/flakes/develop-r8854.sh diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc index 321950e63..1689155b3 100644 --- a/src/libfetchers/git.cc +++ b/src/libfetchers/git.cc @@ -377,15 +377,26 @@ struct GitInputScheme : InputScheme assert(sourcePath); auto gitDir = ".git"; - runProgram("git", true, - { "-C", *sourcePath, "--git-dir", gitDir, "add", "--intent-to-add", "--", std::string(file) }); + auto result = runProgram(RunOptions { + .program = "git", + .args = {"-C", *sourcePath, "--git-dir", gitDir, "check-ignore", "--quiet", std::string(file)}, + }); + auto exitCode = WEXITSTATUS(result.first); - // Pause the logger to allow for user input (such as a gpg passphrase) in `git commit` - logger->pause(); - Finally restoreLogger([]() { logger->resume(); }); - if (commitMsg) + if (exitCode != 0) { + // The path is not `.gitignore`d, we can add the file. runProgram("git", true, - { "-C", *sourcePath, "--git-dir", gitDir, "commit", std::string(file), "-m", *commitMsg }); + { "-C", *sourcePath, "--git-dir", gitDir, "add", "--intent-to-add", "--", std::string(file) }); + + + if (commitMsg) { + // Pause the logger to allow for user input (such as a gpg passphrase) in `git commit` + logger->pause(); + Finally restoreLogger([]() { logger->resume(); }); + runProgram("git", true, + { "-C", *sourcePath, "--git-dir", gitDir, "commit", std::string(file), "-m", *commitMsg }); + } + } } std::pair getActualUrl(const Input & input) const diff --git a/tests/functional/flakes/develop-r8854.sh b/tests/functional/flakes/develop-r8854.sh new file mode 100644 index 000000000..efe129bf4 --- /dev/null +++ b/tests/functional/flakes/develop-r8854.sh @@ -0,0 +1,45 @@ +source ../common.sh + +# regression test for #8854 (nix develop fails when lockfile is ignored) + +clearStore +rm -rf $TEST_HOME/.cache $TEST_HOME/.config $TEST_HOME/.local + +# Create flake under test. +mkdir -p $TEST_HOME/t +cp ../shell-hello.nix ../config.nix $TEST_HOME/t +cat <$TEST_HOME/t/flake.nix +{ + inputs.nixpkgs.url = "$TEST_HOME/nixpkgs"; + outputs = {self, nixpkgs}: { + packages.$system.hello = (import ./config.nix).mkDerivation { + name = "hello"; + outputs = [ "out" "dev" ]; + meta.outputsToInstall = [ "out" ]; + buildCommand = ""; + }; + }; +} +EOF + +# Create fake nixpkgs flake. +mkdir -p $TEST_HOME/nixpkgs +cp ../config.nix ../shell.nix $TEST_HOME/nixpkgs +cat <$TEST_HOME/nixpkgs/flake.nix +{ + outputs = {self}: { + legacyPackages.$system.bashInteractive = (import ./shell.nix {}).bashInteractive; + }; +} +EOF + +cd $TEST_HOME/t + +git init . +echo flake.lock > .gitignore +git add config.nix shell-hello.nix flake.nix .gitignore + +# flake.lock is ignored, but nix develop should still not fail +nix develop .#hello <<<"true" + +clearStore diff --git a/tests/functional/local.mk b/tests/functional/local.mk index 9f50a6dcc..f94f514c9 100644 --- a/tests/functional/local.mk +++ b/tests/functional/local.mk @@ -3,6 +3,7 @@ nix_tests = \ init.sh \ flakes/flakes.sh \ flakes/develop.sh \ + flakes/develop-r8854.sh \ flakes/run.sh \ flakes/mercurial.sh \ flakes/circular.sh \