From 398c4136c2a963a38b8d7664c47f2bd8c60e86c3 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Mon, 31 Mar 2025 21:35:15 -0400 Subject: [PATCH] Improve and fix the error message when a file is not tracked by Git (cherry picked from commit 62e2304891375f642ac7b52358d36455ce99171a) --- src/libfetchers/git.cc | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/libfetchers/git.cc b/src/libfetchers/git.cc index f46334d30..5684583cd 100644 --- a/src/libfetchers/git.cc +++ b/src/libfetchers/git.cc @@ -534,11 +534,21 @@ struct GitInputScheme : InputScheme static MakeNotAllowedError makeNotAllowedError(std::string url) { - return [url{std::move(url)}](const CanonPath & path) -> RestrictedPathError - { - if (nix::pathExists(path.abs())) - return RestrictedPathError("access to path '%s' is forbidden because it is not under Git control; maybe you should 'git add' it to the repository '%s'?", path, url); - else + return [url{std::move(url)}](const CanonPath & path) -> RestrictedPathError { + if (nix::pathExists(url + "/" + path.abs())) { + auto relativePath = path.rel(); // .makeRelative(CanonPath("/")); + + return RestrictedPathError( + "'%s' is not tracked by Git.\n" + "\n" + "To use '%s', stage it in the Git repository at '%s':\n" + "\n" + "git add %s", + relativePath, + relativePath, + url, + relativePath); + } else return RestrictedPathError("path '%s' does not exist in Git repository '%s'", path, url); }; }