-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyaml-merge.cpp
328 lines (283 loc) · 6.81 KB
/
yaml-merge.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
This file is part of git-merge-drivers.
git-merge-drivers is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
git-merge-drivers is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this git-merge-drivers. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <fstream>
#include <cassert>
#include <yaml.h>
using namespace std;
void
replace_substring(
string str,
const string& pattern,
const string& replace)
{
assert(not pattern.empty());
for (
size_t pos = str.find(pattern);
pos != string::npos;
str.replace(pos, pattern.size(), replace),
pos = str.find(pattern, pos + replace.size())
);
}
string
escape_string_path(
string str)
{
replace_substring(str, "~", "~0");
replace_substring(str, "/", "~1");
return str;
}
bool
compare_nodes(
const YAML::Node& node1,
const YAML::Node& node2)
{
if (node1.is(node2))
return true;
if (node1.Type() != node2.Type())
return false;
switch(node1.Type())
{
case YAML::NodeType::Map:
case YAML::NodeType::Sequence:
case YAML::NodeType::Undefined:
return false;
case YAML::NodeType::Null:
return true;
case YAML::NodeType::Scalar:
return node1.as<string>().compare(node2.as<string>()) == 0;
}
}
YAML::Node
diff(
const YAML::Node& source,
const YAML::Node& target,
const string& path = "")
{
YAML::Node result = YAML::Load("[]");
if (compare_nodes(source, target))
{
return result;
}
else if (source.Type() == YAML::NodeType::Map && target.Type() == YAML::NodeType::Map)
{
// First, look at existing key in target object.
// - if key found, recursively call diff function.
// - if key NOT found, add remove instruction in result array.
for (YAML::const_iterator it = source.begin(); it != source.end(); it++)
{
YAML::Node value = it->second;
string key = it->first.as<string>();
string esc_key = escape_string_path(key);
// Key not found in target .. remove it
if (!target[key])
{
YAML::Node item;
item["op"] = "remove";
item["path"] = path + "/" + esc_key;
result.push_back(item);
}
else
{
YAML::Node rec_result = diff(value, target[key], path + "/" + esc_key);
for (YAML::const_iterator rec_it = rec_result.begin(); rec_it != rec_result.end(); rec_it++)
{
result.push_back(*rec_it);
}
}
}
// Walk target object to add new keys in result array
for (YAML::const_iterator it = target.begin(); it != target.end(); it++)
{
YAML::Node value = it->second;
string key = it->first.as<string>();
string esc_key = escape_string_path(key);
if (!source[key])
{
YAML::Node item;
item["op"] = "add";
item["path"] = path + "/" + esc_key;
item["value"] = value;
result.push_back(item);
}
}
}
else if (source.Type() == YAML::NodeType::Sequence && target.Type() == YAML::NodeType::Sequence)
{
size_t i = 0;
while (i < source.size() && i < target.size())
{
YAML::Node rec_result = diff(source[i], target[i], path + "/" + to_string(i));
for (YAML::const_iterator rec_it = rec_result.begin(); rec_it != rec_result.end(); rec_it++)
{
result.push_back(*rec_it);
}
i++;
}
while (i < target.size())
{
YAML::Node item;
item["op"] = "add";
item["path"] = path + "/" + to_string(i);
item["value"] = target[i];
result.push_back(item);
i++;
}
}
else
{
YAML::Node item;
item["op"] = "replace";
item["path"] = path;
item["value"] = target;
result.push_back(item);
}
return result;
}
bool
is_number(
const string& str)
{
return find_if(
str.begin(),
str.end(),
[](char c) { return !isdigit(c); }) == str.end();
}
YAML::Node
patch(
YAML::Node& origin,
const YAML::Node& patches)
{
assert(patches.IsSequence());
for (size_t i = 0; i < patches.size(); i++)
{
YAML::Node patch = patches[i];
assert(patch["op"]);
assert(patch["path"]);
string op = patch["op"].as<string>();
string path = patch["path"].as<string>();
vector<YAML::Node> tree = { origin };
string part;
stringstream ss;
// Create string stream from patch path
ss.str(path);
// Walk through each part of the path
while (getline(ss, part, '/'))
{
if (part.empty())
continue;
YAML::Node next;
if (is_number(part))
next = tree.back()[stoi(part)];
else
next = tree.back()[part];
tree.push_back(next);
}
if (op.compare("add") == 0 || op.compare("replace") == 0)
{
tree.back() = patch["value"];
}
else if (op.compare("remove") == 0)
{
tree.at(tree.size() - 2).remove(part);
}
else if (op.compare("move") == 0)
{
throw "Not implemented";
}
else if (op.compare("copy") == 0)
{
throw "Not implemented";
}
else if (op.compare("test") == 0)
{
throw "Not implemented";
}
}
return origin;
}
void
merge(
istream& our_stream,
istream& older_stream,
istream& their_stream,
ostream& dst_stream)
{
auto our = YAML::Load(our_stream);
auto older = YAML::Load(older_stream);
auto their = YAML::Load(their_stream);
dst_stream << patch(our, diff(older, their));
}
void
usage(char *program)
{
cerr << "Usage: " << program << " <current> <older> <other>" << endl;
}
int main(
int argc,
char *argv[])
{
// Validate number of arguments or show usage.
if (argc < 4)
{
cerr << "Too few arguments" << endl;
usage(argv[0]);
exit(1);
}
// 3 input streams, 1 buffer and 1 output stream
ifstream our_stream;
ifstream older_stream;
ifstream their_stream;
ofstream output_stream;
ostringstream buffer_stream;
buffer_stream = ostringstream();
our_stream.open(argv[1]);
if (!our_stream.is_open())
{
cerr << "Cannot open current file " << argv[1] << endl;
exit(1);
}
older_stream.open(argv[2], fstream::in);
if (!older_stream.is_open())
{
cerr << "Cannot open older file " << argv[2] << endl;
exit(1);
}
their_stream.open(argv[3], fstream::in);
if (!their_stream.is_open())
{
cerr << "Cannot open other file " << argv[3] << endl;
exit(1);
}
// Parse and merge JSON streams
merge(our_stream, older_stream, their_stream, buffer_stream);
// Close input streams
our_stream.close();
older_stream.close();
their_stream.close();
// (Re)open our stream in output and dump buffer
output_stream.open(argv[1], ofstream::out);
if (!output_stream.is_open())
{
cerr << "Cannot open output file " << argv[1] << endl;
exit(1);
}
output_stream << buffer_stream.str();
// Close output stream
output_stream.close();
return 0;
}