File: cmd_files.cc

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (232 lines) | stat: -rw-r--r-- 6,171 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
// 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 <iostream>

#include "annotate.hh"
#include "cmd.hh"
#include "diff_patch.hh"
#include "localized_file_io.hh"
#include "packet.hh"
#include "simplestring_xform.hh"
#include "transforms.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, N_("debug"), "", N_("load file contents into db"), options::opts::none)
{
  string s = get_stdin();

  file_id f_id;
  file_data f_data(s);

  calculate_ident (f_data, f_id);

  packet_db_writer dbw(app);
  dbw.consume_file_data(f_id, f_data);
}

CMD(fmerge, N_("debug"), N_("<parent> <left> <right>"),
    N_("merge 3 files and output result"),
    options::opts::none)
{
  if (args.size() != 3)
    throw usage(name);

  file_id 
    anc_id(idx(args, 0)()), 
    left_id(idx(args, 1)()), 
    right_id(idx(args, 2)());

  file_data anc, left, right;

  N(app.db.file_version_exists (anc_id),
    F("ancestor file id does not exist"));

  N(app.db.file_version_exists (left_id),
    F("left file id does not exist"));

  N(app.db.file_version_exists (right_id),
    F("right file id does not exist"));

  app.db.get_file_version(anc_id, anc);
  app.db.get_file_version(left_id, left);
  app.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);
  N(merge3(anc_lines, left_lines, right_lines, merged_lines), F("merge failed"));
  copy(merged_lines.begin(), merged_lines.end(), ostream_iterator<string>(cout, "\n"));

}

CMD(fdiff, N_("debug"), N_("SRCNAME DESTNAME SRCID DESTID"),
    N_("diff 2 files and output result"),
    options::opts::diff_options)
{
  if (args.size() != 4)
    throw usage(name);

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

  file_id 
    src_id(idx(args, 2)()), 
    dst_id(idx(args, 3)());

  file_data src, dst;

  N(app.db.file_version_exists (src_id),
    F("source file id does not exist"));

  N(app.db.file_version_exists (dst_id),
    F("destination file id does not exist"));

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

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

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

CMD(annotate, N_("informative"), N_("PATH"),
    N_("print annotated copy of the file from REVISION"),
    options::opts::revision | options::opts::brief)
{
  revision_id rid;

  if (app.opts.revision_selectors.size() == 0)
    app.require_workspace();

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

  file_path file = file_path_external(idx(args, 0));
  split_path sp;
  file.split(sp);

  if (app.opts.revision_selectors.size() == 0)
    app.work.get_revision_id(rid);
  else
    complete(app, idx(app.opts.revision_selectors, 0)(), rid);

  N(!null_id(rid), 
    F("no revision for file '%s' in database") % file);
  N(app.db.revision_exists(rid), 
    F("no such revision '%s'") % rid);

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

  // find the version of the file requested
  roster_t roster;
  marking_map marks;
  app.db.get_roster(rid, roster, marks);
  N(roster.has_node(sp), 
    F("no such file '%s' in revision '%s'") % file % rid);
  node_t node = roster.get_node(sp);
  N(is_file_t(node), 
    F("'%s' in revision '%s' is not a file") % file % rid);

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

CMD(identify, N_("debug"), N_("[PATH]"),
    N_("calculate identity of PATH or stdin"),
    options::opts::none)
{
  if (!(args.size() == 0 || args.size() == 1))
    throw usage(name);

  data dat;

  if (args.size() == 1)
    {
      read_localized_data(file_path_external(idx(args, 0)), 
                          dat, app.lua);
    }
  else
    {
      dat = get_stdin();
    }

  hexenc<id> ident;
  calculate_ident(dat, ident);
  cout << ident << "\n";
}

CMD(cat, N_("informative"),
    N_("FILENAME"),
    N_("write file from database to stdout"),
    options::opts::revision)
{
  if (args.size() != 1)
    throw usage(name);

  if (app.opts.revision_selectors.size() == 0)
    app.require_workspace();

  transaction_guard guard(app.db, false);

  revision_id rid;
  if (app.opts.revision_selectors.size() == 0)
    app.work.get_revision_id(rid);
  else
    complete(app, idx(app.opts.revision_selectors, 0)(), rid);
  N(app.db.revision_exists(rid), 
    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;
  split_path sp;
  fp = file_path_external(idx(args, 0));
  fp.split(sp);

  roster_t roster;
  marking_map marks;
  app.db.get_roster(rid, roster, marks);
  N(roster.has_node(sp), F("no file '%s' found in revision '%s'") % fp % rid);
  node_t node = roster.get_node(sp);
  N((!null_node(node->self) && is_file_t(node)), F("no file '%s' found in revision '%s'") % fp % rid);

  file_t file_node = downcast_to_file_t(node);
  file_id ident = file_node->content;
  file_data dat;
  L(FL("dumping file '%s'") % ident);
  app.db.get_file_version(ident, dat);
  cout.write(dat.inner()().data(), dat.inner()().size());

  guard.commit();
}

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