monotone

monotone Mtn Source Tree

Root/cmd_files.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
12#include "annotate.hh"
13#include "cmd.hh"
14#include "diff_patch.hh"
15#include "localized_file_io.hh"
16#include "packet.hh"
17#include "simplestring_xform.hh"
18#include "transforms.hh"
19
20using std::cout;
21using std::ostream_iterator;
22using std::string;
23using std::vector;
24
25// fload, fmerge, and fdiff are simple commands for debugging the line
26// merger.
27
28CMD(fload, N_("debug"), "", N_("load file contents into db"), OPT_NONE)
29{
30 string s = get_stdin();
31
32 file_id f_id;
33 file_data f_data(s);
34
35 calculate_ident (f_data, f_id);
36
37 packet_db_writer dbw(app);
38 dbw.consume_file_data(f_id, f_data);
39}
40
41CMD(fmerge, N_("debug"), N_("<parent> <left> <right>"),
42 N_("merge 3 files and output result"),
43 OPT_NONE)
44{
45 if (args.size() != 3)
46 throw usage(name);
47
48 file_id
49 anc_id(idx(args, 0)()),
50 left_id(idx(args, 1)()),
51 right_id(idx(args, 2)());
52
53 file_data anc, left, right;
54
55 N(app.db.file_version_exists (anc_id),
56 F("ancestor file id does not exist"));
57
58 N(app.db.file_version_exists (left_id),
59 F("left file id does not exist"));
60
61 N(app.db.file_version_exists (right_id),
62 F("right file id does not exist"));
63
64 app.db.get_file_version(anc_id, anc);
65 app.db.get_file_version(left_id, left);
66 app.db.get_file_version(right_id, right);
67
68 vector<string> anc_lines, left_lines, right_lines, merged_lines;
69
70 split_into_lines(anc.inner()(), anc_lines);
71 split_into_lines(left.inner()(), left_lines);
72 split_into_lines(right.inner()(), right_lines);
73 N(merge3(anc_lines, left_lines, right_lines, merged_lines), F("merge failed"));
74 copy(merged_lines.begin(), merged_lines.end(), ostream_iterator<string>(cout, "\n"));
75
76}
77
78CMD(fdiff, N_("debug"), N_("SRCNAME DESTNAME SRCID DESTID"),
79 N_("diff 2 files and output result"),
80 OPT_CONTEXT_DIFF % OPT_UNIFIED_DIFF % OPT_NO_SHOW_ENCLOSER)
81{
82 if (args.size() != 4)
83 throw usage(name);
84
85 string const
86 & src_name = idx(args, 0)(),
87 & dst_name = idx(args, 1)();
88
89 file_id
90 src_id(idx(args, 2)()),
91 dst_id(idx(args, 3)());
92
93 file_data src, dst;
94
95 N(app.db.file_version_exists (src_id),
96 F("source file id does not exist"));
97
98 N(app.db.file_version_exists (dst_id),
99 F("destination file id does not exist"));
100
101 app.db.get_file_version(src_id, src);
102 app.db.get_file_version(dst_id, dst);
103
104 string pattern("");
105 if (app.diff_show_encloser)
106 app.lua.hook_get_encloser_pattern(file_path_external(src_name), pattern);
107
108 make_diff(src_name, dst_name,
109 src_id, dst_id,
110 src.inner(), dst.inner(),
111 cout, app.diff_format, pattern);
112}
113
114CMD(annotate, N_("informative"), N_("PATH"),
115 N_("print annotated copy of the file from REVISION"),
116 OPT_REVISION % OPT_BRIEF)
117{
118 revision_id rid;
119
120 if (app.revision_selectors.size() == 0)
121 app.require_workspace();
122
123 if ((args.size() != 1) || (app.revision_selectors.size() > 1))
124 throw usage(name);
125
126 extern bool global_slow_assertions_version_check;
127 global_slow_assertions_version_check = false;
128
129 file_path file = file_path_external(idx(args, 0));
130 split_path sp;
131 file.split(sp);
132
133 if (app.revision_selectors.size() == 0)
134 get_revision_id(rid);
135 else
136 complete(app, idx(app.revision_selectors, 0)(), rid);
137
138 N(!null_id(rid),
139 F("no revision for file '%s' in database") % file);
140 N(app.db.revision_exists(rid),
141 F("no such revision '%s'") % rid);
142
143 L(FL("annotate file file_path '%s'") % file);
144
145 // find the version of the file requested
146 roster_t roster;
147 marking_map marks;
148 app.db.get_roster(rid, roster, marks);
149 N(roster.has_node(sp),
150 F("no such file '%s' in revision '%s'") % file % rid);
151 node_t node = roster.get_node(sp);
152 N(is_file_t(node),
153 F("'%s' in revision '%s' is not a file") % file % rid);
154
155 file_t file_node = downcast_to_file_t(node);
156 L(FL("annotate for file_id %s") % file_node->self);
157 do_annotate(app, file_node, rid);
158}
159
160CMD(identify, N_("debug"), N_("[PATH]"),
161 N_("calculate identity of PATH or stdin"),
162 OPT_NONE)
163{
164 if (!(args.size() == 0 || args.size() == 1))
165 throw usage(name);
166
167 data dat;
168
169 if (args.size() == 1)
170 {
171 read_localized_data(file_path_external(idx(args, 0)),
172 dat, app.lua);
173 }
174 else
175 {
176 dat = get_stdin();
177 }
178
179 hexenc<id> ident;
180 calculate_ident(dat, ident);
181 cout << ident << "\n";
182}
183
184CMD(cat, N_("informative"),
185 N_("FILENAME"),
186 N_("write file from database to stdout"),
187 OPT_REVISION)
188{
189 if (args.size() != 1)
190 throw usage(name);
191
192 if (app.revision_selectors.size() == 0)
193 app.require_workspace();
194
195 transaction_guard guard(app.db, false);
196
197 revision_id rid;
198 if (app.revision_selectors.size() == 0)
199 get_revision_id(rid);
200 else
201 complete(app, idx(app.revision_selectors, 0)(), rid);
202 N(app.db.revision_exists(rid),
203 F("no such revision '%s'") % rid);
204
205 // Paths are interpreted as standard external ones when we're in a
206 // workspace, but as project-rooted external ones otherwise.
207 file_path fp;
208 split_path sp;
209 fp = file_path_external(idx(args, 0));
210 fp.split(sp);
211
212 roster_t roster;
213 marking_map marks;
214 app.db.get_roster(rid, roster, marks);
215 N(roster.has_node(sp), F("no file '%s' found in revision '%s'") % fp % rid);
216 node_t node = roster.get_node(sp);
217 N((!null_node(node->self) && is_file_t(node)), F("no file '%s' found in revision '%s'") % fp % rid);
218
219 file_t file_node = downcast_to_file_t(node);
220 file_id ident = file_node->content;
221 file_data dat;
222 L(FL("dumping file '%s'") % ident);
223 app.db.get_file_version(ident, dat);
224 cout.write(dat.inner()().data(), dat.inner()().size());
225
226 guard.commit();
227}
228
229// Local Variables:
230// mode: C++
231// fill-column: 76
232// c-file-style: "gnu"
233// indent-tabs-mode: nil
234// End:
235// 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