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