2018-03-30 01:56:13 +03:00
|
|
|
#include "command.hh"
|
|
|
|
#include "store-api.hh"
|
2022-03-22 22:14:58 +02:00
|
|
|
#include "make-content-addressed.hh"
|
2019-12-18 18:39:02 +02:00
|
|
|
#include "common-args.hh"
|
2022-11-16 17:49:49 +02:00
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
2018-03-30 01:56:13 +03:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2022-11-16 17:49:49 +02:00
|
|
|
using nlohmann::json;
|
|
|
|
|
2022-03-23 00:40:04 +02:00
|
|
|
struct CmdMakeContentAddressed : virtual CopyCommand, virtual StorePathsCommand, MixJSON
|
2018-03-30 01:56:13 +03:00
|
|
|
{
|
2022-03-22 22:54:49 +02:00
|
|
|
CmdMakeContentAddressed()
|
2018-03-30 01:56:13 +03:00
|
|
|
{
|
2020-07-15 21:05:42 +03:00
|
|
|
realiseMode = Realise::Outputs;
|
2018-03-30 01:56:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
2020-12-10 18:36:59 +02:00
|
|
|
return "rewrite a path or closure to content-addressed form";
|
2018-03-30 01:56:13 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 18:36:59 +02:00
|
|
|
std::string doc() override
|
2019-10-21 18:58:17 +03:00
|
|
|
{
|
2020-12-10 18:36:59 +02:00
|
|
|
return
|
2022-03-22 22:54:49 +02:00
|
|
|
#include "make-content-addressed.md"
|
2020-12-10 18:36:59 +02:00
|
|
|
;
|
2019-10-21 18:58:17 +03:00
|
|
|
}
|
2020-05-05 16:18:23 +03:00
|
|
|
|
2022-03-23 00:40:04 +02:00
|
|
|
void run(ref<Store> srcStore, StorePaths && storePaths) override
|
2018-03-30 01:56:13 +03:00
|
|
|
{
|
2022-03-23 00:40:04 +02:00
|
|
|
auto dstStore = dstUri.empty() ? openStore() : openStore(dstUri);
|
|
|
|
|
|
|
|
auto remappings = makeContentAddressed(*srcStore, *dstStore,
|
2022-03-22 22:14:58 +02:00
|
|
|
StorePathSet(storePaths.begin(), storePaths.end()));
|
|
|
|
|
|
|
|
if (json) {
|
2022-12-01 17:29:09 +02:00
|
|
|
auto jsonRewrites = json::object();
|
2022-03-22 22:14:58 +02:00
|
|
|
for (auto & path : storePaths) {
|
|
|
|
auto i = remappings.find(path);
|
|
|
|
assert(i != remappings.end());
|
2022-11-16 17:49:49 +02:00
|
|
|
jsonRewrites[srcStore->printStorePath(path)] = srcStore->printStorePath(i->second);
|
2022-03-22 22:14:58 +02:00
|
|
|
}
|
2022-12-01 17:29:09 +02:00
|
|
|
auto json = json::object();
|
|
|
|
json["rewrites"] = jsonRewrites;
|
2023-03-02 16:02:24 +02:00
|
|
|
logger->cout("%s", json);
|
2022-03-22 22:14:58 +02:00
|
|
|
} else {
|
|
|
|
for (auto & path : storePaths) {
|
|
|
|
auto i = remappings.find(path);
|
|
|
|
assert(i != remappings.end());
|
|
|
|
notice("rewrote '%s' to '%s'",
|
2022-03-23 00:40:04 +02:00
|
|
|
srcStore->printStorePath(path),
|
|
|
|
srcStore->printStorePath(i->second));
|
2018-03-30 01:56:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-22 22:54:49 +02:00
|
|
|
static auto rCmdMakeContentAddressed = registerCommand2<CmdMakeContentAddressed>({"store", "make-content-addressed"});
|