monotone

monotone Mtn Source Tree

Root/update.cc

1// Copyright (C) 2002 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 <set>
12#include <map>
13#include "vector.hh"
14#include "lexical_cast.hh"
15
16#include "app_state.hh"
17#include "database.hh"
18#include "sanity.hh"
19#include "cert.hh"
20#include "transforms.hh"
21#include "ui.hh"
22#include "update.hh"
23#include "vocab.hh"
24#include "revision.hh"
25
26// these functions just encapsulate the (somewhat complex) logic behind
27// picking an update target. the actual updating takes place in
28// commands.cc, along with most other file-modifying actions.
29
30// the algorithm is:
31// - do a depth-first traversal of the current revision's descendent set
32// - for each revision, check to see whether it is
33// - in the correct branch
34// - has acceptable test results
35// and add it to the candidate set if so
36// - this gives a set containing every descendent that we might want to
37// update to.
38// - run erase_ancestors on that set, to get just the heads
39// - If there are any non-suspended revisions in the set, then remove the
40// suspended revisions.
41// the idea is that this should be correct even in the presence of
42// discontinuous branches, test results that go from good to bad to good to
43// bad to good, etc.
44// it may be somewhat inefficient to use erase_ancestors here, but deal with
45// that when and if the time comes...
46
47using std::make_pair;
48using std::map;
49using std::set;
50using std::vector;
51
52using boost::lexical_cast;
53
54static void
55get_test_results_for_revision(revision_id const & id,
56 map<rsa_keypair_id, bool> & results,
57 app_state & app)
58{
59 vector< revision<cert> > certs;
60 app.get_project().get_revision_certs_by_name(id, cert_name(testresult_cert_name), certs);
61 for (vector< revision<cert> >::const_iterator i = certs.begin();
62 i != certs.end(); ++i)
63 {
64 cert_value cv;
65 decode_base64(i->inner().value, cv);
66 try
67 {
68 bool test_ok = lexical_cast<bool>(cv());
69 results.insert(make_pair(i->inner().key, test_ok));
70 }
71 catch(boost::bad_lexical_cast &)
72 {
73 W(F("failed to decode boolean testresult cert value '%s'") % cv);
74 }
75 }
76}
77
78static bool
79acceptable_descendent(branch_name const & branch,
80 revision_id const & base,
81 map<rsa_keypair_id, bool> & base_results,
82 revision_id const & target,
83 app_state & app)
84{
85 L(FL("Considering update target %s") % target);
86
87 // step 1: check the branch
88 if (!app.get_project().revision_is_in_branch(target, branch))
89 {
90 L(FL("%s not in branch %s") % target % branch);
91 return false;
92 }
93
94 // step 2: check the testresults
95 map<rsa_keypair_id, bool> target_results;
96 get_test_results_for_revision(target, target_results, app);
97 if (app.lua.hook_accept_testresult_change(base_results, target_results))
98 {
99 L(FL("%s is acceptable update candidate") % target);
100 return true;
101 }
102 else
103 {
104 L(FL("%s has unacceptable test results") % target);
105 return false;
106 }
107}
108
109static void
110calculate_update_set(revision_id const & base,
111 branch_name const & branch,
112 app_state & app,
113 set<revision_id> & candidates)
114{
115 map<rsa_keypair_id, bool> base_results;
116 get_test_results_for_revision(base, base_results, app);
117
118 candidates.clear();
119 // we possibly insert base into the candidate set as well; returning a set
120 // containing just it means that we are up to date; returning an empty set
121 // means that there is no acceptable update.
122 if (acceptable_descendent(branch, base, base_results, base, app))
123 candidates.insert(base);
124
125 // keep a visited set to avoid repeating work
126 set<revision_id> visited;
127 set<revision_id> children;
128 vector<revision_id> to_traverse;
129
130 app.db.get_revision_children(base, children);
131 copy(children.begin(), children.end(), back_inserter(to_traverse));
132
133 while (!to_traverse.empty())
134 {
135 revision_id target = to_traverse.back();
136 to_traverse.pop_back();
137
138 // If we've traversed this id before via a different path, skip it.
139 if (visited.find(target) != visited.end())
140 continue;
141 visited.insert(target);
142
143 // then, possibly insert this revision as a candidate
144 if (acceptable_descendent(branch, base, base_results, target, app))
145 candidates.insert(target);
146
147 // and traverse its children as well
148 app.db.get_revision_children(target, children);
149 copy(children.begin(), children.end(), back_inserter(to_traverse));
150 }
151
152 erase_ancestors(candidates, app);
153
154 if (app.opts.ignore_suspend_certs)
155 return;
156
157 set<revision_id> active_candidates;
158 for (set<revision_id>::const_iterator i = candidates.begin();
159 i != candidates.end(); i++)
160 if (!app.get_project().revision_is_suspended_in_branch(*i, branch))
161 safe_insert(active_candidates, *i);
162
163 if (!active_candidates.empty())
164 candidates = active_candidates;
165}
166
167void pick_update_candidates(revision_id const & base_ident,
168 app_state & app,
169 set<revision_id> & candidates)
170{
171 N(app.opts.branchname() != "",
172 F("cannot determine branch for update"));
173 I(!null_id(base_ident));
174
175 calculate_update_set(base_ident, app.opts.branchname,
176 app, candidates);
177}
178
179
180
181// Local Variables:
182// mode: C++
183// fill-column: 76
184// c-file-style: "gnu"
185// indent-tabs-mode: nil
186// End:
187// 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