-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXformCvsStatus.cpp
226 lines (206 loc) · 5.03 KB
/
XformCvsStatus.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//TODO: Add unit tests
#include "XformCvsStatus.h"
#include "Exceptions.h"
#include "main.h"
#include "Utils.h"
#include <algorithm>
#include <boost/algorithm/string/trim.hpp>
#include <boost/format.hpp>
#include <cstdlib>
#include <iostream>
#include <regex>
#include <stdexcept>
#include <utility>
#if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
#endif
namespace b = ::boost;
using ::std::cin;
using ::std::cout;
using ::std::endl;
using ::std::getline;
using ::std::ostream;
using ::std::pair;
using ::std::regex;
using ::std::smatch;
using ::std::string;
using ::std::string_view;
static const char k_lastWorkingDirPattern[] = "^cvs (?:server|status): Examining (.*)$";
static const char k_newFilePattern[] = "^(\\?) (.*)$";
static const char k_fileStatusPattern[] = "^File: (?:no file )?(.*)[ \t]+Status: (.*)$";
#if !defined(CMDLINEUTIL_TEST_MODE)
int main(int argCount, const char*const*const argList)
{
return commonMain<XformCvsStatus>(argCount, argList);
}
#endif
int XformCvsStatus::usage(ostream& strm, string_view progName, const char* pMsg)
{
int exitCode = EXIT_SUCCESS;
if (pMsg != nullptr && *pMsg != '\0')
{
exitCode = EXIT_FAILURE;
strm << endl << pMsg << endl;
}
strm << "\n"
"Usage: " << progName << " [-n] [-u] [-l]\n"
"\n"
"Takes the output of the \"cvs status\" command and transforms it\n"
"to a more succinct summary, with one file per line. Options:\n"
"\n"
" -n Suppresses new (unknown) files from the listing\n"
"\n"
" -u Suppresses up-to-date files from the listing\n"
"\n"
" -l Suppresses locally modified, locally added, and locally\n"
" removed files from the listing\n"
<< endl;
return exitCode;
}
XformCvsStatus::XformCvsStatus(::std::span<const char*const> args) :
m_suppressNew(false),
m_suppressUpToDate(false),
m_suppressLocal(false),
m_lastWorkingDir("."),
m_map()
{
for (auto pArg : args)
{
if (isIEqual(pArg, "-?") || isIEqual(pArg, "-h") || isIEqual(pArg, "-help"))
{
throw CmdLineError();
}
else if (isIEqual(pArg, "-n"))
{
m_suppressNew = true;
}
else if (isIEqual(pArg, "-u"))
{
m_suppressUpToDate = true;
}
else if (isIEqual(pArg, "-l"))
{
m_suppressLocal = true;
}
else
{
throw CmdLineError(b::format("Unrecognized option \"%1%\"") % pArg);
}
}
}
string XformCvsStatus::pathStringToConsoleString(const Path::string_type& str)
{
#if defined(_WIN32)
int bufSize = ::WideCharToMultiByte(CP_OEMCP, 0, str.c_str(), -1, 0, 0, 0, 0);
if (bufSize == 0 && ::GetLastError() != ERROR_SUCCESS)
{
throw WindowsError(b::format("WideCharToMultiByte failure, 1st call, error code %1%")
% ::GetLastError());
}
bufSize += 2;
::std::vector<char> buffer(bufSize);
if (::WideCharToMultiByte(CP_OEMCP, 0, str.c_str(), -1, &(buffer[0]), bufSize, 0, 0) == 0
&& ::GetLastError() != ERROR_SUCCESS)
{
throw WindowsError(b::format("WideCharToMultiByte failure, 2nd call, error code %1%")
% ::GetLastError());
}
return &(buffer[0]);
#else
return str;
#endif
}
int XformCvsStatus::run()
{
regex lwdRegex(k_lastWorkingDirPattern);
regex newRegex(k_newFilePattern);
regex statusRegex(k_fileStatusPattern);
while (cin)
{
string line;
getline(cin, line);
smatch match;
if (regex_match(line, match, lwdRegex))
{
m_lastWorkingDir = string(match[1]);
}
else if (regex_match(line, match, newRegex))
{
insertInMap(match[2], match[1]);
}
else if (regex_match(line, match, statusRegex))
{
insertInMap(match[1], match[2]);
}
}
for (auto const& mapEntry : m_map)
{
string st = mapEntry.second;
if ((!m_suppressNew || st != "? ")
&& (!m_suppressUpToDate || st != " ")
&& (!m_suppressLocal || (st != "M " && st != "A " && st != "D ")))
{
cout << st << " " << pathStringToConsoleString(mapEntry.first.native()) << endl;
}
}
return EXIT_SUCCESS;
}
void XformCvsStatus::insertInMap(const string& rawFile, const string& rawStatus)
{
Path file = buildPath(rawFile);
string status = xlateStatus(rawStatus);
pair<FileToStatusMap::iterator, bool> result = m_map.insert(
make_pair(file, status));
if (!result.second && result.first->second == "? " && status == "A ")
{
result.first->second = status;
}
}
XformCvsStatus::Path XformCvsStatus::buildPath(const string& file) const
{
string trFile = b::trim_copy(file);
return (m_lastWorkingDir.empty() || m_lastWorkingDir == ".")
? Path{trFile}
: m_lastWorkingDir / trFile;
}
string XformCvsStatus::xlateStatus(const string& status)
{
string trStatus = b::trim_copy(status);
if (trStatus == "Locally Added")
{
return "A ";
}
else if (trStatus == "Locally Modified")
{
return "M ";
}
else if (trStatus == "Locally Removed")
{
return "D ";
}
else if (trStatus == "Up-to-date")
{
return " ";
}
else if (trStatus == "?" || trStatus == "Unknown")
{
return "? ";
}
else if (trStatus == "Needs Checkout" || trStatus == "Needs Patch")
{
return " *";
}
else if (trStatus == "Needs Merge")
{
return "M*";
}
else if (trStatus == "File had conflicts on merge" || trStatus == "Unresolved Conflict")
{
return "C ";
}
else
{
return trStatus;
}
}