2020-03-30 12:03:28 +00:00
|
|
|
|
#include "fetchers.hh"
|
|
|
|
|
#include "cache.hh"
|
2020-01-21 15:27:53 +00:00
|
|
|
|
#include "globals.hh"
|
2024-05-28 11:14:13 +00:00
|
|
|
|
#include "processes.hh"
|
2020-01-21 15:27:53 +00:00
|
|
|
|
#include "tarfile.hh"
|
|
|
|
|
#include "store-api.hh"
|
2020-09-21 16:22:45 +00:00
|
|
|
|
#include "url-parts.hh"
|
2021-08-02 11:39:48 +00:00
|
|
|
|
#include "pathlocks.hh"
|
2024-05-28 13:40:03 +00:00
|
|
|
|
#include "users.hh"
|
2022-05-04 12:32:21 +00:00
|
|
|
|
#include "git.hh"
|
2024-03-04 05:01:09 +00:00
|
|
|
|
#include "logging.hh"
|
|
|
|
|
#include "finally.hh"
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2022-03-01 01:29:34 +00:00
|
|
|
|
#include "fetch-settings.hh"
|
|
|
|
|
|
2021-03-03 01:13:20 +00:00
|
|
|
|
#include <regex>
|
2022-01-29 19:22:55 +00:00
|
|
|
|
#include <string.h>
|
2020-01-21 15:27:53 +00:00
|
|
|
|
#include <sys/time.h>
|
2021-04-19 18:31:58 +00:00
|
|
|
|
#include <sys/wait.h>
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
|
|
using namespace std::string_literals;
|
|
|
|
|
|
|
|
|
|
namespace nix::fetchers {
|
2022-12-07 11:58:58 +00:00
|
|
|
|
|
2022-01-29 19:12:01 +00:00
|
|
|
|
namespace {
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2021-09-24 14:01:27 +00:00
|
|
|
|
// Explicit initial branch of our bare repo to suppress warnings from new version of git.
|
|
|
|
|
// The value itself does not matter, since we always fetch a specific revision or branch.
|
|
|
|
|
// It is set with `-c init.defaultBranch=` instead of `--initial-branch=` to stay compatible with
|
|
|
|
|
// old version of git, which will ignore unrecognized `-c` options.
|
2022-01-29 19:12:01 +00:00
|
|
|
|
const std::string gitInitialBranch = "__nix_dummy_branch";
|
2021-09-24 14:01:27 +00:00
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
|
bool isCacheFileWithinTtl(time_t now, const struct stat & st)
|
2022-05-02 11:37:53 +00:00
|
|
|
|
{
|
2021-03-03 01:13:20 +00:00
|
|
|
|
return st.st_mtime + settings.tarballTtl > now;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
|
bool touchCacheFile(const Path & path, time_t touch_time)
|
2022-01-29 19:22:55 +00:00
|
|
|
|
{
|
2022-12-07 11:58:58 +00:00
|
|
|
|
struct timeval times[2];
|
|
|
|
|
times[0].tv_sec = touch_time;
|
|
|
|
|
times[0].tv_usec = 0;
|
|
|
|
|
times[1].tv_sec = touch_time;
|
|
|
|
|
times[1].tv_usec = 0;
|
2022-01-29 19:22:55 +00:00
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
|
return lutimes(path.c_str(), times) == 0;
|
2022-01-29 19:22:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
|
Path getCachePath(std::string_view key)
|
2022-01-29 19:22:55 +00:00
|
|
|
|
{
|
2021-03-03 01:13:20 +00:00
|
|
|
|
return getCacheDir() + "/nix/gitv3/" +
|
|
|
|
|
hashString(htSHA256, key).to_string(Base32, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the name of the HEAD branch.
|
|
|
|
|
//
|
|
|
|
|
// Returns the head branch name as reported by git ls-remote --symref, e.g., if
|
|
|
|
|
// ls-remote returns the output below, "main" is returned based on the ref line.
|
|
|
|
|
//
|
|
|
|
|
// ref: refs/heads/main HEAD
|
|
|
|
|
// ...
|
2022-01-29 19:12:01 +00:00
|
|
|
|
std::optional<std::string> readHead(const Path & path)
|
2021-03-03 01:13:20 +00:00
|
|
|
|
{
|
2023-05-18 10:18:34 +00:00
|
|
|
|
auto [status, output] = runProgram(RunOptions {
|
2021-03-03 01:13:20 +00:00
|
|
|
|
.program = "git",
|
2022-12-07 11:58:58 +00:00
|
|
|
|
// FIXME: use 'HEAD' to avoid returning all refs
|
2021-03-03 01:13:20 +00:00
|
|
|
|
.args = {"ls-remote", "--symref", path},
|
2023-05-18 10:18:34 +00:00
|
|
|
|
.isInteractive = true,
|
2021-03-03 01:13:20 +00:00
|
|
|
|
});
|
2022-12-07 11:58:58 +00:00
|
|
|
|
if (status != 0) return std::nullopt;
|
2021-03-03 01:13:20 +00:00
|
|
|
|
|
2022-04-29 22:30:00 +00:00
|
|
|
|
std::string_view line = output;
|
|
|
|
|
line = line.substr(0, line.find("\n"));
|
2022-05-04 12:32:21 +00:00
|
|
|
|
if (const auto parseResult = git::parseLsRemoteLine(line)) {
|
|
|
|
|
switch (parseResult->kind) {
|
|
|
|
|
case git::LsRemoteRefLine::Kind::Symbolic:
|
|
|
|
|
debug("resolved HEAD ref '%s' for repo '%s'", parseResult->target, path);
|
|
|
|
|
break;
|
|
|
|
|
case git::LsRemoteRefLine::Kind::Object:
|
|
|
|
|
debug("resolved HEAD rev '%s' for repo '%s'", parseResult->target, path);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return parseResult->target;
|
2021-03-03 01:13:20 +00:00
|
|
|
|
}
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-29 19:22:55 +00:00
|
|
|
|
// Persist the HEAD ref from the remote repo in the local cached repo.
|
2022-12-07 11:58:58 +00:00
|
|
|
|
bool storeCachedHead(const std::string & actualUrl, const std::string & headRef)
|
2022-01-29 19:22:55 +00:00
|
|
|
|
{
|
|
|
|
|
Path cacheDir = getCachePath(actualUrl);
|
|
|
|
|
try {
|
2022-12-07 11:58:58 +00:00
|
|
|
|
runProgram("git", true, { "-C", cacheDir, "--git-dir", ".", "symbolic-ref", "--", "HEAD", headRef });
|
2022-01-29 19:22:55 +00:00
|
|
|
|
} catch (ExecError &e) {
|
|
|
|
|
if (!WIFEXITED(e.status)) throw;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
/* No need to touch refs/HEAD, because `git symbolic-ref` updates the mtime. */
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
|
std::optional<std::string> readHeadCached(const std::string & actualUrl)
|
2020-02-04 20:55:57 +00:00
|
|
|
|
{
|
2021-03-03 01:13:20 +00:00
|
|
|
|
// Create a cache path to store the branch of the HEAD ref. Append something
|
|
|
|
|
// in front of the URL to prevent collision with the repository itself.
|
2022-01-29 19:22:55 +00:00
|
|
|
|
Path cacheDir = getCachePath(actualUrl);
|
|
|
|
|
Path headRefFile = cacheDir + "/HEAD";
|
|
|
|
|
|
2021-03-03 01:13:20 +00:00
|
|
|
|
time_t now = time(0);
|
|
|
|
|
struct stat st;
|
|
|
|
|
std::optional<std::string> cachedRef;
|
2022-01-29 19:22:55 +00:00
|
|
|
|
if (stat(headRefFile.c_str(), &st) == 0) {
|
|
|
|
|
cachedRef = readHead(cacheDir);
|
|
|
|
|
if (cachedRef != std::nullopt &&
|
|
|
|
|
*cachedRef != gitInitialBranch &&
|
2022-12-07 11:58:58 +00:00
|
|
|
|
isCacheFileWithinTtl(now, st))
|
|
|
|
|
{
|
2021-03-03 01:13:20 +00:00
|
|
|
|
debug("using cached HEAD ref '%s' for repo '%s'", *cachedRef, actualUrl);
|
|
|
|
|
return cachedRef;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto ref = readHead(actualUrl);
|
2022-12-07 11:58:58 +00:00
|
|
|
|
if (ref) return ref;
|
2021-03-03 01:13:20 +00:00
|
|
|
|
|
|
|
|
|
if (cachedRef) {
|
|
|
|
|
// If the cached git ref is expired in fetch() below, and the 'git fetch'
|
|
|
|
|
// fails, it falls back to continuing with the most recent version.
|
|
|
|
|
// This function must behave the same way, so we return the expired
|
|
|
|
|
// cached ref here.
|
|
|
|
|
warn("could not get HEAD ref for repository '%s'; using expired cached ref '%s'", actualUrl, *cachedRef);
|
|
|
|
|
return *cachedRef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::nullopt;
|
2020-02-04 20:55:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-29 19:12:01 +00:00
|
|
|
|
bool isNotDotGitDirectory(const Path & path)
|
2020-04-07 11:45:17 +00:00
|
|
|
|
{
|
2022-04-09 15:00:14 +00:00
|
|
|
|
return baseNameOf(path) != ".git";
|
2020-04-07 11:45:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-29 19:12:01 +00:00
|
|
|
|
struct WorkdirInfo
|
|
|
|
|
{
|
|
|
|
|
bool clean = false;
|
|
|
|
|
bool hasHead = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Returns whether a git workdir is clean and has commits.
|
|
|
|
|
WorkdirInfo getWorkdirInfo(const Input & input, const Path & workdir)
|
|
|
|
|
{
|
|
|
|
|
const bool submodules = maybeGetBoolAttr(input.attrs, "submodules").value_or(false);
|
2022-05-24 12:20:48 +00:00
|
|
|
|
std::string gitDir(".git");
|
2022-01-29 19:12:01 +00:00
|
|
|
|
|
|
|
|
|
auto env = getEnv();
|
|
|
|
|
// Set LC_ALL to C: because we rely on the error messages from git rev-parse to determine what went wrong
|
|
|
|
|
// that way unknown errors can lead to a failure instead of continuing through the wrong code path
|
|
|
|
|
env["LC_ALL"] = "C";
|
|
|
|
|
|
|
|
|
|
/* Check whether HEAD points to something that looks like a commit,
|
|
|
|
|
since that is the refrence we want to use later on. */
|
|
|
|
|
auto result = runProgram(RunOptions {
|
|
|
|
|
.program = "git",
|
|
|
|
|
.args = { "-C", workdir, "--git-dir", gitDir, "rev-parse", "--verify", "--no-revs", "HEAD^{commit}" },
|
|
|
|
|
.environment = env,
|
|
|
|
|
.mergeStderrToStdout = true
|
|
|
|
|
});
|
|
|
|
|
auto exitCode = WEXITSTATUS(result.first);
|
|
|
|
|
auto errorMessage = result.second;
|
|
|
|
|
|
|
|
|
|
if (errorMessage.find("fatal: not a git repository") != std::string::npos) {
|
|
|
|
|
throw Error("'%s' is not a Git repository", workdir);
|
|
|
|
|
} else if (errorMessage.find("fatal: Needed a single revision") != std::string::npos) {
|
|
|
|
|
// indicates that the repo does not have any commits
|
|
|
|
|
// we want to proceed and will consider it dirty later
|
|
|
|
|
} else if (exitCode != 0) {
|
|
|
|
|
// any other errors should lead to a failure
|
|
|
|
|
throw Error("getting the HEAD of the Git tree '%s' failed with exit code %d:\n%s", workdir, exitCode, errorMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool clean = false;
|
|
|
|
|
bool hasHead = exitCode == 0;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (hasHead) {
|
|
|
|
|
// Using git diff is preferrable over lower-level operations here,
|
|
|
|
|
// because its conceptually simpler and we only need the exit code anyways.
|
2022-06-10 10:57:13 +00:00
|
|
|
|
auto gitDiffOpts = Strings({ "-C", workdir, "--git-dir", gitDir, "diff", "HEAD", "--quiet"});
|
2022-01-29 19:12:01 +00:00
|
|
|
|
if (!submodules) {
|
|
|
|
|
// Changes in submodules should only make the tree dirty
|
|
|
|
|
// when those submodules will be copied as well.
|
|
|
|
|
gitDiffOpts.emplace_back("--ignore-submodules");
|
|
|
|
|
}
|
|
|
|
|
gitDiffOpts.emplace_back("--");
|
|
|
|
|
runProgram("git", true, gitDiffOpts);
|
|
|
|
|
|
|
|
|
|
clean = true;
|
|
|
|
|
}
|
|
|
|
|
} catch (ExecError & e) {
|
|
|
|
|
if (!WIFEXITED(e.status) || WEXITSTATUS(e.status) != 1) throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return WorkdirInfo { .clean = clean, .hasHead = hasHead };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::pair<StorePath, Input> fetchFromWorkdir(ref<Store> store, Input & input, const Path & workdir, const WorkdirInfo & workdirInfo)
|
|
|
|
|
{
|
|
|
|
|
const bool submodules = maybeGetBoolAttr(input.attrs, "submodules").value_or(false);
|
2022-06-10 10:57:13 +00:00
|
|
|
|
auto gitDir = ".git";
|
2022-01-29 19:12:01 +00:00
|
|
|
|
|
|
|
|
|
if (!fetchSettings.allowDirty)
|
|
|
|
|
throw Error("Git tree '%s' is dirty", workdir);
|
|
|
|
|
|
|
|
|
|
if (fetchSettings.warnDirty)
|
|
|
|
|
warn("Git tree '%s' is dirty", workdir);
|
|
|
|
|
|
2022-06-10 10:57:13 +00:00
|
|
|
|
auto gitOpts = Strings({ "-C", workdir, "--git-dir", gitDir, "ls-files", "-z" });
|
2022-01-29 19:12:01 +00:00
|
|
|
|
if (submodules)
|
|
|
|
|
gitOpts.emplace_back("--recurse-submodules");
|
|
|
|
|
|
|
|
|
|
auto files = tokenizeString<std::set<std::string>>(
|
|
|
|
|
runProgram("git", true, gitOpts), "\0"s);
|
|
|
|
|
|
|
|
|
|
Path actualPath(absPath(workdir));
|
|
|
|
|
|
|
|
|
|
PathFilter filter = [&](const Path & p) -> bool {
|
2024-03-18 02:14:18 +00:00
|
|
|
|
assert(p.starts_with(actualPath));
|
2022-01-29 19:12:01 +00:00
|
|
|
|
std::string file(p, actualPath.size() + 1);
|
|
|
|
|
|
|
|
|
|
auto st = lstat(p);
|
|
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
|
auto prefix = file + "/";
|
|
|
|
|
auto i = files.lower_bound(prefix);
|
2024-06-12 17:42:38 +00:00
|
|
|
|
return (i != files.end() && (*i).starts_with(prefix)) || files.count(file);
|
2022-01-29 19:12:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return files.count(file);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto storePath = store->addToStore(input.getName(), actualPath, FileIngestionMethod::Recursive, htSHA256, filter);
|
|
|
|
|
|
|
|
|
|
// FIXME: maybe we should use the timestamp of the last
|
|
|
|
|
// modified dirty file?
|
|
|
|
|
input.attrs.insert_or_assign(
|
|
|
|
|
"lastModified",
|
2022-06-10 10:57:13 +00:00
|
|
|
|
workdirInfo.hasHead ? std::stoull(runProgram("git", true, { "-C", actualPath, "--git-dir", gitDir, "log", "-1", "--format=%ct", "--no-show-signature", "HEAD" })) : 0);
|
2022-01-29 19:12:01 +00:00
|
|
|
|
|
2021-10-14 12:44:45 +00:00
|
|
|
|
if (workdirInfo.hasHead) {
|
|
|
|
|
input.attrs.insert_or_assign("dirtyRev", chomp(
|
|
|
|
|
runProgram("git", true, { "-C", actualPath, "--git-dir", gitDir, "rev-parse", "--verify", "HEAD" })) + "-dirty");
|
|
|
|
|
input.attrs.insert_or_assign("dirtyShortRev", chomp(
|
|
|
|
|
runProgram("git", true, { "-C", actualPath, "--git-dir", gitDir, "rev-parse", "--verify", "--short", "HEAD" })) + "-dirty");
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-29 19:12:01 +00:00
|
|
|
|
return {std::move(storePath), input};
|
|
|
|
|
}
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
struct GitInputScheme : InputScheme
|
2020-01-21 15:27:53 +00:00
|
|
|
|
{
|
2023-08-01 14:07:20 +00:00
|
|
|
|
std::optional<Input> inputFromURL(const ParsedURL & url, bool requireTree) const override
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
{
|
|
|
|
|
if (url.scheme != "git" &&
|
|
|
|
|
url.scheme != "git+http" &&
|
|
|
|
|
url.scheme != "git+https" &&
|
|
|
|
|
url.scheme != "git+ssh" &&
|
|
|
|
|
url.scheme != "git+file") return {};
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
auto url2(url);
|
2024-03-18 02:14:18 +00:00
|
|
|
|
if (url2.scheme.starts_with("git+")) url2.scheme = std::string(url2.scheme, 4);
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
url2.query.clear();
|
2020-01-31 18:16:40 +00:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
Attrs attrs;
|
|
|
|
|
attrs.emplace("type", "git");
|
|
|
|
|
attrs.emplace("url", url2.to_string());
|
|
|
|
|
|
2024-05-04 10:55:10 +00:00
|
|
|
|
emplaceURLQueryIntoAttrs(
|
|
|
|
|
url,
|
|
|
|
|
attrs,
|
|
|
|
|
{"lastModified", "revCount"},
|
|
|
|
|
{"shallow", "submodules", "allRefs"}
|
|
|
|
|
);
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
return inputFromAttrs(attrs);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
|
std::optional<Input> inputFromAttrs(const Attrs & attrs) const override
|
2020-01-21 15:27:53 +00:00
|
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
if (maybeGetStrAttr(attrs, "type") != "git") return {};
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
for (auto & [name, value] : attrs)
|
2021-10-14 12:44:45 +00:00
|
|
|
|
if (name != "type" && name != "url" && name != "ref" && name != "rev" && name != "shallow" && name != "submodules" && name != "lastModified" && name != "revCount" && name != "narHash" && name != "allRefs" && name != "name" && name != "dirtyRev" && name != "dirtyShortRev")
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
throw Error("unsupported Git input attribute '%s'", name);
|
|
|
|
|
|
|
|
|
|
parseURL(getStrAttr(attrs, "url"));
|
|
|
|
|
maybeGetBoolAttr(attrs, "shallow");
|
|
|
|
|
maybeGetBoolAttr(attrs, "submodules");
|
2020-07-17 18:34:57 +00:00
|
|
|
|
maybeGetBoolAttr(attrs, "allRefs");
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
|
|
|
|
|
if (auto ref = maybeGetStrAttr(attrs, "ref")) {
|
2020-06-03 14:15:22 +00:00
|
|
|
|
if (std::regex_search(*ref, badGitRefRegex))
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
throw BadURL("invalid Git branch/tag name '%s'", *ref);
|
|
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
Input input;
|
|
|
|
|
input.attrs = attrs;
|
|
|
|
|
return input;
|
|
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
|
ParsedURL toURL(const Input & input) const override
|
2020-01-21 15:27:53 +00:00
|
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
auto url = parseURL(getStrAttr(input.attrs, "url"));
|
|
|
|
|
if (url.scheme != "git") url.scheme = "git+" + url.scheme;
|
|
|
|
|
if (auto rev = input.getRev()) url.query.insert_or_assign("rev", rev->gitRev());
|
|
|
|
|
if (auto ref = input.getRef()) url.query.insert_or_assign("ref", *ref);
|
|
|
|
|
if (maybeGetBoolAttr(input.attrs, "shallow").value_or(false))
|
|
|
|
|
url.query.insert_or_assign("shallow", "1");
|
|
|
|
|
return url;
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
|
bool hasAllInfo(const Input & input) const override
|
2020-01-31 18:16:40 +00:00
|
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
bool maybeDirty = !input.getRef();
|
|
|
|
|
bool shallow = maybeGetBoolAttr(input.attrs, "shallow").value_or(false);
|
|
|
|
|
return
|
|
|
|
|
maybeGetIntAttr(input.attrs, "lastModified")
|
|
|
|
|
&& (shallow || maybeDirty || maybeGetIntAttr(input.attrs, "revCount"));
|
2020-01-31 18:16:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
Input applyOverrides(
|
|
|
|
|
const Input & input,
|
|
|
|
|
std::optional<std::string> ref,
|
2022-12-07 11:58:58 +00:00
|
|
|
|
std::optional<Hash> rev) const override
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
{
|
|
|
|
|
auto res(input);
|
|
|
|
|
if (rev) res.attrs.insert_or_assign("rev", rev->gitRev());
|
|
|
|
|
if (ref) res.attrs.insert_or_assign("ref", *ref);
|
|
|
|
|
if (!res.getRef() && res.getRev())
|
|
|
|
|
throw Error("Git input '%s' has a commit hash but no branch/tag name", res.to_string());
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 11:58:58 +00:00
|
|
|
|
void clone(const Input & input, const Path & destDir) const override
|
2020-01-21 15:27:53 +00:00
|
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
auto [isLocal, actualUrl] = getActualUrl(input);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
|
|
Strings args = {"clone"};
|
|
|
|
|
|
|
|
|
|
args.push_back(actualUrl);
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
if (auto ref = input.getRef()) {
|
2020-01-21 15:27:53 +00:00
|
|
|
|
args.push_back("--branch");
|
|
|
|
|
args.push_back(*ref);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 18:13:37 +00:00
|
|
|
|
if (input.getRev()) throw UnimplementedError("cloning a specific revision is not implemented");
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
|
|
args.push_back(destDir);
|
|
|
|
|
|
2023-05-18 10:18:34 +00:00
|
|
|
|
runProgram("git", true, args, {}, true);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 16:18:15 +00:00
|
|
|
|
std::optional<Path> getSourcePath(const Input & input) const override
|
2020-01-21 15:27:53 +00:00
|
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
auto url = parseURL(getStrAttr(input.attrs, "url"));
|
|
|
|
|
if (url.scheme == "file" && !input.getRef() && !input.getRev())
|
2020-01-21 15:27:53 +00:00
|
|
|
|
return url.path;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2020-03-16 12:20:32 +00:00
|
|
|
|
|
2023-10-25 16:18:15 +00:00
|
|
|
|
void putFile(
|
|
|
|
|
const Input & input,
|
|
|
|
|
const CanonPath & path,
|
|
|
|
|
std::string_view contents,
|
|
|
|
|
std::optional<std::string> commitMsg) const override
|
2020-02-02 15:32:46 +00:00
|
|
|
|
{
|
2023-10-25 16:18:15 +00:00
|
|
|
|
auto root = getSourcePath(input);
|
|
|
|
|
if (!root)
|
|
|
|
|
throw Error("cannot commit '%s' to Git repository '%s' because it's not a working tree", path, input.to_string());
|
|
|
|
|
|
|
|
|
|
writeFile((CanonPath(*root) + path).abs(), contents);
|
|
|
|
|
|
2022-05-24 12:20:48 +00:00
|
|
|
|
auto gitDir = ".git";
|
2020-02-05 13:48:49 +00:00
|
|
|
|
|
2024-03-07 13:33:43 +00:00
|
|
|
|
auto result = runProgram(RunOptions {
|
|
|
|
|
.program = "git",
|
2023-10-25 16:18:15 +00:00
|
|
|
|
.args = {"-C", *root, "--git-dir", gitDir, "check-ignore", "--quiet", std::string(path.rel())},
|
2024-03-07 13:33:43 +00:00
|
|
|
|
});
|
|
|
|
|
auto exitCode = WEXITSTATUS(result.first);
|
|
|
|
|
|
|
|
|
|
if (exitCode != 0) {
|
|
|
|
|
// The path is not `.gitignore`d, we can add the file.
|
2020-02-05 13:48:49 +00:00
|
|
|
|
runProgram("git", true,
|
2023-10-25 16:18:15 +00:00
|
|
|
|
{ "-C", *root, "--git-dir", gitDir, "add", "--intent-to-add", "--", std::string(path.rel()) });
|
2024-03-07 13:33:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
2024-05-12 20:42:43 +00:00
|
|
|
|
{ "-C", *root, "--git-dir", gitDir, "commit", std::string(path.rel()), "-F", "-" },
|
|
|
|
|
*commitMsg);
|
2024-03-07 13:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-02 15:32:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
std::pair<bool, std::string> getActualUrl(const Input & input) const
|
2020-01-21 15:27:53 +00:00
|
|
|
|
{
|
2021-02-21 16:08:28 +00:00
|
|
|
|
// file:// URIs are normally not cloned (but otherwise treated the
|
|
|
|
|
// same as remote URIs, i.e. we don't use the working tree or
|
|
|
|
|
// HEAD). Exception: If _NIX_FORCE_HTTP is set, or the repo is a bare git
|
|
|
|
|
// repo, treat as a remote URI to force a clone.
|
2020-01-21 15:27:53 +00:00
|
|
|
|
static bool forceHttp = getEnv("_NIX_FORCE_HTTP") == "1"; // for testing
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
auto url = parseURL(getStrAttr(input.attrs, "url"));
|
2021-02-21 16:08:28 +00:00
|
|
|
|
bool isBareRepository = url.scheme == "file" && !pathExists(url.path + "/.git");
|
|
|
|
|
bool isLocal = url.scheme == "file" && !forceHttp && !isBareRepository;
|
2020-02-02 12:06:00 +00:00
|
|
|
|
return {isLocal, isLocal ? url.path : url.base};
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 13:33:31 +00:00
|
|
|
|
std::pair<StorePath, Input> fetch(ref<Store> store, const Input & _input) override
|
2020-01-21 15:27:53 +00:00
|
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
Input input(_input);
|
2022-05-24 12:20:48 +00:00
|
|
|
|
auto gitDir = ".git";
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2021-07-05 11:09:46 +00:00
|
|
|
|
std::string name = input.getName();
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
bool shallow = maybeGetBoolAttr(input.attrs, "shallow").value_or(false);
|
2021-09-22 15:25:25 +00:00
|
|
|
|
bool submodules = maybeGetBoolAttr(input.attrs, "submodules").value_or(false);
|
2020-07-17 18:34:57 +00:00
|
|
|
|
bool allRefs = maybeGetBoolAttr(input.attrs, "allRefs").value_or(false);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2020-04-07 11:45:17 +00:00
|
|
|
|
std::string cacheType = "git";
|
|
|
|
|
if (shallow) cacheType += "-shallow";
|
|
|
|
|
if (submodules) cacheType += "-submodules";
|
2020-07-17 18:34:57 +00:00
|
|
|
|
if (allRefs) cacheType += "-all-refs";
|
2020-03-17 21:32:26 +00:00
|
|
|
|
|
2022-04-08 17:38:43 +00:00
|
|
|
|
auto checkHashType = [&](const std::optional<Hash> & hash)
|
|
|
|
|
{
|
|
|
|
|
if (hash.has_value() && !(hash->type == htSHA1 || hash->type == htSHA256))
|
|
|
|
|
throw Error("Hash '%s' is not supported by Git. Supported types are sha1 and sha256.", hash->to_string(Base16, true));
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-24 17:09:00 +00:00
|
|
|
|
auto getLockedAttrs = [&]()
|
2020-03-17 21:32:26 +00:00
|
|
|
|
{
|
2022-04-08 17:38:43 +00:00
|
|
|
|
checkHashType(input.getRev());
|
|
|
|
|
|
2020-03-17 21:32:26 +00:00
|
|
|
|
return Attrs({
|
|
|
|
|
{"type", cacheType},
|
|
|
|
|
{"name", name},
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
{"rev", input.getRev()->gitRev()},
|
2020-03-17 21:32:26 +00:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto makeResult = [&](const Attrs & infoAttrs, StorePath && storePath)
|
2022-02-15 13:33:31 +00:00
|
|
|
|
-> std::pair<StorePath, Input>
|
2020-03-17 21:32:26 +00:00
|
|
|
|
{
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
assert(input.getRev());
|
|
|
|
|
assert(!_input.getRev() || _input.getRev() == input.getRev());
|
|
|
|
|
if (!shallow)
|
|
|
|
|
input.attrs.insert_or_assign("revCount", getIntAttr(infoAttrs, "revCount"));
|
|
|
|
|
input.attrs.insert_or_assign("lastModified", getIntAttr(infoAttrs, "lastModified"));
|
2022-02-15 13:33:31 +00:00
|
|
|
|
return {std::move(storePath), input};
|
2020-03-17 21:32:26 +00:00
|
|
|
|
};
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
if (input.getRev()) {
|
2022-02-24 17:09:00 +00:00
|
|
|
|
if (auto res = getCache()->lookup(store, getLockedAttrs()))
|
2020-03-17 21:32:26 +00:00
|
|
|
|
return makeResult(res->first, std::move(res->second));
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
auto [isLocal, actualUrl_] = getActualUrl(input);
|
2020-01-22 20:26:19 +00:00
|
|
|
|
auto actualUrl = actualUrl_; // work around clang bug
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2022-01-29 19:12:01 +00:00
|
|
|
|
/* If this is a local directory and no ref or revision is given,
|
|
|
|
|
allow fetching directly from a dirty workdir. */
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
if (!input.getRef() && !input.getRev() && isLocal) {
|
2022-01-29 19:12:01 +00:00
|
|
|
|
auto workdirInfo = getWorkdirInfo(input, actualUrl);
|
|
|
|
|
if (!workdirInfo.clean) {
|
|
|
|
|
return fetchFromWorkdir(store, input, actualUrl, workdirInfo);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 14:15:28 +00:00
|
|
|
|
Attrs unlockedAttrs({
|
2020-03-17 21:32:26 +00:00
|
|
|
|
{"type", cacheType},
|
|
|
|
|
{"name", name},
|
|
|
|
|
{"url", actualUrl},
|
|
|
|
|
});
|
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
|
Path repoDir;
|
|
|
|
|
|
|
|
|
|
if (isLocal) {
|
2022-01-29 19:22:55 +00:00
|
|
|
|
if (!input.getRef()) {
|
|
|
|
|
auto head = readHead(actualUrl);
|
|
|
|
|
if (!head) {
|
|
|
|
|
warn("could not read HEAD ref from repo at '%s', using 'master'", actualUrl);
|
|
|
|
|
head = "master";
|
|
|
|
|
}
|
|
|
|
|
input.attrs.insert_or_assign("ref", *head);
|
2022-05-27 14:15:28 +00:00
|
|
|
|
unlockedAttrs.insert_or_assign("ref", *head);
|
2022-01-29 19:22:55 +00:00
|
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
if (!input.getRev())
|
|
|
|
|
input.attrs.insert_or_assign("rev",
|
2022-04-24 16:06:36 +00:00
|
|
|
|
Hash::parseAny(chomp(runProgram("git", true, { "-C", actualUrl, "--git-dir", gitDir, "rev-parse", *input.getRef() })), htSHA1).gitRev());
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
|
|
repoDir = actualUrl;
|
|
|
|
|
} else {
|
2022-01-29 19:22:55 +00:00
|
|
|
|
const bool useHeadRef = !input.getRef();
|
|
|
|
|
if (useHeadRef) {
|
|
|
|
|
auto head = readHeadCached(actualUrl);
|
|
|
|
|
if (!head) {
|
|
|
|
|
warn("could not read HEAD ref from repo at '%s', using 'master'", actualUrl);
|
|
|
|
|
head = "master";
|
|
|
|
|
}
|
|
|
|
|
input.attrs.insert_or_assign("ref", *head);
|
2022-05-27 14:15:28 +00:00
|
|
|
|
unlockedAttrs.insert_or_assign("ref", *head);
|
2022-10-14 23:04:47 +00:00
|
|
|
|
} else {
|
|
|
|
|
if (!input.getRev()) {
|
|
|
|
|
unlockedAttrs.insert_or_assign("ref", input.getRef().value());
|
|
|
|
|
}
|
2022-01-29 19:22:55 +00:00
|
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2022-02-24 17:09:00 +00:00
|
|
|
|
if (auto res = getCache()->lookup(store, unlockedAttrs)) {
|
2020-07-01 22:34:18 +00:00
|
|
|
|
auto rev2 = Hash::parseAny(getStrAttr(res->first, "rev"), htSHA1);
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
if (!input.getRev() || input.getRev() == rev2) {
|
|
|
|
|
input.attrs.insert_or_assign("rev", rev2.gitRev());
|
2020-03-17 21:32:26 +00:00
|
|
|
|
return makeResult(res->first, std::move(res->second));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-03 01:13:20 +00:00
|
|
|
|
Path cacheDir = getCachePath(actualUrl);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
repoDir = cacheDir;
|
2022-04-24 16:06:36 +00:00
|
|
|
|
gitDir = ".";
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2021-08-02 11:39:48 +00:00
|
|
|
|
createDirs(dirOf(cacheDir));
|
2021-10-30 10:25:59 +00:00
|
|
|
|
PathLocks cacheDirLock({cacheDir + ".lock"});
|
2021-08-02 11:39:48 +00:00
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
|
if (!pathExists(cacheDir)) {
|
2021-09-24 14:01:27 +00:00
|
|
|
|
runProgram("git", true, { "-c", "init.defaultBranch=" + gitInitialBranch, "init", "--bare", repoDir });
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Path localRefFile =
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
input.getRef()->compare(0, 5, "refs/") == 0
|
|
|
|
|
? cacheDir + "/" + *input.getRef()
|
|
|
|
|
: cacheDir + "/refs/heads/" + *input.getRef();
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
|
|
bool doFetch;
|
|
|
|
|
time_t now = time(0);
|
|
|
|
|
|
|
|
|
|
/* If a rev was specified, we need to fetch if it's not in the
|
|
|
|
|
repo. */
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
if (input.getRev()) {
|
2020-01-21 15:27:53 +00:00
|
|
|
|
try {
|
2022-04-30 13:56:12 +00:00
|
|
|
|
runProgram("git", true, { "-C", repoDir, "--git-dir", gitDir, "cat-file", "-e", input.getRev()->gitRev() });
|
2020-01-21 15:27:53 +00:00
|
|
|
|
doFetch = false;
|
|
|
|
|
} catch (ExecError & e) {
|
|
|
|
|
if (WIFEXITED(e.status)) {
|
|
|
|
|
doFetch = true;
|
|
|
|
|
} else {
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-07-17 18:34:57 +00:00
|
|
|
|
if (allRefs) {
|
|
|
|
|
doFetch = true;
|
|
|
|
|
} else {
|
|
|
|
|
/* If the local ref is older than ‘tarball-ttl’ seconds, do a
|
|
|
|
|
git fetch to update the local ref to the remote ref. */
|
|
|
|
|
struct stat st;
|
|
|
|
|
doFetch = stat(localRefFile.c_str(), &st) != 0 ||
|
2021-03-03 01:13:20 +00:00
|
|
|
|
!isCacheFileWithinTtl(now, st);
|
2020-07-17 18:34:57 +00:00
|
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doFetch) {
|
|
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("fetching Git repository '%s'", actualUrl));
|
|
|
|
|
|
|
|
|
|
// FIXME: git stderr messes up our progress indicator, so
|
|
|
|
|
// we're using --quiet for now. Should process its stderr.
|
|
|
|
|
try {
|
2020-06-03 14:15:22 +00:00
|
|
|
|
auto ref = input.getRef();
|
2020-07-17 18:34:57 +00:00
|
|
|
|
auto fetchRef = allRefs
|
|
|
|
|
? "refs/*"
|
|
|
|
|
: ref->compare(0, 5, "refs/") == 0
|
|
|
|
|
? *ref
|
2021-03-27 13:15:28 +00:00
|
|
|
|
: ref == "HEAD"
|
|
|
|
|
? *ref
|
|
|
|
|
: "refs/heads/" + *ref;
|
2023-05-18 10:26:23 +00:00
|
|
|
|
runProgram("git", true, { "-C", repoDir, "--git-dir", gitDir, "fetch", "--quiet", "--force", "--", actualUrl, fmt("%s:%s", fetchRef, fetchRef) }, {}, true);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
} catch (Error & e) {
|
|
|
|
|
if (!pathExists(localRefFile)) throw;
|
|
|
|
|
warn("could not update local clone of Git repository '%s'; continuing with the most recent version", actualUrl);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-29 19:22:55 +00:00
|
|
|
|
if (!touchCacheFile(localRefFile, now))
|
|
|
|
|
warn("could not update mtime for file '%s': %s", localRefFile, strerror(errno));
|
|
|
|
|
if (useHeadRef && !storeCachedHead(actualUrl, *input.getRef()))
|
|
|
|
|
warn("could not update cached head '%s' for '%s'", *input.getRef(), actualUrl);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
if (!input.getRev())
|
2020-07-16 17:28:52 +00:00
|
|
|
|
input.attrs.insert_or_assign("rev", Hash::parseAny(chomp(readFile(localRefFile)), htSHA1).gitRev());
|
2021-10-30 10:25:59 +00:00
|
|
|
|
|
|
|
|
|
// cache dir lock is removed at scope end; we will only use read-only operations on specific revisions in the remainder
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 16:06:36 +00:00
|
|
|
|
bool isShallow = chomp(runProgram("git", true, { "-C", repoDir, "--git-dir", gitDir, "rev-parse", "--is-shallow-repository" })) == "true";
|
2020-03-16 12:20:32 +00:00
|
|
|
|
|
2020-03-17 20:34:38 +00:00
|
|
|
|
if (isShallow && !shallow)
|
2022-06-26 12:00:00 +00:00
|
|
|
|
throw Error("'%s' is a shallow Git repository, but shallow repositories are only allowed when `shallow = true;` is specified.", actualUrl);
|
2020-03-17 20:34:38 +00:00
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
|
// FIXME: check whether rev is an ancestor of ref.
|
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
printTalkative("using revision %s of repo '%s'", input.getRev()->gitRev(), actualUrl);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2020-03-17 21:32:26 +00:00
|
|
|
|
/* Now that we know the ref, check again whether we have it in
|
|
|
|
|
the store. */
|
2022-02-24 17:09:00 +00:00
|
|
|
|
if (auto res = getCache()->lookup(store, getLockedAttrs()))
|
2020-03-17 21:32:26 +00:00
|
|
|
|
return makeResult(res->first, std::move(res->second));
|
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
|
Path tmpDir = createTempDir();
|
|
|
|
|
AutoDelete delTmpDir(tmpDir, true);
|
2020-04-07 11:45:17 +00:00
|
|
|
|
PathFilter filter = defaultPathFilter;
|
|
|
|
|
|
2021-09-14 06:19:41 +00:00
|
|
|
|
auto result = runProgram(RunOptions {
|
2021-09-13 21:22:09 +00:00
|
|
|
|
.program = "git",
|
2022-04-30 13:56:12 +00:00
|
|
|
|
.args = { "-C", repoDir, "--git-dir", gitDir, "cat-file", "commit", input.getRev()->gitRev() },
|
2021-09-13 21:22:09 +00:00
|
|
|
|
.mergeStderrToStdout = true
|
|
|
|
|
});
|
2020-07-14 18:59:24 +00:00
|
|
|
|
if (WEXITSTATUS(result.first) == 128
|
2021-09-13 21:22:09 +00:00
|
|
|
|
&& result.second.find("bad file") != std::string::npos)
|
|
|
|
|
{
|
2020-07-14 18:59:24 +00:00
|
|
|
|
throw Error(
|
|
|
|
|
"Cannot find Git revision '%s' in ref '%s' of repository '%s'! "
|
2022-12-07 11:58:58 +00:00
|
|
|
|
"Please make sure that the " ANSI_BOLD "rev" ANSI_NORMAL " exists on the "
|
|
|
|
|
ANSI_BOLD "ref" ANSI_NORMAL " you've specified or add " ANSI_BOLD
|
|
|
|
|
"allRefs = true;" ANSI_NORMAL " to " ANSI_BOLD "fetchGit" ANSI_NORMAL ".",
|
2020-07-14 18:59:24 +00:00
|
|
|
|
input.getRev()->gitRev(),
|
|
|
|
|
*input.getRef(),
|
|
|
|
|
actualUrl
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 11:45:17 +00:00
|
|
|
|
if (submodules) {
|
|
|
|
|
Path tmpGitDir = createTempDir();
|
|
|
|
|
AutoDelete delTmpGitDir(tmpGitDir, true);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2021-09-24 14:01:27 +00:00
|
|
|
|
runProgram("git", true, { "-c", "init.defaultBranch=" + gitInitialBranch, "init", tmpDir, "--separate-git-dir", tmpGitDir });
|
2023-02-03 20:42:42 +00:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// TODO: repoDir might lack the ref (it only checks if rev
|
|
|
|
|
// exists, see FIXME above) so use a big hammer and fetch
|
|
|
|
|
// everything to ensure we get the rev.
|
2023-02-07 21:22:50 +00:00
|
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("making temporary clone of '%s'", repoDir));
|
2023-05-18 10:26:23 +00:00
|
|
|
|
runProgram("git", true, { "-C", tmpDir, "fetch", "--quiet", "--force",
|
2023-05-18 10:18:34 +00:00
|
|
|
|
"--update-head-ok", "--", repoDir, "refs/*:refs/*" }, {}, true);
|
2023-02-03 20:42:42 +00:00
|
|
|
|
}
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
runProgram("git", true, { "-C", tmpDir, "checkout", "--quiet", input.getRev()->gitRev() });
|
2023-02-03 20:42:42 +00:00
|
|
|
|
|
2023-02-07 14:35:32 +00:00
|
|
|
|
/* Ensure that we use the correct origin for fetching
|
|
|
|
|
submodules. This matters for submodules with relative
|
|
|
|
|
URLs. */
|
|
|
|
|
if (isLocal) {
|
|
|
|
|
writeFile(tmpGitDir + "/config", readFile(repoDir + "/" + gitDir + "/config"));
|
|
|
|
|
|
|
|
|
|
/* Restore the config.bare setting we may have just
|
2023-02-09 15:42:45 +00:00
|
|
|
|
copied erroneously from the user's repo. */
|
2023-02-07 14:35:32 +00:00
|
|
|
|
runProgram("git", true, { "-C", tmpDir, "config", "core.bare", "false" });
|
|
|
|
|
} else
|
|
|
|
|
runProgram("git", true, { "-C", tmpDir, "config", "remote.origin.url", actualUrl });
|
2023-02-03 20:42:42 +00:00
|
|
|
|
|
2023-02-07 14:50:35 +00:00
|
|
|
|
/* As an optimisation, copy the modules directory of the
|
|
|
|
|
source repo if it exists. */
|
|
|
|
|
auto modulesPath = repoDir + "/" + gitDir + "/modules";
|
|
|
|
|
if (pathExists(modulesPath)) {
|
|
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("copying submodules of '%s'", actualUrl));
|
|
|
|
|
runProgram("cp", true, { "-R", "--", modulesPath, tmpGitDir + "/modules" });
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 20:42:42 +00:00
|
|
|
|
{
|
2023-02-09 15:42:14 +00:00
|
|
|
|
Activity act(*logger, lvlTalkative, actUnknown, fmt("fetching submodules of '%s'", actualUrl));
|
2023-05-18 10:26:23 +00:00
|
|
|
|
runProgram("git", true, { "-C", tmpDir, "submodule", "--quiet", "update", "--init", "--recursive" }, {}, true);
|
2023-02-03 20:42:42 +00:00
|
|
|
|
}
|
2020-04-07 11:45:17 +00:00
|
|
|
|
|
|
|
|
|
filter = isNotDotGitDirectory;
|
|
|
|
|
} else {
|
|
|
|
|
// FIXME: should pipe this, or find some better way to extract a
|
|
|
|
|
// revision.
|
|
|
|
|
auto source = sinkToSource([&](Sink & sink) {
|
2021-09-13 21:22:09 +00:00
|
|
|
|
runProgram2({
|
|
|
|
|
.program = "git",
|
2022-04-30 13:56:12 +00:00
|
|
|
|
.args = { "-C", repoDir, "--git-dir", gitDir, "archive", input.getRev()->gitRev() },
|
2021-09-13 21:22:09 +00:00
|
|
|
|
.standardOut = &sink
|
|
|
|
|
});
|
2020-04-07 11:45:17 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
unpackTarfile(*source, tmpDir);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 15:32:41 +00:00
|
|
|
|
auto storePath = store->addToStore(name, tmpDir, FileIngestionMethod::Recursive, htSHA256, filter);
|
2020-03-16 12:20:32 +00:00
|
|
|
|
|
2022-04-30 13:56:12 +00:00
|
|
|
|
auto lastModified = std::stoull(runProgram("git", true, { "-C", repoDir, "--git-dir", gitDir, "log", "-1", "--format=%ct", "--no-show-signature", input.getRev()->gitRev() }));
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2020-03-17 21:32:26 +00:00
|
|
|
|
Attrs infoAttrs({
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
{"rev", input.getRev()->gitRev()},
|
2020-03-17 21:32:26 +00:00
|
|
|
|
{"lastModified", lastModified},
|
|
|
|
|
});
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
2020-03-17 21:32:26 +00:00
|
|
|
|
if (!shallow)
|
|
|
|
|
infoAttrs.insert_or_assign("revCount",
|
2022-04-30 13:56:12 +00:00
|
|
|
|
std::stoull(runProgram("git", true, { "-C", repoDir, "--git-dir", gitDir, "rev-list", "--count", input.getRev()->gitRev() })));
|
2020-03-17 21:32:26 +00:00
|
|
|
|
|
Remove TreeInfo
The attributes previously stored in TreeInfo (narHash, revCount,
lastModified) are now stored in Input. This makes it less arbitrary
what attributes are stored where.
As a result, the lock file format has changed. An entry like
"info": {
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github"
},
is now stored as
"locked": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "b88ff468e9850410070d4e0ccd68c7011f15b2be",
"type": "github",
"lastModified": 1585405475,
"narHash": "sha256-bESW0n4KgPmZ0luxvwJ+UyATrC6iIltVCsGdLiphVeE="
},
The 'Input' class is now a dumb set of attributes. All the fetcher
implementations subclass InputScheme, not Input. This simplifies the
API.
Also, fix substitution of flake inputs. This was broken since lazy
flake fetching started using fetchTree internally.
2020-05-29 22:44:11 +00:00
|
|
|
|
if (!_input.getRev())
|
2020-03-17 21:32:26 +00:00
|
|
|
|
getCache()->add(
|
|
|
|
|
store,
|
2022-02-24 17:09:00 +00:00
|
|
|
|
unlockedAttrs,
|
2020-03-17 21:32:26 +00:00
|
|
|
|
infoAttrs,
|
|
|
|
|
storePath,
|
|
|
|
|
false);
|
|
|
|
|
|
|
|
|
|
getCache()->add(
|
|
|
|
|
store,
|
2022-02-24 17:09:00 +00:00
|
|
|
|
getLockedAttrs(),
|
2020-03-17 21:32:26 +00:00
|
|
|
|
infoAttrs,
|
|
|
|
|
storePath,
|
|
|
|
|
true);
|
|
|
|
|
|
|
|
|
|
return makeResult(infoAttrs, std::move(storePath));
|
2020-01-21 15:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
|
static auto rGitInputScheme = OnStartup([] { registerInputScheme(std::make_unique<GitInputScheme>()); });
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
|
|
}
|