Repository navigation
Expand file tree
/
Copy pathdiff-closures.cc
More file actions
157 lines (127 loc) · 4.86 KB
/
Copy pathdiff-closures.cc
File metadata and controls
157 lines (127 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "nix/cmd/command.hh"
#include "nix/main/shared.hh"
#include "nix/store/store-api.hh"
#include "nix/main/common-args.hh"
#include "nix/store/names.hh"
#include <regex>
#include "nix/util/strings.hh"
namespace nix {
namespace {
struct Info
{
std::string outputName;
};
} // namespace
// name -> version -> store paths
typedef std::map<std::string, std::map<std::string, std::map<StorePath, Info>>> GroupedPaths;
GroupedPaths getClosureInfo(ref<Store> store, const StorePath & toplevel)
{
StorePathSet closure;
store->computeFSClosure({toplevel}, closure);
GroupedPaths groupedPaths;
for (auto const & path : closure) {
/* Strip the output name. Unfortunately this is ambiguous (we
can't distinguish between output names like "bin" and
version suffixes like "unstable"). */
static std::regex regex("(.*)-([a-z]+|lib32|lib64)");
std::cmatch match;
std::string name{path.name()};
std::string_view const origName = path.name();
std::string outputName;
if (std::regex_match(origName.begin(), origName.end(), match, regex)) {
name = match[1];
outputName = match[2];
}
DrvName drvName(name);
groupedPaths[drvName.name][drvName.version].emplace(path, Info{.outputName = outputName});
}
return groupedPaths;
}
std::string showVersions(const StringSet & versions)
{
if (versions.empty())
return "(absent)";
StringSet versions2;
for (auto & version : versions)
versions2.insert(version.empty() ? "(no version)" : version);
return concatStringsSep(", ", versions2);
}
void printClosureDiff(
ref<Store> store, const StorePath & beforePath, const StorePath & afterPath, std::string_view indent)
{
auto beforeClosure = getClosureInfo(store, beforePath);
auto afterClosure = getClosureInfo(store, afterPath);
StringSet allNames;
for (auto & [name, _] : beforeClosure)
allNames.insert(name);
for (auto & [name, _] : afterClosure)
allNames.insert(name);
for (auto & name : allNames) {
auto & beforeVersions = beforeClosure[name];
auto & afterVersions = afterClosure[name];
auto totalSize = [&](const std::map<std::string, std::map<StorePath, Info>> & versions) {
uint64_t sum = 0;
for (auto & [_, paths] : versions)
for (auto & [path, _] : paths)
sum += store->queryPathInfo(path)->narSize;
return sum;
};
auto beforeSize = totalSize(beforeVersions);
auto afterSize = totalSize(afterVersions);
auto sizeDelta = (int64_t) afterSize - (int64_t) beforeSize;
auto showDelta = std::abs(sizeDelta) >= 8 * 1024;
StringSet removed, unchanged;
for (auto & [version, _] : beforeVersions)
if (!afterVersions.count(version))
removed.insert(version);
else
unchanged.insert(version);
StringSet added;
for (auto & [version, _] : afterVersions)
if (!beforeVersions.count(version))
added.insert(version);
if (showDelta || !removed.empty() || !added.empty()) {
std::vector<std::string> items;
if (!removed.empty() && !added.empty()) {
items.push_back(fmt("%s → %s", showVersions(removed), showVersions(added)));
} else if (!removed.empty()) {
items.push_back(fmt("%s removed", showVersions(removed)));
} else if (!added.empty()) {
items.push_back(fmt("%s added", showVersions(added)));
}
if (showDelta)
items.push_back(fmt("%s%s" ANSI_NORMAL, sizeDelta > 0 ? ANSI_RED : ANSI_GREEN, renderSize(sizeDelta)));
logger->cout("%s%s: %s", indent, name, concatStringsSep(", ", items));
}
}
}
} // namespace nix
using namespace nix;
struct CmdDiffClosures : SourceExprCommand, MixOperateOnOptions
{
std::string _before, _after;
CmdDiffClosures()
{
expectArg("before", &_before);
expectArg("after", &_after);
}
std::string description() override
{
return "show what packages and versions were added and removed between two closures";
}
std::string doc() override
{
return
#include "diff-closures.md"
;
}
void run(ref<Store> store) override
{
auto before = parseInstallable(store, _before);
auto beforePath = Installable::toStorePath(getEvalStore(), store, Realise::Outputs, operateOn, before);
auto after = parseInstallable(store, _after);
auto afterPath = Installable::toStorePath(getEvalStore(), store, Realise::Outputs, operateOn, after);
printClosureDiff(store, beforePath, afterPath, "");
}
};
static auto rCmdDiffClosures = registerCommand2<CmdDiffClosures>({"store", "diff-closures"});