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 "frontends/alert.h"
18 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
22 #include "support/Path.h"
23 #include "support/Systemcall.h"
25 #include <boost/regex.hpp>
30 using namespace lyx::support;
33 using boost::regex_match;
39 int VCS::doVCCommand(string const & cmd, FileName const & path)
41 LYXERR(Debug::LYXVC, "doVCCommand: " << cmd);
43 support::PathChanger p(path);
44 int const ret = one.startscript(Systemcall::Wait, cmd);
46 frontend::Alert::error(_("Revision control error."),
47 bformat(_("Please check you have installed the program called in\n"
54 /////////////////////////////////////////////////////////////////////
58 /////////////////////////////////////////////////////////////////////
60 RCS::RCS(FileName const & m)
67 FileName const RCS::findFile(FileName const & file)
69 // Check if *,v exists.
70 FileName tmp(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 << " is under rcs.");
77 // Check if RCS/*,v exists.
78 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
79 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
80 if (tmp.isReadableFile()) {
81 LYXERR(Debug::LYXVC, "Yes " << file << " it is under rcs.");
89 void RCS::retrieve(FileName const & file)
91 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
92 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
97 void RCS::scanMaster()
99 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster.");
101 ifstream ifs(master_.toFilesystemEncoding().c_str());
104 bool read_enough = false;
106 while (!read_enough && ifs >> token) {
107 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
112 else if (token == "head") {
116 tmv = rtrim(tmv, ";");
118 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
119 } else if (contains(token, "access")
120 || contains(token, "symbols")
121 || contains(token, "strict")) {
123 } else if (contains(token, "locks")) {
125 if (contains(token, ';')) {
126 locker_ = "Unlocked";
135 s1 = rtrim(tmpt, ";");
136 // tmp is now in the format <user>:<version>
137 s1 = split(s1, s2, ':');
138 // s2 is user, and s1 is version
139 if (s1 == version_) {
144 } while (!contains(tmpt, ';'));
146 } else if (token == "comment") {
147 // we don't need to read any further than this.
151 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
157 void RCS::registrer(string const & msg)
159 string cmd = "ci -q -u -i -t-\"";
162 cmd += quoteName(onlyFilename(owner_->absFileName()));
163 doVCCommand(cmd, FileName(owner_->filePath()));
167 void RCS::checkIn(string const & msg)
169 doVCCommand("ci -q -u -m\"" + msg + "\" "
170 + quoteName(onlyFilename(owner_->absFileName())),
171 FileName(owner_->filePath()));
174 bool RCS::checkInEnabled()
176 return owner_ && !owner_->isReadonly();
182 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
183 FileName(owner_->filePath()));
187 bool RCS::checkOutEnabled()
189 return owner_ && owner_->isReadonly();
195 doVCCommand("co -f -u" + version() + " "
196 + quoteName(onlyFilename(owner_->absFileName())),
197 FileName(owner_->filePath()));
198 // We ignore changes and just reload!
205 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
206 doVCCommand("rcs -o" + version() + " "
207 + quoteName(onlyFilename(owner_->absFileName())),
208 FileName(owner_->filePath()));
212 bool RCS::undoLastEnabled()
218 void RCS::getLog(FileName const & tmpf)
220 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
221 + " > " + tmpf.toFilesystemEncoding(),
222 FileName(owner_->filePath()));
226 bool RCS::toggleReadOnlyEnabled()
232 /////////////////////////////////////////////////////////////////////
236 /////////////////////////////////////////////////////////////////////
238 CVS::CVS(FileName const & m, FileName const & f)
246 FileName const CVS::findFile(FileName const & file)
248 // First we look for the CVS/Entries in the same dir
249 // where we have file.
250 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
251 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
252 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
253 << "' for `" << tmpf << '\'');
254 if (entries.isReadableFile()) {
255 // Ok we are at least in a CVS dir. Parse the CVS/Entries
256 // and see if we can find this file. We do a fast and
258 ifstream ifs(entries.toFilesystemEncoding().c_str());
260 while (getline(ifs, line)) {
261 LYXERR(Debug::LYXVC, "\tEntries: " << line);
262 if (contains(line, tmpf))
270 void CVS::scanMaster()
272 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
273 // Ok now we do the real scan...
274 ifstream ifs(master_.toFilesystemEncoding().c_str());
275 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
276 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
278 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
279 while (getline(ifs, line)) {
280 LYXERR(Debug::LYXVC, "\t line: " << line);
281 if (contains(line, tmpf)) {
282 // Ok extract the fields.
285 regex_match(line, sm, reg);
287 //sm[0]; // whole matched string
289 version_ = sm.str(2);
290 string const file_date = sm.str(3);
293 //sm[5]; // tag or tagdate
294 // FIXME: must double check file is stattable/existing
295 time_t mod = file_.lastModified();
296 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
297 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
298 << "'\nModification date of file: `" << mod_date << '\'');
299 //FIXME this whole locking bussiness is not working under cvs and the machinery
300 // conforms to the ci usage, not cvs.
301 if (file_date == mod_date) {
302 locker_ = "Unlocked";
305 // Here we should also to some more checking
306 // to see if there are conflicts or not.
316 void CVS::registrer(string const & msg)
318 doVCCommand("cvs -q add -m \"" + msg + "\" "
319 + quoteName(onlyFilename(owner_->absFileName())),
320 FileName(owner_->filePath()));
324 void CVS::checkIn(string const & msg)
326 doVCCommand("cvs -q commit -m \"" + msg + "\" "
327 + quoteName(onlyFilename(owner_->absFileName())),
328 FileName(owner_->filePath()));
332 bool CVS::checkInEnabled()
340 // cvs update or perhaps for cvs this should be a noop
341 // we need to detect conflict (eg "C" in output)
342 // before we can do this.
343 lyxerr << "Sorry not implemented." << endl;
347 bool CVS::checkOutEnabled()
355 // This is sensitive operation, so at lest some check before
356 if (doVCCommand("cvs --help", FileName(owner_->filePath())))
358 // Reverts to the version in CVS repository and
359 // gets the updated version from the repository.
360 string const fil = quoteName(onlyFilename(owner_->absFileName()));
362 doVCCommand("rm -f " + fil + "; cvs update " + fil,
363 FileName(owner_->filePath()));
370 // merge the current with the previous version
371 // in a reverse patch kind of way, so that the
372 // result is to revert the last changes.
373 lyxerr << "Sorry not implemented." << endl;
377 bool CVS::undoLastEnabled()
383 void CVS::getLog(FileName const & tmpf)
385 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
386 + " > " + tmpf.toFilesystemEncoding(),
387 FileName(owner_->filePath()));
390 bool CVS::toggleReadOnlyEnabled()
395 /////////////////////////////////////////////////////////////////////
399 /////////////////////////////////////////////////////////////////////
401 SVN::SVN(FileName const & m, FileName const & f)
409 FileName const SVN::findFile(FileName const & file)
411 // First we look for the CVS/Entries in the same dir
412 // where we have file.
413 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
414 string const tmpf = onlyFilename(file.absFilename());
415 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
416 << "' for `" << tmpf << '\'');
417 if (entries.isReadableFile()) {
418 // Ok we are at least in a CVS dir. Parse the CVS/Entries
419 // and see if we can find this file. We do a fast and
421 ifstream ifs(entries.toFilesystemEncoding().c_str());
422 string line, oldline;
423 while (getline(ifs, line)) {
424 if (line == "dir" || line == "file")
425 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
426 if (oldline == tmpf && line == "file")
435 void SVN::scanMaster()
437 // if we want some locking under svn
438 // we need different infrastructure around
439 locker_ = "Unlocked";
444 void SVN::registrer(string const & msg)
446 doVCCommand("svn -q add " + quoteName(onlyFilename(owner_->absFileName())),
447 FileName(owner_->filePath()));
451 void SVN::checkIn(string const & msg)
453 doVCCommand("svn -q commit -m \"" + msg + "\" "
454 + quoteName(onlyFilename(owner_->absFileName())),
455 FileName(owner_->filePath()));
459 bool SVN::checkInEnabled()
467 lyxerr << "Sorry not implemented." << endl;
471 bool SVN::checkOutEnabled()
479 // Reverts to the version in CVS repository and
480 // gets the updated version from the repository.
481 string const fil = quoteName(onlyFilename(owner_->absFileName()));
483 doVCCommand("svn revert -q " + fil,
484 FileName(owner_->filePath()));
491 // merge the current with the previous version
492 // in a reverse patch kind of way, so that the
493 // result is to revert the last changes.
494 lyxerr << "Sorry not implemented." << endl;
498 bool SVN::undoLastEnabled()
504 void SVN::getLog(FileName const & tmpf)
506 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
507 + " > " + tmpf.toFilesystemEncoding(),
508 FileName(owner_->filePath()));
512 bool SVN::toggleReadOnlyEnabled()