monotone

monotone Mtn Source Tree

Root/cset.cc

1// Copyright (C) 2005 Nathaniel Smith <njs@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#include "base.hh"
11#include <map>
12#include <set>
13
14#include "basic_io.hh"
15#include "cset.hh"
16#include "sanity.hh"
17#include "safe_map.hh"
18
19using std::set;
20using std::map;
21using std::pair;
22using std::string;
23using std::make_pair;
24
25static void
26check_normalized(cset const & cs)
27{
28 MM(cs);
29
30 // normalize:
31 //
32 // add_file foo@id1 + apply_delta id1->id2
33 // clear_attr foo:bar + set_attr foo:bar=baz
34 //
35 // possibly more?
36
37 // no file appears in both the "added" list and the "patched" list
38 {
39 map<file_path, file_id>::const_iterator a = cs.files_added.begin();
40 map<file_path, pair<file_id, file_id> >::const_iterator
41 d = cs.deltas_applied.begin();
42 while (a != cs.files_added.end() && d != cs.deltas_applied.end())
43 {
44 // SPEEDUP?: should this use lower_bound instead of ++? it should
45 // make this loop iterate about as many times as the shorter list,
46 // rather the sum of the two lists...
47 if (a->first < d->first)
48 ++a;
49 else if (d->first < a->first)
50 ++d;
51 else
52 I(false);
53 }
54 }
55
56 // no file+attr pair appears in both the "set" list and the "cleared" list
57 {
58 set<pair<file_path, attr_key> >::const_iterator c = cs.attrs_cleared.begin();
59 map<pair<file_path, attr_key>, attr_value>::const_iterator
60 s = cs.attrs_set.begin();
61 while (c != cs.attrs_cleared.end() && s != cs.attrs_set.end())
62 {
63 if (*c < s->first)
64 ++c;
65 else if (s->first < *c)
66 ++s;
67 else
68 I(false);
69 }
70 }
71}
72
73bool
74cset::empty() const
75{
76 return
77 nodes_deleted.empty()
78 && dirs_added.empty()
79 && files_added.empty()
80 && nodes_renamed.empty()
81 && deltas_applied.empty()
82 && attrs_cleared.empty()
83 && attrs_set.empty();
84}
85
86void
87cset::clear()
88{
89 nodes_deleted.clear();
90 dirs_added.clear();
91 files_added.clear();
92 nodes_renamed.clear();
93 deltas_applied.clear();
94 attrs_cleared.clear();
95 attrs_set.clear();
96}
97
98struct
99detach
100{
101 detach(file_path const & src)
102 : src_path(src),
103 reattach(false)
104 {}
105
106 detach(file_path const & src,
107 file_path const & dst)
108 : src_path(src),
109 reattach(true),
110 dst_path(dst)
111 {}
112
113 file_path src_path;
114 bool reattach;
115 file_path dst_path;
116
117 bool operator<(struct detach const & other) const
118 {
119 // We sort detach operations bottom-up by src path
120 // SPEEDUP?: simply sort by path.size() rather than full lexicographical
121 // comparison?
122 return other.src_path < src_path;
123 }
124};
125
126struct
127attach
128{
129 attach(node_id n,
130 file_path const & p)
131 : node(n), path(p)
132 {}
133
134 node_id node;
135 file_path path;
136
137 bool operator<(struct attach const & other) const
138 {
139 // We sort attach operations top-down by path
140 // SPEEDUP?: simply sort by path.size() rather than full lexicographical
141 // comparison?
142 return path < other.path;
143 }
144};
145
146void
147cset::apply_to(editable_tree & t) const
148{
149 // SPEEDUP?: use vectors and sort them once, instead of maintaining sorted
150 // sets?
151 set<detach> detaches;
152 set<attach> attaches;
153 set<node_id> drops;
154
155 MM(*this);
156
157 check_normalized(*this);
158
159 // Decompose all additions into a set of pending attachments to be
160 // executed top-down. We might as well do this first, to be sure we
161 // can form the new nodes -- such as in a filesystem -- before we do
162 // anything else potentially destructive. This should all be
163 // happening in a temp directory anyways.
164
165 // NB: it's very important we do safe_insert's here, because our comparison
166 // operator for attach and detach does not distinguish all nodes! the nodes
167 // that it does not distinguish are ones where we're attaching or detaching
168 // repeatedly from the same place, so they're impossible anyway, but we need
169 // to error out if someone tries to add them.
170
171 for (set<file_path>::const_iterator i = dirs_added.begin();
172 i != dirs_added.end(); ++i)
173 safe_insert(attaches, attach(t.create_dir_node(), *i));
174
175 for (map<file_path, file_id>::const_iterator i = files_added.begin();
176 i != files_added.end(); ++i)
177 safe_insert(attaches, attach(t.create_file_node(i->second), i->first));
178
179
180 // Decompose all path deletion and the first-half of renamings on
181 // existing paths into the set of pending detaches, to be executed
182 // bottom-up.
183
184 for (set<file_path>::const_iterator i = nodes_deleted.begin();
185 i != nodes_deleted.end(); ++i)
186 safe_insert(detaches, detach(*i));
187
188 for (map<file_path, file_path>::const_iterator i = nodes_renamed.begin();
189 i != nodes_renamed.end(); ++i)
190 safe_insert(detaches, detach(i->first, i->second));
191
192
193 // Execute all the detaches, rescheduling the results of each detach
194 // for either attaching or dropping.
195
196 for (set<detach>::const_iterator i = detaches.begin();
197 i != detaches.end(); ++i)
198 {
199 node_id n = t.detach_node(i->src_path);
200 if (i->reattach)
201 safe_insert(attaches, attach(n, i->dst_path));
202 else
203 safe_insert(drops, n);
204 }
205
206
207 // Execute all the attaches.
208
209 for (set<attach>::const_iterator i = attaches.begin(); i != attaches.end(); ++i)
210 t.attach_node(i->node, i->path);
211
212
213 // Execute all the drops.
214
215 for (set<node_id>::const_iterator i = drops.begin(); i != drops.end(); ++i)
216 t.drop_detached_node (*i);
217
218
219 // Execute all the in-place edits
220 for (map<file_path, pair<file_id, file_id> >::const_iterator i = deltas_applied.begin();
221 i != deltas_applied.end(); ++i)
222 t.apply_delta(i->first, i->second.first, i->second.second);
223
224 for (set<pair<file_path, attr_key> >::const_iterator i = attrs_cleared.begin();
225 i != attrs_cleared.end(); ++i)
226 t.clear_attr(i->first, i->second);
227
228 for (map<pair<file_path, attr_key>, attr_value>::const_iterator i = attrs_set.begin();
229 i != attrs_set.end(); ++i)
230 t.set_attr(i->first.first, i->first.second, i->second);
231
232 t.commit();
233}
234
235////////////////////////////////////////////////////////////////////
236// I/O routines
237////////////////////////////////////////////////////////////////////
238
239namespace
240{
241 namespace syms
242 {
243 // cset symbols
244 symbol const delete_node("delete");
245 symbol const rename_node("rename");
246 symbol const content("content");
247 symbol const add_file("add_file");
248 symbol const add_dir("add_dir");
249 symbol const patch("patch");
250 symbol const from("from");
251 symbol const to("to");
252 symbol const clear("clear");
253 symbol const set("set");
254 symbol const attr("attr");
255 symbol const value("value");
256 }
257}
258
259void
260print_cset(basic_io::printer & printer,
261 cset const & cs)
262{
263 for (set<file_path>::const_iterator i = cs.nodes_deleted.begin();
264 i != cs.nodes_deleted.end(); ++i)
265 {
266 basic_io::stanza st;
267 st.push_file_pair(syms::delete_node, *i);
268 printer.print_stanza(st);
269 }
270
271 for (map<file_path, file_path>::const_iterator i = cs.nodes_renamed.begin();
272 i != cs.nodes_renamed.end(); ++i)
273 {
274 basic_io::stanza st;
275 st.push_file_pair(syms::rename_node, i->first);
276 st.push_file_pair(syms::to, i->second);
277 printer.print_stanza(st);
278 }
279
280 for (set<file_path>::const_iterator i = cs.dirs_added.begin();
281 i != cs.dirs_added.end(); ++i)
282 {
283 basic_io::stanza st;
284 st.push_file_pair(syms::add_dir, *i);
285 printer.print_stanza(st);
286 }
287
288 for (map<file_path, file_id>::const_iterator i = cs.files_added.begin();
289 i != cs.files_added.end(); ++i)
290 {
291 basic_io::stanza st;
292 st.push_file_pair(syms::add_file, i->first);
293 st.push_hex_pair(syms::content, i->second.inner());
294 printer.print_stanza(st);
295 }
296
297 for (map<file_path, pair<file_id, file_id> >::const_iterator i = cs.deltas_applied.begin();
298 i != cs.deltas_applied.end(); ++i)
299 {
300 basic_io::stanza st;
301 st.push_file_pair(syms::patch, i->first);
302 st.push_hex_pair(syms::from, i->second.first.inner());
303 st.push_hex_pair(syms::to, i->second.second.inner());
304 printer.print_stanza(st);
305 }
306
307 for (set<pair<file_path, attr_key> >::const_iterator i = cs.attrs_cleared.begin();
308 i != cs.attrs_cleared.end(); ++i)
309 {
310 basic_io::stanza st;
311 st.push_file_pair(syms::clear, i->first);
312 st.push_str_pair(syms::attr, i->second());
313 printer.print_stanza(st);
314 }
315
316 for (map<pair<file_path, attr_key>, attr_value>::const_iterator i = cs.attrs_set.begin();
317 i != cs.attrs_set.end(); ++i)
318 {
319 basic_io::stanza st;
320 st.push_file_pair(syms::set, i->first.first);
321 st.push_str_pair(syms::attr, i->first.second());
322 st.push_str_pair(syms::value, i->second());
323 printer.print_stanza(st);
324 }
325}
326
327
328static inline void
329parse_path(basic_io::parser & parser, file_path & sp)
330{
331 string s;
332 parser.str(s);
333 sp = file_path_internal(s);
334}
335
336void
337parse_cset(basic_io::parser & parser,
338 cset & cs)
339{
340 cs.clear();
341 string t1, t2;
342 MM(t1);
343 MM(t2);
344 file_path p1, p2;
345 MM(p1);
346 MM(p2);
347
348 file_path prev_path;
349 MM(prev_path);
350 pair<file_path, attr_key> prev_pair;
351 MM(prev_pair.first);
352 MM(prev_pair.second);
353
354 // we make use of the fact that a valid file_path is never empty
355 prev_path.clear();
356 while (parser.symp(syms::delete_node))
357 {
358 parser.sym();
359 parse_path(parser, p1);
360 I(prev_path.empty() || prev_path < p1);
361 prev_path = p1;
362 safe_insert(cs.nodes_deleted, p1);
363 }
364
365 prev_path.clear();
366 while (parser.symp(syms::rename_node))
367 {
368 parser.sym();
369 parse_path(parser, p1);
370 I(prev_path.empty() || prev_path < p1);
371 prev_path = p1;
372 parser.esym(syms::to);
373 parse_path(parser, p2);
374 safe_insert(cs.nodes_renamed, make_pair(p1, p2));
375 }
376
377 prev_path.clear();
378 while (parser.symp(syms::add_dir))
379 {
380 parser.sym();
381 parse_path(parser, p1);
382 I(prev_path.empty() || prev_path < p1);
383 prev_path = p1;
384 safe_insert(cs.dirs_added, p1);
385 }
386
387 prev_path.clear();
388 while (parser.symp(syms::add_file))
389 {
390 parser.sym();
391 parse_path(parser, p1);
392 I(prev_path.empty() || prev_path < p1);
393 prev_path = p1;
394 parser.esym(syms::content);
395 parser.hex(t1);
396 safe_insert(cs.files_added, make_pair(p1, file_id(t1)));
397 }
398
399 prev_path.clear();
400 while (parser.symp(syms::patch))
401 {
402 parser.sym();
403 parse_path(parser, p1);
404 I(prev_path.empty() || prev_path < p1);
405 prev_path = p1;
406 parser.esym(syms::from);
407 parser.hex(t1);
408 parser.esym(syms::to);
409 parser.hex(t2);
410 safe_insert(cs.deltas_applied,
411 make_pair(p1, make_pair(file_id(t1), file_id(t2))));
412 }
413
414 prev_pair.first.clear();
415 while (parser.symp(syms::clear))
416 {
417 parser.sym();
418 parse_path(parser, p1);
419 parser.esym(syms::attr);
420 parser.str(t1);
421 pair<file_path, attr_key> new_pair(p1, attr_key(t1));
422 I(prev_pair.first.empty() || new_pair > prev_pair);
423 prev_pair = new_pair;
424 safe_insert(cs.attrs_cleared, new_pair);
425 }
426
427 prev_pair.first.clear();
428 while (parser.symp(syms::set))
429 {
430 parser.sym();
431 parse_path(parser, p1);
432 parser.esym(syms::attr);
433 parser.str(t1);
434 pair<file_path, attr_key> new_pair(p1, attr_key(t1));
435 I(prev_pair.first.empty() || new_pair > prev_pair);
436 prev_pair = new_pair;
437 parser.esym(syms::value);
438 parser.str(t2);
439 safe_insert(cs.attrs_set, make_pair(new_pair, attr_value(t2)));
440 }
441}
442
443void
444write_cset(cset const & cs, data & dat)
445{
446 basic_io::printer pr;
447 print_cset(pr, cs);
448 dat = data(pr.buf);
449}
450
451void
452read_cset(data const & dat, cset & cs)
453{
454 MM(dat);
455 MM(cs);
456 basic_io::input_source src(dat(), "cset");
457 basic_io::tokenizer tok(src);
458 basic_io::parser pars(tok);
459 parse_cset(pars, cs);
460 I(src.lookahead == EOF);
461}
462
463template <> void
464dump(cset const & cs, string & out)
465{
466 data dat;
467 write_cset(cs, dat);
468 out = dat();
469}
470
471#ifdef BUILD_UNIT_TESTS
472#include "unit_tests.hh"
473
474#include "roster.hh"
475
476using std::logic_error;
477
478static void
479setup_roster(roster_t & r, file_id const & fid, node_id_source & nis)
480{
481 // sets up r to have a root dir, a dir in it name "foo", and a file under
482 // that named "bar", and the file has the given id.
483 // the file has attr "attr_file=value_file", and the dir has
484 // "attr_dir=value_dir".
485 r = roster_t();
486
487 {
488 r.attach_node(r.create_dir_node(nis), file_path_internal(""));
489 }
490 {
491 file_path fp = file_path_internal("foo");
492 r.attach_node(r.create_dir_node(nis), fp);
493 r.set_attr(fp, attr_key("attr_dir"), attr_value("value_dir"));
494 }
495 {
496 file_path fp = file_path_internal("foo/bar");
497 r.attach_node(r.create_file_node(fid, nis), fp);
498 r.set_attr(fp, attr_key("attr_file"), attr_value("value_file"));
499 }
500}
501
502UNIT_TEST(cset, cset_written)
503{
504 {
505 L(FL("TEST: cset reading - operation misordering"));
506 // bad cset, add_dir should be before add_file
507 string s("delete \"foo\"\n"
508 "\n"
509 "rename \"quux\"\n"
510 " to \"baz\"\n"
511 "\n"
512 "add_file \"bar\"\n"
513 " content [0000000000000000000000000000000000000000]\n"
514 "\n"
515 "add_dir \"pling\"\n");
516 data d1(s);
517 cset cs;
518 UNIT_TEST_CHECK_THROW(read_cset(d1, cs), logic_error);
519 // check that it still fails if there's extra stanzas past the
520 // mis-ordered entries
521 data d2(s + "\n"
522 " set \"bar\"\n"
523 " attr \"flavoursome\"\n"
524 "value \"mostly\"\n");
525 UNIT_TEST_CHECK_THROW(read_cset(d2, cs), logic_error);
526 }
527
528 {
529 L(FL("TEST: cset reading - misordered files in delete"));
530 // bad cset, bar should be before foo
531 data dat("delete \"foo\"\n"
532 "\n"
533 "delete \"bar\"\n");
534 cset cs;
535 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
536 }
537
538 {
539 L(FL("TEST: cset reading - misordered files in rename"));
540 // bad cset, bar should be before foo
541 data dat("rename \"foo\"\n"
542 " to \"foonew\"\n"
543 "\n"
544 "rename \"bar\"\n"
545 " to \"barnew\"\n");
546 cset cs;
547 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
548 }
549
550 {
551 L(FL("TEST: cset reading - misordered files in add_dir"));
552 // bad cset, bar should be before foo
553 data dat("add_dir \"foo\"\n"
554 "\n"
555 "add_dir \"bar\"\n");
556 cset cs;
557 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
558 }
559
560 {
561 L(FL("TEST: cset reading - misordered files in add_file"));
562 // bad cset, bar should be before foo
563 data dat("add_file \"foo\"\n"
564 " content [0000000000000000000000000000000000000000]\n"
565 "\n"
566 "add_file \"bar\"\n"
567 " content [0000000000000000000000000000000000000000]\n");
568 cset cs;
569 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
570 }
571
572 {
573 L(FL("TEST: cset reading - misordered files in add_file"));
574 // bad cset, bar should be before foo
575 data dat("add_file \"foo\"\n"
576 " content [0000000000000000000000000000000000000000]\n"
577 "\n"
578 "add_file \"bar\"\n"
579 " content [0000000000000000000000000000000000000000]\n");
580 cset cs;
581 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
582 }
583
584 {
585 L(FL("TEST: cset reading - misordered files in patch"));
586 // bad cset, bar should be before foo
587 data dat("patch \"foo\"\n"
588 " from [0000000000000000000000000000000000000000]\n"
589 " to [1000000000000000000000000000000000000000]\n"
590 "\n"
591 "patch \"bar\"\n"
592 " from [0000000000000000000000000000000000000000]\n"
593 " to [1000000000000000000000000000000000000000]\n");
594 cset cs;
595 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
596 }
597
598 {
599 L(FL("TEST: cset reading - misordered files in clear"));
600 // bad cset, bar should be before foo
601 data dat("clear \"foo\"\n"
602 " attr \"flavoursome\"\n"
603 "\n"
604 "clear \"bar\"\n"
605 " attr \"flavoursome\"\n");
606 cset cs;
607 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
608 }
609
610 {
611 L(FL("TEST: cset reading - misordered files in set"));
612 // bad cset, bar should be before foo
613 data dat(" set \"foo\"\n"
614 " attr \"flavoursome\"\n"
615 "value \"yes\"\n"
616 "\n"
617 " set \"bar\"\n"
618 " attr \"flavoursome\"\n"
619 "value \"yes\"\n");
620 cset cs;
621 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
622 }
623
624 {
625 L(FL("TEST: cset reading - duplicate entries"));
626 data dat("delete \"foo\"\n"
627 "\n"
628 "delete \"foo\"\n");
629 cset cs;
630 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
631 }
632
633 {
634 L(FL("TEST: cset reading - multiple different attrs"));
635 // should succeed
636 data dat( " set \"bar\"\n"
637 " attr \"flavoursome\"\n"
638 "value \"mostly\"\n"
639 "\n"
640 " set \"bar\"\n"
641 " attr \"smell\"\n"
642 "value \"socks\"\n");
643 cset cs;
644 UNIT_TEST_CHECK_NOT_THROW(read_cset(dat, cs), logic_error);
645 }
646
647 {
648 L(FL("TEST: cset reading - wrong attr ordering in clear"));
649 // fooish should be before quuxy
650 data dat( "clear \"bar\"\n"
651 " attr \"quuxy\"\n"
652 "\n"
653 "clear \"bar\"\n"
654 " attr \"fooish\"\n");
655 cset cs;
656 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
657 }
658
659 {
660 L(FL("TEST: cset reading - wrong attr ordering in set"));
661 // fooish should be before quuxy
662 data dat( " set \"bar\"\n"
663 " attr \"quuxy\"\n"
664 "value \"mostly\"\n"
665 "\n"
666 " set \"bar\"\n"
667 " attr \"fooish\"\n"
668 "value \"seldom\"\n");
669 cset cs;
670 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
671 }
672
673 {
674 L(FL("TEST: cset reading - duplicate attrs"));
675 // can't have dups.
676 data dat( " set \"bar\"\n"
677 " attr \"flavoursome\"\n"
678 "value \"mostly\"\n"
679 "\n"
680 " set \"bar\"\n"
681 " attr \"flavoursome\"\n"
682 "value \"sometimes\"\n");
683 cset cs;
684 UNIT_TEST_CHECK_THROW(read_cset(dat, cs), logic_error);
685 }
686
687 {
688 L(FL("TEST: cset writing - normalisation"));
689 cset cs; MM(cs);
690 file_id f1(string("1234567800000000000000000000000000000000"));
691 file_id f2(string("9876543212394657263900000000000000000000"));
692 file_id f3(string("0000000000011111111000000000000000000000"));
693
694 file_path foo = file_path_internal("foo");
695 file_path foo_quux = file_path_internal("foo/quux");
696 file_path bar = file_path_internal("bar");
697 file_path quux = file_path_internal("quux");
698 file_path idle = file_path_internal("idle");
699 file_path fish = file_path_internal("fish");
700 file_path womble = file_path_internal("womble");
701 file_path policeman = file_path_internal("policeman");
702
703 cs.dirs_added.insert(foo_quux);
704 cs.dirs_added.insert(foo);
705 cs.files_added.insert(make_pair(bar, f1));
706 cs.nodes_deleted.insert(quux);
707 cs.nodes_deleted.insert(idle);
708 cs.nodes_renamed.insert(make_pair(fish, womble));
709 cs.deltas_applied.insert(make_pair(womble, make_pair(f2, f3)));
710 cs.attrs_cleared.insert(make_pair(policeman, attr_key("yodel")));
711 cs.attrs_set.insert(make_pair(make_pair(policeman,
712 attr_key("axolotyl")), attr_value("fruitily")));
713 cs.attrs_set.insert(make_pair(make_pair(policeman,
714 attr_key("spin")), attr_value("capybara")));
715
716 data dat; MM(dat);
717 write_cset(cs, dat);
718 data expected("delete \"idle\"\n"
719 "\n"
720 "delete \"quux\"\n"
721 "\n"
722 "rename \"fish\"\n"
723 " to \"womble\"\n"
724 "\n"
725 "add_dir \"foo\"\n"
726 "\n"
727 "add_dir \"foo/quux\"\n"
728 "\n"
729 "add_file \"bar\"\n"
730 " content [1234567800000000000000000000000000000000]\n"
731 "\n"
732 "patch \"womble\"\n"
733 " from [9876543212394657263900000000000000000000]\n"
734 " to [0000000000011111111000000000000000000000]\n"
735 "\n"
736 "clear \"policeman\"\n"
737 " attr \"yodel\"\n"
738 "\n"
739 " set \"policeman\"\n"
740 " attr \"axolotyl\"\n"
741 "value \"fruitily\"\n"
742 "\n"
743 " set \"policeman\"\n"
744 " attr \"spin\"\n"
745 "value \"capybara\"\n"
746 );
747 MM(expected);
748 // I() so that it'll dump on failure
749 UNIT_TEST_CHECK_NOT_THROW(I(expected == dat), logic_error);
750 }
751}
752
753UNIT_TEST(cset, basic_csets)
754{
755
756 temp_node_id_source nis;
757 roster_t r;
758 MM(r);
759
760 editable_roster_base tree(r, nis);
761
762 file_id f1(string("0000000000000000000000000000000000000001"));
763 file_id f2(string("0000000000000000000000000000000000000002"));
764
765 file_path root;
766 file_path foo = file_path_internal("foo");
767 file_path foo_bar = file_path_internal("foo/bar");
768 file_path baz = file_path_internal("baz");
769 file_path quux = file_path_internal("quux");
770
771 // some basic tests that should succeed
772 {
773 L(FL("TEST: cset add file"));
774 setup_roster(r, f1, nis);
775 cset cs; MM(cs);
776 cs.files_added.insert(make_pair(baz, f2));
777 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
778 UNIT_TEST_CHECK(is_file_t(r.get_node(baz)));
779 UNIT_TEST_CHECK(downcast_to_file_t(r.get_node(baz))->content == f2);
780 UNIT_TEST_CHECK(r.all_nodes().size() == 4);
781 }
782
783 {
784 L(FL("TEST: cset add dir"));
785 setup_roster(r, f1, nis);
786 cset cs; MM(cs);
787 cs.dirs_added.insert(quux);
788 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
789 UNIT_TEST_CHECK(is_dir_t(r.get_node(quux)));
790 UNIT_TEST_CHECK(r.all_nodes().size() == 4);
791 }
792
793 {
794 L(FL("TEST: cset delete"));
795 setup_roster(r, f1, nis);
796 cset cs; MM(cs);
797 cs.nodes_deleted.insert(foo_bar);
798 cs.nodes_deleted.insert(foo);
799 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
800 UNIT_TEST_CHECK(r.all_nodes().size() == 1); // only the root left
801 }
802
803 {
804 L(FL("TEST: cset rename file"));
805 setup_roster(r, f1, nis);
806 cset cs; MM(cs);
807 cs.nodes_renamed.insert(make_pair(foo_bar, quux));
808 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
809 UNIT_TEST_CHECK(is_file_t(r.get_node(quux)));
810 UNIT_TEST_CHECK(is_dir_t(r.get_node(foo)));
811 UNIT_TEST_CHECK(!r.has_node(foo_bar));
812 UNIT_TEST_CHECK(r.all_nodes().size() == 3);
813 }
814
815 {
816 L(FL("TEST: cset rename dir"));
817 file_path quux_bar = file_path_internal("quux/bar");
818 setup_roster(r, f1, nis);
819 cset cs; MM(cs);
820 cs.nodes_renamed.insert(make_pair(foo, quux));
821 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
822 UNIT_TEST_CHECK(is_dir_t(r.get_node(quux)));
823 UNIT_TEST_CHECK(is_file_t(r.get_node(quux_bar)));
824 UNIT_TEST_CHECK(!r.has_node(foo));
825 UNIT_TEST_CHECK(r.all_nodes().size() == 3);
826 }
827
828 {
829 L(FL("TEST: patch file"));
830 setup_roster(r, f1, nis);
831 cset cs; MM(cs);
832 cs.deltas_applied.insert(make_pair(foo_bar, make_pair(f1, f2)));
833 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
834 UNIT_TEST_CHECK(is_dir_t(r.get_node(foo)));
835 UNIT_TEST_CHECK(is_file_t(r.get_node(foo_bar)));
836 UNIT_TEST_CHECK(downcast_to_file_t(r.get_node(foo_bar))->content == f2);
837 UNIT_TEST_CHECK(r.all_nodes().size() == 3);
838 }
839
840 {
841 L(FL("TEST: set attr"));
842 setup_roster(r, f1, nis);
843 cset cs; MM(cs);
844 cs.attrs_set.insert(make_pair(make_pair(foo_bar, attr_key("ping")),
845 attr_value("klang")));
846 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
847
848 full_attr_map_t attrs = (r.get_node(foo_bar))->attrs;
849 UNIT_TEST_CHECK(attrs[attr_key("ping")] == make_pair(true, attr_value("klang")));
850
851 attrs = (r.get_node(foo))->attrs;
852 UNIT_TEST_CHECK(attrs[attr_key("attr_dir")] == make_pair(true, attr_value("value_dir")));
853
854 UNIT_TEST_CHECK(r.all_nodes().size() == 3);
855 }
856
857 {
858 L(FL("TEST: clear attr file"));
859 setup_roster(r, f1, nis);
860 cset cs; MM(cs);
861 cs.attrs_set.insert(make_pair(make_pair(foo_bar, attr_key("ping")),
862 attr_value("klang")));
863 cs.attrs_cleared.insert(make_pair(foo_bar, attr_key("attr_file")));
864 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
865 UNIT_TEST_CHECK((r.get_node(foo_bar))->attrs[attr_key("attr_file")]
866 == make_pair(false, attr_value("")));
867 UNIT_TEST_CHECK(r.all_nodes().size() == 3);
868 }
869
870 // some renaming tests
871 {
872 L(FL("TEST: renaming at different levels"));
873 setup_roster(r, f1, nis);
874
875 file_path quux_bar = file_path_internal("quux/bar");
876 file_path foo_bar = file_path_internal("foo/bar");
877 file_path quux_sub = file_path_internal("quux/sub");
878 file_path foo_sub = file_path_internal("foo/sub");
879 file_path foo_sub_thing = file_path_internal("foo/sub/thing");
880 file_path quux_sub_thing = file_path_internal("quux/sub/thing");
881 file_path foo_sub_deep = file_path_internal("foo/sub/deep");
882 file_path foo_subsub = file_path_internal("foo/subsub");
883 file_path foo_subsub_deep = file_path_internal("foo/subsub/deep");
884
885 { // build a tree
886 cset cs; MM(cs);
887 cs.dirs_added.insert(quux);
888 cs.dirs_added.insert(quux_sub);
889 cs.dirs_added.insert(foo_sub);
890 cs.files_added.insert(make_pair(foo_sub_deep, f2));
891 cs.files_added.insert(make_pair(quux_sub_thing, f1));
892 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
893 UNIT_TEST_CHECK(r.all_nodes().size() == 8);
894 }
895
896 { // some renames
897 cset cs; MM(cs);
898 cs.nodes_renamed.insert(make_pair(foo, quux));
899 cs.nodes_renamed.insert(make_pair(quux, foo));
900 cs.nodes_renamed.insert(make_pair(foo_sub, foo_subsub));
901 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
902 }
903
904 UNIT_TEST_CHECK(r.all_nodes().size() == 8);
905 // /foo/bar -> /quux/bar
906 UNIT_TEST_CHECK(is_file_t(r.get_node(quux_bar)));
907 UNIT_TEST_CHECK(!(r.has_node(foo_bar)));
908 // /foo/sub/deep -> /foo/subsub/deep
909 UNIT_TEST_CHECK(is_file_t(r.get_node(foo_subsub_deep)));
910 UNIT_TEST_CHECK(!(r.has_node(foo_sub_deep)));
911 // /quux/sub -> /foo/sub
912 UNIT_TEST_CHECK(is_dir_t(r.get_node(foo_sub)));
913 UNIT_TEST_CHECK(!(r.has_node(quux_sub)));
914 // /quux/sub/thing -> /foo/sub/thing
915 UNIT_TEST_CHECK(is_file_t(r.get_node(foo_sub_thing)));
916 }
917
918 {
919 L(FL("delete targets pre-renamed nodes"));
920 setup_roster(r, f1, nis);
921 cset cs; MM(cs);
922 cs.nodes_renamed.insert(make_pair(foo_bar, foo));
923 cs.nodes_deleted.insert(foo);
924 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
925 UNIT_TEST_CHECK(r.all_nodes().size() == 2);
926 UNIT_TEST_CHECK(is_file_t(r.get_node(foo)));
927 }
928}
929
930UNIT_TEST(cset, invalid_csets)
931{
932 temp_node_id_source nis;
933 roster_t r;
934 MM(r);
935 editable_roster_base tree(r, nis);
936
937 file_id f1(string("0000000000000000000000000000000000000001"));
938 file_id f2(string("0000000000000000000000000000000000000002"));
939
940 file_path root;
941 file_path foo = file_path_internal("foo");
942 file_path foo_bar = file_path_internal("foo/bar");
943 file_path baz = file_path_internal("baz");
944 file_path quux = file_path_internal("quux");
945
946 {
947 L(FL("TEST: can't double-delete"));
948 setup_roster(r, f1, nis);
949 cset cs; MM(cs);
950 cs.nodes_deleted.insert(foo_bar);
951 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
952 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
953 }
954 {
955 L(FL("TEST: can't double-add file"));
956 setup_roster(r, f1, nis);
957 cset cs; MM(cs);
958 cs.files_added.insert(make_pair(baz, f2));
959 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
960 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
961 }
962 {
963 L(FL("TEST: can't add file on top of dir"));
964 setup_roster(r, f1, nis);
965 cset cs; MM(cs);
966 cs.files_added.insert(make_pair(foo, f2));
967 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
968 }
969 {
970 L(FL("TEST: can't delete+rename"));
971 setup_roster(r, f1, nis);
972 cset cs; MM(cs);
973 cs.nodes_deleted.insert(foo_bar);
974 cs.nodes_renamed.insert(make_pair(foo_bar, baz));
975 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
976 }
977 {
978 L(FL("TEST: can't add+rename"));
979 setup_roster(r, f1, nis);
980 cset cs; MM(cs);
981 cs.dirs_added.insert(baz);
982 cs.nodes_renamed.insert(make_pair(baz, quux));
983 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
984 }
985 {
986 L(FL("TEST: can't add on top of root dir"));
987 setup_roster(r, f1, nis);
988 cset cs; MM(cs);
989 cs.dirs_added.insert(root);
990 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
991 }
992 {
993 L(FL("TEST: can't rename on top of root dir"));
994 setup_roster(r, f1, nis);
995 cset cs; MM(cs);
996 cs.nodes_renamed.insert(make_pair(foo, root));
997 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
998 }
999 {
1000 L(FL("TEST: can't rename 'a' 'a'"));
1001 setup_roster(r, f1, nis);
1002 cset cs; MM(cs);
1003 cs.nodes_renamed.insert(make_pair(foo_bar, foo_bar));
1004 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1005 }
1006 {
1007 L(FL("TEST: can't rename 'a' 'b'; rename 'a/foo' 'b/foo'"));
1008 setup_roster(r, f1, nis);
1009 cset cs; MM(cs);
1010 file_path baz_bar = file_path_internal("baz/bar");
1011 cs.nodes_renamed.insert(make_pair(foo, baz));
1012 cs.nodes_renamed.insert(make_pair(foo_bar, baz_bar));
1013 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1014 }
1015 {
1016 L(FL("TEST: can't attr_set + attr_cleared"));
1017 setup_roster(r, f1, nis);
1018 cset cs; MM(cs);
1019 cs.attrs_set.insert(make_pair(make_pair(foo_bar, attr_key("blah")),
1020 attr_value("blahblah")));
1021 cs.attrs_cleared.insert(make_pair(foo_bar, attr_key("blah")));
1022 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1023 }
1024 {
1025 L(FL("TEST: can't no-op attr_set"));
1026 setup_roster(r, f1, nis);
1027 cset cs; MM(cs);
1028 cs.attrs_set.insert(make_pair(make_pair(foo_bar, attr_key("attr_file")),
1029 attr_value("value_file")));
1030 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1031 }
1032 {
1033 L(FL("TEST: can't clear non-existent attr"));
1034 setup_roster(r, f1, nis);
1035 cset cs; MM(cs);
1036 cs.attrs_cleared.insert(make_pair(foo_bar, attr_key("blah")));
1037 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1038 }
1039 {
1040 L(FL("TEST: can't clear non-existent attr that once existed"));
1041 setup_roster(r, f1, nis);
1042 cset cs; MM(cs);
1043 cs.attrs_cleared.insert(make_pair(foo_bar, attr_key("attr_file")));
1044 // exists now, so should be fine
1045 UNIT_TEST_CHECK_NOT_THROW(cs.apply_to(tree), logic_error);
1046 // but last time killed it, so can't be killed again
1047 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1048 }
1049 {
1050 L(FL("TEST: can't have no-op deltas"));
1051 setup_roster(r, f1, nis);
1052 cset cs; MM(cs);
1053 cs.deltas_applied.insert(make_pair(foo_bar,
1054 make_pair(f1, f1)));
1055 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1056 }
1057 {
1058 L(FL("TEST: can't have add+delta"));
1059 setup_roster(r, f1, nis);
1060 cset cs; MM(cs);
1061 cs.files_added.insert(make_pair(baz, f1));
1062 cs.deltas_applied.insert(make_pair(baz,
1063 make_pair(f1, f2)));
1064 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1065 }
1066 {
1067 L(FL("TEST: can't delta a directory"));
1068 setup_roster(r, f1, nis);
1069 cset cs; MM(cs);
1070 cs.deltas_applied.insert(make_pair(foo,
1071 make_pair(f1, f2)));
1072 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1073 }
1074 {
1075 L(FL("TEST: can't delete non-empty directory"));
1076 setup_roster(r, f1, nis);
1077 cset cs; MM(cs);
1078 cs.nodes_deleted.insert(foo);
1079 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1080 }
1081 {
1082 L(FL("TEST: attach node with no root directory present"));
1083 // for this test, make sure original roster has no contents
1084 r = roster_t();
1085 cset cs; MM(cs);
1086 file_path sp = file_path_internal("blah/blah/blah");
1087 cs.dirs_added.insert(sp);
1088 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1089 }
1090 {
1091 L(FL("TEST: can't move a directory underneath itself"));
1092 setup_roster(r, f1, nis);
1093 cset cs; MM(cs);
1094 file_path foo_blah = file_path_internal("foo/blah");
1095 cs.nodes_renamed.insert(make_pair(foo, foo_blah));
1096 UNIT_TEST_CHECK_THROW(cs.apply_to(tree), logic_error);
1097 }
1098}
1099
1100UNIT_TEST(cset, root_dir)
1101{
1102 temp_node_id_source nis;
1103 roster_t r;
1104 MM(r);
1105 editable_roster_base tree(r, nis);
1106
1107 file_id f1(string("0000000000000000000000000000000000000001"));
1108
1109 file_path root, baz = file_path_internal("baz");
1110
1111 {
1112 L(FL("TEST: can rename root"));
1113 setup_roster(r, f1, nis);
1114 cset cs; MM(cs);
1115 file_path sp1, sp2;
1116 cs.dirs_added.insert(root);
1117 cs.nodes_renamed.insert(make_pair(root, baz));
1118 cs.apply_to(tree);
1119 r.check_sane(true);
1120 }
1121 {
1122 L(FL("TEST: can delete root (but it makes us insane)"));
1123 // for this test, make sure root has no contents
1124 r = roster_t();
1125 r.attach_node(r.create_dir_node(nis), root);
1126 cset cs; MM(cs);
1127 cs.nodes_deleted.insert(root);
1128 cs.apply_to(tree);
1129 UNIT_TEST_CHECK_THROW(r.check_sane(true), logic_error);
1130 }
1131 {
1132 L(FL("TEST: can delete and replace root"));
1133 r = roster_t();
1134 r.attach_node(r.create_dir_node(nis), root);
1135 cset cs; MM(cs);
1136 cs.nodes_deleted.insert(root);
1137 cs.dirs_added.insert(root);
1138 cs.apply_to(tree);
1139 r.check_sane(true);
1140 }
1141}
1142
1143#endif // BUILD_UNIT_TESTS
1144
1145
1146
1147// Local Variables:
1148// mode: C++
1149// fill-column: 76
1150// c-file-style: "gnu"
1151// indent-tabs-mode: nil
1152// End:
1153// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status