Skip to content

Commit 7f46b90

Browse files
authored
Merge pull request #243 from DeterminateSystems/eelcodolstra/fh-699-abbreviate-flakerefs-in-nix-error-messages
Abbreviate flakerefs in error messages
2 parents 2ea3231 + 8f34600 commit 7f46b90

File tree

9 files changed

+40
-35
lines changed

9 files changed

+40
-35
lines changed

src/libfetchers/fetchers.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,17 @@ std::optional<std::string> Input::getFingerprint(ref<Store> store) const
129129
return fingerprint;
130130
}
131131

132-
ParsedURL Input::toURL() const
132+
ParsedURL Input::toURL(bool abbreviate) const
133133
{
134134
if (!scheme)
135135
throw Error("cannot show unsupported input '%s'", attrsToJSON(attrs));
136-
return scheme->toURL(*this);
136+
137+
auto url = scheme->toURL(*this, abbreviate);
138+
139+
if (abbreviate)
140+
url.query.erase("narHash");
141+
142+
return url;
137143
}
138144

139145
std::string Input::toURLString(const StringMap & extraQuery) const
@@ -144,9 +150,9 @@ std::string Input::toURLString(const StringMap & extraQuery) const
144150
return url.to_string();
145151
}
146152

147-
std::string Input::to_string() const
153+
std::string Input::to_string(bool abbreviate) const
148154
{
149-
return toURL().to_string();
155+
return toURL(abbreviate).to_string();
150156
}
151157

152158
bool Input::isDirect() const
@@ -348,7 +354,7 @@ std::pair<ref<SourceAccessor>, Input> Input::getAccessorUnchecked(ref<Store> sto
348354

349355
// FIXME: ideally we would use the `showPath()` of the
350356
// "real" accessor for this fetcher type.
351-
accessor->setPathDisplay("«" + to_string() + "»");
357+
accessor->setPathDisplay("«" + to_string(true) + "»");
352358

353359
return {accessor, *this};
354360
};
@@ -490,7 +496,7 @@ std::optional<time_t> Input::getLastModified() const
490496
return {};
491497
}
492498

493-
ParsedURL InputScheme::toURL(const Input & input) const
499+
ParsedURL InputScheme::toURL(const Input & input, bool abbreviate) const
494500
{
495501
throw Error("don't know how to convert input '%s' to a URL", attrsToJSON(input.attrs));
496502
}

src/libfetchers/git.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,17 @@ struct GitInputScheme : InputScheme
241241
return input;
242242
}
243243

244-
ParsedURL toURL(const Input & input) const override
244+
ParsedURL toURL(const Input & input, bool abbreviate) const override
245245
{
246246
auto url = parseURL(getStrAttr(input.attrs, "url"));
247247
if (url.scheme != "git")
248248
url.scheme = "git+" + url.scheme;
249249
if (auto rev = input.getRev())
250250
url.query.insert_or_assign("rev", rev->gitRev());
251-
if (auto ref = input.getRef())
252-
url.query.insert_or_assign("ref", *ref);
251+
if (auto ref = input.getRef()) {
252+
if (!abbreviate || (*ref != "master" && *ref != "main"))
253+
url.query.insert_or_assign("ref", *ref);
254+
}
253255
if (getShallowAttr(input))
254256
url.query.insert_or_assign("shallow", "1");
255257
if (getLfsAttr(input))
@@ -720,7 +722,7 @@ struct GitInputScheme : InputScheme
720722

721723
bool exportIgnore = getExportIgnoreAttr(input);
722724
bool smudgeLfs = getLfsAttr(input);
723-
auto accessor = repo->getAccessor(rev, exportIgnore, "«" + input.to_string() + "»", smudgeLfs);
725+
auto accessor = repo->getAccessor(rev, exportIgnore, "«" + input.to_string(true) + "»", smudgeLfs);
724726

725727
/* If the repo has submodules, fetch them and return a mounted
726728
input accessor consisting of the accessor for the top-level
@@ -757,7 +759,7 @@ struct GitInputScheme : InputScheme
757759
attrs.insert_or_assign("allRefs", Explicit<bool>{true});
758760
auto submoduleInput = fetchers::Input::fromAttrs(*input.settings, std::move(attrs));
759761
auto [submoduleAccessor, submoduleInput2] = submoduleInput.getAccessor(store);
760-
submoduleAccessor->setPathDisplay("«" + submoduleInput.to_string() + "»");
762+
submoduleAccessor->setPathDisplay("«" + submoduleInput.to_string(true) + "»");
761763
mounts.insert_or_assign(submodule.path, submoduleAccessor);
762764
}
763765

@@ -810,7 +812,7 @@ struct GitInputScheme : InputScheme
810812

811813
auto submoduleInput = fetchers::Input::fromAttrs(*input.settings, std::move(attrs));
812814
auto [submoduleAccessor, submoduleInput2] = submoduleInput.getAccessor(store);
813-
submoduleAccessor->setPathDisplay("«" + submoduleInput.to_string() + "»");
815+
submoduleAccessor->setPathDisplay("«" + submoduleInput.to_string(true) + "»");
814816

815817
/* If the submodule is dirty, mark this repo dirty as
816818
well. */

src/libfetchers/github.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ struct GitArchiveInputScheme : InputScheme
134134
return input;
135135
}
136136

137-
ParsedURL toURL(const Input & input) const override
137+
ParsedURL toURL(const Input & input, bool abbreviate) const override
138138
{
139139
auto owner = getStrAttr(input.attrs, "owner");
140140
auto repo = getStrAttr(input.attrs, "repo");
@@ -145,7 +145,7 @@ struct GitArchiveInputScheme : InputScheme
145145
if (ref)
146146
path.push_back(*ref);
147147
if (rev)
148-
path.push_back(rev->to_string(HashFormat::Base16, false));
148+
path.push_back(abbreviate ? rev->gitShortRev() : rev->gitRev());
149149
auto url = ParsedURL{
150150
.scheme = std::string{schemeName()},
151151
.path = path,
@@ -324,8 +324,8 @@ struct GitArchiveInputScheme : InputScheme
324324
#endif
325325
input.attrs.insert_or_assign("lastModified", uint64_t(tarballInfo.lastModified));
326326

327-
auto accessor =
328-
input.settings->getTarballCache()->getAccessor(tarballInfo.treeHash, false, "«" + input.to_string() + "»");
327+
auto accessor = input.settings->getTarballCache()->getAccessor(
328+
tarballInfo.treeHash, false, "«" + input.to_string(true) + "»");
329329

330330
if (!input.settings->trustTarballsFromGitForges)
331331
// FIXME: computing the NAR hash here is wasteful if
@@ -421,8 +421,7 @@ struct GitHubInputScheme : GitArchiveInputScheme
421421
: headers.empty() ? "https://%s/%s/%s/archive/%s.tar.gz"
422422
: "https://api.%s/repos/%s/%s/tarball/%s";
423423

424-
const auto url =
425-
fmt(urlFmt, host, getOwner(input), getRepo(input), input.getRev()->to_string(HashFormat::Base16, false));
424+
const auto url = fmt(urlFmt, host, getOwner(input), getRepo(input), input.getRev()->gitRev());
426425

427426
return DownloadUrl{parseURL(url), headers};
428427
}
@@ -501,7 +500,7 @@ struct GitLabInputScheme : GitArchiveInputScheme
501500
host,
502501
getStrAttr(input.attrs, "owner"),
503502
getStrAttr(input.attrs, "repo"),
504-
input.getRev()->to_string(HashFormat::Base16, false));
503+
input.getRev()->gitRev());
505504

506505
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
507506
return DownloadUrl{parseURL(url), headers};
@@ -593,7 +592,7 @@ struct SourceHutInputScheme : GitArchiveInputScheme
593592
host,
594593
getStrAttr(input.attrs, "owner"),
595594
getStrAttr(input.attrs, "repo"),
596-
input.getRev()->to_string(HashFormat::Base16, false));
595+
input.getRev()->gitRev());
597596

598597
Headers headers = makeHeadersWithAuthTokens(*input.settings, host, input);
599598
return DownloadUrl{parseURL(url), headers};

src/libfetchers/include/nix/fetchers/fetchers.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ public:
6868
*/
6969
static Input fromAttrs(const Settings & settings, Attrs && attrs);
7070

71-
ParsedURL toURL() const;
71+
ParsedURL toURL(bool abbreviate = false) const;
7272

7373
std::string toURLString(const StringMap & extraQuery = {}) const;
7474

75-
std::string to_string() const;
75+
std::string to_string(bool abbreviate = false) const;
7676

7777
Attrs toAttrs() const;
7878

@@ -219,7 +219,7 @@ struct InputScheme
219219
*/
220220
virtual StringSet allowedAttrs() const = 0;
221221

222-
virtual ParsedURL toURL(const Input & input) const;
222+
virtual ParsedURL toURL(const Input & input, bool abbreviate = false) const;
223223

224224
virtual Input applyOverrides(const Input & input, std::optional<std::string> ref, std::optional<Hash> rev) const;
225225

src/libfetchers/indirect.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct IndirectInputScheme : InputScheme
8181
return input;
8282
}
8383

84-
ParsedURL toURL(const Input & input) const override
84+
ParsedURL toURL(const Input & input, bool abbreviate) const override
8585
{
8686
ParsedURL url{
8787
.scheme = "flake",

src/libfetchers/mercurial.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct MercurialInputScheme : InputScheme
9494
return input;
9595
}
9696

97-
ParsedURL toURL(const Input & input) const override
97+
ParsedURL toURL(const Input & input, bool abbreviate) const override
9898
{
9999
auto url = parseURL(getStrAttr(input.attrs, "url"));
100100
url.scheme = "hg+" + url.scheme;
@@ -222,9 +222,7 @@ struct MercurialInputScheme : InputScheme
222222

223223
auto revInfoKey = [&](const Hash & rev) {
224224
if (rev.algo != HashAlgorithm::SHA1)
225-
throw Error(
226-
"Hash '%s' is not supported by Mercurial. Only sha1 is supported.",
227-
rev.to_string(HashFormat::Base16, true));
225+
throw Error("Hash '%s' is not supported by Mercurial. Only sha1 is supported.", rev.gitRev());
228226

229227
return Cache::Key{"hgRev", {{"store", store->storeDir}, {"name", name}, {"rev", input.getRev()->gitRev()}}};
230228
};
@@ -333,7 +331,7 @@ struct MercurialInputScheme : InputScheme
333331
// We just added it, it should be there.
334332
auto accessor = ref{store->getFSAccessor(storePath)};
335333

336-
accessor->setPathDisplay("«" + input.to_string() + "»");
334+
accessor->setPathDisplay("«" + input.to_string(true) + "»");
337335

338336
return {accessor, input};
339337
}

src/libfetchers/path.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct PathInputScheme : InputScheme
6565
return input;
6666
}
6767

68-
ParsedURL toURL(const Input & input) const override
68+
ParsedURL toURL(const Input & input, bool abbreviate) const override
6969
{
7070
auto query = attrsToQuery(input.attrs);
7171
query.erase("path");

src/libfetchers/tarball.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ struct CurlInputScheme : InputScheme
309309
return input;
310310
}
311311

312-
ParsedURL toURL(const Input & input) const override
312+
ParsedURL toURL(const Input & input, bool abbreviate) const override
313313
{
314314
auto url = parseURL(getStrAttr(input.attrs, "url"));
315315
// NAR hashes are preferred over file hashes since tar/zip
@@ -355,7 +355,7 @@ struct FileInputScheme : CurlInputScheme
355355

356356
auto accessor = ref{store->getFSAccessor(file.storePath)};
357357

358-
accessor->setPathDisplay("«" + input.to_string() + "»");
358+
accessor->setPathDisplay("«" + input.to_string(true) + "»");
359359

360360
return {accessor, input};
361361
}
@@ -382,7 +382,7 @@ struct TarballInputScheme : CurlInputScheme
382382
auto input(_input);
383383

384384
auto result =
385-
downloadTarball_(*input.settings, getStrAttr(input.attrs, "url"), {}, "«" + input.to_string() + "»");
385+
downloadTarball_(*input.settings, getStrAttr(input.attrs, "url"), {}, "«" + input.to_string(true) + "»");
386386

387387
if (result.immutableUrl) {
388388
auto immutableInput = Input::fromURL(*input.settings, *result.immutableUrl);

tests/functional/flakes/source-paths.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ expectStderr 1 nix eval "$repo#y" | grepQuiet "at $repo/flake.nix:"
3030

3131
git -C "$repo" commit -a -m foo
3232

33-
expectStderr 1 nix eval "git+file://$repo?ref=master#y" | grepQuiet "at «git+file://$repo?ref=master&rev=.*»/flake.nix:"
33+
expectStderr 1 nix eval "git+file://$repo?ref=master#y" | grepQuiet "at «git+file://$repo?rev=.*»/flake.nix:"
3434

3535
expectStderr 1 nix eval "$repo#z" | grepQuiet "error: Path 'foo' does not exist in Git repository \"$repo\"."
36-
expectStderr 1 nix eval "git+file://$repo?ref=master#z" | grepQuiet "error: '«git+file://$repo?ref=master&rev=.*»/foo' does not exist"
36+
expectStderr 1 nix eval "git+file://$repo?ref=master#z" | grepQuiet "error: '«git+file://$repo?rev=.*»/foo' does not exist"
3737
expectStderr 1 nix eval "$repo#a" | grepQuiet "error: Path 'foo' does not exist in Git repository \"$repo\"."
3838

3939
echo 123 > "$repo/foo"

0 commit comments

Comments
 (0)