monotone

monotone Mtn Source Tree

Root/cmd.hh

1#ifndef __CMD_HH__
2#define __CMD_HH__
3
4// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
5//
6// This program is made available under the GNU GPL version 2.0 or
7// greater. See the accompanying file COPYING for details.
8//
9// This program is distributed WITHOUT ANY WARRANTY; without even the
10// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11// PURPOSE.
12
13#include "app_state.hh"
14#include "commands.hh"
15#include "constants.hh"
16#include "options.hh"
17#include "sanity.hh"
18
19namespace commands
20{
21 extern const std::string hidden_group;
22 const char * safe_gettext(const char * msgid);
23 struct no_opts {};
24 struct command_opts
25 {
26 std::set<int> opts;
27 command_opts() {}
28 command_opts & operator%(int o)
29 { opts.insert(o); return *this; }
30 command_opts & operator%(no_opts o)
31 { return *this; }
32 command_opts & operator%(command_opts const &o)
33 { opts.insert(o.opts.begin(), o.opts.end()); return *this; }
34 };
35 extern const no_opts OPT_NONE;
36
37 struct command
38 {
39 // NB: these strings are stored _un_translated, because we cannot
40 // translate them until after main starts, by which time the
41 // command objects have all been constructed.
42 std::string name;
43 std::string cmdgroup;
44 std::string params_;
45 std::string desc_;
46 bool use_workspace_options;
47 command_opts options;
48 command(std::string const & n,
49 std::string const & g,
50 std::string const & p,
51 std::string const & d,
52 bool u,
53 command_opts const & o);
54 virtual ~command();
55 virtual std::string params();
56 virtual std::string desc();
57 virtual void exec(app_state & app,
58 std::vector<utf8> const & args) = 0;
59 };
60};
61
62inline std::vector<file_path>
63args_to_paths(std::vector<utf8> const & args)
64{
65 std::vector<file_path> paths;
66 for (std::vector<utf8>::const_iterator i = args.begin(); i != args.end(); ++i)
67 paths.push_back(file_path_external(*i));
68 return paths;
69}
70
71std::string
72get_stdin();
73
74std::string
75describe_revision(app_state & app,
76 revision_id const & id);
77
78void
79complete(app_state & app,
80 std::string const & str,
81 revision_id & completion,
82 bool must_exist=true);
83
84void
85complete(app_state & app,
86 std::string const & str,
87 std::set<revision_id> & completion,
88 bool must_exist=true);
89
90template<typename ID>
91static void
92complete(app_state & app,
93 std::string const & str,
94 ID & completion)
95{
96 N(str.find_first_not_of(constants::legal_id_bytes) == std::string::npos,
97 F("non-hex digits in id"));
98 if (str.size() == constants::idlen)
99 {
100 completion = ID(str);
101 return;
102 }
103 std::set<ID> completions;
104 app.db.complete(str, completions);
105 N(completions.size() != 0,
106 F("partial id '%s' does not have an expansion") % str);
107 if (completions.size() > 1)
108 {
109 std::string err =
110 (F("partial id '%s' has multiple ambiguous expansions:\n")
111 % str).str();
112 for (typename std::set<ID>::const_iterator i = completions.begin();
113 i != completions.end(); ++i)
114 err += (i->inner()() + "\n");
115 N(completions.size() == 1, i18n_format(err));
116 }
117 completion = *(completions.begin());
118 P(F("expanded partial id '%s' to '%s'")
119 % str % completion);
120}
121
122void
123maybe_update_inodeprints(app_state & app);
124
125void
126notify_if_multiple_heads(app_state & app);
127
128void
129process_commit_message_args(bool & given,
130 std::string & log_message,
131 app_state & app);
132
133#define CMD(C, group, params, desc, opts) \
134namespace commands { \
135 struct cmd_ ## C : public command \
136 { \
137 cmd_ ## C() : command(#C, group, params, desc, true, \
138 command_opts() % opts) \
139 {} \
140 virtual void exec(app_state & app, \
141 std::vector<utf8> const & args); \
142 }; \
143 static cmd_ ## C C ## _cmd; \
144} \
145void commands::cmd_ ## C::exec(app_state & app, \
146 std::vector<utf8> const & args)
147
148// Use this for commands that want to define a params() function
149// instead of having a static description. (Good for "automate"
150// and possibly "list".)
151#define CMD_PARAMS_FN(C, group, desc, opts) \
152namespace commands { \
153 struct cmd_ ## C : public command \
154 { \
155 cmd_ ## C() : command(#C, group, "", desc, true, \
156 command_opts() % opts) \
157 {} \
158 virtual void exec(app_state & app, \
159 std::vector<utf8> const & args); \
160 std::string params(); \
161 }; \
162 static cmd_ ## C C ## _cmd; \
163} \
164void commands::cmd_ ## C::exec(app_state & app, \
165 std::vector<utf8> const & args)
166
167// Use this for commands that should specifically _not_ look for an
168// _MTN dir and load options from it.
169
170#define CMD_NO_WORKSPACE(C, group, params, desc, opts) \
171namespace commands { \
172 struct cmd_ ## C : public command \
173 { \
174 cmd_ ## C() : command(#C, group, params, desc, false, \
175 command_opts() % opts) \
176 {} \
177 virtual void exec(app_state & app, \
178 std::vector<utf8> const & args); \
179 }; \
180 static cmd_ ## C C ## _cmd; \
181} \
182void commands::cmd_ ## C::exec(app_state & app, \
183 std::vector<utf8> const & args) \
184
185#define ALIAS(C, realcommand) \
186CMD(C, realcommand##_cmd.cmdgroup, realcommand##_cmd.params_, \
187 realcommand##_cmd.desc_ + "\nAlias for " #realcommand, \
188 realcommand##_cmd.options) \
189{ \
190 process(app, std::string(#realcommand), args); \
191}
192
193namespace automation {
194 struct automate
195 {
196 std::string name;
197 std::string params;
198 automate(std::string const & n, std::string const & p);
199 virtual void run(std::vector<utf8> args,
200 std::string const & help_name,
201 app_state & app,
202 std::ostream & output) const = 0;
203 virtual ~automate();
204 };
205}
206
207#define AUTOMATE(NAME, PARAMS) \
208namespace automation { \
209 struct auto_ ## NAME : public automate \
210 { \
211 auto_ ## NAME () : automate(#NAME, PARAMS) {} \
212 void run(std::vector<utf8> args, std::string const & help_name, \
213 app_state & app, std::ostream & output) const; \
214 virtual ~auto_ ## NAME() {} \
215 }; \
216 static auto_ ## NAME NAME ## _auto; \
217} \
218void automation::auto_ ## NAME :: run(std::vector<utf8> args, \
219 std::string const & help_name,\
220 app_state & app, \
221 std::ostream & output) const
222
223
224
225
226// Local Variables:
227// mode: C++
228// fill-column: 76
229// c-file-style: "gnu"
230// indent-tabs-mode: nil
231// End:
232// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
233
234#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status