monotone

monotone Mtn Source Tree

Root/revision.cc

1// Copyright (C) 2004 Graydon Hoare <graydon@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 <cctype>
12#include <cstdlib>
13#include <map>
14#include <queue>
15#include <set>
16#include <sstream>
17#include <stack>
18#include <iterator>
19#include <functional>
20#include <list>
21
22#include "lexical_cast.hh"
23#include <boost/dynamic_bitset.hpp>
24#include <boost/shared_ptr.hpp>
25
26#include "botan/botan.h"
27
28#include "app_state.hh"
29#include "basic_io.hh"
30#include "cert.hh"
31#include "cset.hh"
32#include "constants.hh"
33#include "interner.hh"
34#include "keys.hh"
35#include "numeric_vocab.hh"
36#include "revision.hh"
37#include "mtn-sanity.hh"
38#include "transforms.hh"
39#include "simplestring_xform.hh"
40#include "ui.hh"
41#include "vocab.hh"
42#include "safe_map.hh"
43#include "legacy.hh"
44#include "rev_height.hh"
45#include "cmd.hh"
46
47using std::back_inserter;
48using std::copy;
49using std::deque;
50using std::list;
51using std::make_pair;
52using std::map;
53using std::max;
54using std::multimap;
55using std::ostringstream;
56using std::pair;
57using std::queue;
58using std::set;
59using std::stack;
60using std::string;
61using std::vector;
62
63using boost::dynamic_bitset;
64using boost::shared_ptr;
65
66void revision_t::check_sane() const
67{
68 // null id in current manifest only permitted if previous
69 // state was null and no changes
70 // FIXME: above comment makes no sense. This should just be
71 // I(!null_id(new_manifest)), and the only reason I am not making it so
72 // right now is that I don't have time to immediately track down all the
73 // fallout.
74 if (null_id(new_manifest))
75 {
76 for (edge_map::const_iterator i = edges.begin();
77 i != edges.end(); ++i)
78 I(null_id(edge_old_revision(i)));
79 }
80
81 if (edges.size() == 1)
82 {
83 // no particular checks to be done right now
84 }
85 else if (edges.size() == 2)
86 {
87 // merge nodes cannot have null revisions
88 for (edge_map::const_iterator i = edges.begin(); i != edges.end(); ++i)
89 I(!null_id(edge_old_revision(i)));
90 }
91 else
92 // revisions must always have either 1 or 2 edges
93 I(false);
94
95 // we used to also check that if there were multiple edges that had patches
96 // for the same file, then the new hashes on each edge matched each other.
97 // this is not ported over to roster-style revisions because it's an
98 // inadequate check, and the real check, that the new manifest id is correct
99 // (done in put_revision, for instance) covers this case automatically.
100}
101
102bool
103revision_t::is_merge_node() const
104{
105 return edges.size() > 1;
106}
107
108bool
109revision_t::is_nontrivial() const
110{
111 check_sane();
112 // merge revisions are never trivial, because even if the resulting node
113 // happens to be identical to both parents, the merge is still recording
114 // that fact.
115 if (is_merge_node())
116 return true;
117 else
118 return !edge_changes(edges.begin()).empty();
119}
120
121revision_t::revision_t(revision_t const & other)
122{
123 /* behave like normal constructor if other is empty */
124 made_for = made_for_nobody;
125 if (null_id(other.new_manifest) && other.edges.empty()) return;
126 other.check_sane();
127 new_manifest = other.new_manifest;
128 edges = other.edges;
129 made_for = other.made_for;
130}
131
132revision_t const &
133revision_t::operator=(revision_t const & other)
134{
135 other.check_sane();
136 new_manifest = other.new_manifest;
137 edges = other.edges;
138 made_for = other.made_for;
139 return *this;
140}
141
142
143// For a surprisingly long time, we have been using an algorithm which
144// is nonsense, based on a misunderstanding of what "LCA" means. The
145// LCA of two nodes is *not* the first common ancestor which you find
146// when iteratively expanding their ancestor sets. Instead, the LCA is
147// the common ancestor which is a descendent of all other common
148// ancestors.
149//
150// In general, a set of nodes in a DAG doesn't always have an
151// LCA. There might be multiple common ancestors which are not parents
152// of one another. So we implement something which is "functionally
153// useful" for finding a merge point (and moreover, which always
154// terminates): we find an LCA of the input set if it exists,
155// otherwise we replace the input set with the nodes we did find and
156// repeat.
157//
158// All previous discussions in monotone-land, before say August 2005,
159// of LCA (and LCAD) are essentially wrong due to our silly
160// misunderstanding. It's unfortunate, but our half-baked
161// approximations worked almost well enough to take us through 3 years
162// of deployed use. Hopefully this more accurate new use will serve us
163// even longer.
164
165typedef unsigned long ctx;
166typedef dynamic_bitset<> bitmap;
167typedef shared_ptr<bitmap> shared_bitmap;
168
169static void
170calculate_ancestors_from_graph(interner<ctx> & intern,
171 revision_id const & init,
172 multimap<revision_id, revision_id> const & graph,
173 map< ctx, shared_bitmap > & ancestors,
174 shared_bitmap & total_union);
175
176void
177find_common_ancestor_for_merge(revision_id const & left,
178 revision_id const & right,
179 revision_id & anc,
180 app_state & app)
181{
182 interner<ctx> intern;
183 set<ctx> leaves;
184 map<ctx, shared_bitmap> ancestors;
185
186 shared_bitmap isect = shared_bitmap(new bitmap());
187 shared_bitmap isect_ancs = shared_bitmap(new bitmap());
188
189 leaves.insert(intern.intern(left.inner()()));
190 leaves.insert(intern.intern(right.inner()()));
191
192
193 multimap<revision_id, revision_id> inverse_graph;
194 {
195 multimap<revision_id, revision_id> graph;
196 app.db.get_revision_ancestry(graph);
197 typedef multimap<revision_id, revision_id>::const_iterator gi;
198 for (gi i = graph.begin(); i != graph.end(); ++i)
199 inverse_graph.insert(make_pair(i->second, i->first));
200 }
201
202
203 while (leaves.size() != 1)
204 {
205 isect->clear();
206 isect_ancs->clear();
207
208 // First intersect all ancestors of current leaf set
209 for (set<ctx>::const_iterator i = leaves.begin(); i != leaves.end(); ++i)
210 {
211 ctx curr_leaf = *i;
212 shared_bitmap curr_leaf_ancestors;
213 map<ctx, shared_bitmap >::const_iterator j = ancestors.find(*i);
214 if (j != ancestors.end())
215 curr_leaf_ancestors = j->second;
216 else
217 {
218 curr_leaf_ancestors = shared_bitmap(new bitmap());
219 calculate_ancestors_from_graph(intern, revision_id(intern.lookup(curr_leaf)),
220 inverse_graph, ancestors,
221 curr_leaf_ancestors);
222 }
223 if (isect->size() > curr_leaf_ancestors->size())
224 curr_leaf_ancestors->resize(isect->size());
225
226 if (curr_leaf_ancestors->size() > isect->size())
227 isect->resize(curr_leaf_ancestors->size());
228
229 if (i == leaves.begin())
230 *isect = *curr_leaf_ancestors;
231 else
232 (*isect) &= (*curr_leaf_ancestors);
233 }
234
235 // isect is now the set of common ancestors of leaves, but that is not enough.
236 // We need the set of leaves of isect; to do that we calculate the set of
237 // ancestors of isect, in order to subtract it from isect (below).
238 set<ctx> new_leaves;
239 for (ctx i = 0; i < isect->size(); ++i)
240 {
241 if (isect->test(i))
242 {
243 calculate_ancestors_from_graph(intern, revision_id(intern.lookup(i)),
244 inverse_graph, ancestors, isect_ancs);
245 }
246 }
247
248 // Finally, the subtraction step: for any element i of isect, if
249 // it's *not* in isect_ancs, it survives as a new leaf.
250 leaves.clear();
251 for (ctx i = 0; i < isect->size(); ++i)
252 {
253 if (!isect->test(i))
254 continue;
255 if (i < isect_ancs->size() && isect_ancs->test(i))
256 continue;
257 safe_insert(leaves, i);
258 }
259 }
260
261 I(leaves.size() == 1);
262 anc = revision_id(intern.lookup(*leaves.begin()));
263}
264
265// FIXME: this algorithm is incredibly inefficient; it's O(n) where n is the
266// size of the entire revision graph.
267
268template<typename T> static bool
269is_ancestor(T const & ancestor_id,
270 T const & descendent_id,
271 multimap<T, T> const & graph)
272{
273
274 set<T> visited;
275 queue<T> queue;
276
277 queue.push(ancestor_id);
278
279 while (!queue.empty())
280 {
281 T current_id = queue.front();
282 queue.pop();
283
284 if (current_id == descendent_id)
285 return true;
286 else
287 {
288 typedef typename multimap<T, T>::const_iterator gi;
289 pair<gi, gi> children = graph.equal_range(current_id);
290 for (gi i = children.first; i != children.second; ++i)
291 {
292 if (visited.find(i->second) == visited.end())
293 {
294 queue.push(i->second);
295 visited.insert(i->second);
296 }
297 }
298 }
299 }
300 return false;
301}
302
303bool
304is_ancestor(revision_id const & ancestor_id,
305 revision_id const & descendent_id,
306 app_state & app)
307{
308 L(FL("checking whether %s is an ancestor of %s") % ancestor_id % descendent_id);
309
310 multimap<revision_id, revision_id> graph;
311 app.db.get_revision_ancestry(graph);
312 return is_ancestor(ancestor_id, descendent_id, graph);
313}
314
315
316static void
317add_bitset_to_union(shared_bitmap src,
318 shared_bitmap dst)
319{
320 if (dst->size() > src->size())
321 src->resize(dst->size());
322 if (src->size() > dst->size())
323 dst->resize(src->size());
324 *dst |= *src;
325}
326
327
328static void
329calculate_ancestors_from_graph(interner<ctx> & intern,
330 revision_id const & init,
331 multimap<revision_id, revision_id> const & graph,
332 map< ctx, shared_bitmap > & ancestors,
333 shared_bitmap & total_union)
334{
335 typedef multimap<revision_id, revision_id>::const_iterator gi;
336 stack<ctx> stk;
337
338 stk.push(intern.intern(init.inner()()));
339
340 while (! stk.empty())
341 {
342 ctx us = stk.top();
343 revision_id rev(hexenc<id>(intern.lookup(us)));
344
345 pair<gi,gi> parents = graph.equal_range(rev);
346 bool pushed = false;
347
348 // first make sure all parents are done
349 for (gi i = parents.first; i != parents.second; ++i)
350 {
351 ctx parent = intern.intern(i->second.inner()());
352 if (ancestors.find(parent) == ancestors.end())
353 {
354 stk.push(parent);
355 pushed = true;
356 break;
357 }
358 }
359
360 // if we pushed anything we stop now. we'll come back later when all
361 // the parents are done.
362 if (pushed)
363 continue;
364
365 shared_bitmap b = shared_bitmap(new bitmap());
366
367 for (gi i = parents.first; i != parents.second; ++i)
368 {
369 ctx parent = intern.intern(i->second.inner()());
370
371 // set all parents
372 if (b->size() <= parent)
373 b->resize(parent + 1);
374 b->set(parent);
375
376 // ensure all parents are loaded into the ancestor map
377 I(ancestors.find(parent) != ancestors.end());
378
379 // union them into our map
380 map< ctx, shared_bitmap >::const_iterator j = ancestors.find(parent);
381 I(j != ancestors.end());
382 add_bitset_to_union(j->second, b);
383 }
384
385 add_bitset_to_union(b, total_union);
386 ancestors.insert(make_pair(us, b));
387 stk.pop();
388 }
389}
390
391void
392toposort(set<revision_id> const & revisions,
393 vector<revision_id> & sorted,
394 app_state & app)
395{
396 map<rev_height, revision_id> work;
397
398 for (set<revision_id>::const_iterator i = revisions.begin();
399 i != revisions.end(); ++i)
400 {
401 rev_height height;
402 app.db.get_rev_height(*i, height);
403 work.insert(make_pair(height, *i));
404 }
405
406 sorted.clear();
407
408 for (map<rev_height, revision_id>::const_iterator i = work.begin();
409 i != work.end(); ++i)
410 {
411 sorted.push_back(i->second);
412 }
413}
414
415static void
416accumulate_strict_ancestors(revision_id const & start,
417 set<revision_id> & all_ancestors,
418 multimap<revision_id, revision_id> const & inverse_graph)
419{
420 typedef multimap<revision_id, revision_id>::const_iterator gi;
421
422 vector<revision_id> frontier;
423 frontier.push_back(start);
424 while (!frontier.empty())
425 {
426 revision_id rid = frontier.back();
427 frontier.pop_back();
428 pair<gi, gi> parents = inverse_graph.equal_range(rid);
429 for (gi i = parents.first; i != parents.second; ++i)
430 {
431 revision_id const & parent = i->second;
432 if (all_ancestors.find(parent) == all_ancestors.end())
433 {
434 all_ancestors.insert(parent);
435 frontier.push_back(parent);
436 }
437 }
438 }
439}
440
441// this call is equivalent to running:
442// remove_if(candidates.begin(), candidates.end(), p);
443// erase_ancestors(candidates, app);
444// however, by interleaving the two operations, it can in common cases make
445// many fewer calls to the predicate, which can be a significant speed win.
446//
447// FIXME: once we have heights, we should use them here. The strategy will
448// be, to calculate the minimum height of all the candidates, and teach
449// accumulate_ancestors to stop at a certain height, and teach the code that
450// removes items from the candidate set to occasionally rescan the candidate
451// set to get a new minimum height (perhaps, whenever we remove the minimum
452// height candidate).
453//
454// Note: The strategy outlined above does only work well for small sets of
455// candidates, because of the overhead induced by fetching heights for all
456// members of the set.
457void
458erase_ancestors_and_failures(std::set<revision_id> & candidates,
459 is_failure & p,
460 app_state & app)
461{
462 // Load up the ancestry graph
463 multimap<revision_id, revision_id> inverse_graph;
464
465 {
466 multimap<revision_id, revision_id> graph;
467 app.db.get_revision_ancestry(graph);
468 for (multimap<revision_id, revision_id>::const_iterator i = graph.begin();
469 i != graph.end(); ++i)
470 inverse_graph.insert(make_pair(i->second, i->first));
471 }
472
473 // Keep a set of all ancestors that we've traversed -- to avoid
474 // combinatorial explosion.
475 set<revision_id> all_ancestors;
476
477 vector<revision_id> todo(candidates.begin(), candidates.end());
478 std::random_shuffle(todo.begin(), todo.end());
479
480 size_t predicates = 0;
481 while (!todo.empty())
482 {
483 revision_id rid = todo.back();
484 todo.pop_back();
485 // check if this one has already been eliminated
486 if (all_ancestors.find(rid) != all_ancestors.end())
487 continue;
488 // and then whether it actually should stay in the running:
489 ++predicates;
490 if (p(rid))
491 {
492 candidates.erase(rid);
493 continue;
494 }
495 // okay, it is good enough that all its ancestors should be
496 // eliminated
497 accumulate_strict_ancestors(rid, all_ancestors, inverse_graph);
498 }
499
500 // now go and eliminate the ancestors
501 for (set<revision_id>::const_iterator i = all_ancestors.begin();
502 i != all_ancestors.end(); ++i)
503 candidates.erase(*i);
504
505 L(FL("called predicate %s times") % predicates);
506}
507
508// This function looks at a set of revisions, and for every pair A, B in that
509// set such that A is an ancestor of B, it erases A.
510
511namespace
512{
513 struct no_failures : public is_failure
514 {
515 virtual bool operator()(revision_id const & rid)
516 {
517 return false;
518 }
519 };
520}
521void
522erase_ancestors(set<revision_id> & revisions, app_state & app)
523{
524 no_failures p;
525 erase_ancestors_and_failures(revisions, p, app);
526}
527
528// This function takes a revision A and a set of revision Bs, calculates the
529// ancestry of each, and returns the set of revisions that are in A's ancestry
530// but not in the ancestry of any of the Bs. It tells you 'what's new' in A
531// that's not in the Bs. If the output set if non-empty, then A will
532// certainly be in it; but the output set might be empty.
533void
534ancestry_difference(revision_id const & a, set<revision_id> const & bs,
535 set<revision_id> & new_stuff,
536 app_state & app)
537{
538 new_stuff.clear();
539 typedef multimap<revision_id, revision_id>::const_iterator gi;
540 multimap<revision_id, revision_id> graph;
541 multimap<revision_id, revision_id> inverse_graph;
542
543 app.db.get_revision_ancestry(graph);
544 for (gi i = graph.begin(); i != graph.end(); ++i)
545 inverse_graph.insert(make_pair(i->second, i->first));
546
547 interner<ctx> intern;
548 map< ctx, shared_bitmap > ancestors;
549
550 shared_bitmap u = shared_bitmap(new bitmap());
551
552 for (set<revision_id>::const_iterator i = bs.begin();
553 i != bs.end(); ++i)
554 {
555 calculate_ancestors_from_graph(intern, *i, inverse_graph, ancestors, u);
556 ctx c = intern.intern(i->inner()());
557 if (u->size() <= c)
558 u->resize(c + 1);
559 u->set(c);
560 }
561
562 shared_bitmap au = shared_bitmap(new bitmap());
563 calculate_ancestors_from_graph(intern, a, inverse_graph, ancestors, au);
564 {
565 ctx c = intern.intern(a.inner()());
566 if (au->size() <= c)
567 au->resize(c + 1);
568 au->set(c);
569 }
570
571 au->resize(max(au->size(), u->size()));
572 u->resize(max(au->size(), u->size()));
573
574 *au -= *u;
575
576 for (unsigned int i = 0; i != au->size(); ++i)
577 {
578 if (au->test(i))
579 {
580 revision_id rid(intern.lookup(i));
581 if (!null_id(rid))
582 new_stuff.insert(rid);
583 }
584 }
585}
586
587void
588select_nodes_modified_by_rev(revision_t const & rev,
589 roster_t const new_roster,
590 set<node_id> & nodes_modified,
591 app_state & app)
592{
593 nodes_modified.clear();
594
595 for (edge_map::const_iterator i = rev.edges.begin();
596 i != rev.edges.end(); ++i)
597 {
598 set<node_id> edge_nodes_modified;
599 roster_t old_roster;
600 app.db.get_roster(edge_old_revision(i), old_roster);
601 select_nodes_modified_by_cset(edge_changes(i),
602 old_roster,
603 new_roster,
604 edge_nodes_modified);
605
606 copy(edge_nodes_modified.begin(), edge_nodes_modified.end(),
607 inserter(nodes_modified, nodes_modified.begin()));
608 }
609}
610
611
612void
613make_revision(revision_id const & old_rev_id,
614 roster_t const & old_roster,
615 roster_t const & new_roster,
616 revision_t & rev)
617{
618 shared_ptr<cset> cs(new cset());
619
620 rev.edges.clear();
621 make_cset(old_roster, new_roster, *cs);
622
623 calculate_ident(new_roster, rev.new_manifest);
624 L(FL("new manifest_id is %s") % rev.new_manifest);
625
626 safe_insert(rev.edges, make_pair(old_rev_id, cs));
627 rev.made_for = made_for_database;
628}
629
630void
631make_revision(revision_id const & old_rev_id,
632 roster_t const & old_roster,
633 cset const & changes,
634 revision_t & rev)
635{
636 roster_t new_roster = old_roster;
637 {
638 temp_node_id_source nis;
639 editable_roster_base er(new_roster, nis);
640 changes.apply_to(er);
641 }
642
643 shared_ptr<cset> cs(new cset(changes));
644 rev.edges.clear();
645
646 calculate_ident(new_roster, rev.new_manifest);
647 L(FL("new manifest_id is %s") % rev.new_manifest);
648
649 safe_insert(rev.edges, make_pair(old_rev_id, cs));
650 rev.made_for = made_for_database;
651}
652
653void
654make_revision(parent_map const & old_rosters,
655 roster_t const & new_roster,
656 revision_t & rev)
657{
658 edge_map edges;
659 for (parent_map::const_iterator i = old_rosters.begin();
660 i != old_rosters.end();
661 i++)
662 {
663 shared_ptr<cset> cs(new cset());
664 make_cset(parent_roster(i), new_roster, *cs);
665 safe_insert(edges, make_pair(parent_id(i), cs));
666 }
667
668 rev.edges = edges;
669 calculate_ident(new_roster, rev.new_manifest);
670 L(FL("new manifest_id is %s") % rev.new_manifest);
671}
672
673static void
674recalculate_manifest_id_for_restricted_rev(parent_map const & old_rosters,
675 edge_map & edges,
676 revision_t & rev)
677{
678 // In order to get the correct manifest ID, recalculate the new roster
679 // using one of the restricted csets. It doesn't matter which of the
680 // parent roster/cset pairs we use for this; by construction, they must
681 // all produce the same result.
682 revision_id id = parent_id(old_rosters.begin());
683 roster_t restricted_roster = *(safe_get(old_rosters, id).first);
684
685 temp_node_id_source nis;
686 editable_roster_base er(restricted_roster, nis);
687 safe_get(edges, id)->apply_to(er);
688
689 calculate_ident(restricted_roster, rev.new_manifest);
690 rev.edges = edges;
691 L(FL("new manifest_id is %s") % rev.new_manifest);
692}
693
694void
695make_restricted_revision(parent_map const & old_rosters,
696 roster_t const & new_roster,
697 node_restriction const & mask,
698 revision_t & rev)
699{
700 edge_map edges;
701 cset dummy;
702 for (parent_map::const_iterator i = old_rosters.begin();
703 i != old_rosters.end();
704 i++)
705 {
706 shared_ptr<cset> included(new cset());
707 make_restricted_csets(parent_roster(i), new_roster,
708 *included, dummy, mask);
709 check_restricted_cset(parent_roster(i), *included);
710 safe_insert(edges, make_pair(parent_id(i), included));
711 }
712
713 recalculate_manifest_id_for_restricted_rev(old_rosters, edges, rev);
714}
715
716void
717make_restricted_revision(parent_map const & old_rosters,
718 roster_t const & new_roster,
719 node_restriction const & mask,
720 revision_t & rev,
721 cset & excluded,
722 commands::command_id const & cmd_name)
723{
724 edge_map edges;
725 bool no_excludes = true;
726 for (parent_map::const_iterator i = old_rosters.begin();
727 i != old_rosters.end();
728 i++)
729 {
730 shared_ptr<cset> included(new cset());
731 make_restricted_csets(parent_roster(i), new_roster,
732 *included, excluded, mask);
733 check_restricted_cset(parent_roster(i), *included);
734 safe_insert(edges, make_pair(parent_id(i), included));
735 if (!excluded.empty())
736 no_excludes = false;
737 }
738
739 N(old_rosters.size() == 1 || no_excludes,
740 F("the command '%s %s' cannot be restricted in a two-parent workspace")
741 % ui.prog_name % join_words(cmd_name)());
742
743 recalculate_manifest_id_for_restricted_rev(old_rosters, edges, rev);
744}
745
746// Workspace-only revisions, with fake rev.new_manifest and content
747// changes suppressed.
748void
749make_revision_for_workspace(revision_id const & old_rev_id,
750 cset const & changes,
751 revision_t & rev)
752{
753 MM(old_rev_id);
754 MM(changes);
755 MM(rev);
756 shared_ptr<cset> cs(new cset(changes));
757 cs->deltas_applied.clear();
758
759 rev.edges.clear();
760 safe_insert(rev.edges, make_pair(old_rev_id, cs));
761 if (!null_id(old_rev_id))
762 rev.new_manifest = manifest_id(fake_id());
763 rev.made_for = made_for_workspace;
764}
765
766void
767make_revision_for_workspace(revision_id const & old_rev_id,
768 roster_t const & old_roster,
769 roster_t const & new_roster,
770 revision_t & rev)
771{
772 MM(old_rev_id);
773 MM(old_roster);
774 MM(new_roster);
775 MM(rev);
776 cset changes;
777 make_cset(old_roster, new_roster, changes);
778 make_revision_for_workspace(old_rev_id, changes, rev);
779}
780
781void
782make_revision_for_workspace(parent_map const & old_rosters,
783 roster_t const & new_roster,
784 revision_t & rev)
785{
786 edge_map edges;
787 for (parent_map::const_iterator i = old_rosters.begin();
788 i != old_rosters.end();
789 i++)
790 {
791 shared_ptr<cset> cs(new cset());
792 make_cset(parent_roster(i), new_roster, *cs);
793 cs->deltas_applied.clear();
794 safe_insert(edges, make_pair(parent_id(i), cs));
795 }
796
797 rev.edges = edges;
798 rev.new_manifest = manifest_id(fake_id());
799 rev.made_for = made_for_workspace;
800}
801
802
803// Stuff related to rebuilding the revision graph. Unfortunately this is a
804// real enough error case that we need support code for it.
805
806typedef map<u64, pair<shared_ptr<roster_t>, shared_ptr<marking_map> > >
807parent_roster_map;
808
809template <> void
810dump(parent_roster_map const & prm, string & out)
811{
812 ostringstream oss;
813 for (parent_roster_map::const_iterator i = prm.begin(); i != prm.end(); ++i)
814 {
815 oss << "roster: " << i->first << '\n';
816 string roster_str, indented_roster_str;
817 dump(*i->second.first, roster_str);
818 prefix_lines_with(" ", roster_str, indented_roster_str);
819 oss << indented_roster_str;
820 oss << "\nroster's marking:\n";
821 string marking_str, indented_marking_str;
822 dump(*i->second.second, marking_str);
823 prefix_lines_with(" ", marking_str, indented_marking_str);
824 oss << indented_marking_str;
825 oss << "\n\n";
826 }
827 out = oss.str();
828}
829
830struct anc_graph
831{
832 anc_graph(bool existing, app_state & a) :
833 existing_graph(existing),
834 app(a),
835 max_node(0),
836 n_nodes("nodes", "n", 1),
837 n_certs_in("certs in", "c", 1),
838 n_revs_out("revs out", "r", 1),
839 n_certs_out("certs out", "C", 1)
840 {}
841
842 bool existing_graph;
843 app_state & app;
844 u64 max_node;
845
846 ticker n_nodes;
847 ticker n_certs_in;
848 ticker n_revs_out;
849 ticker n_certs_out;
850
851 map<u64,manifest_id> node_to_old_man;
852 map<manifest_id,u64> old_man_to_node;
853
854 map<u64,revision_id> node_to_old_rev;
855 map<revision_id,u64> old_rev_to_node;
856
857 map<u64,revision_id> node_to_new_rev;
858 map<revision_id,u64> new_rev_to_node;
859
860 map<u64, legacy::renames_map> node_to_renames;
861
862 multimap<u64, pair<cert_name, cert_value> > certs;
863 multimap<u64, u64> ancestry;
864 set<string> branches;
865
866 void add_node_ancestry(u64 child, u64 parent);
867 void write_certs();
868 void kluge_for_bogus_merge_edges();
869 void rebuild_ancestry();
870 void get_node_manifest(u64 node, manifest_id & man);
871 u64 add_node_for_old_manifest(manifest_id const & man);
872 u64 add_node_for_oldstyle_revision(revision_id const & rev);
873 void construct_revisions_from_ancestry();
874 void fixup_node_identities(parent_roster_map const & parent_rosters,
875 roster_t & child_roster,
876 legacy::renames_map const & renames);
877};
878
879
880void anc_graph::add_node_ancestry(u64 child, u64 parent)
881{
882 L(FL("noting ancestry from child %d -> parent %d") % child % parent);
883 ancestry.insert(make_pair(child, parent));
884}
885
886void anc_graph::get_node_manifest(u64 node, manifest_id & man)
887{
888 map<u64,manifest_id>::const_iterator i = node_to_old_man.find(node);
889 I(i != node_to_old_man.end());
890 man = i->second;
891}
892
893void anc_graph::write_certs()
894{
895 {
896 // regenerate epochs on all branches to random states
897
898 for (set<string>::const_iterator i = branches.begin(); i != branches.end(); ++i)
899 {
900 char buf[constants::epochlen_bytes];
901 Botan::Global_RNG::randomize(reinterpret_cast<Botan::byte *>(buf), constants::epochlen_bytes);
902 hexenc<data> hexdata;
903 encode_hexenc(data(string(buf, buf + constants::epochlen_bytes)), hexdata);
904 epoch_data new_epoch(hexdata);
905 L(FL("setting epoch for %s to %s") % *i % new_epoch);
906 app.db.set_epoch(branch_name(*i), new_epoch);
907 }
908 }
909
910
911 typedef multimap<u64, pair<cert_name, cert_value> >::const_iterator ci;
912
913 for (map<u64,revision_id>::const_iterator i = node_to_new_rev.begin();
914 i != node_to_new_rev.end(); ++i)
915 {
916 revision_id rev(i->second);
917
918 pair<ci,ci> range = certs.equal_range(i->first);
919
920 for (ci j = range.first; j != range.second; ++j)
921 {
922 cert_name name(j->second.first);
923 cert_value val(j->second.second);
924
925 cert new_cert;
926 make_simple_cert(rev.inner(), name, val, app, new_cert);
927 revision<cert> rcert(new_cert);
928 if (app.db.put_revision_cert(rcert))
929 ++n_certs_out;
930 }
931 }
932}
933
934void
935anc_graph::kluge_for_bogus_merge_edges()
936{
937 // This kluge exists because in the 0.24-era monotone databases, several
938 // bad merges still existed in which one side of the merge is an ancestor
939 // of the other side of the merge. In other words, graphs which look like
940 // this:
941 //
942 // a ----------------------> e
943 // \ /
944 // \---> b -> c -> d ----/
945 //
946 // Such merges confuse the roster-building algorithm, because they should
947 // never have occurred in the first place: a was not a head at the time
948 // of the merge, e should simply have been considered an extension of d.
949 //
950 // So... we drop the a->e edges entirely.
951 //
952 // Note: this kluge drops edges which are a struct superset of those
953 // dropped by a previous kluge ("3-ancestor") so we have removed that
954 // code.
955
956 P(F("scanning for bogus merge edges"));
957
958 multimap<u64,u64> parent_to_child_map;
959 for (multimap<u64, u64>::const_iterator i = ancestry.begin();
960 i != ancestry.end(); ++i)
961 parent_to_child_map.insert(make_pair(i->second, i->first));
962
963 map<u64, u64> edges_to_kill;
964 for (multimap<u64, u64>::const_iterator i = ancestry.begin();
965 i != ancestry.end(); ++i)
966 {
967 multimap<u64, u64>::const_iterator j = i;
968 ++j;
969 u64 child = i->first;
970 // NB: ancestry is a multimap from child->parent(s)
971 if (j != ancestry.end())
972 {
973 if (j->first == i->first)
974 {
975 L(FL("considering old merge edge %s") %
976 safe_get(node_to_old_rev, i->first));
977 u64 parent1 = i->second;
978 u64 parent2 = j->second;
979 if (is_ancestor (parent1, parent2, parent_to_child_map))
980 safe_insert(edges_to_kill, make_pair(child, parent1));
981 else if (is_ancestor (parent2, parent1, parent_to_child_map))
982 safe_insert(edges_to_kill, make_pair(child, parent2));
983 }
984 }
985 }
986
987 for (map<u64, u64>::const_iterator i = edges_to_kill.begin();
988 i != edges_to_kill.end(); ++i)
989 {
990 u64 child = i->first;
991 u64 parent = i->second;
992 bool killed = false;
993 for (multimap<u64, u64>::iterator j = ancestry.lower_bound(child);
994 j->first == child; ++j)
995 {
996 if (j->second == parent)
997 {
998 P(F("optimizing out redundant edge %d -> %d")
999 % parent % child);
1000 ancestry.erase(j);
1001 killed = true;
1002 break;
1003 }
1004 }
1005
1006 if (!killed)
1007 W(F("failed to eliminate edge %d -> %d")
1008 % parent % child);
1009 }
1010}
1011
1012
1013void
1014anc_graph::rebuild_ancestry()
1015{
1016 kluge_for_bogus_merge_edges();
1017
1018 P(F("rebuilding %d nodes") % max_node);
1019 {
1020 transaction_guard guard(app.db);
1021 if (existing_graph)
1022 app.db.delete_existing_revs_and_certs();
1023 construct_revisions_from_ancestry();
1024 write_certs();
1025 if (existing_graph)
1026 app.db.delete_existing_manifests();
1027 guard.commit();
1028 }
1029}
1030
1031u64
1032anc_graph::add_node_for_old_manifest(manifest_id const & man)
1033{
1034 I(!existing_graph);
1035 u64 node = 0;
1036 if (old_man_to_node.find(man) == old_man_to_node.end())
1037 {
1038 node = max_node++;
1039 ++n_nodes;
1040 L(FL("node %d = manifest %s") % node % man);
1041 old_man_to_node.insert(make_pair(man, node));
1042 node_to_old_man.insert(make_pair(node, man));
1043
1044 // load certs
1045 vector< manifest<cert> > mcerts;
1046 app.db.get_manifest_certs(man, mcerts);
1047 erase_bogus_certs(mcerts, app);
1048 for(vector< manifest<cert> >::const_iterator i = mcerts.begin();
1049 i != mcerts.end(); ++i)
1050 {
1051 L(FL("loaded '%s' manifest cert for node %s") % i->inner().name % node);
1052 cert_value tv;
1053 decode_base64(i->inner().value, tv);
1054 ++n_certs_in;
1055 certs.insert(make_pair(node,
1056 make_pair(i->inner().name, tv)));
1057 }
1058 }
1059 else
1060 {
1061 node = old_man_to_node[man];
1062 }
1063 return node;
1064}
1065
1066u64 anc_graph::add_node_for_oldstyle_revision(revision_id const & rev)
1067{
1068 I(existing_graph);
1069 I(!null_id(rev));
1070 u64 node = 0;
1071 if (old_rev_to_node.find(rev) == old_rev_to_node.end())
1072 {
1073 node = max_node++;
1074 ++n_nodes;
1075
1076 manifest_id man;
1077 legacy::renames_map renames;
1078 legacy::get_manifest_and_renames_for_rev(app, rev, man, renames);
1079
1080 L(FL("node %d = revision %s = manifest %s") % node % rev % man);
1081 old_rev_to_node.insert(make_pair(rev, node));
1082 node_to_old_rev.insert(make_pair(node, rev));
1083 node_to_old_man.insert(make_pair(node, man));
1084 node_to_renames.insert(make_pair(node, renames));
1085
1086 // load certs
1087 vector< revision<cert> > rcerts;
1088 app.db.get_revision_certs(rev, rcerts);
1089 erase_bogus_certs(rcerts, app);
1090 for(vector< revision<cert> >::const_iterator i = rcerts.begin();
1091 i != rcerts.end(); ++i)
1092 {
1093 L(FL("loaded '%s' revision cert for node %s") % i->inner().name % node);
1094 cert_value tv;
1095 decode_base64(i->inner().value, tv);
1096 ++n_certs_in;
1097 certs.insert(make_pair(node,
1098 make_pair(i->inner().name, tv)));
1099
1100 if (i->inner().name == branch_cert_name)
1101 branches.insert(tv());
1102 }
1103 }
1104 else
1105 {
1106 node = old_rev_to_node[rev];
1107 }
1108
1109 return node;
1110}
1111
1112static bool
1113not_dead_yet(node_id nid, u64 birth_rev,
1114 parent_roster_map const & parent_rosters,
1115 multimap<u64, u64> const & child_to_parents)
1116{
1117 // Any given node, at each point in the revision graph, is in one of the
1118 // states "alive", "unborn", "dead". The invariant we must maintain in
1119 // constructing our revision graph is that if a node is dead in any parent,
1120 // then it must also be dead in the child. The purpose of this function is
1121 // to take a node, and a list of parents, and determine whether that node is
1122 // allowed to be alive in a child of the given parents.
1123
1124 // "Alive" means, the node currently exists in the revision's tree.
1125 // "Unborn" means, the node does not exist in the revision's tree, and the
1126 // node's birth revision is _not_ an ancestor of the revision.
1127 // "Dead" means, the node does not exist in the revision's tree, and the
1128 // node's birth revision _is_ an ancestor of the revision.
1129
1130 // L(FL("testing liveliness of node %d, born in rev %d") % nid % birth_rev);
1131 for (parent_roster_map::const_iterator r = parent_rosters.begin();
1132 r != parent_rosters.end(); ++r)
1133 {
1134 shared_ptr<roster_t> parent = r->second.first;
1135 // L(FL("node %d %s in parent roster %d")
1136 // % nid
1137 // % (parent->has_node(n->first) ? "exists" : "does not exist" )
1138 // % r->first);
1139
1140 if (!parent->has_node(nid))
1141 {
1142 deque<u64> work;
1143 set<u64> seen;
1144 work.push_back(r->first);
1145 while (!work.empty())
1146 {
1147 u64 curr = work.front();
1148 work.pop_front();
1149 // L(FL("examining ancestor %d of parent roster %d, looking for anc=%d")
1150 // % curr % r->first % birth_rev);
1151
1152 if (seen.find(curr) != seen.end())
1153 continue;
1154 seen.insert(curr);
1155
1156 if (curr == birth_rev)
1157 {
1158 // L(FL("node is dead in %d") % r->first);
1159 return false;
1160 }
1161 typedef multimap<u64, u64>::const_iterator ci;
1162 pair<ci,ci> range = child_to_parents.equal_range(curr);
1163 for (ci i = range.first; i != range.second; ++i)
1164 {
1165 if (i->first != curr)
1166 continue;
1167 work.push_back(i->second);
1168 }
1169 }
1170 }
1171 }
1172 // L(FL("node is alive in all parents, returning true"));
1173 return true;
1174}
1175
1176
1177static file_path
1178find_old_path_for(map<file_path, file_path> const & renames,
1179 file_path const & new_path)
1180{
1181 map<file_path, file_path>::const_iterator i = renames.find(new_path);
1182 if (i != renames.end())
1183 return i->second;
1184
1185 // ??? root directory rename possible in the old schema?
1186 // if not, do this first.
1187 if (new_path.empty())
1188 return new_path;
1189
1190 file_path dir;
1191 path_component base;
1192 new_path.dirname_basename(dir, base);
1193 return find_old_path_for(renames, dir) / base;
1194}
1195
1196static file_path
1197find_new_path_for(map<file_path, file_path> const & renames,
1198 file_path const & old_path)
1199{
1200 map<file_path, file_path> reversed;
1201 for (map<file_path, file_path>::const_iterator i = renames.begin();
1202 i != renames.end(); ++i)
1203 reversed.insert(make_pair(i->second, i->first));
1204 // this is a hackish kluge. seems to work, though.
1205 return find_old_path_for(reversed, old_path);
1206}
1207
1208// Recursive helper function for insert_into_roster.
1209static void
1210insert_parents_into_roster(roster_t & child_roster,
1211 temp_node_id_source & nis,
1212 file_path const & pth,
1213 file_path const & full)
1214{
1215 if (child_roster.has_node(pth))
1216 {
1217 E(is_dir_t(child_roster.get_node(pth)),
1218 F("Directory %s for path %s cannot be added, "
1219 "as there is a file in the way") % pth % full);
1220 return;
1221 }
1222
1223 if (!pth.empty())
1224 insert_parents_into_roster(child_roster, nis, pth.dirname(), full);
1225
1226 child_roster.attach_node(child_roster.create_dir_node(nis), pth);
1227}
1228
1229static void
1230insert_into_roster(roster_t & child_roster,
1231 temp_node_id_source & nis,
1232 file_path const & pth,
1233 file_id const & fid)
1234{
1235 if (child_roster.has_node(pth))
1236 {
1237 node_t n = child_roster.get_node(pth);
1238 E(is_file_t(n),
1239 F("Path %s cannot be added, as there is a directory in the way") % pth);
1240 file_t f = downcast_to_file_t(n);
1241 E(f->content == fid,
1242 F("Path %s added twice with differing content") % pth);
1243 return;
1244 }
1245
1246 insert_parents_into_roster(child_roster, nis, pth.dirname(), pth);
1247 child_roster.attach_node(child_roster.create_file_node(fid, nis), pth);
1248}
1249
1250void
1251anc_graph::fixup_node_identities(parent_roster_map const & parent_rosters,
1252 roster_t & child_roster,
1253 legacy::renames_map const & renames)
1254{
1255 // Our strategy here is to iterate over every node in every parent, and
1256 // for each parent node P find zero or one tmp nodes in the child which
1257 // represents the fate of P:
1258 //
1259 // - If any of the parents thinks that P has died, we do not search for
1260 // it in the child; we leave it as "dropped".
1261 //
1262 // - We fetch the name N of the parent node P, and apply the rename map
1263 // to N, getting "remapped name" M. If we find a child node C with
1264 // name M in the child roster, with the same type as P, we identify P
1265 // and C, and swap P for C in the child.
1266
1267
1268 // Map node_id -> birth rev
1269 map<node_id, u64> nodes_in_any_parent;
1270
1271 // Stage 1: collect all nodes (and their birth revs) in any parent.
1272 for (parent_roster_map::const_iterator i = parent_rosters.begin();
1273 i != parent_rosters.end(); ++i)
1274 {
1275 shared_ptr<roster_t> parent_roster = i->second.first;
1276 shared_ptr<marking_map> parent_marking = i->second.second;
1277
1278 node_map const & nodes = parent_roster->all_nodes();
1279 for (node_map::const_iterator j = nodes.begin(); j != nodes.end(); ++j)
1280 {
1281 node_id n = j->first;
1282 revision_id birth_rev = safe_get(*parent_marking, n).birth_revision;
1283 u64 birth_node = safe_get(new_rev_to_node, birth_rev);
1284 map<node_id, u64>::const_iterator i = nodes_in_any_parent.find(n);
1285 if (i != nodes_in_any_parent.end())
1286 I(i->second == birth_node);
1287 else
1288 safe_insert(nodes_in_any_parent,
1289 make_pair(n, birth_node));
1290 }
1291 }
1292
1293 // Stage 2: For any node which is actually live, try to locate a mapping
1294 // from a parent instance of it to a child node.
1295 for (map<node_id, u64>::const_iterator i = nodes_in_any_parent.begin();
1296 i != nodes_in_any_parent.end(); ++i)
1297 {
1298 node_id n = i->first;
1299 u64 birth_rev = i->second;
1300
1301 if (child_roster.has_node(n))
1302 continue;
1303
1304 if (not_dead_yet(n, birth_rev, parent_rosters, ancestry))
1305 {
1306 for (parent_roster_map::const_iterator j = parent_rosters.begin();
1307 j != parent_rosters.end(); ++j)
1308 {
1309 shared_ptr<roster_t> parent_roster = j->second.first;
1310
1311 if (!parent_roster->has_node(n))
1312 continue;
1313
1314 file_path fp;
1315 parent_roster->get_name(n, fp);
1316
1317 // Try remapping the name.
1318 if (node_to_old_rev.find(j->first) != node_to_old_rev.end())
1319 {
1320 legacy::renames_map::const_iterator rmap;
1321 revision_id parent_rid = safe_get(node_to_old_rev, j->first);
1322 rmap = renames.find(parent_rid);
1323 if (rmap != renames.end())
1324 fp = find_new_path_for(rmap->second, fp);
1325 }
1326
1327 // See if we can match this node against a child.
1328 if ((!child_roster.has_node(n))
1329 && child_roster.has_node(fp))
1330 {
1331 node_t pn = parent_roster->get_node(n);
1332 node_t cn = child_roster.get_node(fp);
1333 if (is_file_t(pn) == is_file_t(cn))
1334 {
1335 child_roster.replace_node_id(cn->self, n);
1336 break;
1337 }
1338 }
1339 }
1340 }
1341 }
1342}
1343
1344struct
1345current_rev_debugger
1346{
1347 u64 node;
1348 anc_graph const & agraph;
1349 current_rev_debugger(u64 n, anc_graph const & ag)
1350 : node(n), agraph(ag)
1351 {
1352 }
1353};
1354
1355template <> void
1356dump(current_rev_debugger const & d, string & out)
1357{
1358 typedef multimap<u64, pair<cert_name, cert_value> >::const_iterator ci;
1359 pair<ci,ci> range = d.agraph.certs.equal_range(d.node);
1360 for(ci i = range.first; i != range.second; ++i)
1361 {
1362 if (i->first == d.node)
1363 {
1364 out += "cert '" + i->second.first() + "'";
1365 out += "= '" + i->second.second() + "'\n";
1366 }
1367 }
1368}
1369
1370
1371void
1372anc_graph::construct_revisions_from_ancestry()
1373{
1374 // This is an incredibly cheesy, and also reasonably simple sorting
1375 // system: we put all the root nodes in the work queue. we take a
1376 // node out of the work queue and check if its parents are done. if
1377 // they are, we process it and insert its children. otherwise we put
1378 // it back on the end of the work queue. This both ensures that we're
1379 // always processing something *like* a frontier, while avoiding the
1380 // need to worry about one side of the frontier advancing faster than
1381 // another.
1382
1383 typedef multimap<u64,u64>::const_iterator ci;
1384 multimap<u64,u64> parent_to_child_map;
1385 deque<u64> work;
1386 set<u64> done;
1387
1388 {
1389 // Set up the parent->child mapping and prime the work queue
1390
1391 set<u64> children, all;
1392 for (multimap<u64, u64>::const_iterator i = ancestry.begin();
1393 i != ancestry.end(); ++i)
1394 {
1395 parent_to_child_map.insert(make_pair(i->second, i->first));
1396 children.insert(i->first);
1397 }
1398 for (map<u64,manifest_id>::const_iterator i = node_to_old_man.begin();
1399 i != node_to_old_man.end(); ++i)
1400 {
1401 all.insert(i->first);
1402 }
1403
1404 set_difference(all.begin(), all.end(),
1405 children.begin(), children.end(),
1406 back_inserter(work));
1407 }
1408
1409 while (!work.empty())
1410 {
1411
1412 u64 child = work.front();
1413
1414 current_rev_debugger dbg(child, *this);
1415 MM(dbg);
1416
1417 work.pop_front();
1418
1419 if (done.find(child) != done.end())
1420 continue;
1421
1422 pair<ci,ci> parent_range = ancestry.equal_range(child);
1423 set<u64> parents;
1424 bool parents_all_done = true;
1425 for (ci i = parent_range.first; parents_all_done && i != parent_range.second; ++i)
1426 {
1427 if (i->first != child)
1428 continue;
1429 u64 parent = i->second;
1430 if (done.find(parent) == done.end())
1431 {
1432 work.push_back(child);
1433 parents_all_done = false;
1434 }
1435 else
1436 parents.insert(parent);
1437 }
1438
1439 if (parents_all_done
1440 && (node_to_new_rev.find(child) == node_to_new_rev.end()))
1441 {
1442 L(FL("processing node %d") % child);
1443
1444 manifest_id old_child_mid;
1445 legacy::manifest_map old_child_man;
1446
1447 get_node_manifest(child, old_child_mid);
1448 manifest_data mdat;
1449 app.db.get_manifest_version(old_child_mid, mdat);
1450 legacy::read_manifest_map(mdat, old_child_man);
1451
1452 // Load all the parent rosters into a temporary roster map
1453 parent_roster_map parent_rosters;
1454 MM(parent_rosters);
1455
1456 for (ci i = parent_range.first; parents_all_done && i != parent_range.second; ++i)
1457 {
1458 if (i->first != child)
1459 continue;
1460 u64 parent = i->second;
1461 if (parent_rosters.find(parent) == parent_rosters.end())
1462 {
1463 shared_ptr<roster_t> ros = shared_ptr<roster_t>(new roster_t());
1464 shared_ptr<marking_map> mm = shared_ptr<marking_map>(new marking_map());
1465 app.db.get_roster(safe_get(node_to_new_rev, parent), *ros, *mm);
1466 safe_insert(parent_rosters, make_pair(parent, make_pair(ros, mm)));
1467 }
1468 }
1469
1470 file_path attr_path = file_path_internal(".mt-attrs");
1471 file_path old_ignore_path = file_path_internal(".mt-ignore");
1472 file_path new_ignore_path = file_path_internal(".mtn-ignore");
1473
1474 roster_t child_roster;
1475 MM(child_roster);
1476 temp_node_id_source nis;
1477
1478 // all rosters shall have a root node.
1479 child_roster.attach_node(child_roster.create_dir_node(nis),
1480 file_path_internal(""));
1481
1482 for (legacy::manifest_map::const_iterator i = old_child_man.begin();
1483 i != old_child_man.end(); ++i)
1484 {
1485 if (i->first == attr_path)
1486 continue;
1487 // convert .mt-ignore to .mtn-ignore... except if .mtn-ignore
1488 // already exists, just leave things alone.
1489 else if (i->first == old_ignore_path
1490 && old_child_man.find(new_ignore_path) == old_child_man.end())
1491 insert_into_roster(child_roster, nis, new_ignore_path, i->second);
1492 else
1493 insert_into_roster(child_roster, nis, i->first, i->second);
1494 }
1495
1496 // migrate attributes out of .mt-attrs
1497 {
1498 legacy::manifest_map::const_iterator i = old_child_man.find(attr_path);
1499 if (i != old_child_man.end())
1500 {
1501 file_data dat;
1502 app.db.get_file_version(i->second, dat);
1503 legacy::dot_mt_attrs_map attrs;
1504 legacy::read_dot_mt_attrs(dat.inner(), attrs);
1505 for (legacy::dot_mt_attrs_map::const_iterator j = attrs.begin();
1506 j != attrs.end(); ++j)
1507 {
1508 if (child_roster.has_node(j->first))
1509 {
1510 map<string, string> const &
1511 fattrs = j->second;
1512 for (map<string, string>::const_iterator
1513 k = fattrs.begin();
1514 k != fattrs.end(); ++k)
1515 {
1516 string key = k->first;
1517 if (app.opts.attrs_to_drop.find(key) != app.opts.attrs_to_drop.end())
1518 {
1519 // ignore it
1520 }
1521 else if (key == "execute" || key == "manual_merge")
1522 child_roster.set_attr(j->first,
1523 attr_key("mtn:" + key),
1524 attr_value(k->second));
1525 else
1526 E(false, F("unknown attribute '%s' on path '%s'\n"
1527 "please contact %s so we can work out the right way to migrate this\n"
1528 "(if you just want it to go away, see the switch --drop-attr, but\n"
1529 "seriously, if you'd like to keep it, we're happy to figure out how)")
1530 % key % j->first % PACKAGE_BUGREPORT);
1531 }
1532 }
1533 }
1534 }
1535 }
1536
1537 // Now knit the parent node IDs into child node IDs (which are currently all
1538 // tmpids), wherever possible.
1539 fixup_node_identities(parent_rosters, child_roster, node_to_renames[child]);
1540
1541 revision_t rev;
1542 rev.made_for = made_for_database;
1543 MM(rev);
1544 calculate_ident(child_roster, rev.new_manifest);
1545
1546 // For each parent, construct an edge in the revision structure by analyzing the
1547 // relationship between the parent roster and the child roster (and placing the
1548 // result in a cset)
1549
1550 for (parent_roster_map::const_iterator i = parent_rosters.begin();
1551 i != parent_rosters.end(); ++i)
1552 {
1553 u64 parent = i->first;
1554 revision_id parent_rid = safe_get(node_to_new_rev, parent);
1555 shared_ptr<roster_t> parent_roster = i->second.first;
1556 shared_ptr<cset> cs = shared_ptr<cset>(new cset());
1557 MM(*cs);
1558 make_cset(*parent_roster, child_roster, *cs);
1559 safe_insert(rev.edges, make_pair(parent_rid, cs));
1560 }
1561
1562 // It is possible that we're at a "root" node here -- a node
1563 // which had no parent in the old rev graph -- in which case we
1564 // synthesize an edge from the empty revision to the current,
1565 // containing a cset which adds all the files in the child.
1566
1567 if (rev.edges.empty())
1568 {
1569 revision_id parent_rid;
1570 shared_ptr<roster_t> parent_roster = shared_ptr<roster_t>(new roster_t());
1571 shared_ptr<cset> cs = shared_ptr<cset>(new cset());
1572 MM(*cs);
1573 make_cset(*parent_roster, child_roster, *cs);
1574 safe_insert(rev.edges, make_pair (parent_rid, cs));
1575
1576 }
1577
1578 // Finally, put all this excitement into the database and save
1579 // the new_rid for use in the cert-writing pass.
1580
1581 revision_id new_rid;
1582 calculate_ident(rev, new_rid);
1583 node_to_new_rev.insert(make_pair(child, new_rid));
1584 new_rev_to_node.insert(make_pair(new_rid, child));
1585
1586 /*
1587 P(F("------------------------------------------------"));
1588 P(F("made revision %s with %d edges, manifest id = %s")
1589 % new_rid % rev.edges.size() % rev.new_manifest);
1590
1591 {
1592 string rtmp;
1593 data dtmp;
1594 dump(dbg, rtmp);
1595 write_revision(rev, dtmp);
1596 P(F("%s") % rtmp);
1597 P(F("%s") % dtmp);
1598 }
1599 P(F("------------------------------------------------"));
1600 */
1601
1602 L(FL("mapped node %d to revision %s") % child % new_rid);
1603 if (app.db.put_revision(new_rid, rev))
1604 ++n_revs_out;
1605
1606 // Mark this child as done, hooray!
1607 safe_insert(done, child);
1608
1609 // Extend the work queue with all the children of this child
1610 pair<ci,ci> grandchild_range = parent_to_child_map.equal_range(child);
1611 for (ci i = grandchild_range.first;
1612 i != grandchild_range.second; ++i)
1613 {
1614 if (i->first != child)
1615 continue;
1616 if (done.find(i->second) == done.end())
1617 work.push_back(i->second);
1618 }
1619 }
1620 }
1621}
1622
1623void
1624build_roster_style_revs_from_manifest_style_revs(app_state & app)
1625{
1626 app.db.ensure_open_for_format_changes();
1627 app.db.check_is_not_rosterified();
1628
1629 real_sanity.set_relaxed(true);
1630 anc_graph graph(true, app);
1631
1632 P(F("converting existing revision graph to new roster-style revisions"));
1633 multimap<revision_id, revision_id> existing_graph;
1634
1635 {
1636 // early short-circuit to avoid failure after lots of work
1637 rsa_keypair_id key;
1638 get_user_key(key,app);
1639 require_password(key, app);
1640 }
1641
1642 // cross-check that we're getting everything
1643 // in fact the code in this function is wrong, because if a revision has no
1644 // parents and no children (it is a root revision, and no children have been
1645 // committed under it), then we will simply drop it!
1646 // This code at least causes this case to throw an assertion; FIXME: make
1647 // this case actually work.
1648 set<revision_id> all_rev_ids;
1649 app.db.get_revision_ids(all_rev_ids);
1650
1651 app.db.get_revision_ancestry(existing_graph);
1652 for (multimap<revision_id, revision_id>::const_iterator i = existing_graph.begin();
1653 i != existing_graph.end(); ++i)
1654 {
1655 // FIXME: insert for the null id as well, and do the same for the
1656 // changesetify code, and then reach rebuild_ancestry how to deal with
1657 // such things. (I guess u64(0) should represent the null parent?)
1658 if (!null_id(i->first))
1659 {
1660 u64 parent_node = graph.add_node_for_oldstyle_revision(i->first);
1661 all_rev_ids.erase(i->first);
1662 u64 child_node = graph.add_node_for_oldstyle_revision(i->second);
1663 all_rev_ids.erase(i->second);
1664 graph.add_node_ancestry(child_node, parent_node);
1665 }
1666 }
1667
1668 for (set<revision_id>::const_iterator i = all_rev_ids.begin();
1669 i != all_rev_ids.end(); ++i)
1670 {
1671 graph.add_node_for_oldstyle_revision(*i);
1672 }
1673
1674 real_sanity.set_relaxed(false);
1675 graph.rebuild_ancestry();
1676}
1677
1678
1679void
1680build_changesets_from_manifest_ancestry(app_state & app)
1681{
1682 app.db.ensure_open_for_format_changes();
1683 app.db.check_is_not_rosterified();
1684
1685 anc_graph graph(false, app);
1686
1687 P(F("rebuilding revision graph from manifest certs"));
1688
1689 {
1690 // early short-circuit to avoid failure after lots of work
1691 rsa_keypair_id key;
1692 get_user_key(key,app);
1693 require_password(key, app);
1694 }
1695
1696 vector< manifest<cert> > tmp;
1697 app.db.get_manifest_certs(cert_name("ancestor"), tmp);
1698 erase_bogus_certs(tmp, app);
1699
1700 for (vector< manifest<cert> >::const_iterator i = tmp.begin();
1701 i != tmp.end(); ++i)
1702 {
1703 cert_value tv;
1704 decode_base64(i->inner().value, tv);
1705 manifest_id child, parent;
1706 child = manifest_id(i->inner().ident);
1707 parent = manifest_id(tv());
1708
1709 u64 parent_node = graph.add_node_for_old_manifest(parent);
1710 u64 child_node = graph.add_node_for_old_manifest(child);
1711 graph.add_node_ancestry(child_node, parent_node);
1712 }
1713
1714 graph.rebuild_ancestry();
1715}
1716
1717
1718// This is a special function solely for the use of regenerate_caches -- it
1719// must work even when caches (especially, the height cache!) do not exist.
1720// For all other purposes, use toposort above.
1721static void
1722allrevs_toposorted(vector<revision_id> & revisions,
1723 app_state & app)
1724{
1725 // get the complete ancestry
1726 rev_ancestry_map graph;
1727 app.db.get_revision_ancestry(graph);
1728 toposort_rev_ancestry(graph, revisions);
1729}
1730
1731void
1732regenerate_caches(app_state & app)
1733{
1734 P(F("regenerating cached rosters and heights"));
1735
1736 app.db.ensure_open_for_format_changes();
1737
1738 transaction_guard guard(app.db);
1739
1740 app.db.delete_existing_rosters();
1741 app.db.delete_existing_heights();
1742
1743 vector<revision_id> sorted_ids;
1744 allrevs_toposorted(sorted_ids, app);
1745
1746 ticker done(_("regenerated"), "r", 5);
1747 done.set_total(sorted_ids.size());
1748
1749 for (std::vector<revision_id>::const_iterator i = sorted_ids.begin();
1750 i != sorted_ids.end(); ++i)
1751 {
1752 revision_t rev;
1753 revision_id const & rev_id = *i;
1754 app.db.get_revision(rev_id, rev);
1755 app.db.put_roster_for_revision(rev_id, rev);
1756 app.db.put_height_for_revision(rev_id, rev);
1757 ++done;
1758 }
1759
1760 guard.commit();
1761
1762 P(F("finished regenerating cached rosters and heights"));
1763}
1764
1765CMD_HIDDEN(rev_height, "rev_height", "", CMD_REF(informative), N_("REV"),
1766 N_("Shows a revision's height"),
1767 "",
1768 options::opts::none)
1769{
1770 if (args.size() != 1)
1771 throw usage(execid);
1772 revision_id rid(idx(args, 0)());
1773 N(app.db.revision_exists(rid), F("No such revision %s") % rid);
1774 rev_height height;
1775 app.db.get_rev_height(rid, height);
1776 P(F("cached height: %s") % height);
1777}
1778
1779// i/o stuff
1780
1781namespace
1782{
1783 namespace syms
1784 {
1785 symbol const format_version("format_version");
1786 symbol const old_revision("old_revision");
1787 symbol const new_manifest("new_manifest");
1788 }
1789}
1790
1791void
1792print_edge(basic_io::printer & printer,
1793 edge_entry const & e)
1794{
1795 basic_io::stanza st;
1796 st.push_hex_pair(syms::old_revision, edge_old_revision(e).inner());
1797 printer.print_stanza(st);
1798 print_cset(printer, edge_changes(e));
1799}
1800
1801static void
1802print_insane_revision(basic_io::printer & printer,
1803 revision_t const & rev)
1804{
1805
1806 basic_io::stanza format_stanza;
1807 format_stanza.push_str_pair(syms::format_version, "1");
1808 printer.print_stanza(format_stanza);
1809
1810 basic_io::stanza manifest_stanza;
1811 manifest_stanza.push_hex_pair(syms::new_manifest, rev.new_manifest.inner());
1812 printer.print_stanza(manifest_stanza);
1813
1814 for (edge_map::const_iterator edge = rev.edges.begin();
1815 edge != rev.edges.end(); ++edge)
1816 print_edge(printer, *edge);
1817}
1818
1819void
1820print_revision(basic_io::printer & printer,
1821 revision_t const & rev)
1822{
1823 rev.check_sane();
1824 print_insane_revision(printer, rev);
1825}
1826
1827
1828void
1829parse_edge(basic_io::parser & parser,
1830 edge_map & es)
1831{
1832 shared_ptr<cset> cs(new cset());
1833 MM(*cs);
1834 manifest_id old_man;
1835 revision_id old_rev;
1836 string tmp;
1837
1838 parser.esym(syms::old_revision);
1839 parser.hex(tmp);
1840 old_rev = revision_id(tmp);
1841
1842 parse_cset(parser, *cs);
1843
1844 es.insert(make_pair(old_rev, cs));
1845}
1846
1847
1848void
1849parse_revision(basic_io::parser & parser,
1850 revision_t & rev)
1851{
1852 MM(rev);
1853 rev.edges.clear();
1854 rev.made_for = made_for_database;
1855 string tmp;
1856 parser.esym(syms::format_version);
1857 parser.str(tmp);
1858 E(tmp == "1",
1859 F("encountered a revision with unknown format, version '%s'\n"
1860 "I only know how to understand the version '1' format\n"
1861 "a newer version of monotone is required to complete this operation")
1862 % tmp);
1863 parser.esym(syms::new_manifest);
1864 parser.hex(tmp);
1865 rev.new_manifest = manifest_id(tmp);
1866 while (parser.symp(syms::old_revision))
1867 parse_edge(parser, rev.edges);
1868 rev.check_sane();
1869}
1870
1871void
1872read_revision(data const & dat,
1873 revision_t & rev)
1874{
1875 MM(rev);
1876 basic_io::input_source src(dat(), "revision");
1877 basic_io::tokenizer tok(src);
1878 basic_io::parser pars(tok);
1879 parse_revision(pars, rev);
1880 I(src.lookahead == EOF);
1881 rev.check_sane();
1882}
1883
1884void
1885read_revision(revision_data const & dat,
1886 revision_t & rev)
1887{
1888 read_revision(dat.inner(), rev);
1889 rev.check_sane();
1890}
1891
1892static void write_insane_revision(revision_t const & rev,
1893 data & dat)
1894{
1895 basic_io::printer pr;
1896 print_insane_revision(pr, rev);
1897 dat = data(pr.buf);
1898}
1899
1900template <> void
1901dump(revision_t const & rev, string & out)
1902{
1903 data dat;
1904 write_insane_revision(rev, dat);
1905 out = dat();
1906}
1907
1908void
1909write_revision(revision_t const & rev,
1910 data & dat)
1911{
1912 rev.check_sane();
1913 write_insane_revision(rev, dat);
1914}
1915
1916void
1917write_revision(revision_t const & rev,
1918 revision_data & dat)
1919{
1920 data d;
1921 write_revision(rev, d);
1922 dat = revision_data(d);
1923}
1924
1925void calculate_ident(revision_t const & cs,
1926 revision_id & ident)
1927{
1928 data tmp;
1929 hexenc<id> tid;
1930 write_revision(cs, tmp);
1931 calculate_ident(tmp, tid);
1932 ident = revision_id(tid);
1933}
1934
1935#ifdef BUILD_UNIT_TESTS
1936#include "unit_tests.hh"
1937#include "sanity.hh"
1938
1939UNIT_TEST(revision, find_old_new_path_for)
1940{
1941 map<file_path, file_path> renames;
1942 file_path foo = file_path_internal("foo");
1943 file_path foo_bar = file_path_internal("foo/bar");
1944 file_path foo_baz = file_path_internal("foo/baz");
1945 file_path quux = file_path_internal("quux");
1946 file_path quux_baz = file_path_internal("quux/baz");
1947 I(foo == find_old_path_for(renames, foo));
1948 I(foo == find_new_path_for(renames, foo));
1949 I(foo_bar == find_old_path_for(renames, foo_bar));
1950 I(foo_bar == find_new_path_for(renames, foo_bar));
1951 I(quux == find_old_path_for(renames, quux));
1952 I(quux == find_new_path_for(renames, quux));
1953 renames.insert(make_pair(foo, quux));
1954 renames.insert(make_pair(foo_bar, foo_baz));
1955 I(quux == find_old_path_for(renames, foo));
1956 I(foo == find_new_path_for(renames, quux));
1957 I(quux_baz == find_old_path_for(renames, foo_baz));
1958 I(foo_baz == find_new_path_for(renames, quux_baz));
1959 I(foo_baz == find_old_path_for(renames, foo_bar));
1960 I(foo_bar == find_new_path_for(renames, foo_baz));
1961}
1962
1963#endif // BUILD_UNIT_TESTS
1964
1965// Local Variables:
1966// mode: C++
1967// fill-column: 76
1968// c-file-style: "gnu"
1969// indent-tabs-mode: nil
1970// End:
1971// 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