monotone

monotone Mtn Source Tree

Root/selectors.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 "selectors.hh"
11#include "sanity.hh"
12#include "app_state.hh"
13#include "constants.hh"
14
15using std::make_pair;
16using std::pair;
17using std::set;
18using std::string;
19using std::vector;
20
21namespace selectors
22{
23
24 static void
25 decode_selector(string const & orig_sel,
26 selector_type & type,
27 string & sel,
28 app_state & app)
29 {
30 sel = orig_sel;
31
32 L(FL("decoding selector '%s'") % sel);
33
34 string tmp;
35 if (sel.size() < 2 || sel[1] != ':')
36 {
37 if (!app.lua.hook_expand_selector(sel, tmp))
38 {
39 L(FL("expansion of selector '%s' failed") % sel);
40 }
41 else
42 {
43 P(F("expanded selector '%s' -> '%s'") % sel % tmp);
44 sel = tmp;
45 }
46 }
47
48 if (sel.size() >= 2 && sel[1] == ':')
49 {
50 switch (sel[0])
51 {
52 case 'a':
53 type = sel_author;
54 break;
55 case 'b':
56 type = sel_branch;
57 break;
58 case 'h':
59 type = sel_head;
60 break;
61 case 'd':
62 type = sel_date;
63 break;
64 case 'i':
65 type = sel_ident;
66 break;
67 case 't':
68 type = sel_tag;
69 break;
70 case 'c':
71 type = sel_cert;
72 break;
73 case 'l':
74 type = sel_later;
75 break;
76 case 'e':
77 type = sel_earlier;
78 break;
79 default:
80 W(F("unknown selector type: %c") % sel[0]);
81 break;
82 }
83 sel.erase(0,2);
84
85 /* a selector date-related should be validated */
86 if (sel_date==type || sel_later==type || sel_earlier==type)
87 {
88 N (app.lua.hook_expand_date(sel, tmp),
89 F ("selector '%s' is not a valid date\n") % sel);
90
91 if (tmp.size()<8 && (sel_later==type || sel_earlier==type))
92 tmp += "-01T00:00:00";
93 else if (tmp.size()<11 && (sel_later==type || sel_earlier==type))
94 tmp += "T00:00:00";
95 N(tmp.size()==19 || sel_date==type, F ("selector '%s' is not a valid date (%s)") % sel % tmp);
96 if (sel != tmp)
97 {
98 P (F ("expanded date '%s' -> '%s'\n") % sel % tmp);
99 sel = tmp;
100 }
101 }
102 }
103 }
104
105 void
106 complete_selector(string const & orig_sel,
107 vector<pair<selector_type, string> > const & limit,
108 selector_type & type,
109 set<string> & completions,
110 app_state & app)
111 {
112 string sel;
113 decode_selector(orig_sel, type, sel, app);
114 app.db.complete(type, sel, limit, completions);
115 }
116
117 vector<pair<selector_type, string> >
118 parse_selector(string const & str,
119 app_state & app)
120 {
121 vector<pair<selector_type, string> > sels;
122
123 // this rule should always be enabled, even if the user specifies
124 // --norc: if you provide a revision id, you get a revision id.
125 if (str.find_first_not_of(constants::legal_id_bytes) == string::npos
126 && str.size() == constants::idlen)
127 {
128 sels.push_back(make_pair(sel_ident, str));
129 }
130 else
131 {
132 typedef boost::tokenizer<boost::escaped_list_separator<char> > tokenizer;
133 boost::escaped_list_separator<char> slash("\\", "/", "");
134 tokenizer tokens(str, slash);
135
136 vector<string> selector_strings;
137 copy(tokens.begin(), tokens.end(), back_inserter(selector_strings));
138
139 for (vector<string>::const_iterator i = selector_strings.begin();
140 i != selector_strings.end(); ++i)
141 {
142 string sel;
143 selector_type type = sel_unknown;
144
145 decode_selector(*i, type, sel, app);
146 sels.push_back(make_pair(type, sel));
147 }
148 }
149
150 return sels;
151 }
152
153}; // namespace selectors
154
155// Local Variables:
156// mode: C++
157// fill-column: 76
158// c-file-style: "gnu"
159// indent-tabs-mode: nil
160// End:
161// 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