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 "VCBackend.h"
16 #include "support/debug.h"
17 #include "support/filetools.h"
18 #include "support/lstrings.h"
19 #include "support/Path.h"
20 #include "support/Systemcall.h"
22 #include <boost/regex.hpp>
27 using namespace lyx::support;
30 using boost::regex_match;
36 int VCS::doVCCommand(string const & cmd, FileName const & path)
38 LYXERR(Debug::LYXVC, "doVCCommand: " << cmd);
40 support::PathChanger p(path);
41 int const ret = one.startscript(Systemcall::Wait, cmd);
46 /////////////////////////////////////////////////////////////////////
50 /////////////////////////////////////////////////////////////////////
52 RCS::RCS(FileName const & m)
59 FileName const RCS::findFile(FileName const & file)
61 // Check if *,v exists.
62 FileName tmp(file.absFilename() + ",v");
63 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
64 if (tmp.isReadableFile()) {
65 LYXERR(Debug::LYXVC, "Yes " << file << " is under rcs.");
69 // Check if RCS/*,v exists.
70 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
71 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
72 if (tmp.isReadableFile()) {
73 LYXERR(Debug::LYXVC, "Yes " << file << " it is under rcs.");
81 void RCS::retrieve(FileName const & file)
83 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
84 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
89 void RCS::scanMaster()
91 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster.");
93 ifstream ifs(master_.toFilesystemEncoding().c_str());
96 bool read_enough = false;
98 while (!read_enough && ifs >> token) {
99 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
104 else if (token == "head") {
108 tmv = rtrim(tmv, ";");
110 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
111 } else if (contains(token, "access")
112 || contains(token, "symbols")
113 || contains(token, "strict")) {
115 } else if (contains(token, "locks")) {
117 if (contains(token, ';')) {
118 locker_ = "Unlocked";
127 s1 = rtrim(tmpt, ";");
128 // tmp is now in the format <user>:<version>
129 s1 = split(s1, s2, ':');
130 // s2 is user, and s1 is version
131 if (s1 == version_) {
136 } while (!contains(tmpt, ';'));
138 } else if (token == "comment") {
139 // we don't need to read any further than this.
143 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
149 void RCS::registrer(string const & msg)
151 string cmd = "ci -q -u -i -t-\"";
154 cmd += quoteName(onlyFilename(owner_->absFileName()));
155 doVCCommand(cmd, FileName(owner_->filePath()));
159 void RCS::checkIn(string const & msg)
161 doVCCommand("ci -q -u -m\"" + msg + "\" "
162 + quoteName(onlyFilename(owner_->absFileName())),
163 FileName(owner_->filePath()));
166 bool RCS::checkInEnabled()
168 return owner_ && !owner_->isReadonly();
174 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
175 FileName(owner_->filePath()));
179 bool RCS::checkOutEnabled()
181 return owner_ && owner_->isReadonly();
187 doVCCommand("co -f -u" + version() + " "
188 + quoteName(onlyFilename(owner_->absFileName())),
189 FileName(owner_->filePath()));
190 // We ignore changes and just reload!
197 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
198 doVCCommand("rcs -o" + version() + " "
199 + quoteName(onlyFilename(owner_->absFileName())),
200 FileName(owner_->filePath()));
204 bool RCS::undoLastEnabled()
210 void RCS::getLog(FileName const & tmpf)
212 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
213 + " > " + tmpf.toFilesystemEncoding(),
214 FileName(owner_->filePath()));
218 /////////////////////////////////////////////////////////////////////
222 /////////////////////////////////////////////////////////////////////
224 CVS::CVS(FileName const & m, FileName const & f)
232 FileName const CVS::findFile(FileName const & file)
234 // First we look for the CVS/Entries in the same dir
235 // where we have file.
236 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
237 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
238 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
239 << "' for `" << tmpf << '\'');
240 if (entries.isReadableFile()) {
241 // Ok we are at least in a CVS dir. Parse the CVS/Entries
242 // and see if we can find this file. We do a fast and
244 ifstream ifs(entries.toFilesystemEncoding().c_str());
246 while (getline(ifs, line)) {
247 LYXERR(Debug::LYXVC, "\tEntries: " << line);
248 if (contains(line, tmpf))
256 void CVS::scanMaster()
258 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
259 // Ok now we do the real scan...
260 ifstream ifs(master_.toFilesystemEncoding().c_str());
261 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
262 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
264 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
265 while (getline(ifs, line)) {
266 LYXERR(Debug::LYXVC, "\t line: " << line);
267 if (contains(line, tmpf)) {
268 // Ok extract the fields.
271 regex_match(line, sm, reg);
273 //sm[0]; // whole matched string
275 version_ = sm.str(2);
276 string const file_date = sm.str(3);
279 //sm[5]; // tag or tagdate
280 // FIXME: must double check file is stattable/existing
281 time_t mod = file_.lastModified();
282 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
283 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
284 << "'\nModification date of file: `" << mod_date << '\'');
285 //FIXME this whole locking bussiness is not working under cvs and the machinery
286 // conforms to the ci usage, not cvs.
287 if (file_date == mod_date) {
288 locker_ = "Unlocked";
291 // Here we should also to some more checking
292 // to see if there are conflicts or not.
302 void CVS::registrer(string const & msg)
304 doVCCommand("cvs -q add -m \"" + msg + "\" "
305 + quoteName(onlyFilename(owner_->absFileName())),
306 FileName(owner_->filePath()));
310 void CVS::checkIn(string const & msg)
312 doVCCommand("cvs -q commit -m \"" + msg + "\" "
313 + quoteName(onlyFilename(owner_->absFileName())),
314 FileName(owner_->filePath()));
318 bool CVS::checkInEnabled()
326 // cvs update or perhaps for cvs this should be a noop
327 lyxerr << "Sorry not implemented." << endl;
331 bool CVS::checkOutEnabled()
339 // Reverts to the version in CVS repository and
340 // gets the updated version from the repository.
341 string const fil = quoteName(onlyFilename(owner_->absFileName()));
343 doVCCommand("rm -f " + fil + "; cvs update " + fil,
344 FileName(owner_->filePath()));
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 bool CVS::undoLastEnabled()
364 void CVS::getLog(FileName const & tmpf)
366 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
367 + " > " + tmpf.toFilesystemEncoding(),
368 FileName(owner_->filePath()));