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