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 "set_epoch BRANCH EPOCH\n"),
61 N_("manipulate database state"),
62 OPT_DROP_ATTR)
63{
64 if (args.size() == 1)
65 {
66 if (idx(args, 0)() == "init")
67 app.db.initialize();
68 else if (idx(args, 0)() == "info")
69 app.db.info(cout);
70 else if (idx(args, 0)() == "version")
71 app.db.version(cout);
72 else if (idx(args, 0)() == "dump")
73 app.db.dump(cout);
74 else if (idx(args, 0)() == "load")
75 app.db.load(cin);
76 else if (idx(args, 0)() == "migrate")
77 app.db.migrate();
78 else if (idx(args, 0)() == "check")
79 check_db(app);
80 else if (idx(args, 0)() == "changesetify")
81 build_changesets_from_manifest_ancestry(app);
82 else if (idx(args, 0)() == "rosterify")
83 build_roster_style_revs_from_manifest_style_revs(app);
84 else
85 throw usage(name);
86 }
87 else if (args.size() == 2)
88 {
89 if (idx(args, 0)() == "execute")
90 app.db.debug(idx(args, 1)(), cout);
91 else if (idx(args, 0)() == "kill_rev_locally")
92 kill_rev_locally(app,idx(args, 1)());
93 else if (idx(args, 0)() == "clear_epoch")
94 app.db.clear_epoch(cert_value(idx(args, 1)()));
95 else if (idx(args, 0)() == "kill_branch_certs_locally")
96 app.db.delete_branch_named(cert_value(idx(args, 1)()));
97 else if (idx(args, 0)() == "kill_tag_locally")
98 app.db.delete_tag_named(cert_value(idx(args, 1)()));
99 else
100 throw usage(name);
101 }
102 else if (args.size() == 3)
103 {
104 if (idx(args, 0)() == "set_epoch")
105 {
106 epoch_data ed(idx(args,2)());
107 N(ed.inner()().size() == constants::epochlen,
108 F("The epoch must be %s characters")
109 % constants::epochlen);
110 app.db.set_epoch(cert_value(idx(args, 1)()), ed);
111 }
112 else
113 throw usage(name);
114 }
115 else
116 throw usage(name);
117}
118
119CMD(set, N_("vars"), N_("DOMAIN NAME VALUE"),
120 N_("set the database variable NAME to VALUE, in domain DOMAIN"),
121 OPT_NONE)
122{
123 if (args.size() != 3)
124 throw usage(name);
125
126 var_domain d;
127 var_name n;
128 var_value v;
129 internalize_var_domain(idx(args, 0), d);
130 n = var_name(idx(args, 1)());
131 v = var_value(idx(args, 2)());
132 app.db.set_var(make_pair(d, n), v);
133}
134
135CMD(unset, N_("vars"), N_("DOMAIN NAME"),
136 N_("remove the database variable NAME in domain DOMAIN"),
137 OPT_NONE)
138{
139 if (args.size() != 2)
140 throw usage(name);
141
142 var_domain d;
143 var_name n;
144 internalize_var_domain(idx(args, 0), d);
145 n = var_name(idx(args, 1)());
146 var_key k(d, n);
147 N(app.db.var_exists(k),
148 F("no var with name %s in domain %s") % n % d);
149 app.db.clear_var(k);
150}
151
152CMD(complete, N_("informative"), N_("(revision|file|key) PARTIAL-ID"),
153 N_("complete partial id"),
154 OPT_VERBOSE)
155{
156 if (args.size() != 2)
157 throw usage(name);
158
159 bool verbose = app.verbose;
160
161 N(idx(args, 1)().find_first_not_of("abcdef0123456789") == string::npos,
162 F("non-hex digits in partial id"));
163
164 if (idx(args, 0)() == "revision")
165 {
166 set<revision_id> completions;
167 app.db.complete(idx(args, 1)(), completions);
168 for (set<revision_id>::const_iterator i = completions.begin();
169 i != completions.end(); ++i)
170 {
171 if (!verbose) cout << i->inner()() << "\n";
172 else cout << describe_revision(app, i->inner()) << "\n";
173 }
174 }
175 else if (idx(args, 0)() == "file")
176 {
177 set<file_id> completions;
178 app.db.complete(idx(args, 1)(), completions);
179 for (set<file_id>::const_iterator i = completions.begin();
180 i != completions.end(); ++i)
181 cout << i->inner()() << "\n";
182 }
183 else if (idx(args, 0)() == "key")
184 {
185 typedef set< pair<key_id, utf8 > > completions_t;
186 completions_t completions;
187 app.db.complete(idx(args, 1)(), completions);
188 for (completions_t::const_iterator i = completions.begin();
189 i != completions.end(); ++i)
190 {
191 cout << i->first.inner()();
192 if (verbose) cout << " " << i->second();
193 cout << "\n";
194 }
195 }
196 else
197 throw usage(name);
198}
199
200// Local Variables:
201// mode: C++
202// fill-column: 76
203// c-file-style: "gnu"
204// indent-tabs-mode: nil
205// End:
206// 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