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::doVCCommandCall(string const & cmd, FileName const & path){
40 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
42 support::PathChanger p(path);
43 return one.startscript(Systemcall::Wait, cmd);
46 int VCS::doVCCommand(string const & cmd, FileName const & path)
48 owner_->setBusy(true);
49 int const ret = doVCCommandCall(cmd, path);
50 owner_->setBusy(false);
52 frontend::Alert::error(_("Revision control error."),
53 bformat(_("Some problem occured while running the command:\n"
60 /////////////////////////////////////////////////////////////////////
64 /////////////////////////////////////////////////////////////////////
66 RCS::RCS(FileName const & m)
73 FileName const RCS::findFile(FileName const & file)
75 // Check if *,v exists.
76 FileName tmp(file.absFilename() + ",v");
77 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
78 if (tmp.isReadableFile()) {
79 LYXERR(Debug::LYXVC, "Yes " << file << " is under rcs.");
83 // Check if RCS/*,v exists.
84 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
85 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
86 if (tmp.isReadableFile()) {
87 LYXERR(Debug::LYXVC, "Yes " << file << " it is under rcs.");
95 void RCS::retrieve(FileName const & file)
97 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
98 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
103 void RCS::scanMaster()
105 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster.");
107 ifstream ifs(master_.toFilesystemEncoding().c_str());
110 bool read_enough = false;
112 while (!read_enough && ifs >> token) {
113 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
118 else if (token == "head") {
122 tmv = rtrim(tmv, ";");
124 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
125 } else if (contains(token, "access")
126 || contains(token, "symbols")
127 || contains(token, "strict")) {
129 } else if (contains(token, "locks")) {
131 if (contains(token, ';')) {
132 locker_ = "Unlocked";
141 s1 = rtrim(tmpt, ";");
142 // tmp is now in the format <user>:<version>
143 s1 = split(s1, s2, ':');
144 // s2 is user, and s1 is version
145 if (s1 == version_) {
150 } while (!contains(tmpt, ';'));
152 } else if (token == "comment") {
153 // we don't need to read any further than this.
157 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
163 void RCS::registrer(string const & msg)
165 string cmd = "ci -q -u -i -t-\"";
168 cmd += quoteName(onlyFilename(owner_->absFileName()));
169 doVCCommand(cmd, FileName(owner_->filePath()));
173 void RCS::checkIn(string const & msg)
175 doVCCommand("ci -q -u -m\"" + msg + "\" "
176 + quoteName(onlyFilename(owner_->absFileName())),
177 FileName(owner_->filePath()));
180 bool RCS::checkInEnabled()
182 return owner_ && !owner_->isReadonly();
188 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
189 FileName(owner_->filePath()));
193 bool RCS::checkOutEnabled()
195 return owner_ && owner_->isReadonly();
201 doVCCommand("co -f -u" + version() + " "
202 + quoteName(onlyFilename(owner_->absFileName())),
203 FileName(owner_->filePath()));
204 // We ignore changes and just reload!
211 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
212 doVCCommand("rcs -o" + version() + " "
213 + quoteName(onlyFilename(owner_->absFileName())),
214 FileName(owner_->filePath()));
218 bool RCS::undoLastEnabled()
224 void RCS::getLog(FileName const & tmpf)
226 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
227 + " > " + tmpf.toFilesystemEncoding(),
228 FileName(owner_->filePath()));
232 bool RCS::toggleReadOnlyEnabled()
238 /////////////////////////////////////////////////////////////////////
242 /////////////////////////////////////////////////////////////////////
244 CVS::CVS(FileName const & m, FileName const & f)
252 FileName const CVS::findFile(FileName const & file)
254 // First we look for the CVS/Entries in the same dir
255 // where we have file.
256 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
257 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
258 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
259 << "' for `" << tmpf << '\'');
260 if (entries.isReadableFile()) {
261 // Ok we are at least in a CVS dir. Parse the CVS/Entries
262 // and see if we can find this file. We do a fast and
264 ifstream ifs(entries.toFilesystemEncoding().c_str());
266 while (getline(ifs, line)) {
267 LYXERR(Debug::LYXVC, "\tEntries: " << line);
268 if (contains(line, tmpf))
276 void CVS::scanMaster()
278 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
279 // Ok now we do the real scan...
280 ifstream ifs(master_.toFilesystemEncoding().c_str());
281 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
282 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
284 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
285 while (getline(ifs, line)) {
286 LYXERR(Debug::LYXVC, "\t line: " << line);
287 if (contains(line, tmpf)) {
288 // Ok extract the fields.
291 regex_match(line, sm, reg);
293 //sm[0]; // whole matched string
295 version_ = sm.str(2);
296 string const file_date = sm.str(3);
299 //sm[5]; // tag or tagdate
300 // FIXME: must double check file is stattable/existing
301 time_t mod = file_.lastModified();
302 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
303 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
304 << "'\nModification date of file: `" << mod_date << '\'');
305 //FIXME this whole locking bussiness is not working under cvs and the machinery
306 // conforms to the ci usage, not cvs.
307 if (file_date == mod_date) {
308 locker_ = "Unlocked";
311 // Here we should also to some more checking
312 // to see if there are conflicts or not.
322 void CVS::registrer(string const & msg)
324 doVCCommand("cvs -q add -m \"" + msg + "\" "
325 + quoteName(onlyFilename(owner_->absFileName())),
326 FileName(owner_->filePath()));
330 void CVS::checkIn(string const & msg)
332 doVCCommand("cvs -q commit -m \"" + msg + "\" "
333 + quoteName(onlyFilename(owner_->absFileName())),
334 FileName(owner_->filePath()));
338 bool CVS::checkInEnabled()
346 // cvs update or perhaps for cvs this should be a noop
347 // we need to detect conflict (eg "C" in output)
348 // before we can do this.
349 lyxerr << "Sorry not implemented." << endl;
353 bool CVS::checkOutEnabled()
361 // Reverts to the version in CVS repository and
362 // gets the updated version from the repository.
363 string const fil = quoteName(onlyFilename(owner_->absFileName()));
364 // This is sensitive operation, so at lest some check about
365 // existence of cvs program and its file
366 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
368 FileName f(owner_->absFileName());
370 doVCCommand("cvs update " + fil,
371 FileName(owner_->filePath()));
378 // merge the current with the previous version
379 // in a reverse patch kind of way, so that the
380 // result is to revert the last changes.
381 lyxerr << "Sorry not implemented." << endl;
385 bool CVS::undoLastEnabled()
391 void CVS::getLog(FileName const & tmpf)
393 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
394 + " > " + tmpf.toFilesystemEncoding(),
395 FileName(owner_->filePath()));
398 bool CVS::toggleReadOnlyEnabled()
403 /////////////////////////////////////////////////////////////////////
407 /////////////////////////////////////////////////////////////////////
409 SVN::SVN(FileName const & m, FileName const & f)
417 FileName const SVN::findFile(FileName const & file)
419 // First we look for the CVS/Entries in the same dir
420 // where we have file.
421 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
422 string const tmpf = onlyFilename(file.absFilename());
423 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
424 << "' for `" << tmpf << '\'');
425 if (entries.isReadableFile()) {
426 // Ok we are at least in a CVS dir. Parse the CVS/Entries
427 // and see if we can find this file. We do a fast and
429 ifstream ifs(entries.toFilesystemEncoding().c_str());
430 string line, oldline;
431 while (getline(ifs, line)) {
432 if (line == "dir" || line == "file")
433 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
434 if (oldline == tmpf && line == "file")
443 void SVN::scanMaster()
445 // if we want some locking under svn
446 // we need different infrastructure around
447 locker_ = "Unlocked";
452 void SVN::registrer(string const & msg)
454 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
455 FileName(owner_->filePath()));
459 void SVN::checkIn(string const & msg)
461 FileName tmpf = FileName::tempName("lyxvcout");
463 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
467 doVCCommand("svn commit -m \"" + msg + "\" "
468 + quoteName(onlyFilename(owner_->absFileName()))
469 + " 2> " + tmpf.toFilesystemEncoding(),
470 FileName(owner_->filePath()));
472 string res = scanLogFile(tmpf);
474 frontend::Alert::error(_("Revision control error."),
475 _("Error when commiting to repository.\n"
476 "You have to manually resolve the problem.\n"
477 "After pressing OK, LyX will reopen the document."));
482 bool SVN::checkInEnabled()
487 // FIXME Correctly return code should be checked instead of this.
488 // This would need another solution than just plain startscript.
489 // Hint from Andre': QProcess::readAllStandardError()...
490 string SVN::scanLogFile(FileName const & f)
492 ifstream ifs(f.toFilesystemEncoding().c_str());
497 lyxerr << line << "\n";
498 if (prefixIs(line, "C "))
500 if (contains(line, "Commit failed"))
509 FileName tmpf = FileName::tempName("lyxvcout");
511 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
515 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
516 + " > " + tmpf.toFilesystemEncoding(),
517 FileName(owner_->filePath()));
519 string res = scanLogFile(tmpf);
521 frontend::Alert::error(_("Revision control error."),
522 bformat(_("Error when updating from repository.\n"
523 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
524 "After pressing OK, LyX will try to reopen resolved document."),
530 bool SVN::checkOutEnabled()
538 // Reverts to the version in CVS repository and
539 // gets the updated version from the repository.
540 string const fil = quoteName(onlyFilename(owner_->absFileName()));
542 doVCCommand("svn revert -q " + fil,
543 FileName(owner_->filePath()));
550 // merge the current with the previous version
551 // in a reverse patch kind of way, so that the
552 // result is to revert the last changes.
553 lyxerr << "Sorry not implemented." << endl;
557 bool SVN::undoLastEnabled()
563 void SVN::getLog(FileName const & tmpf)
565 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
566 + " > " + tmpf.toFilesystemEncoding(),
567 FileName(owner_->filePath()));
571 bool SVN::toggleReadOnlyEnabled()