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: " << master_);
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 string RCS::checkIn(string const & msg)
175 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
176 + quoteName(onlyFilename(owner_->absFileName())),
177 FileName(owner_->filePath()));
178 return ret ? string() : "RCS: Proceeded";
181 bool RCS::checkInEnabled()
183 return owner_ && !owner_->isReadonly();
186 string RCS::checkOut()
189 int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
190 FileName(owner_->filePath()));
191 return ret ? string() : "RCS: Proceeded";
195 bool RCS::checkOutEnabled()
197 return owner_ && owner_->isReadonly();
203 doVCCommand("co -f -u" + version() + " "
204 + quoteName(onlyFilename(owner_->absFileName())),
205 FileName(owner_->filePath()));
206 // We ignore changes and just reload!
213 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
214 doVCCommand("rcs -o" + version() + " "
215 + quoteName(onlyFilename(owner_->absFileName())),
216 FileName(owner_->filePath()));
220 bool RCS::undoLastEnabled()
226 void RCS::getLog(FileName const & tmpf)
228 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
229 + " > " + tmpf.toFilesystemEncoding(),
230 FileName(owner_->filePath()));
234 bool RCS::toggleReadOnlyEnabled()
240 /////////////////////////////////////////////////////////////////////
244 /////////////////////////////////////////////////////////////////////
246 CVS::CVS(FileName const & m, FileName const & f)
254 FileName const CVS::findFile(FileName const & file)
256 // First we look for the CVS/Entries in the same dir
257 // where we have file.
258 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
259 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
260 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
261 << "' for `" << tmpf << '\'');
262 if (entries.isReadableFile()) {
263 // Ok we are at least in a CVS dir. Parse the CVS/Entries
264 // and see if we can find this file. We do a fast and
266 ifstream ifs(entries.toFilesystemEncoding().c_str());
268 while (getline(ifs, line)) {
269 LYXERR(Debug::LYXVC, "\tEntries: " << line);
270 if (contains(line, tmpf))
278 void CVS::scanMaster()
280 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
281 // Ok now we do the real scan...
282 ifstream ifs(master_.toFilesystemEncoding().c_str());
283 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
284 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
286 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
287 while (getline(ifs, line)) {
288 LYXERR(Debug::LYXVC, "\t line: " << line);
289 if (contains(line, tmpf)) {
290 // Ok extract the fields.
293 regex_match(line, sm, reg);
295 //sm[0]; // whole matched string
297 version_ = sm.str(2);
298 string const file_date = sm.str(3);
301 //sm[5]; // tag or tagdate
302 // FIXME: must double check file is stattable/existing
303 time_t mod = file_.lastModified();
304 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
305 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
306 << "'\nModification date of file: `" << mod_date << '\'');
307 //FIXME this whole locking bussiness is not working under cvs and the machinery
308 // conforms to the ci usage, not cvs.
309 if (file_date == mod_date) {
310 locker_ = "Unlocked";
313 // Here we should also to some more checking
314 // to see if there are conflicts or not.
324 void CVS::registrer(string const & msg)
326 doVCCommand("cvs -q add -m \"" + msg + "\" "
327 + quoteName(onlyFilename(owner_->absFileName())),
328 FileName(owner_->filePath()));
332 string CVS::checkIn(string const & msg)
334 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
335 + quoteName(onlyFilename(owner_->absFileName())),
336 FileName(owner_->filePath()));
337 return ret ? string() : "CVS: Proceeded";
341 bool CVS::checkInEnabled()
347 string CVS::checkOut()
349 // cvs update or perhaps for cvs this should be a noop
350 // we need to detect conflict (eg "C" in output)
351 // before we can do this.
352 lyxerr << "Sorry not implemented." << endl;
357 bool CVS::checkOutEnabled()
365 // Reverts to the version in CVS repository and
366 // gets the updated version from the repository.
367 string const fil = quoteName(onlyFilename(owner_->absFileName()));
368 // This is sensitive operation, so at lest some check about
369 // existence of cvs program and its file
370 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
372 FileName f(owner_->absFileName());
374 doVCCommand("cvs update " + fil,
375 FileName(owner_->filePath()));
382 // merge the current with the previous version
383 // in a reverse patch kind of way, so that the
384 // result is to revert the last changes.
385 lyxerr << "Sorry not implemented." << endl;
389 bool CVS::undoLastEnabled()
395 void CVS::getLog(FileName const & tmpf)
397 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
398 + " > " + tmpf.toFilesystemEncoding(),
399 FileName(owner_->filePath()));
402 bool CVS::toggleReadOnlyEnabled()
407 /////////////////////////////////////////////////////////////////////
411 /////////////////////////////////////////////////////////////////////
413 SVN::SVN(FileName const & m, FileName const & f)
421 FileName const SVN::findFile(FileName const & file)
423 // First we look for the CVS/Entries in the same dir
424 // where we have file.
425 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
426 string const tmpf = onlyFilename(file.absFilename());
427 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
428 << "' for `" << tmpf << '\'');
429 if (entries.isReadableFile()) {
430 // Ok we are at least in a CVS dir. Parse the CVS/Entries
431 // and see if we can find this file. We do a fast and
433 ifstream ifs(entries.toFilesystemEncoding().c_str());
434 string line, oldline;
435 while (getline(ifs, line)) {
436 if (line == "dir" || line == "file")
437 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
438 if (oldline == tmpf && line == "file")
447 void SVN::scanMaster()
449 // if we want some locking under svn
450 // we need different infrastructure around
451 locker_ = "Unlocked";
456 void SVN::registrer(string const & /*msg*/)
458 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
459 FileName(owner_->filePath()));
463 string SVN::checkIn(string const & msg)
465 FileName tmpf = FileName::tempName("lyxvcout");
467 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
468 return N_("Error: Could not generate logfile.");
471 doVCCommand("svn commit -m \"" + msg + "\" "
472 + quoteName(onlyFilename(owner_->absFileName()))
473 + " > " + tmpf.toFilesystemEncoding(),
474 FileName(owner_->filePath()));
477 string res = scanLogFile(tmpf, log);
479 frontend::Alert::error(_("Revision control error."),
480 _("Error when commiting to repository.\n"
481 "You have to manually resolve the problem.\n"
482 "After pressing OK, LyX will reopen the document."));
484 return "SVN: " + log;
488 bool SVN::checkInEnabled()
493 // FIXME Correctly return code should be checked instead of this.
494 // This would need another solution than just plain startscript.
495 // Hint from Andre': QProcess::readAllStandardError()...
496 string SVN::scanLogFile(FileName const & f, string & status)
498 ifstream ifs(f.toFilesystemEncoding().c_str());
503 lyxerr << line << "\n";
504 if (!line.empty()) status += line + "; ";
505 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
515 string SVN::checkOut()
517 FileName tmpf = FileName::tempName("lyxvcout");
519 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
520 return N_("Error: Could not generate logfile.");
523 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
524 + " > " + tmpf.toFilesystemEncoding(),
525 FileName(owner_->filePath()));
528 string res = scanLogFile(tmpf, log);
530 frontend::Alert::error(_("Revision control error."),
531 bformat(_("Error when updating from repository.\n"
532 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
533 "After pressing OK, LyX will try to reopen resolved document."),
536 return "SVN: " + log;
540 bool SVN::checkOutEnabled()
548 // Reverts to the version in CVS repository and
549 // gets the updated version from the repository.
550 string const fil = quoteName(onlyFilename(owner_->absFileName()));
552 doVCCommand("svn revert -q " + fil,
553 FileName(owner_->filePath()));
560 // merge the current with the previous version
561 // in a reverse patch kind of way, so that the
562 // result is to revert the last changes.
563 lyxerr << "Sorry not implemented." << endl;
567 bool SVN::undoLastEnabled()
573 void SVN::getLog(FileName const & tmpf)
575 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
576 + " > " + tmpf.toFilesystemEncoding(),
577 FileName(owner_->filePath()));
581 bool SVN::toggleReadOnlyEnabled()