-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathversion.cpp
More file actions
195 lines (171 loc) · 4.53 KB
/
version.cpp
File metadata and controls
195 lines (171 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (C) 2016-2017, Egor Pugin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "version.h"
#include <regex>
const std::regex r_branch_name(R"(([a-zA-Z_][a-zA-Z0-9_-]*))");
const std::regex r_version1(R"((\d+))");
const std::regex r_version2(R"((\d+)\.(\d+))");
const std::regex r_version3(R"((\d+)\.(\d+)\.(\d+))");
Version::Version(ProjectVersionNumber ma, ProjectVersionNumber mi, ProjectVersionNumber pa)
: major(ma), minor(mi), patch(pa)
{
}
Version::Version(const String &s)
{
if (s == "*")
{
type = VersionType::Any;
return;
}
if (s == "=")
{
type = VersionType::Equal;
return;
}
type = VersionType::Version;
std::smatch m;
if (std::regex_match(s, m, r_version3))
{
major = std::stoi(m[1].str());
minor = std::stoi(m[2].str());
patch = std::stoi(m[3].str());
}
else if (std::regex_match(s, m, r_version2))
{
major = std::stoi(m[1].str());
minor = std::stoi(m[2].str());
}
else if (std::regex_match(s, m, r_version1))
{
major = std::stoi(m[1].str());
}
else if (std::regex_match(s, m, r_branch_name))
{
branch = m[1].str();
String error;
if (!check_branch_name(branch, &error))
throw std::runtime_error(error);
type = VersionType::Branch;
}
else
throw std::runtime_error("Bad version");
if (!isValid())
throw std::runtime_error("Bad version");
}
String Version::toString() const
{
if (!branch.empty())
return branch;
String s;
s += std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(patch);
return s;
}
String Version::toAnyVersion() const
{
if (!branch.empty())
return branch;
if (type == VersionType::Equal)
return "=";
if (major == -1 && minor == -1 && patch == -1)
return "*";
String s;
s += std::to_string(major) + ".";
if (minor != -1)
{
s += std::to_string(minor) + ".";
}
if (patch != -1)
{
s += std::to_string(patch) + ".";
}
s.resize(s.size() - 1);
return s;
}
path Version::toPath() const
{
if (!branch.empty())
return branch;
path p;
p /= std::to_string(major);
p /= std::to_string(minor);
p /= std::to_string(patch);
return p;
}
bool Version::isValid() const
{
if (!branch.empty())
return check_branch_name(branch);
if (major == 0 && minor == 0 && patch == 0)
return false;
if (major < -1 || minor < -1 || patch < -1)
return false;
return true;
}
bool Version::isBranch() const
{
return !branch.empty();
}
bool Version::isVersion() const
{
return !isBranch();
}
bool Version::operator<(const Version &rhs) const
{
if (isBranch() && rhs.isBranch())
return branch < rhs.branch;
if (isBranch())
return true;
if (rhs.isBranch())
return false;
return std::tie(major, minor, patch) < std::tie(rhs.major, rhs.minor, rhs.patch);
}
bool Version::operator==(const Version &rhs) const
{
if (isBranch() && rhs.isBranch())
return branch == rhs.branch;
if (isBranch() || rhs.isBranch())
return false;
return std::tie(major, minor, patch) == std::tie(rhs.major, rhs.minor, rhs.patch);
}
bool Version::operator!=(const Version &rhs) const
{
return !operator==(rhs);
}
bool Version::canBe(const Version &rhs) const
{
if (*this == rhs)
return true;
// *.*.* canBe anything
if (major == -1 && minor == -1 && patch == -1)
return true;
// 1.*.* == 1.*.*
if (major == rhs.major && minor == -1 && patch == -1)
return true;
// 1.2.* == 1.2.*
if (major == rhs.major && minor == rhs.minor && patch == -1)
return true;
return false;
}
bool Version::check_branch_name(const String &n, String *error)
{
if (!std::regex_match(n, r_branch_name))
{
if (error)
*error = "Branch name should be a-zA-Z0-9_- starting with letter or _";
return false;
}
return true;
}