monotone

monotone Mtn Source Tree

Root/cmd_db.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 <iostream>
11#include <utility>
12
13#include "charset.hh"
14#include "cmd.hh"
15#include "database_check.hh"
16#include "revision.hh"
17
18using std::cin;
19using std::cout;
20using std::make_pair;
21using std::pair;
22using std::set;
23using std::string;
24
25// Deletes a revision from the local database. This can be used to
26// 'undo' a changed revision from a local database without leaving
27// (much of) a trace.
28
29static void
30kill_rev_locally(app_state& app, string const& id)
31{
32 revision_id ident;
33 complete(app, id, ident);
34 N(app.db.revision_exists(ident),
35 F("no such revision '%s'") % ident);
36
37 //check that the revision does not have any children
38 set<revision_id> children;
39 app.db.get_revision_children(ident, children);
40 N(!children.size(),
41 F("revision %s already has children. We cannot kill it.") % ident);
42
43 app.db.delete_existing_rev_and_certs(ident);
44}
45
46CMD(db, N_("database"),
47 N_("init\n"
48 "info\n"
49 "version\n"
50 "dump\n"
51 "load\n"
52 "migrate\n"
53 "execute\n"
54 "kill_rev_locally ID\n"
55 "kill_branch_certs_locally BRANCH\n"
56 "kill_tag_locally TAG\n"
57 "check\n"
58 "changesetify\n"
59 "rosterify\n"
60 "regenerate_caches\n"
61 "set_epoch BRANCH EPOCH\n"),
62 N_("manipulate database state"),
63 options::opts::drop_attr)
64{
65 if (args.size() == 1)
66 {
67 if (idx(args, 0)() == "init")
68 app.db.initialize();
69 else if (idx(args, 0)() == "info")
70 app.db.info(cout);
71 else if (idx(args, 0)() == "version")
72 app.db.version(cout);
73 else if (idx(args, 0)() == "dump")
74 app.db.dump(cout);
75 else if (idx(args, 0)() == "load")
76 app.db.load(cin);
77 else if (idx(args, 0)() == "migrate")
78 app.db.migrate();
79 else if (idx(args, 0)() == "check")
80 check_db(app);
81 else if (idx(args, 0)() == "changesetify")
82 build_changesets_from_manifest_ancestry(app);
83 else if (idx(args, 0)() == "rosterify")
84 build_roster_style_revs_from_manifest_style_revs(app);
85 else if (idx(args, 0)() == "regenerate_caches")
86 regenerate_caches(app);
87 else
88 throw usage(name);
89 }
90 else if (args.size() == 2)
91 {
92 if (idx(args, 0)() == "execute")
93 app.db.debug(idx(args, 1)(), cout);
94 else if (idx(args, 0)() == "kill_rev_locally")
95 kill_rev_locally(app,idx(args, 1)());
96 else if (idx(args, 0)() == "clear_epoch")
97 app.db.clear_epoch(branch_name(idx(args, 1)()));
98 else if (idx(args, 0)() == "kill_branch_certs_locally")
99 app.db.delete_branch_named(cert_value(idx(args, 1)()));
100 else if (idx(args, 0)() == "kill_tag_locally")
101 app.db.delete_tag_named(cert_value(idx(args, 1)()));
102 else
103 throw usage(name);
104 }
105 else if (args.size() == 3)
106 {
107 if (idx(args, 0)() == "set_epoch")
108 {
109 epoch_data ed(idx(args,2)());
110 N(ed.inner()().size() == constants::epochlen,
111 F("The epoch must be %s characters")
112 % constants::epochlen);
113 app.db.set_epoch(branch_name(idx(args, 1)()), ed);
114 }
115 else
116 throw usage(name);
117 }
118 else
119 throw usage(name);
120}
121
122CMD(set, N_("vars"), N_("DOMAIN NAME VALUE"),
123 N_("set the database variable NAME to VALUE, in domain DOMAIN"),
124 options::opts::none)
125{
126 if (args.size() != 3)
127 throw usage(name);
128
129 var_domain d;
130 var_name n;
131 var_value v;
132 internalize_var_domain(idx(args, 0), d);
133 n = var_name(idx(args, 1)());
134 v = var_value(idx(args, 2)());
135 app.db.set_var(make_pair(d, n), v);
136}
137
138CMD(unset, N_("vars"), N_("DOMAIN NAME"),
139 N_("remove the database variable NAME in domain DOMAIN"),
140 options::opts::none)
141{
142 if (args.size() != 2)
143 throw usage(name);
144
145 var_domain d;
146 var_name n;
147 internalize_var_domain(idx(args, 0), d);
148 n = var_name(idx(args, 1)());
149 var_key k(d, n);
150 N(app.db.var_exists(k),
151 F("no var with name %s in domain %s") % n % d);
152 app.db.clear_var(k);
153}
154
155CMD(complete, N_("informative"), N_("(revision|file|key) PARTIAL-ID"),
156 N_("complete partial id"),
157 options::opts::verbose)
158{
159 if (args.size() != 2)
160 throw usage(name);
161
162 bool verbose = app.opts.verbose;
163
164 N(idx(args, 1)().find_first_not_of("abcdef0123456789") == string::npos,
165 F("non-hex digits in partial id"));
166
167 if (idx(args, 0)() == "revision")
168 {
169 set<revision_id> completions;
170 app.db.complete(idx(args, 1)(), completions);
171 for (set<revision_id>::const_iterator i = completions.begin();
172 i != completions.end(); ++i)
173 {
174 if (!verbose) cout << i->inner()() << '\n';
175 else cout << describe_revision(app, *i) << '\n';
176 }
177 }
178 else if (idx(args, 0)() == "file")
179 {
180 set<file_id> completions;
181 app.db.complete(idx(args, 1)(), completions);
182 for (set<file_id>::const_iterator i = completions.begin();
183 i != completions.end(); ++i)
184 cout << i->inner()() << '\n';
185 }
186 else if (idx(args, 0)() == "key")
187 {
188 typedef set< pair<key_id, utf8 > > completions_t;
189 completions_t completions;
190 app.db.complete(idx(args, 1)(), completions);
191 for (completions_t::const_iterator i = completions.begin();
192 i != completions.end(); ++i)
193 {
194 cout << i->first.inner()();
195 if (verbose) cout << ' ' << i->second();
196 cout << '\n';
197 }
198 }
199 else
200 throw usage(name);
201}
202
203CMD(test_migration_step, hidden_group(), "SCHEMA",
204 "run one step of migration - from SCHEMA to its successor -\n"
205 "on the specified database", options::opts::none)
206{
207 if (args.size() != 1)
208 throw usage(name);
209 app.db.test_migration_step(idx(args,0)());
210}
211
212// Local Variables:
213// mode: C++
214// fill-column: 76
215// c-file-style: "gnu"
216// indent-tabs-mode: nil
217// End:
218// 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