Skip to content

Commit

Permalink
libfetchers/git: Allow Git Remote Helpers
Browse files Browse the repository at this point in the history
Remote helpers are programs that are invoked when Git needs to interact
with remote repositories that it does not support natively, see
<https://git-scm.com/docs/gitremote-helpers>

The following two changes make use of such remote helpers possible in
conjunction with Nix:

  1. Relax URL scheme filtering for the Git fetcher, such that unknown
     URL schemes are not rejected directly.
     In case there is no corresponding remote helper available, the
     following output is produced:

         warning: URL scheme 'git+invalid' is non-standard and requires 'git-remote-invalid'.
         git: 'remote-invalid' is not a git command. See 'git --help'.
         error: program 'git' failed with exit code 128

  2. Add `GIT_DIR` to the environment, since this is required by remote
     helpers.
  • Loading branch information
lorenzleutgeb committed Apr 19, 2024
1 parent 8c4c215 commit ee66ec3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/libfetchers/git-utils.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "environment-variables.hh"
#include "git-utils.hh"
#include "fs-input-accessor.hh"
#include "input-accessor.hh"
Expand Down Expand Up @@ -377,18 +378,23 @@ struct GitRepoImpl : GitRepo, std::enable_shared_from_this<GitRepoImpl>
auto dir = this->path;
Strings gitArgs;
if (shallow) {
gitArgs = { "-C", dir.string(), "fetch", "--quiet", "--force", "--depth", "1", "--", url, refspec };
gitArgs = { "fetch", "--quiet", "--force", "--depth", "1", "--", url, refspec };
}
else {
gitArgs = { "-C", dir.string(), "fetch", "--quiet", "--force", "--", url, refspec };
gitArgs = { "fetch", "--quiet", "--force", "--", url, refspec };
}


std::map<std::string, std::string> gitEnv = getEnv();
gitEnv.emplace("GIT_DIR", dir.string());

runProgram(RunOptions {
.program = "git",
.searchPath = true,
// FIXME: git stderr messes up our progress indicator, so
// we're using --quiet for now. Should process its stderr.
.args = gitArgs,
.environment = gitEnv,
.input = {},
.isInteractive = true
});
Expand Down
7 changes: 6 additions & 1 deletion src/libfetchers/unix/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,16 @@ struct GitInputScheme : InputScheme
{
std::optional<Input> inputFromURL(const ParsedURL & url, bool requireTree) const override
{
if (url.scheme != "git" && url.scheme.compare(0, 4, "git+") != 0)
return {};

if (url.scheme != "git" &&
url.scheme != "git+http" &&
url.scheme != "git+https" &&
url.scheme != "git+ssh" &&
url.scheme != "git+file") return {};
url.scheme != "git+file")
warn("URL scheme '%s' is non-standard and requires 'git-remote-%s'.", url.scheme, url.scheme.substr(4, url.scheme.length() - 4));


auto url2(url);
if (hasPrefix(url2.scheme, "git+")) url2.scheme = std::string(url2.scheme, 4);
Expand Down

0 comments on commit ee66ec3

Please sign in to comment.