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 'h':
50 type = sel_head;
51 break;
52 case 'd':
53 type = sel_date;
54 break;
55 case 'i':
56 type = sel_ident;
57 break;
58 case 't':
59 type = sel_tag;
60 break;
61 case 'c':
62 type = sel_cert;
63 break;
64 case 'l':
65 type = sel_later;
66 break;
67 case 'e':
68 type = sel_earlier;
69 break;
70 default:
71 W(F("unknown selector type: %c\n") % sel[0]);
72 break;
73 }
74 sel.erase(0,2);
75
76 /* a selector date-related should be validated */
77 if (sel_date==type || sel_later==type || sel_earlier==type)
78 {
79 N (app.lua.hook_expand_date(sel, tmp),
80 F ("selector '%s' is not a valid date\n") % sel);
81
82 if (tmp.size()<8 && (sel_later==type || sel_earlier==type))
83 tmp += "-01T00:00:00";
84 else if (tmp.size()<11 && (sel_later==type || sel_earlier==type))
85 tmp += "T00:00:00";
86 N(tmp.size()==19 || sel_date==type, F ("selector '%s' is not a valid date (%s)\n") % sel % tmp);
87 if (sel != tmp)
88 {
89 P (F ("expanded date '%s' -> '%s'\n") % sel % tmp);
90 sel = tmp;
91 }
92 }
93 }
94 }
95
96 void
97 complete_selector(std::string const & orig_sel,
98 std::vector<std::pair<selector_type, std::string> > const & limit,
99 selector_type & type,
100 std::set<std::string> & completions,
101 app_state & app)
102 {
103 std::string sel;
104 decode_selector(orig_sel, type, sel, app);
105 app.db.complete(type, sel, limit, completions);
106 }
107
108 std::vector<std::pair<selector_type, std::string> >
109 parse_selector(std::string const & str,
110 app_state & app)
111 {
112 std::vector<std::pair<selector_type, std::string> > sels;
113
114 // this rule should always be enabled, even if the user specifies
115 // --norc: if you provide a revision id, you get a revision id.
116 if (str.find_first_not_of(constants::legal_id_bytes) == std::string::npos
117 && str.size() == constants::idlen)
118 {
119 sels.push_back(std::make_pair(sel_ident, str));
120 }
121 else
122 {
123 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
124 boost::char_separator<char> slash("/");
125 tokenizer tokens(str, slash);
126
127 std::vector<std::string> selector_strings;
128 copy(tokens.begin(), tokens.end(), back_inserter(selector_strings));
129
130 for (std::vector<std::string>::const_iterator i = selector_strings.begin();
131 i != selector_strings.end(); ++i)
132 {
133 std::string sel;
134 selector_type type = sel_unknown;
135
136 decode_selector(*i, type, sel, app);
137 sels.push_back(std::make_pair(type, sel));
138 }
139 }
140
141 return sels;
142 }
143
144}; // namespace selectors

Archive Download this file

Branches

Tags

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