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)
49 owner_->setBusy(true);
51 int const ret = doVCCommandCall(cmd, path);
54 owner_->setBusy(false);
56 frontend::Alert::error(_("Revision control error."),
57 bformat(_("Some problem occured while running the command:\n"
64 /////////////////////////////////////////////////////////////////////
68 /////////////////////////////////////////////////////////////////////
70 RCS::RCS(FileName const & m)
77 FileName const RCS::findFile(FileName const & file)
79 // Check if *,v exists.
80 FileName tmp(file.absFilename() + ",v");
81 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
82 if (tmp.isReadableFile()) {
83 LYXERR(Debug::LYXVC, "Yes " << file << " is under rcs.");
87 // Check if RCS/*,v exists.
88 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
89 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
90 if (tmp.isReadableFile()) {
91 LYXERR(Debug::LYXVC, "Yes " << file << " it is under rcs.");
99 void RCS::retrieve(FileName const & file)
101 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
102 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
107 void RCS::scanMaster()
112 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
114 ifstream ifs(master_.toFilesystemEncoding().c_str());
117 bool read_enough = false;
119 while (!read_enough && ifs >> token) {
120 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
125 else if (token == "head") {
129 tmv = rtrim(tmv, ";");
131 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
132 } else if (contains(token, "access")
133 || contains(token, "symbols")
134 || contains(token, "strict")) {
136 } else if (contains(token, "locks")) {
138 if (contains(token, ';')) {
139 locker_ = "Unlocked";
148 s1 = rtrim(tmpt, ";");
149 // tmp is now in the format <user>:<version>
150 s1 = split(s1, s2, ':');
151 // s2 is user, and s1 is version
152 if (s1 == version_) {
157 } while (!contains(tmpt, ';'));
159 } else if (token == "comment") {
160 // we don't need to read any further than this.
164 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
170 void RCS::registrer(string const & msg)
172 string cmd = "ci -q -u -i -t-\"";
175 cmd += quoteName(onlyFilename(owner_->absFileName()));
176 doVCCommand(cmd, FileName(owner_->filePath()));
180 string RCS::checkIn(string const & msg)
182 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
183 + quoteName(onlyFilename(owner_->absFileName())),
184 FileName(owner_->filePath()));
185 return ret ? string() : "RCS: Proceeded";
188 bool RCS::checkInEnabled()
190 return owner_ && !owner_->isReadonly();
193 string RCS::checkOut()
196 int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
197 FileName(owner_->filePath()));
198 return ret ? string() : "RCS: Proceeded";
202 bool RCS::checkOutEnabled()
204 return owner_ && owner_->isReadonly();
210 doVCCommand("co -f -u" + version() + " "
211 + quoteName(onlyFilename(owner_->absFileName())),
212 FileName(owner_->filePath()));
213 // We ignore changes and just reload!
220 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
221 doVCCommand("rcs -o" + version() + " "
222 + quoteName(onlyFilename(owner_->absFileName())),
223 FileName(owner_->filePath()));
227 bool RCS::undoLastEnabled()
233 void RCS::getLog(FileName const & tmpf)
235 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
236 + " > " + quoteName(tmpf.toFilesystemEncoding()),
237 FileName(owner_->filePath()));
241 bool RCS::toggleReadOnlyEnabled()
247 /////////////////////////////////////////////////////////////////////
251 /////////////////////////////////////////////////////////////////////
253 CVS::CVS(FileName const & m, FileName const & f)
261 FileName const CVS::findFile(FileName const & file)
263 // First we look for the CVS/Entries in the same dir
264 // where we have file.
265 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
266 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
267 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
268 << "' for `" << tmpf << '\'');
269 if (entries.isReadableFile()) {
270 // Ok we are at least in a CVS dir. Parse the CVS/Entries
271 // and see if we can find this file. We do a fast and
273 ifstream ifs(entries.toFilesystemEncoding().c_str());
275 while (getline(ifs, line)) {
276 LYXERR(Debug::LYXVC, "\tEntries: " << line);
277 if (contains(line, tmpf))
285 void CVS::scanMaster()
287 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
288 // Ok now we do the real scan...
289 ifstream ifs(master_.toFilesystemEncoding().c_str());
290 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
291 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
293 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
294 while (getline(ifs, line)) {
295 LYXERR(Debug::LYXVC, "\t line: " << line);
296 if (contains(line, tmpf)) {
297 // Ok extract the fields.
300 regex_match(line, sm, reg);
302 //sm[0]; // whole matched string
304 version_ = sm.str(2);
305 string const file_date = sm.str(3);
308 //sm[5]; // tag or tagdate
309 // FIXME: must double check file is stattable/existing
310 time_t mod = file_.lastModified();
311 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
312 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
313 << "'\nModification date of file: `" << mod_date << '\'');
314 //FIXME this whole locking bussiness is not working under cvs and the machinery
315 // conforms to the ci usage, not cvs.
316 if (file_date == mod_date) {
317 locker_ = "Unlocked";
320 // Here we should also to some more checking
321 // to see if there are conflicts or not.
331 void CVS::registrer(string const & msg)
333 doVCCommand("cvs -q add -m \"" + msg + "\" "
334 + quoteName(onlyFilename(owner_->absFileName())),
335 FileName(owner_->filePath()));
339 string CVS::checkIn(string const & msg)
341 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
342 + quoteName(onlyFilename(owner_->absFileName())),
343 FileName(owner_->filePath()));
344 return ret ? string() : "CVS: Proceeded";
348 bool CVS::checkInEnabled()
354 string CVS::checkOut()
356 // cvs update or perhaps for cvs this should be a noop
357 // we need to detect conflict (eg "C" in output)
358 // before we can do this.
359 lyxerr << "Sorry not implemented." << endl;
364 bool CVS::checkOutEnabled()
372 // Reverts to the version in CVS repository and
373 // gets the updated version from the repository.
374 string const fil = quoteName(onlyFilename(owner_->absFileName()));
375 // This is sensitive operation, so at lest some check about
376 // existence of cvs program and its file
377 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
379 FileName f(owner_->absFileName());
381 doVCCommand("cvs update " + fil,
382 FileName(owner_->filePath()));
389 // merge the current with the previous version
390 // in a reverse patch kind of way, so that the
391 // result is to revert the last changes.
392 lyxerr << "Sorry not implemented." << endl;
396 bool CVS::undoLastEnabled()
402 void CVS::getLog(FileName const & tmpf)
404 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
405 + " > " + quoteName(tmpf.toFilesystemEncoding()),
406 FileName(owner_->filePath()));
409 bool CVS::toggleReadOnlyEnabled()
414 /////////////////////////////////////////////////////////////////////
418 /////////////////////////////////////////////////////////////////////
420 SVN::SVN(FileName const & m, FileName const & f)
430 FileName const SVN::findFile(FileName const & file)
432 // First we look for the .svn/entries in the same dir
433 // where we have file.
434 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
435 string const tmpf = onlyFilename(file.absFilename());
436 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
437 << "' for `" << tmpf << '\'');
438 if (entries.isReadableFile()) {
439 // Ok we are at least in a SVN dir. Parse the .svn/entries
440 // and see if we can find this file. We do a fast and
442 ifstream ifs(entries.toFilesystemEncoding().c_str());
443 string line, oldline;
444 while (getline(ifs, line)) {
445 if (line == "dir" || line == "file")
446 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
447 if (oldline == tmpf && line == "file")
456 void SVN::scanMaster()
458 // if we want some locking under svn
459 // we need different infrastructure around
460 locker_ = "Unlocked";
465 bool SVN::checkLockMode()
467 FileName tmpf = FileName::tempName("lyxvcout");
469 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
470 return N_("Error: Could not generate logfile.");
473 LYXERR(Debug::LYXVC, "Detecting locking mode...");
474 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
475 + " > " + quoteName(tmpf.toFilesystemEncoding()),
479 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
485 LYXERR(Debug::LYXVC, line);
486 if (contains(line, "svn:needs-lock"))
489 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
500 FileName file(file_.absFilename());
501 return !file.isReadOnly();
505 void SVN::registrer(string const & /*msg*/)
507 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
508 FileName(owner_->filePath()));
512 string SVN::checkIn(string const & msg)
514 FileName tmpf = FileName::tempName("lyxvcout");
516 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
517 return N_("Error: Could not generate logfile.");
520 doVCCommand("svn commit -m \"" + msg + "\" "
521 + quoteName(onlyFilename(owner_->absFileName()))
522 + " > " + quoteName(tmpf.toFilesystemEncoding()),
523 FileName(owner_->filePath()));
526 string res = scanLogFile(tmpf, log);
528 frontend::Alert::error(_("Revision control error."),
529 _("Error when commiting to repository.\n"
530 "You have to manually resolve the problem.\n"
531 "After pressing OK, LyX will reopen the document."));
533 fileLock(false, tmpf, log);
536 return "SVN: " + log;
540 bool SVN::checkInEnabled()
549 // FIXME Correctly return code should be checked instead of this.
550 // This would need another solution than just plain startscript.
551 // Hint from Andre': QProcess::readAllStandardError()...
552 string SVN::scanLogFile(FileName const & f, string & status)
554 ifstream ifs(f.toFilesystemEncoding().c_str());
559 lyxerr << line << "\n";
560 if (!line.empty()) status += line + "; ";
561 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
571 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
573 if (!locked_mode_ || (isLocked() == lock))
576 string arg = lock ? "lock " : "unlock ";
577 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
578 + " > " + quoteName(tmpf.toFilesystemEncoding()),
579 FileName(owner_->filePath()));
581 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
585 if (!line.empty()) status += line + "; ";
589 if (!isLocked() && lock)
590 frontend::Alert::error(_("Revision control error."),
591 _("Error when acquiring write lock.\n"
592 "Most probably some other user edit the current document now!\n"
593 "Check also the access to the repository."));
594 if (isLocked() && !lock)
595 frontend::Alert::error(_("Revision control error."),
596 _("Error when releasing write lock.\n"
597 "Check the access to the repository."));
601 string SVN::checkOut()
603 FileName tmpf = FileName::tempName("lyxvcout");
605 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
606 return N_("Error: Could not generate logfile.");
609 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
610 + " > " + quoteName(tmpf.toFilesystemEncoding()),
611 FileName(owner_->filePath()));
614 string res = scanLogFile(tmpf, log);
616 frontend::Alert::error(_("Revision control error."),
617 bformat(_("Error when updating from repository.\n"
618 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
619 "After pressing OK, LyX will try to reopen resolved document."),
620 from_local8bit(res)));
622 fileLock(true, tmpf, log);
625 return "SVN: " + log;
629 bool SVN::checkOutEnabled()
640 // Reverts to the version in CVS repository and
641 // gets the updated version from the repository.
642 string const fil = quoteName(onlyFilename(owner_->absFileName()));
644 doVCCommand("svn revert -q " + fil,
645 FileName(owner_->filePath()));
652 // merge the current with the previous version
653 // in a reverse patch kind of way, so that the
654 // result is to revert the last changes.
655 lyxerr << "Sorry not implemented." << endl;
659 bool SVN::undoLastEnabled()
665 void SVN::getLog(FileName const & tmpf)
667 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
668 + " > " + quoteName(tmpf.toFilesystemEncoding()),
669 FileName(owner_->filePath()));
673 bool SVN::toggleReadOnlyEnabled()