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