-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcmd_ws_commit.cc
1499 lines (1256 loc) · 47.3 KB
/
cmd_ws_commit.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <map>
#include "cmd.hh"
#include "diff_patch.hh"
#include "file_io.hh"
#include "restrictions.hh"
#include "revision.hh"
#include "transforms.hh"
#include "work.hh"
#include "charset.hh"
#include "ui.hh"
#include "app_state.hh"
#include "project.hh"
#include "basic_io.hh"
#include "xdelta.hh"
#include "keys.hh"
#include "key_store.hh"
#include "simplestring_xform.hh"
#include "database.hh"
#include "roster.hh"
using std::cout;
using std::make_pair;
using std::pair;
using std::make_pair;
using std::map;
using std::set;
using std::string;
using std::vector;
using boost::shared_ptr;
static void
revision_summary(revision_t const & rev, branch_name const & branch, utf8 & summary)
{
string out;
// We intentionally do not collapse the final \n into the format
// strings here, for consistency with newline conventions used by most
// other format strings.
out += (F("Current branch: %s") % branch).str() += '\n';
for (edge_map::const_iterator i = rev.edges.begin(); i != rev.edges.end(); ++i)
{
revision_id parent = edge_old_revision(*i);
// A colon at the end of this string looked nicer, but it made
// double-click copying from terminals annoying.
out += (F("Changes against parent %s") % parent).str() += '\n';
cset const & cs = edge_changes(*i);
if (cs.empty())
out += F(" no changes").str() += '\n';
for (set<file_path>::const_iterator i = cs.nodes_deleted.begin();
i != cs.nodes_deleted.end(); ++i)
out += (F(" dropped %s") % *i).str() += '\n';
for (map<file_path, file_path>::const_iterator
i = cs.nodes_renamed.begin();
i != cs.nodes_renamed.end(); ++i)
out += (F(" renamed %s\n"
" to %s") % i->first % i->second).str() += '\n';
for (set<file_path>::const_iterator i = cs.dirs_added.begin();
i != cs.dirs_added.end(); ++i)
out += (F(" added %s") % *i).str() += '\n';
for (map<file_path, file_id>::const_iterator i = cs.files_added.begin();
i != cs.files_added.end(); ++i)
out += (F(" added %s") % i->first).str() += '\n';
for (map<file_path, pair<file_id, file_id> >::const_iterator
i = cs.deltas_applied.begin(); i != cs.deltas_applied.end(); ++i)
out += (F(" patched %s") % (i->first)).str() += '\n';
for (map<pair<file_path, attr_key>, attr_value >::const_iterator
i = cs.attrs_set.begin(); i != cs.attrs_set.end(); ++i)
out += (F(" attr on %s\n"
" attr %s\n"
" value %s")
% (i->first.first) % (i->first.second) % (i->second)
).str() += "\n";
for (set<pair<file_path, attr_key> >::const_iterator
i = cs.attrs_cleared.begin(); i != cs.attrs_cleared.end(); ++i)
out += (F(" unset on %s\n"
" attr %s")
% (i->first) % (i->second)).str() += "\n";
}
summary = utf8(out);
}
static void
get_log_message_interactively(lua_hooks & lua, workspace & work,
revision_t const & cs,
branch_name const & branchname,
utf8 & log_message)
{
utf8 summary;
revision_summary(cs, branchname, summary);
external summary_external;
utf8_to_system_best_effort(summary, summary_external);
utf8 branch_comment = utf8((F("branch \"%s\"\n\n") % branchname).str());
external branch_external;
utf8_to_system_best_effort(branch_comment, branch_external);
string magic_line = _("*****DELETE THIS LINE TO CONFIRM YOUR COMMIT*****");
string commentary_str;
commentary_str += string(70, '-') + "\n";
commentary_str += _("Enter a description of this change.\n"
"Lines beginning with `MTN:' "
"are removed automatically.");
commentary_str += "\n\n";
commentary_str += summary_external();
commentary_str += string(70, '-') + "\n";
external commentary(commentary_str);
utf8 user_log_message;
work.read_user_log(user_log_message);
//if the _MTN/log file was non-empty, we'll append the 'magic' line
utf8 user_log;
if (user_log_message().length() > 0)
user_log = utf8( magic_line + "\n" + user_log_message());
else
user_log = user_log_message;
external user_log_message_external;
utf8_to_system_best_effort(user_log, user_log_message_external);
external log_message_external;
N(lua.hook_edit_comment(commentary, user_log_message_external,
log_message_external),
F("edit of log message failed"));
N(log_message_external().find(magic_line) == string::npos,
F("failed to remove magic line; commit cancelled"));
system_to_utf8(log_message_external, log_message);
}
CMD(revert, "revert", "", CMD_REF(workspace), N_("[PATH]..."),
N_("Reverts files and/or directories"),
N_("In order to revert the entire workspace, specify \".\" as the "
"file name."),
options::opts::depth | options::opts::exclude | options::opts::missing)
{
roster_t old_roster, new_roster;
cset preserved;
N(app.opts.missing || !args.empty() || !app.opts.exclude_patterns.empty(),
F("you must pass at least one path to 'revert' (perhaps '.')"));
database db(app);
workspace work(app);
parent_map parents;
work.get_parent_rosters(db, parents);
N(parents.size() == 1,
F("this command can only be used in a single-parent workspace"));
old_roster = parent_roster(parents.begin());
{
temp_node_id_source nis;
work.get_current_roster_shape(db, nis, new_roster);
}
node_restriction mask(work, args_to_paths(args),
args_to_paths(app.opts.exclude_patterns),
app.opts.depth,
old_roster, new_roster);
if (app.opts.missing)
{
// --missing is a further filter on the files included by a
// restriction we first find all missing files included by the
// specified args and then make a restriction that includes only
// these missing files.
set<file_path> missing;
work.find_missing(new_roster, mask, missing);
if (missing.empty())
{
P(F("no missing files to revert"));
return;
}
std::vector<file_path> missing_files;
for (set<file_path>::const_iterator i = missing.begin();
i != missing.end(); i++)
{
L(FL("reverting missing file: %s") % *i);
missing_files.push_back(*i);
}
// replace the original mask with a more restricted one
mask = node_restriction(work, missing_files,
std::vector<file_path>(), app.opts.depth,
old_roster, new_roster);
}
// We want the restricted roster to include all the changes
// that are to be *kept*. Then, the changes to revert are those
// from the new roster *back* to the restricted roster
roster_t restricted_roster;
make_restricted_roster(new_roster, old_roster, restricted_roster,
mask);
make_cset(old_roster, restricted_roster, preserved);
// The preserved cset will be left pending in MTN/revision
// if/when reverting through the editable_tree interface use
// make_cset(new_roster, restricted_roster, reverted);
// to get a cset that gets us back to the restricted roster
// from the current workspace roster
// the intermediate paths record the paths of all directory nodes
// paths we reverted on the fly for descendant nodes below them.
// if a children of such a directory node should be recreated, we use
// this recorded path here instead of just
// a) the node's old name, which could eventually be wrong if the parent
// path is a rename_target (i.e. a new path), see the
// "revert_drop_not_rename" test
// b) the parent node's new name + the basename of the old name,
// which may be wrong as well in case of a more complex pivot_rename
std::map<node_id, file_path> intermediate_paths;
node_map const & nodes = old_roster.all_nodes();
for (node_map::const_iterator i = nodes.begin();
i != nodes.end(); ++i)
{
node_id nid = i->first;
node_t node = i->second;
if (old_roster.is_root(nid))
continue;
if (!mask.includes(old_roster, nid))
continue;
file_path old_path, new_path, old_parent, new_parent;;
path_component base;
old_roster.get_name(nid, old_path);
old_path.dirname_basename(old_parent, base);
// if we recorded the parent node in this rename already
// use the intermediate path (i.e. the new new_path after this
// action) as target path for the reverted item)
const std::map<node_id, file_path>::iterator it =
intermediate_paths.find(node->parent);
if (it != intermediate_paths.end())
{
new_path = it->second / base;
}
else
{
if (old_roster.is_root(node->parent))
{
new_path = file_path() / base;
}
else
{
new_roster.get_name(node->parent, new_parent);
new_path = new_parent / base;
}
}
if (is_file_t(node))
{
file_t f = downcast_to_file_t(node);
if (file_exists(new_path))
{
file_id ident;
calculate_ident(new_path, ident);
// don't touch unchanged files
if (ident == f->content)
continue;
else
L(FL("skipping unchanged %s") % new_path);
}
P(F("reverting %s") % new_path);
L(FL("reverting %s to [%s]") % new_path
% f->content);
N(db.file_version_exists(f->content),
F("no file version %s found in database for %s")
% f->content % new_path);
file_data dat;
L(FL("writing file %s to %s")
% f->content % new_path);
db.get_file_version(f->content, dat);
write_data(new_path, dat.inner());
}
else
{
intermediate_paths.insert(std::pair<node_id, file_path>(nid, new_path));
if (!directory_exists(new_path))
{
P(F("recreating %s/") % new_path);
mkdir_p(new_path);
}
else
{
L(FL("skipping existing %s/") % new_path);
}
}
}
// Included_work is thrown away which effectively reverts any adds,
// drops and renames it contains. Drops and rename sources will have
// been rewritten above but this may leave rename targets laying
// around.
revision_t remaining;
make_revision_for_workspace(parent_id(parents.begin()), preserved, remaining);
// Race.
work.put_work_rev(remaining);
work.update_any_attrs(db);
work.maybe_update_inodeprints(db);
}
CMD(disapprove, "disapprove", "", CMD_REF(review), N_("REVISION"),
N_("Disapproves a particular revision"),
"",
options::opts::branch | options::opts::messages | options::opts::date |
options::opts::author)
{
database db(app);
key_store keys(app);
project_t project(db);
if (args.size() != 1)
throw usage(execid);
utf8 log_message("");
bool log_message_given;
revision_id r;
revision_t rev, rev_inverse;
shared_ptr<cset> cs_inverse(new cset());
complete(app.opts, app.lua, project, idx(args, 0)(), r);
db.get_revision(r, rev);
N(rev.edges.size() == 1,
F("revision %s has %d changesets, cannot invert")
% r % rev.edges.size());
guess_branch(app.opts, project, r);
N(app.opts.branchname() != "", F("need --branch argument for disapproval"));
process_commit_message_args(app.opts, log_message_given, log_message,
utf8((FL("disapproval of revision '%s'")
% r).str()));
cache_user_key(app.opts, app.lua, db, keys);
edge_entry const & old_edge (*rev.edges.begin());
db.get_revision_manifest(edge_old_revision(old_edge),
rev_inverse.new_manifest);
{
roster_t old_roster, new_roster;
db.get_roster(edge_old_revision(old_edge), old_roster);
db.get_roster(r, new_roster);
make_cset(new_roster, old_roster, *cs_inverse);
}
rev_inverse.edges.insert(make_pair(r, cs_inverse));
{
transaction_guard guard(db);
revision_id inv_id;
revision_data rdat;
write_revision(rev_inverse, rdat);
calculate_ident(rdat, inv_id);
db.put_revision(inv_id, rdat);
project.put_standard_certs_from_options(app.opts, app.lua, keys,
inv_id, app.opts.branchname,
log_message);
guard.commit();
}
}
CMD(mkdir, "mkdir", "", CMD_REF(workspace), N_("[DIRECTORY...]"),
N_("Creates directories and adds them to the workspace"),
"",
options::opts::no_ignore)
{
if (args.size() < 1)
throw usage(execid);
database db(app);
workspace work(app);
set<file_path> paths;
// spin through args and try to ensure that we won't have any collisions
// before doing any real filesystem modification. we'll also verify paths
// against .mtn-ignore here.
for (args_vector::const_iterator i = args.begin(); i != args.end(); ++i)
{
file_path fp = file_path_external(*i);
require_path_is_nonexistent
(fp, F("directory '%s' already exists") % fp);
// we'll treat this as a user (fatal) error. it really wouldn't make
// sense to add a dir to .mtn-ignore and then try to add it to the
// project with a mkdir statement, but one never can tell...
N(app.opts.no_ignore || !work.ignore_file(fp),
F("ignoring directory '%s' [see .mtn-ignore]") % fp);
paths.insert(fp);
}
// this time, since we've verified that there should be no collisions,
// we'll just go ahead and do the filesystem additions.
for (set<file_path>::const_iterator i = paths.begin(); i != paths.end(); ++i)
mkdir_p(*i);
work.perform_additions(db, paths, false, !app.opts.no_ignore);
}
CMD(add, "add", "", CMD_REF(workspace), N_("[PATH]..."),
N_("Adds files to the workspace"),
"",
options::opts::unknown | options::opts::no_ignore |
options::opts::recursive)
{
if (!app.opts.unknown && (args.size() < 1))
throw usage(execid);
database db(app);
workspace work(app);
vector<file_path> roots = args_to_paths(args);
set<file_path> paths;
bool add_recursive = app.opts.recursive;
if (app.opts.unknown)
{
path_restriction mask(work, roots,
args_to_paths(app.opts.exclude_patterns),
app.opts.depth);
set<file_path> ignored;
// if no starting paths have been specified use the workspace root
if (roots.empty())
roots.push_back(file_path());
work.find_unknown_and_ignored(db, mask, roots, paths, ignored);
work.perform_additions(db, ignored,
add_recursive, !app.opts.no_ignore);
}
else
paths = set<file_path>(roots.begin(), roots.end());
work.perform_additions(db, paths, add_recursive, !app.opts.no_ignore);
}
CMD(drop, "drop", "rm", CMD_REF(workspace), N_("[PATH]..."),
N_("Drops files from the workspace"),
"",
options::opts::bookkeep_only | options::opts::missing | options::opts::recursive)
{
if (!app.opts.missing && (args.size() < 1))
throw usage(execid);
database db(app);
workspace work(app);
set<file_path> paths;
if (app.opts.missing)
{
temp_node_id_source nis;
roster_t current_roster_shape;
work.get_current_roster_shape(db, nis, current_roster_shape);
node_restriction mask(work, args_to_paths(args),
args_to_paths(app.opts.exclude_patterns),
app.opts.depth,
current_roster_shape);
work.find_missing(current_roster_shape, mask, paths);
}
else
{
vector<file_path> roots = args_to_paths(args);
paths = set<file_path>(roots.begin(), roots.end());
}
work.perform_deletions(db, paths,
app.opts.recursive, app.opts.bookkeep_only);
}
CMD(rename, "rename", "mv", CMD_REF(workspace),
N_("SRC DEST\n"
"SRC1 [SRC2 [...]] DEST_DIR"),
N_("Renames entries in the workspace"),
"",
options::opts::bookkeep_only)
{
if (args.size() < 2)
throw usage(execid);
database db(app);
workspace work(app);
utf8 dstr = args.back();
file_path dst_path = file_path_external(dstr);
set<file_path> src_paths;
for (size_t i = 0; i < args.size()-1; i++)
{
file_path s = file_path_external(idx(args, i));
src_paths.insert(s);
}
//this catches the case where the user specifies a directory 'by convention'
//that doesn't exist. the code in perform_rename already handles the proper
//cases for more than one source item.
if (src_paths.size() == 1 && dstr()[dstr().size() -1] == '/')
if (get_path_status(*src_paths.begin()) != path::directory)
N(get_path_status(dst_path) == path::directory,
F(_("The specified target directory %s/ doesn't exist.")) % dst_path);
work.perform_rename(db, src_paths, dst_path, app.opts.bookkeep_only);
}
CMD(pivot_root, "pivot_root", "", CMD_REF(workspace), N_("NEW_ROOT PUT_OLD"),
N_("Renames the root directory"),
N_("After this command, the directory that currently "
"has the name NEW_ROOT "
"will be the root directory, and the directory "
"that is currently the root "
"directory will have name PUT_OLD.\n"
"Use of --bookkeep-only is NOT recommended."),
options::opts::bookkeep_only)
{
if (args.size() != 2)
throw usage(execid);
database db(app);
workspace work(app);
file_path new_root = file_path_external(idx(args, 0));
file_path put_old = file_path_external(idx(args, 1));
work.perform_pivot_root(db, new_root, put_old,
app.opts.bookkeep_only);
}
CMD(status, "status", "", CMD_REF(informative), N_("[PATH]..."),
N_("Shows workspace's status information"),
"",
options::opts::depth | options::opts::exclude)
{
roster_t new_roster;
parent_map old_rosters;
revision_t rev;
temp_node_id_source nis;
database db(app);
workspace work(app);
work.get_parent_rosters(db, old_rosters);
work.get_current_roster_shape(db, nis, new_roster);
node_restriction mask(work, args_to_paths(args),
args_to_paths(app.opts.exclude_patterns),
app.opts.depth,
old_rosters, new_roster);
work.update_current_roster_from_filesystem(new_roster, mask);
make_restricted_revision(old_rosters, new_roster, mask, rev);
utf8 summary;
revision_summary(rev, app.opts.branchname, summary);
external summary_external;
utf8_to_system_best_effort(summary, summary_external);
cout << summary_external;
}
CMD(checkout, "checkout", "co", CMD_REF(tree), N_("[DIRECTORY]"),
N_("Checks out a revision from the database into a directory"),
N_("If a revision is given, that's the one that will be checked out. "
"Otherwise, it will be the head of the branch (given or implicit). "
"If no directory is given, the branch name will be used as directory."),
options::opts::branch | options::opts::revision)
{
revision_id revid;
system_path dir;
database db(app);
project_t project(db);
transaction_guard guard(db, false);
if (args.size() > 1 || app.opts.revision_selectors.size() > 1)
throw usage(execid);
if (app.opts.revision_selectors.empty())
{
// use branch head revision
N(!app.opts.branchname().empty(),
F("use --revision or --branch to specify what to checkout"));
set<revision_id> heads;
project.get_branch_heads(app.opts.branchname, heads,
app.opts.ignore_suspend_certs);
N(!heads.empty(),
F("branch '%s' is empty") % app.opts.branchname);
if (heads.size() > 1)
{
P(F("branch %s has multiple heads:") % app.opts.branchname);
for (set<revision_id>::const_iterator i = heads.begin(); i != heads.end(); ++i)
P(i18n_format(" %s")
% describe_revision(project, *i));
P(F("choose one with '%s checkout -r<id>'") % ui.prog_name);
E(false, F("branch %s has multiple heads") % app.opts.branchname);
}
revid = *(heads.begin());
}
else if (app.opts.revision_selectors.size() == 1)
{
// use specified revision
complete(app.opts, app.lua, project, idx(app.opts.revision_selectors, 0)(), revid);
guess_branch(app.opts, project, revid);
I(!app.opts.branchname().empty());
N(project.revision_is_in_branch(revid, app.opts.branchname),
F("revision %s is not a member of branch %s")
% revid % app.opts.branchname);
}
// we do this part of the checking down here, because it is legitimate to
// do
// $ mtn co -r h:net.venge.monotone
// and have mtn guess the branch, and then use that branch name as the
// default directory. But in this case the branch name will not be set
// until after the guess_branch() call above:
{
bool checkout_dot = false;
if (args.empty())
{
// No checkout dir specified, use branch name for dir.
N(!app.opts.branchname().empty(),
F("you must specify a destination directory"));
dir = system_path(app.opts.branchname());
}
else
{
// Checkout to specified dir.
dir = system_path(idx(args, 0));
if (idx(args, 0) == utf8("."))
checkout_dot = true;
}
if (!checkout_dot)
require_path_is_nonexistent
(dir, F("checkout directory '%s' already exists") % dir);
}
workspace::create_workspace(app.opts, app.lua, dir);
workspace work(app);
roster_t empty_roster, current_roster;
L(FL("checking out revision %s to directory %s")
% revid % dir);
db.get_roster(revid, current_roster);
revision_t workrev;
make_revision_for_workspace(revid, cset(), workrev);
work.put_work_rev(workrev);
cset checkout;
make_cset(empty_roster, current_roster, checkout);
content_merge_checkout_adaptor wca(db);
work.perform_content_update(db, checkout, wca, false);
work.update_any_attrs(db);
work.maybe_update_inodeprints(db);
guard.commit();
}
CMD_GROUP(attr, "attr", "", CMD_REF(workspace),
N_("Manages file attributes"),
N_("This command is used to set, get or drop file attributes."));
CMD(attr_drop, "drop", "", CMD_REF(attr), N_("PATH [ATTR]"),
N_("Removes attributes from a file"),
N_("If no attribute is specified, this command removes all attributes "
"attached to the file given in PATH. Otherwise only removes the "
"attribute specified in ATTR."),
options::opts::none)
{
N(args.size() > 0 && args.size() < 3,
F("wrong argument count"));
roster_t new_roster;
temp_node_id_source nis;
database db(app);
workspace work(app);
work.get_current_roster_shape(db, nis, new_roster);
file_path path = file_path_external(idx(args, 0));
N(new_roster.has_node(path), F("Unknown path '%s'") % path);
node_t node = new_roster.get_node(path);
// Clear all attrs (or a specific attr).
if (args.size() == 1)
{
for (full_attr_map_t::iterator i = node->attrs.begin();
i != node->attrs.end(); ++i)
i->second = make_pair(false, "");
}
else
{
I(args.size() == 2);
attr_key a_key = attr_key(idx(args, 1)());
N(node->attrs.find(a_key) != node->attrs.end(),
F("Path '%s' does not have attribute '%s'")
% path % a_key);
node->attrs[a_key] = make_pair(false, "");
}
parent_map parents;
work.get_parent_rosters(db, parents);
revision_t new_work;
make_revision_for_workspace(parents, new_roster, new_work);
work.put_work_rev(new_work);
work.update_any_attrs(db);
}
CMD(attr_get, "get", "", CMD_REF(attr), N_("PATH [ATTR]"),
N_("Gets the values of a file's attributes"),
N_("If no attribute is specified, this command prints all attributes "
"attached to the file given in PATH. Otherwise it only prints the "
"attribute specified in ATTR."),
options::opts::none)
{
N(args.size() > 0 && args.size() < 3,
F("wrong argument count"));
roster_t new_roster;
temp_node_id_source nis;
database db(app);
workspace work(app);
work.get_current_roster_shape(db, nis, new_roster);
file_path path = file_path_external(idx(args, 0));
N(new_roster.has_node(path), F("Unknown path '%s'") % path);
node_t node = new_roster.get_node(path);
if (args.size() == 1)
{
bool has_any_live_attrs = false;
for (full_attr_map_t::const_iterator i = node->attrs.begin();
i != node->attrs.end(); ++i)
if (i->second.first)
{
cout << path << " : "
<< i->first << '='
<< i->second.second << '\n';
has_any_live_attrs = true;
}
if (!has_any_live_attrs)
cout << F("No attributes for '%s'") % path << '\n';
}
else
{
I(args.size() == 2);
attr_key a_key = attr_key(idx(args, 1)());
full_attr_map_t::const_iterator i = node->attrs.find(a_key);
if (i != node->attrs.end() && i->second.first)
cout << path << " : "
<< i->first << '='
<< i->second.second << '\n';
else
cout << (F("No attribute '%s' on path '%s'")
% a_key % path) << '\n';
}
}
CMD(attr_set, "set", "", CMD_REF(attr), N_("PATH ATTR VALUE"),
N_("Sets an attribute on a file"),
N_("Sets the attribute given on ATTR to the value specified in VALUE "
"for the file mentioned in PATH."),
options::opts::none)
{
N(args.size() == 3,
F("wrong argument count"));
roster_t new_roster;
temp_node_id_source nis;
database db(app);
workspace work(app);
work.get_current_roster_shape(db, nis, new_roster);
file_path path = file_path_external(idx(args, 0));
N(new_roster.has_node(path), F("Unknown path '%s'") % path);
node_t node = new_roster.get_node(path);
attr_key a_key = attr_key(idx(args, 1)());
attr_value a_value = attr_value(idx(args, 2)());
node->attrs[a_key] = make_pair(true, a_value);
parent_map parents;
work.get_parent_rosters(db, parents);
revision_t new_work;
make_revision_for_workspace(parents, new_roster, new_work);
work.put_work_rev(new_work);
work.update_any_attrs(db);
}
// Name: get_attributes
// Arguments:
// 1: file / directory name
// Added in: 1.0
// Renamed from attributes to get_attributes in: 5.0
// Purpose: Prints all attributes for the specified path
// Output format: basic_io formatted output, each attribute has its own stanza:
//
// 'format_version'
// used in case this format ever needs to change.
// format: ('format_version', the string "1" currently)
// occurs: exactly once
// 'attr'
// represents an attribute entry
// format: ('attr', name, value), ('state', [unchanged|changed|added|dropped])
// occurs: zero or more times
//
// Error conditions: If the path has no attributes, prints only the
// format version, if the file is unknown, escalates
CMD_AUTOMATE(get_attributes, N_("PATH"),
N_("Prints all attributes for the specified path"),
"",
options::opts::none)
{
N(!args.empty(),
F("wrong argument count"));
database db(app);
workspace work(app);
// retrieve the path
file_path path = file_path_external(idx(args,0));
roster_t base, current;
parent_map parents;
temp_node_id_source nis;
// get the base and the current roster of this workspace
work.get_current_roster_shape(db, nis, current);
work.get_parent_rosters(db, parents);
N(parents.size() == 1,
F("this command can only be used in a single-parent workspace"));
base = parent_roster(parents.begin());
N(current.has_node(path), F("Unknown path '%s'") % path);
// create the printer
basic_io::printer pr;
// print the format version
basic_io::stanza st;
st.push_str_pair(basic_io::syms::format_version, "1");
pr.print_stanza(st);
// the current node holds all current attributes (unchanged and new ones)
node_t n = current.get_node(path);
for (full_attr_map_t::const_iterator i = n->attrs.begin();
i != n->attrs.end(); ++i)
{
std::string value(i->second.second());
std::string state;
// if if the first value of the value pair is false this marks a
// dropped attribute
if (!i->second.first)
{
// if the attribute is dropped, we should have a base roster
// with that node. we need to check that for the attribute as well
// because if it is dropped there as well it was already deleted
// in any previous revision
I(base.has_node(path));
node_t prev_node = base.get_node(path);
// find the attribute in there
full_attr_map_t::const_iterator j = prev_node->attrs.find(i->first);
I(j != prev_node->attrs.end());
// was this dropped before? then ignore it
if (!j->second.first) { continue; }
state = "dropped";
// output the previous (dropped) value later
value = j->second.second();
}
// this marks either a new or an existing attribute
else
{
if (base.has_node(path))
{
node_t prev_node = base.get_node(path);
full_attr_map_t::const_iterator j =
prev_node->attrs.find(i->first);
// the attribute is new if it either hasn't been found
// in the previous roster or has been deleted there
if (j == prev_node->attrs.end() || !j->second.first)
{
state = "added";
}
// check if the attribute's value has been changed
else if (i->second.second() != j->second.second())
{
state = "changed";
}
else
{
state = "unchanged";
}
}
// its added since the whole node has been just added
else
{
state = "added";
}
}
basic_io::stanza st;
st.push_str_triple(basic_io::syms::attr, i->first(), value);
st.push_str_pair(symbol("state"), state);
pr.print_stanza(st);
}
// print the output
output.write(pr.buf.data(), pr.buf.size());
}
// Name: set_attribute
// Arguments:
// 1: file / directory name
// 2: attribute key
// 3: attribute value
// Added in: 5.0
// Purpose: Edits the workspace revision and sets an attribute on a certain path
//
// Error conditions: If PATH is unknown in the new roster, prints an error and
// exits with status 1.
CMD_AUTOMATE(set_attribute, N_("PATH KEY VALUE"),
N_("Sets an attribute on a certain path"),
"",
options::opts::none)
{
N(args.size() == 3,
F("wrong argument count"));
database db(app);
workspace work(app);
roster_t new_roster;
temp_node_id_source nis;
work.get_current_roster_shape(db, nis, new_roster);
file_path path = file_path_external(idx(args,0));
N(new_roster.has_node(path), F("Unknown path '%s'") % path);
node_t node = new_roster.get_node(path);