Skip to content

Commit

Permalink
addToStore should always use NAR hash
Browse files Browse the repository at this point in the history
`addToStoreFromDump` would use the CA-specific method.
  • Loading branch information
Ericson2314 committed Jun 19, 2020
1 parent 3ab213b commit 4a73bd0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
5 changes: 1 addition & 4 deletions src/libstore/local-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1005,10 +1005,7 @@ void LocalStore::addToStore(const ValidPathInfo & info, Source & source,
return n;
});

if (hasPrefix(info.ca, "fixed:git:"))
restoreGit(realPath, wrapperSource, realStoreDir, storeDir);
else
restorePath(realPath, wrapperSource);
restorePath(realPath, wrapperSource);

auto hashResult = hashSink->finish();

Expand Down
29 changes: 20 additions & 9 deletions src/nix/add-to-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct CmdAddToStore : MixDryRun, StoreCommand
{
Path path;
std::optional<std::string> namePart;
bool git = false;
FileIngestionMethod ingestionMethod = FileIngestionMethod::Recursive;

CmdAddToStore()
{
Expand All @@ -26,8 +26,9 @@ struct CmdAddToStore : MixDryRun, StoreCommand

addFlag({
.longName = "git",
.shortName = 0,
.description = "treat path as a git object",
.handler = {&this->git, true},
.handler = {&ingestionMethod, FileIngestionMethod::Git},
});
}

Expand All @@ -48,24 +49,34 @@ struct CmdAddToStore : MixDryRun, StoreCommand
{
if (!namePart) namePart = baseNameOf(path);

auto ingestionMethod = git ? FileIngestionMethod::Git : FileIngestionMethod::Recursive;

StringSink sink;
dumpPath(path, sink);

auto narHash = hashString(htSHA256, *sink.s);
auto hash = git ? dumpGitHash(htSHA1, path) : narHash;

Hash hash;
switch (ingestionMethod) {
case FileIngestionMethod::Recursive: {
hash = narHash;
break;
}
case FileIngestionMethod::Flat: {
abort(); // not yet supported above
}
case FileIngestionMethod::Git: {
hash = dumpGitHash(htSHA1, path);
break;
}
}

ValidPathInfo info(store->makeFixedOutputPath(ingestionMethod, hash, *namePart));
info.narHash = narHash;
info.narSize = sink.s->size();
info.ca = makeFixedOutputCA(ingestionMethod, hash);

if (!dryRun) {
auto addedPath = store->addToStore(*namePart, path, ingestionMethod, git ? htSHA1 : htSHA256);
if (addedPath != info.path)
throw Error("Added path %s does not match calculated path %s; something has changed",
addedPath.to_string(), info.path.to_string());
auto source = StringSource { *sink.s };
store->addToStore(info, source);
}

logger->stdout("%s", store->printStorePath(info.path));
Expand Down

0 comments on commit 4a73bd0

Please sign in to comment.