3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "vc-backend.h"
17 #include "support/FileInfo.h"
18 #include "support/path.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/systemcall.h"
23 #include <boost/regex.hpp>
27 using lyx::support::AddName;
28 using lyx::support::AddPath;
29 using lyx::support::contains;
30 using lyx::support::FileInfo;
31 using lyx::support::OnlyFilename;
32 using lyx::support::OnlyPath;
33 using lyx::support::Path;
34 using lyx::support::rtrim;
35 using lyx::support::split;
36 using lyx::support::Systemcall;
38 #ifndef CXX_GLOBAL_CSTD
49 using boost::regex_match;
51 #ifndef USE_INCLUDED_STRING
58 int VCS::doVCCommand(string const & cmd, string const & path)
60 lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
63 int const ret = one.startscript(Systemcall::Wait, cmd);
68 RCS::RCS(string const & m)
75 string const RCS::find_file(string const & file)
78 // Check if *,v exists.
81 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
83 if (f.newFile(tmp).readable()) {
84 lyxerr[Debug::LYXVC] << "Yes " << file
85 << " is under rcs." << endl;
88 // Check if RCS/*,v exists.
89 tmp = AddName(AddPath(OnlyPath(file), "RCS"), file);
91 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
93 if (f.newFile(tmp).readable()) {
94 lyxerr[Debug::LYXVC] << "Yes " << file
95 << " it is under rcs."<< endl;
103 void RCS::retrieve(string const & file)
105 lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
106 VCS::doVCCommand("co -q -r \""
112 void RCS::scanMaster()
114 lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
116 ifstream ifs(master_.c_str());
119 bool read_enough = false;
121 while (!read_enough && ifs >> token) {
123 << "LyXVC::scanMaster: current lex text: `"
124 << token << '\'' << endl;
128 else if (token == "head") {
132 tmv = rtrim(tmv, ";");
134 lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
136 } else if (contains(token, "access")
137 || contains(token, "symbols")
138 || contains(token, "strict")) {
140 } else if (contains(token, "locks")) {
142 if (contains(token, ";")) {
143 locker_ = "Unlocked";
152 s1 = rtrim(tmpt, ";");
153 // tmp is now in the format <user>:<version>
154 s1 = split(s1, s2, ':');
155 // s2 is user, and s1 is version
156 if (s1 == version_) {
161 } while (!contains(tmpt, ";"));
163 } else if (token == "comment") {
164 // we don't need to read any further than this.
169 << "LyXVC::scanMaster(): unexpected token"
176 void RCS::registrer(string const & msg)
178 string cmd = "ci -q -u -i -t-\"";
181 cmd += OnlyFilename(owner_->fileName());
183 doVCCommand(cmd, owner_->filePath());
187 void RCS::checkIn(string const & msg)
189 doVCCommand("ci -q -u -m\"" + msg + "\" \""
190 + OnlyFilename(owner_->fileName()) + '"',
198 doVCCommand("co -q -l \""
199 + OnlyFilename(owner_->fileName()) + '"',
206 doVCCommand("co -f -u" + version() + " \""
207 + OnlyFilename(owner_->fileName()) + '"',
209 // We ignore changes and just reload!
216 lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
217 doVCCommand("rcs -o" + version() + " \""
218 + OnlyFilename(owner_->fileName()) + '"',
223 void RCS::getLog(string const & tmpf)
225 doVCCommand("rlog \""
226 + OnlyFilename(owner_->fileName()) + "\" > "
227 + tmpf, owner_->filePath());
231 CVS::CVS(string const & m, string const & f)
239 string const CVS::find_file(string const & file)
241 // First we look for the CVS/Entries in the same dir
242 // where we have file.
243 string const dir = OnlyPath(file) + "/CVS/Entries";
244 string const tmpf = "/" + OnlyFilename(file) + "/";
245 lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
246 << "' for `" << tmpf << '\'' << endl;
247 FileInfo const f(dir);
249 // Ok we are at least in a CVS dir. Parse the CVS/Entries
250 // and see if we can find this file. We do a fast and
252 ifstream ifs(dir.c_str());
254 while (getline(ifs, line)) {
255 lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
256 if (contains(line, tmpf)) return dir;
263 void CVS::scanMaster()
265 lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n Checking: "
267 // Ok now we do the real scan...
268 ifstream ifs(master_.c_str());
269 string tmpf = "/" + OnlyFilename(file_) + "/";
270 lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
272 regex reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
273 while (getline(ifs, line)) {
274 lyxerr[Debug::LYXVC] << "\t line: " << line << endl;
275 if (contains(line, tmpf)) {
276 // Ok extract the fields.
277 #ifndef USE_INCLUDED_STRING
282 regex_match(STRCONV(line), sm, reg);
284 //sm[0]; // whole matched string
286 version_ = STRCONV(sm.str(2));
287 string const file_date = STRCONV(sm.str(3));
290 //sm[5]; // tag or tagdate
292 // FIXME: must double check file is stattable/existing
293 time_t mod = fi.getModificationTime();
294 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
296 << "Date in Entries: `" << file_date
297 << "'\nModification date of file: `"
298 << mod_date << '\'' << endl;
299 if (file_date == mod_date) {
300 locker_ = "Unlocked";
303 // Here we should also to some more checking
304 // to see if there are conflicts or not.
314 void CVS::registrer(string const & msg)
316 doVCCommand("cvs -q add -m \"" + msg + "\" \""
317 + OnlyFilename(owner_->fileName()) + '"',
322 void CVS::checkIn(string const & msg)
324 doVCCommand("cvs -q commit -m \"" + msg + "\" \""
325 + OnlyFilename(owner_->fileName()) + '"',
332 // cvs update or perhaps for cvs this should be a noop
333 lyxerr << "Sorry not implemented." << endl;
339 // Reverts to the version in CVS repository and
340 // gets the updated version from the repository.
341 string const fil = OnlyFilename(owner_->fileName());
343 doVCCommand("rm -f \"" + fil + "\"; cvs update \"" + fil + '"',
351 // merge the current with the previous version
352 // in a reverse patch kind of way, so that the
353 // result is to revert the last changes.
354 lyxerr << "Sorry not implemented." << endl;
358 void CVS::getLog(string const & tmpf)
360 doVCCommand("cvs log \""
361 + OnlyFilename(owner_->fileName()) + "\" > " + tmpf,