parseFlakeRef(): Only search for the top-level directory for CLI flakerefs

This commit is contained in:
Eelco Dolstra 2020-07-17 14:54:21 +02:00
parent 52c8be38e0
commit 17f75f9cc4
2 changed files with 45 additions and 43 deletions

View file

@ -102,56 +102,61 @@ std::pair<FlakeRef, std::string> parseFlakeRefWithFragment(
percentDecode(std::string(match[6]))); percentDecode(std::string(match[6])));
} }
/* Check if 'url' is a path (either absolute or relative to
'baseDir'). If so, search upward to the root of the repo
(i.e. the directory containing .git). */
else if (std::regex_match(url, match, pathUrlRegex)) { else if (std::regex_match(url, match, pathUrlRegex)) {
std::string path = match[1]; std::string path = match[1];
if (!baseDir && !hasPrefix(path, "/")) std::string fragment = percentDecode(std::string(match[3]));
throw BadURL("flake reference '%s' is not an absolute path", url);
path = absPath(path, baseDir, true);
if (!S_ISDIR(lstat(path).st_mode)) if (baseDir) {
throw BadURL("path '%s' is not a flake (because it's not a directory)", path); /* Check if 'url' is a path (either absolute or relative
to 'baseDir'). If so, search upward to the root of the
repo (i.e. the directory containing .git). */
if (!allowMissing && !pathExists(path + "/flake.nix")) path = absPath(path, baseDir, true);
throw BadURL("path '%s' is not a flake (because it doesn't contain a 'flake.nix' file)", path);
auto fragment = percentDecode(std::string(match[3])); if (!S_ISDIR(lstat(path).st_mode))
throw BadURL("path '%s' is not a flake (because it's not a directory)", path);
auto flakeRoot = path; if (!allowMissing && !pathExists(path + "/flake.nix"))
std::string subdir; throw BadURL("path '%s' is not a flake (because it doesn't contain a 'flake.nix' file)", path);
while (flakeRoot != "/") { auto flakeRoot = path;
if (pathExists(flakeRoot + "/.git")) { std::string subdir;
auto base = std::string("git+file://") + flakeRoot;
auto parsedURL = ParsedURL{ while (flakeRoot != "/") {
.url = base, // FIXME if (pathExists(flakeRoot + "/.git")) {
.base = base, auto base = std::string("git+file://") + flakeRoot;
.scheme = "git+file",
.authority = "",
.path = flakeRoot,
.query = decodeQuery(match[2]),
};
if (subdir != "") { auto parsedURL = ParsedURL{
if (parsedURL.query.count("dir")) .url = base, // FIXME
throw Error("flake URL '%s' has an inconsistent 'dir' parameter", url); .base = base,
parsedURL.query.insert_or_assign("dir", subdir); .scheme = "git+file",
.authority = "",
.path = flakeRoot,
.query = decodeQuery(match[2]),
};
if (subdir != "") {
if (parsedURL.query.count("dir"))
throw Error("flake URL '%s' has an inconsistent 'dir' parameter", url);
parsedURL.query.insert_or_assign("dir", subdir);
}
if (pathExists(flakeRoot + "/.git/shallow"))
parsedURL.query.insert_or_assign("shallow", "1");
return std::make_pair(
FlakeRef(Input::fromURL(parsedURL), get(parsedURL.query, "dir").value_or("")),
fragment);
} }
if (pathExists(flakeRoot + "/.git/shallow")) subdir = std::string(baseNameOf(flakeRoot)) + (subdir.empty() ? "" : "/" + subdir);
parsedURL.query.insert_or_assign("shallow", "1"); flakeRoot = dirOf(flakeRoot);
return std::make_pair(
FlakeRef(Input::fromURL(parsedURL), get(parsedURL.query, "dir").value_or("")),
fragment);
} }
subdir = std::string(baseNameOf(flakeRoot)) + (subdir.empty() ? "" : "/" + subdir); } else {
flakeRoot = dirOf(flakeRoot); if (!hasPrefix(path, "/"))
throw BadURL("flake reference '%s' is not an absolute path", url);
path = canonPath(path);
} }
fetchers::Attrs attrs; fetchers::Attrs attrs;

View file

@ -18,7 +18,6 @@ registry=$TEST_ROOT/registry.json
flake1Dir=$TEST_ROOT/flake1 flake1Dir=$TEST_ROOT/flake1
flake2Dir=$TEST_ROOT/flake2 flake2Dir=$TEST_ROOT/flake2
flake3Dir=$TEST_ROOT/flake3 flake3Dir=$TEST_ROOT/flake3
flake4Dir=$TEST_ROOT/flake4
flake5Dir=$TEST_ROOT/flake5 flake5Dir=$TEST_ROOT/flake5
flake6Dir=$TEST_ROOT/flake6 flake6Dir=$TEST_ROOT/flake6
flake7Dir=$TEST_ROOT/flake7 flake7Dir=$TEST_ROOT/flake7
@ -390,14 +389,12 @@ cat > $flake3Dir/flake.nix <<EOF
}; };
} }
EOF EOF
git -C $flake3Dir add flake.nix nix flake update $flake3Dir
git -C $flake3Dir add flake.nix flake.lock
git -C $flake3Dir commit -m 'Remove packages.xyzzy' git -C $flake3Dir commit -m 'Remove packages.xyzzy'
git -C $flake3Dir checkout master git -C $flake3Dir checkout master
# Test whether fuzzy-matching works for IsAlias # Test whether fuzzy-matching works for registry entries.
(! nix build -o $TEST_ROOT/result flake4/removeXyzzy#xyzzy)
# Test whether fuzzy-matching works for IsGit
(! nix build -o $TEST_ROOT/result flake4/removeXyzzy#xyzzy) (! nix build -o $TEST_ROOT/result flake4/removeXyzzy#xyzzy)
nix build -o $TEST_ROOT/result flake4/removeXyzzy#sth nix build -o $TEST_ROOT/result flake4/removeXyzzy#sth