File: cmd_files.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (391 lines) | stat: -rw-r--r-- 11,338 bytes parent folder | download
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
// 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 <iterator>

#include "annotate.hh"
#include "revision.hh"
#include "cmd.hh"
#include "diff_output.hh"
#include "merge_content.hh"
#include "file_io.hh"
#include "simplestring_xform.hh"
#include "transforms.hh"
#include "app_state.hh"
#include "project.hh"
#include "database.hh"
#include "work.hh"
#include "roster.hh"

using std::cout;
using std::ostream_iterator;
using std::string;
using std::vector;

// fload, fmerge, and fdiff are simple commands for debugging the line
// merger.

CMD(fload, "fload", "", CMD_REF(debug), "",
    N_("Loads a file's contents into the database"),
    "",
    options::opts::none)
{
  data dat;
  read_data_stdin(dat);

  file_id f_id;
  file_data f_data(dat);

  calculate_ident (f_data, f_id);

  database db(app);
  transaction_guard guard(db);
  db.put_file(f_id, f_data);
  guard.commit();
}

CMD(fmerge, "fmerge", "", CMD_REF(debug), N_("<parent> <left> <right>"),
    N_("Merges 3 files and outputs the result"),
    "",
    options::opts::none)
{
  if (args.size() != 3)
    throw usage(execid);

  file_id
    anc_id(decode_hexenc_as<file_id>(idx(args, 0)(), origin::user)),
    left_id(decode_hexenc_as<file_id>(idx(args, 1)(), origin::user)),
    right_id(decode_hexenc_as<file_id>(idx(args, 2)(), origin::user));

  file_data anc, left, right;

  database db(app);
  E(db.file_version_exists (anc_id), origin::user,
    F("ancestor file id does not exist"));

  E(db.file_version_exists (left_id), origin::user,
    F("left file id does not exist"));

  E(db.file_version_exists (right_id), origin::user,
    F("right file id does not exist"));

  db.get_file_version(anc_id, anc);
  db.get_file_version(left_id, left);
  db.get_file_version(right_id, right);

  vector<string> anc_lines, left_lines, right_lines, merged_lines;

  split_into_lines(anc.inner()(), anc_lines);
  split_into_lines(left.inner()(), left_lines);
  split_into_lines(right.inner()(), right_lines);
  E(merge3(anc_lines, left_lines, right_lines, merged_lines),
    origin::user, F("merge failed"));
  copy(merged_lines.begin(), merged_lines.end(), ostream_iterator<string>(cout, "\n"));

}

CMD(fdiff, "fdiff", "", CMD_REF(debug), N_("SRCNAME DESTNAME SRCID DESTID"),
    N_("Differences 2 files and outputs the result"),
    "",
    options::opts::diff_options)
{
  if (args.size() != 4)
    throw usage(execid);

  string const
    & src_name = idx(args, 0)(),
    & dst_name = idx(args, 1)();

  file_id
    src_id(decode_hexenc_as<file_id>(idx(args, 2)(), origin::user)),
    dst_id(decode_hexenc_as<file_id>(idx(args, 3)(), origin::user));

  file_data src, dst;

  database db(app);
  E(db.file_version_exists (src_id), origin::user,
    F("source file id does not exist"));

  E(db.file_version_exists (dst_id), origin::user,
    F("destination file id does not exist"));

  db.get_file_version(src_id, src);
  db.get_file_version(dst_id, dst);

  string pattern("");
  if (!app.opts.no_show_encloser)
    app.lua.hook_get_encloser_pattern(file_path_external(utf8(src_name,
                                                              origin::user)),
                                      pattern);

  make_diff(src_name, dst_name,
            src_id, dst_id,
            src.inner(), dst.inner(),
            cout, app.opts.diff_format, pattern);
}

CMD(annotate, "annotate", "", CMD_REF(informative), N_("PATH"),
    N_("Prints an annotated copy of a file"),
    N_("Calculates and prints an annotated copy of the given file from "
       "the specified REVISION."),
    options::opts::revision | options::opts::revs_only)
{
  revision_id rid;
  database db(app);
  project_t project(db);

  if ((args.size() != 1) || (app.opts.revision_selectors.size() > 1))
    throw usage(execid);

  file_path file = file_path_external(idx(args, 0));

  L(FL("annotate file '%s'") % file);

  roster_t roster;
  if (app.opts.revision_selectors.empty())
    {
      // What this _should_ do is calculate the current workspace roster
      // and/or revision and hand that to do_annotate.  This should just
      // work, no matter how many parents the workspace has.  However,
      // do_annotate currently expects to be given a file_t and revision_id
      // corresponding to items already in the database.  This is a minor
      // bug in the one-parent case (it means annotate will not show you
      // changes in the working copy) but is fatal in the two-parent case.
      // Thus, what we do instead is get the parent rosters, refuse to
      // proceed if there's more than one, and give do_annotate what it
      // wants.  See tests/two_parent_workspace_annotate.
      workspace work(app);
      revision_t rev;
      work.get_work_rev(rev);
      E(rev.edges.size() == 1, origin::user,
        F("with no revision selected, this command can only be used in "
          "a single-parent workspace"));

      rid = edge_old_revision(rev.edges.begin());

      // this call will change to something else when the above bug is
      // fixed, and so should not be merged with the identical call in
      // the else branch.
      db.get_roster(rid, roster);
    }
  else
    {
      complete(app.opts, app.lua, project, idx(app.opts.revision_selectors, 0)(), rid);
      db.get_roster(rid, roster);
    }

  // find the version of the file requested
  E(roster.has_node(file), origin::user,
    F("no such file '%s' in revision '%s'")
      % file % rid);
  const_node_t node = roster.get_node(file);
  E(is_file_t(node), origin::user,
    F("'%s' in revision '%s' is not a file")
      % file % rid);

  const_file_t file_node = downcast_to_file_t(node);
  L(FL("annotate for file_id %s") % file_node->self);
  do_annotate(app, project, file_node, rid, app.opts.revs_only);
}

