-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcmd_db.cc
452 lines (384 loc) · 12.1 KB
/
cmd_db.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.
#include "base.hh"
#include <iostream>
#include <utility>
#include "charset.hh"
#include "cmd.hh"
#include "revision.hh"
#include "constants.hh"
#include "app_state.hh"
#include "database.hh"
#include "project.hh"
#include "keys.hh"
#include "key_store.hh"
#include "work.hh"
#include "rev_height.hh"
#include "transforms.hh"
using std::cin;
using std::cout;
using std::make_pair;
using std::pair;
using std::set;
using std::string;
CMD_GROUP(db, "db", "", CMD_REF(database),
N_("Deals with the database"),
"");
CMD(db_init, "init", "", CMD_REF(db), "",
N_("Initializes a database"),
N_("Creates a new database file and initializes it."),
options::opts::none)
{
N(args.size() == 0,
F("no arguments needed"));
database db(app);
db.initialize();
}
CMD(db_info, "info", "", CMD_REF(db), "",
N_("Shows information about the database"),
"",
options::opts::none)
{
N(args.size() == 0,
F("no arguments needed"));
database db(app);
db.info(cout);
}
CMD(db_version, "version", "", CMD_REF(db), "",
N_("Shows the database's version"),
"",
options::opts::none)
{
N(args.size() == 0,
F("no arguments needed"));
database db(app);
db.version(cout);
}
CMD(db_dump, "dump", "", CMD_REF(db), "",
N_("Dumps the contents of the database"),
N_("Generates a list of SQL instructions that represent the whole "
"contents of the database. The resulting output is useful to later "
"restore the database from a text file that serves as a backup."),
options::opts::none)
{
N(args.size() == 0,
F("no arguments needed"));
database db(app);
db.dump(cout);
}
CMD(db_load, "load", "", CMD_REF(db), "",
N_("Loads the contents of the database"),
N_("Reads a list of SQL instructions that regenerate the contents of "
"the database. This is supposed to be used in conjunction with the "
"output generated by the 'dump' command."),
options::opts::none)
{
N(args.size() == 0,
F("no arguments needed"));
database db(app);
db.load(cin);
}
CMD(db_migrate, "migrate", "", CMD_REF(db), "",
N_("Migrates the database to a newer schema"),
N_("Updates the database's internal schema to the most recent one. "
"Needed to automatically resolve incompatibilities that may be "
"introduced in newer versions of monotone."),
options::opts::none)
{
key_store keys(app);
N(args.size() == 0,
F("no arguments needed"));
database db(app);
db.migrate(keys);
}
CMD(db_execute, "execute", "", CMD_REF(db), "",
N_("Executes an SQL command on the database"),
N_("Directly executes the given SQL command on the database"),
options::opts::none)
{
if (args.size() != 1)
throw usage(execid);
database db(app);
db.debug(idx(args, 0)(), cout);
}
CMD(db_kill_rev_locally, "kill_rev_locally", "", CMD_REF(db), "ID",
N_("Kills a revision from the local database"),
"",
options::opts::none)
{
if (args.size() != 1)
throw usage(execid);
revision_id revid;
database db(app);
project_t project(db);
complete(app.opts, app.lua, project, idx(args, 0)(), revid);
// Check that the revision does not have any children
std::set<revision_id> children;
db.get_revision_children(revid, children);
N(!children.size(),
F("revision %s already has children. We cannot kill it.")
% revid);
// If we're executing this in a workspace, check if the workspace parent
// revision is the one to kill. If so, write out the changes made in this
// particular revision to _MTN/revision to allow the user redo his (fixed)
// commit afterwards. Of course we can't do this at all if
//
// a) the user is currently not inside a workspace
// b) the user has updated the current workspace to another revision already
// thus the working revision is no longer based on the revision we're
// trying to kill
// c) there are uncomitted changes in the working revision of this workspace.
// this *eventually* could be handled with a workspace merge scenario, but
// is left out for now
if (workspace::found)
{
workspace work(app);
revision_t old_work_rev;
work.get_work_rev(old_work_rev);
for (edge_map::const_iterator i = old_work_rev.edges.begin();
i != old_work_rev.edges.end(); i++)
{
if (edge_old_revision(i) != revid)
continue;
N(!work.has_changes(db),
F("Cannot kill revision %s,\n"
"because it would leave the current workspace in an invalid\n"
"state, from which monotone cannot recover automatically since\n"
"the workspace contains uncommitted changes.\n"
"Consider updating your workspace to another revision first,\n"
"before you try to kill this revision again.")
% revid);
P(F("applying changes from %s on the current workspace")
% revid);
revision_t new_work_rev;
db.get_revision(revid, new_work_rev);
new_work_rev.made_for = made_for_workspace;
work.put_work_rev(new_work_rev);
work.maybe_update_inodeprints(db);
// extra paranoia... we _should_ never run this section twice
// since a merged workspace would fail early with work.has_changes()
break;
}
}
db.delete_existing_rev_and_certs(revid);
}
CMD(db_kill_branch_certs_locally, "kill_branch_certs_locally", "", CMD_REF(db),
"BRANCH",
N_("Kills branch certificates from the local database"),
"",
options::opts::none)
{
if (args.size() != 1)
throw usage(execid);
database db(app);
db.delete_branch_named(cert_value(idx(args, 0)()));
}
CMD(db_kill_tag_locally, "kill_tag_locally", "", CMD_REF(db), "TAG",
N_("Kills a tag from the local database"),
"",
options::opts::none)
{
if (args.size() != 1)
throw usage(execid);
database db(app);
db.delete_tag_named(cert_value(idx(args, 0)()));
}
CMD(db_check, "check", "", CMD_REF(db), "",
N_("Does some sanity checks on the database"),
N_("Ensures that the database is consistent by issuing multiple "
"checks."),
options::opts::none)
{
N(args.size() == 0,
F("no arguments needed"));
database db(app);
check_db(db);
}
CMD(db_changesetify, "changesetify", "", CMD_REF(db), "",
N_("Converts the database to the changeset format"),
"",
options::opts::none)
{
database db(app);
key_store keys(app);
N(args.size() == 0,
F("no arguments needed"));
db.ensure_open_for_format_changes();
db.check_is_not_rosterified();
// early short-circuit to avoid failure after lots of work
cache_user_key(app.opts, app.lua, db, keys);
build_changesets_from_manifest_ancestry(db, keys, set<string>());
}
CMD(db_rosterify, "rosterify", "", CMD_REF(db), "",
N_("Converts the database to the rosters format"),
"",
options::opts::drop_attr)
{
database db(app);
key_store keys(app);
N(args.size() == 0,
F("no arguments needed"));
db.ensure_open_for_format_changes();
db.check_is_not_rosterified();
// early short-circuit to avoid failure after lots of work
cache_user_key(app.opts, app.lua, db, keys);
build_roster_style_revs_from_manifest_style_revs(db, keys,
app.opts.attrs_to_drop);
}
CMD(db_regenerate_caches, "regenerate_caches", "", CMD_REF(db), "",
N_("Regenerates the caches stored in the database"),
"",
options::opts::none)
{
N(args.size() == 0,
F("no arguments needed"));
database db(app);
regenerate_caches(db);
}
CMD_HIDDEN(clear_epoch, "clear_epoch", "", CMD_REF(db), "BRANCH",
N_("Clears the branch's epoch"),
"",
options::opts::none)
{
if (args.size() != 1)
throw usage(execid);
database db(app);
db.clear_epoch(branch_name(idx(args, 0)()));
}
CMD(db_set_epoch, "set_epoch", "", CMD_REF(db), "BRANCH EPOCH",
N_("Sets the branch's epoch"),
"",
options::opts::none)
{
if (args.size() != 2)
throw usage(execid);
N(idx(args, 1)().size() == constants::epochlen,
F("The epoch must be %s characters") % constants::epochlen);
epoch_data ed(decode_hexenc(idx(args, 1)()));
database db(app);
db.set_epoch(branch_name(idx(args, 0)()), ed);
}
CMD(set, "set", "", CMD_REF(variables), N_("DOMAIN NAME VALUE"),
N_("Sets a database variable"),
N_("This command modifies (or adds if it did not exist before) the "
"variable named NAME, stored in the database, and sets it to the "
"given value in VALUE. The variable is placed in the domain DOMAIN."),
options::opts::none)
{
if (args.size() != 3)
throw usage(execid);
var_domain d;
var_name n;
var_value v;
internalize_var_domain(idx(args, 0), d);
n = var_name(idx(args, 1)());
v = var_value(idx(args, 2)());
database db(app);
db.set_var(make_pair(d, n), v);
}
CMD(unset, "unset", "", CMD_REF(variables), N_("DOMAIN NAME"),
N_("Unsets a database variable"),
N_("This command removes the variable NAME from domain DOMAIN, which "
"was previously stored in the database."),
options::opts::none)
{
if (args.size() != 2)
throw usage(execid);
var_domain d;
var_name n;
internalize_var_domain(idx(args, 0), d);
n = var_name(idx(args, 1)());
var_key k(d, n);
database db(app);
N(db.var_exists(k),
F("no var with name %s in domain %s") % n % d);
db.clear_var(k);
}
CMD(complete, "complete", "", CMD_REF(informative),
N_("(revision|file|key) PARTIAL-ID"),
N_("Completes a partial identifier"),
"",
options::opts::verbose)
{
if (args.size() != 2)
throw usage(execid);
database db(app);
project_t project(db);
bool verbose = app.opts.verbose;
N(idx(args, 1)().find_first_not_of("abcdef0123456789") == string::npos,
F("non-hex digits in partial id"));
if (idx(args, 0)() == "revision")
{
set<revision_id> completions;
db.complete(idx(args, 1)(), completions);
for (set<revision_id>::const_iterator i = completions.begin();
i != completions.end(); ++i)
{
if (!verbose) cout << *i << '\n';
else cout << describe_revision(project, *i) << '\n';
}
}
else if (idx(args, 0)() == "file")
{
set<file_id> completions;
db.complete(idx(args, 1)(), completions);
for (set<file_id>::const_iterator i = completions.begin();
i != completions.end(); ++i)
cout << *i << '\n';
}
else if (idx(args, 0)() == "key")
{
typedef set< pair<key_id, utf8 > > completions_t;
completions_t completions;
db.complete(idx(args, 1)(), completions);
for (completions_t::const_iterator i = completions.begin();
i != completions.end(); ++i)
{
cout << i->first;
if (verbose) cout << ' ' << i->second();
cout << '\n';
}
}
else
throw usage(execid);
}
CMD_HIDDEN(test_migration_step, "test_migration_step", "", CMD_REF(db),
"SCHEMA",
N_("Runs one step of migration on the specified database"),
N_("This command migrates the given database from the specified "
"schema in SCHEMA to its successor."),
options::opts::none)
{
database db(app);
key_store keys(app);
if (args.size() != 1)
throw usage(execid);
db.test_migration_step(keys, idx(args,0)());
}
CMD_HIDDEN(rev_height, "rev_height", "", CMD_REF(informative), N_("REV"),
N_("Shows a revision's height"),
"",
options::opts::none)
{
if (args.size() != 1)
throw usage(execid);
revision_id rid(idx(args, 0)());
database db(app);
N(db.revision_exists(rid), F("no such revision '%s'") % rid);
rev_height height;
db.get_rev_height(rid, height);
P(F("cached height: %s") % height);
}
// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: