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()
108 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
110 ifstream ifs(master_.toFilesystemEncoding().c_str());
113 bool read_enough = false;
115 while (!read_enough && ifs >> token) {
116 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
121 else if (token == "head") {
125 tmv = rtrim(tmv, ";");
127 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
128 } else if (contains(token, "access")
129 || contains(token, "symbols")
130 || contains(token, "strict")) {
132 } else if (contains(token, "locks")) {
134 if (contains(token, ';')) {
135 locker_ = "Unlocked";
144 s1 = rtrim(tmpt, ";");
145 // tmp is now in the format <user>:<version>
146 s1 = split(s1, s2, ':');
147 // s2 is user, and s1 is version
148 if (s1 == version_) {
153 } while (!contains(tmpt, ';'));
155 } else if (token == "comment") {
156 // we don't need to read any further than this.
160 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
166 void RCS::registrer(string const & msg)
168 string cmd = "ci -q -u -i -t-\"";
171 cmd += quoteName(onlyFilename(owner_->absFileName()));
172 doVCCommand(cmd, FileName(owner_->filePath()));
176 string RCS::checkIn(string const & msg)
178 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
179 + quoteName(onlyFilename(owner_->absFileName())),
180 FileName(owner_->filePath()));
181 return ret ? string() : "RCS: Proceeded";
184 bool RCS::checkInEnabled()
186 return owner_ && !owner_->isReadonly();
189 string RCS::checkOut()
192 int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
193 FileName(owner_->filePath()));
194 return ret ? string() : "RCS: Proceeded";
198 bool RCS::checkOutEnabled()
200 return owner_ && owner_->isReadonly();
206 doVCCommand("co -f -u" + version() + " "
207 + quoteName(onlyFilename(owner_->absFileName())),
208 FileName(owner_->filePath()));
209 // We ignore changes and just reload!
216 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
217 doVCCommand("rcs -o" + version() + " "
218 + quoteName(onlyFilename(owner_->absFileName())),
219 FileName(owner_->filePath()));
223 bool RCS::undoLastEnabled()
229 void RCS::getLog(FileName const & tmpf)
231 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
232 + " > " + tmpf.toFilesystemEncoding(),
233 FileName(owner_->filePath()));
237 bool RCS::toggleReadOnlyEnabled()
243 /////////////////////////////////////////////////////////////////////
247 /////////////////////////////////////////////////////////////////////
249 CVS::CVS(FileName const & m, FileName const & f)
257 FileName const CVS::findFile(FileName const & file)
259 // First we look for the CVS/Entries in the same dir
260 // where we have file.
261 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
262 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
263 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
264 << "' for `" << tmpf << '\'');
265 if (entries.isReadableFile()) {
266 // Ok we are at least in a CVS dir. Parse the CVS/Entries
267 // and see if we can find this file. We do a fast and
269 ifstream ifs(entries.toFilesystemEncoding().c_str());
271 while (getline(ifs, line)) {
272 LYXERR(Debug::LYXVC, "\tEntries: " << line);
273 if (contains(line, tmpf))
281 void CVS::scanMaster()
283 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
284 // Ok now we do the real scan...
285 ifstream ifs(master_.toFilesystemEncoding().c_str());
286 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
287 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
289 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
290 while (getline(ifs, line)) {
291 LYXERR(Debug::LYXVC, "\t line: " << line);
292 if (contains(line, tmpf)) {
293 // Ok extract the fields.
296 regex_match(line, sm, reg);
298 //sm[0]; // whole matched string
300 version_ = sm.str(2);
301 string const file_date = sm.str(3);
304 //sm[5]; // tag or tagdate
305 // FIXME: must double check file is stattable/existing
306 time_t mod = file_.lastModified();
307 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
308 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
309 << "'\nModification date of file: `" << mod_date << '\'');
310 //FIXME this whole locking bussiness is not working under cvs and the machinery
311 // conforms to the ci usage, not cvs.
312 if (file_date == mod_date) {
313 locker_ = "Unlocked";
316 // Here we should also to some more checking
317 // to see if there are conflicts or not.
327 void CVS::registrer(string const & msg)
329 doVCCommand("cvs -q add -m \"" + msg + "\" "
330 + quoteName(onlyFilename(owner_->absFileName())),
331 FileName(owner_->filePath()));
335 string CVS::checkIn(string const & msg)
337 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
338 + quoteName(onlyFilename(owner_->absFileName())),
339 FileName(owner_->filePath()));
340 return ret ? string() : "CVS: Proceeded";
344 bool CVS::checkInEnabled()
350 string CVS::checkOut()
352 // cvs update or perhaps for cvs this should be a noop
353 // we need to detect conflict (eg "C" in output)
354 // before we can do this.
355 lyxerr << "Sorry not implemented." << endl;
360 bool CVS::checkOutEnabled()
368 // Reverts to the version in CVS repository and
369 // gets the updated version from the repository.
370 string const fil = quoteName(onlyFilename(owner_->absFileName()));
371 // This is sensitive operation, so at lest some check about
372 // existence of cvs program and its file
373 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
375 FileName f(owner_->absFileName());
377 doVCCommand("cvs update " + fil,
378 FileName(owner_->filePath()));
385 // merge the current with the previous version
386 // in a reverse patch kind of way, so that the
387 // result is to revert the last changes.
388 lyxerr << "Sorry not implemented." << endl;
392 bool CVS::undoLastEnabled()
398 void CVS::getLog(FileName const & tmpf)
400 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
401 + " > " + tmpf.toFilesystemEncoding(),
402 FileName(owner_->filePath()));
405 bool CVS::toggleReadOnlyEnabled()
410 /////////////////////////////////////////////////////////////////////
414 /////////////////////////////////////////////////////////////////////
416 SVN::SVN(FileName const & m, FileName const & f)
424 FileName const SVN::findFile(FileName const & file)
426 // First we look for the CVS/Entries in the same dir
427 // where we have file.
428 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
429 string const tmpf = onlyFilename(file.absFilename());
430 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
431 << "' for `" << tmpf << '\'');
432 if (entries.isReadableFile()) {
433 // Ok we are at least in a CVS dir. Parse the CVS/Entries
434 // and see if we can find this file. We do a fast and
436 ifstream ifs(entries.toFilesystemEncoding().c_str());
437 string line, oldline;
438 while (getline(ifs, line)) {
439 if (line == "dir" || line == "file")
440 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
441 if (oldline == tmpf && line == "file")
450 void SVN::scanMaster()
452 // if we want some locking under svn
453 // we need different infrastructure around
454 locker_ = "Unlocked";
459 void SVN::registrer(string const & /*msg*/)
461 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
462 FileName(owner_->filePath()));
466 string SVN::checkIn(string const & msg)
468 FileName tmpf = FileName::tempName("lyxvcout");
470 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
471 return N_("Error: Could not generate logfile.");
474 doVCCommand("svn commit -m \"" + msg + "\" "
475 + quoteName(onlyFilename(owner_->absFileName()))
476 + " > " + tmpf.toFilesystemEncoding(),
477 FileName(owner_->filePath()));
480 string res = scanLogFile(tmpf, log);
482 frontend::Alert::error(_("Revision control error."),
483 _("Error when commiting to repository.\n"
484 "You have to manually resolve the problem.\n"
485 "After pressing OK, LyX will reopen the document."));
487 return "SVN: " + log;
491 bool SVN::checkInEnabled()
496 // FIXME Correctly return code should be checked instead of this.
497 // This would need another solution than just plain startscript.
498 // Hint from Andre': QProcess::readAllStandardError()...
499 string SVN::scanLogFile(FileName const & f, string & status)
501 ifstream ifs(f.toFilesystemEncoding().c_str());
506 lyxerr << line << "\n";
507 if (!line.empty()) status += line + "; ";
508 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
518 string SVN::checkOut()
520 FileName tmpf = FileName::tempName("lyxvcout");
522 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
523 return N_("Error: Could not generate logfile.");
526 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
527 + " > " + tmpf.toFilesystemEncoding(),
528 FileName(owner_->filePath()));
531 string res = scanLogFile(tmpf, log);
533 frontend::Alert::error(_("Revision control error."),
534 bformat(_("Error when updating from repository.\n"
535 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
536 "After pressing OK, LyX will try to reopen resolved document."),
539 return "SVN: " + log;
543 bool SVN::checkOutEnabled()
551 // Reverts to the version in CVS repository and
552 // gets the updated version from the repository.
553 string const fil = quoteName(onlyFilename(owner_->absFileName()));
555 doVCCommand("svn revert -q " + fil,
556 FileName(owner_->filePath()));
563 // merge the current with the previous version
564 // in a reverse patch kind of way, so that the
565 // result is to revert the last changes.
566 lyxerr << "Sorry not implemented." << endl;
570 bool SVN::undoLastEnabled()
576 void SVN::getLog(FileName const & tmpf)
578 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
579 + " > " + tmpf.toFilesystemEncoding(),
580 FileName(owner_->filePath()));
584 bool SVN::toggleReadOnlyEnabled()