CMD(identify, "identify", "", CMD_REF(debug), N_("[PATH]"),
    N_("Calculates the identity of a file or stdin"),
    N_("If any PATH is given, calculates their identity; otherwise, the "
       "one from the standard input is calculated."),
    options::opts::none)
{
  if (args.size() > 1)
    throw usage(execid);

  data dat;

  if (args.empty())
    read_data_stdin(dat);
  else
    read_data_for_command_line(idx(args, 0), dat);

  id ident;
  calculate_ident(dat, ident);
  cout << ident << '\n';
}

// Name: identify
// Arguments:
//   1: a file path
// Added in: 4.2
// Purpose: Prints the fileid of the given file (aka hash)
//
// Output format: a single, 40 byte long hex-encoded id
//
// Error conditions: If the file path doesn't point to a valid file prints
// an error message to stderr and exits with status 1.
CMD_AUTOMATE(identify, N_("PATH"),
             N_("Prints the file identifier of a file"),
             "",
             options::opts::none)
{
  E(args.size() == 1, origin::user,
    F("wrong argument count"));

  utf8 path = idx(args, 0);

  E(path() != "-", origin::user,
    F("Cannot read from stdin"));

  data dat;
  read_data_for_command_line(path, dat);

  id ident;
  calculate_ident(dat, ident);
  output << ident << '\n';
}

static void
dump_file(database & db, std::ostream & output, file_id const & ident)
{
  E(db.file_version_exists(ident), origin::user,
    F("no file version %s found in database") % ident);

  file_data dat;
  L(FL("dumping file %s") % ident);
  db.get_file_version(ident, dat);
  output << dat;
}

static void
dump_file(database & db, std::ostream & output, revision_id rid, utf8 filename)
{
  E(db.revision_exists(rid), origin::user,
    F("no such revision '%s'") % rid);

  // Paths are interpreted as standard external ones when we're in a
  // workspace, but as project-rooted external ones otherwise.
  file_path fp = file_path_external(filename);

  roster_t roster;
  marking_map marks;
  db.get_roster(rid, roster, marks);
  E(roster.has_node(fp), origin::user,
    F("no file '%s' found in revision '%s'") % fp % rid);

  const_node_t node = roster.get_node(fp);
  E((!null_node(node->self) && is_file_t(node)), origin::user,
    F("no file '%s' found in revision '%s'") % fp % rid);

  const_file_t file_node = downcast_to_file_t(node);
  dump_file(db, output, file_node->content);
}

CMD(cat, "cat", "", CMD_REF(informative),
    N_("FILENAME"),
    N_("Prints a file from the database"),
    N_("Fetches the given file FILENAME from the database and prints it "
       "to the standard output."),
    options::opts::revision)
{
  if (args.size() != 1)
    throw usage(execid);

  database db(app);
  revision_id rid;
  if (app.opts.revision_selectors.empty())
    {
      workspace work(app);
      parent_map parents;
      work.get_parent_rosters(db, parents);
      E(parents.size() == 1, origin::user,
        F("this command can only be used in a single-parent workspace"));
      rid = parent_id(parents.begin());
    }
  else
    {
      project_t project(db);
      complete(app.opts, app.lua, project, idx(app.opts.revision_selectors, 0)(), rid);
    }

  dump_file(db, cout, rid, idx(args, 0));
}

// Name: get_file
// Arguments:
//   1: a file id
// Added in: 1.0
// Purpose: Prints the contents of the specified file.
//
// Output format: The file contents are output without modification.
//
// Error conditions: If the file id specified is unknown or invalid prints
// an error message to stderr and exits with status 1.
CMD_AUTOMATE(get_file, N_("FILEID"),
             N_("Prints the contents of a file (given an identifier)"),
             "",
             options::opts::none)
{
  E(args.size() == 1, origin::user,
    F("wrong argument count"));

  database db(app);
  hexenc<id> hident(idx(args, 0)(), origin::user);
  file_id ident(decode_hexenc_as<file_id>(hident(), hident.made_from));
  dump_file(db, output, ident);
}

// Name: get_file_of
// Arguments:
//   1: a filename
//
// Options:
//   r: a revision id
//
// Added in: 4.0
// Purpose: Prints the contents of the specified file.
//
// Output format: The file contents are output without modification.
//
// Error conditions: If the file id specified is unknown or invalid prints
// an error message to stderr and exits with status 1.
CMD_AUTOMATE(get_file_of, N_("FILENAME"),
             N_("Prints the contents of a file (given a name)"),
             "",
             options::opts::revision)
{
  E(args.size() == 1, origin::user,
    F("wrong argument count"));

  database db(app);

  revision_id rid;
  if (app.opts.revision_selectors.empty())
    {
      workspace work(app);

      parent_map parents;
      work.get_parent_rosters(db, parents);
      E(parents.size() == 1, origin::user,
        F("this command can only be used in a single-parent workspace"));
      rid = parent_id(parents.begin());
    }
  else
    {
      project_t project(db);
      complete(app.opts, app.lua, project, idx(app.opts.revision_selectors, 0)(), rid);
    }

  dump_file(db, output, rid, idx(args, 0));
}

// 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: