monotone

monotone Mtn Source Tree

Root/selectors.cc

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

Archive Download this file

Branches

Tags

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