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 << " 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();
207 string RCS::lockingToggle()
209 lyxerr << "Sorry, not implemented." << endl;
214 bool RCS::lockingToggleEnabled()
222 doVCCommand("co -f -u" + version() + " "
223 + quoteName(onlyFilename(owner_->absFileName())),
224 FileName(owner_->filePath()));
225 // We ignore changes and just reload!
232 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
233 doVCCommand("rcs -o" + version() + " "
234 + quoteName(onlyFilename(owner_->absFileName())),
235 FileName(owner_->filePath()));
239 bool RCS::undoLastEnabled()
245 void RCS::getLog(FileName const & tmpf)
247 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
248 + " > " + quoteName(tmpf.toFilesystemEncoding()),
249 FileName(owner_->filePath()));
253 bool RCS::toggleReadOnlyEnabled()
259 /////////////////////////////////////////////////////////////////////
263 /////////////////////////////////////////////////////////////////////
265 CVS::CVS(FileName const & m, FileName const & f)
273 FileName const CVS::findFile(FileName const & file)
275 // First we look for the CVS/Entries in the same dir
276 // where we have file.
277 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
278 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
279 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
280 << "' for `" << tmpf << '\'');
281 if (entries.isReadableFile()) {
282 // Ok we are at least in a CVS dir. Parse the CVS/Entries
283 // and see if we can find this file. We do a fast and
285 ifstream ifs(entries.toFilesystemEncoding().c_str());
287 while (getline(ifs, line)) {
288 LYXERR(Debug::LYXVC, "\tEntries: " << line);
289 if (contains(line, tmpf))
297 void CVS::scanMaster()
299 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
300 // Ok now we do the real scan...
301 ifstream ifs(master_.toFilesystemEncoding().c_str());
302 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
303 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
305 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
306 while (getline(ifs, line)) {
307 LYXERR(Debug::LYXVC, "\t line: " << line);
308 if (contains(line, tmpf)) {
309 // Ok extract the fields.
312 regex_match(line, sm, reg);
314 //sm[0]; // whole matched string
316 version_ = sm.str(2);
317 string const file_date = sm.str(3);
320 //sm[5]; // tag or tagdate
321 // FIXME: must double check file is stattable/existing
322 time_t mod = file_.lastModified();
323 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
324 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
325 << "'\nModification date of file: `" << mod_date << '\'');
326 //FIXME this whole locking bussiness is not working under cvs and the machinery
327 // conforms to the ci usage, not cvs.
328 if (file_date == mod_date) {
329 locker_ = "Unlocked";
332 // Here we should also to some more checking
333 // to see if there are conflicts or not.
343 void CVS::registrer(string const & msg)
345 doVCCommand("cvs -q add -m \"" + msg + "\" "
346 + quoteName(onlyFilename(owner_->absFileName())),
347 FileName(owner_->filePath()));
351 string CVS::checkIn(string const & msg)
353 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
354 + quoteName(onlyFilename(owner_->absFileName())),
355 FileName(owner_->filePath()));
356 return ret ? string() : "CVS: Proceeded";
360 bool CVS::checkInEnabled()
366 string CVS::checkOut()
368 // cvs update or perhaps for cvs this should be a noop
369 // we need to detect conflict (eg "C" in output)
370 // before we can do this.
371 lyxerr << "Sorry, not implemented." << endl;
376 bool CVS::checkOutEnabled()
382 string CVS::lockingToggle()
384 lyxerr << "Sorry, not implemented." << endl;
389 bool CVS::lockingToggleEnabled()
397 // Reverts to the version in CVS repository and
398 // gets the updated version from the repository.
399 string const fil = quoteName(onlyFilename(owner_->absFileName()));
400 // This is sensitive operation, so at lest some check about
401 // existence of cvs program and its file
402 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
404 FileName f(owner_->absFileName());
406 doVCCommand("cvs update " + fil,
407 FileName(owner_->filePath()));
414 // merge the current with the previous version
415 // in a reverse patch kind of way, so that the
416 // result is to revert the last changes.
417 lyxerr << "Sorry, not implemented." << endl;
421 bool CVS::undoLastEnabled()
427 void CVS::getLog(FileName const & tmpf)
429 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
430 + " > " + quoteName(tmpf.toFilesystemEncoding()),
431 FileName(owner_->filePath()));
434 bool CVS::toggleReadOnlyEnabled()
439 /////////////////////////////////////////////////////////////////////
443 /////////////////////////////////////////////////////////////////////
445 SVN::SVN(FileName const & m, FileName const & f)
455 FileName const SVN::findFile(FileName const & file)
457 // First we look for the .svn/entries in the same dir
458 // where we have file.
459 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
460 string const tmpf = onlyFilename(file.absFilename());
461 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
462 << "' for `" << tmpf << '\'');
463 if (entries.isReadableFile()) {
464 // Ok we are at least in a SVN dir. Parse the .svn/entries
465 // and see if we can find this file. We do a fast and
467 ifstream ifs(entries.toFilesystemEncoding().c_str());
468 string line, oldline;
469 while (getline(ifs, line)) {
470 if (line == "dir" || line == "file")
471 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
472 if (oldline == tmpf && line == "file")
481 void SVN::scanMaster()
484 vcstatus = NOLOCKING;
485 if (checkLockMode()) {
490 locker_ = "Unlocked";
497 bool SVN::checkLockMode()
499 FileName tmpf = FileName::tempName("lyxvcout");
501 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
502 return N_("Error: Could not generate logfile.");
505 LYXERR(Debug::LYXVC, "Detecting locking mode...");
506 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
507 + " > " + quoteName(tmpf.toFilesystemEncoding()),
511 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
517 LYXERR(Debug::LYXVC, line);
518 if (contains(line, "svn:needs-lock"))
521 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
529 bool SVN::isLocked() const
532 FileName file(file_.absFilename());
533 return !file.isReadOnly();
537 void SVN::registrer(string const & /*msg*/)
539 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
540 FileName(owner_->filePath()));
544 string SVN::checkIn(string const & msg)
546 FileName tmpf = FileName::tempName("lyxvcout");
548 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
549 return N_("Error: Could not generate logfile.");
552 doVCCommand("svn commit -m \"" + msg + "\" "
553 + quoteName(onlyFilename(owner_->absFileName()))
554 + " > " + quoteName(tmpf.toFilesystemEncoding()),
555 FileName(owner_->filePath()));
558 string res = scanLogFile(tmpf, log);
560 frontend::Alert::error(_("Revision control error."),
561 _("Error when committing to repository.\n"
562 "You have to manually resolve the problem.\n"
563 "After pressing OK, LyX will reopen the document."));
565 fileLock(false, tmpf, log);
568 return "SVN: " + log;
572 bool SVN::checkInEnabled()
581 // FIXME Correctly return code should be checked instead of this.
582 // This would need another solution than just plain startscript.
583 // Hint from Andre': QProcess::readAllStandardError()...
584 string SVN::scanLogFile(FileName const & f, string & status)
586 ifstream ifs(f.toFilesystemEncoding().c_str());
591 lyxerr << line << "\n";
592 if (!line.empty()) status += line + "; ";
593 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
597 if (contains(line, "svn:needs-lock")) {
607 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
609 if (!locked_mode_ || (isLocked() == lock))
612 string arg = lock ? "lock " : "unlock ";
613 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
614 + " > " + quoteName(tmpf.toFilesystemEncoding()),
615 FileName(owner_->filePath()));
617 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
621 if (!line.empty()) status += line + "; ";
625 if (!isLocked() && lock)
626 frontend::Alert::error(_("Revision control error."),
627 _("Error when acquiring write lock.\n"
628 "Most probably another user is editing\n"
629 "the current document now!\n"
630 "Also check the access to the repository."));
631 if (isLocked() && !lock)
632 frontend::Alert::error(_("Revision control error."),
633 _("Error when releasing write lock.\n"
634 "Check the access to the repository."));
638 string SVN::checkOut()
640 FileName tmpf = FileName::tempName("lyxvcout");
642 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
643 return N_("Error: Could not generate logfile.");
646 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
647 + " > " + quoteName(tmpf.toFilesystemEncoding()),
648 FileName(owner_->filePath()));
651 string res = scanLogFile(tmpf, log);
653 frontend::Alert::error(_("Revision control error."),
654 bformat(_("Error when updating from repository.\n"
655 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
656 "After pressing OK, LyX will try to reopen resolved document."),
657 from_local8bit(res)));
659 fileLock(true, tmpf, log);
662 return "SVN: " + log;
666 bool SVN::checkOutEnabled()
675 string SVN::lockingToggle()
677 FileName tmpf = FileName::tempName("lyxvcout");
679 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
680 return N_("Error: Could not generate logfile.");
683 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
684 + " > " + quoteName(tmpf.toFilesystemEncoding()),
685 FileName(owner_->filePath()));
690 string res = scanLogFile(tmpf, log);
691 bool locking = contains(res, "svn:needs-lock");
693 ret = doVCCommand("svn propset svn:needs-lock ON "
694 + quoteName(onlyFilename(owner_->absFileName()))
695 + " > " + quoteName(tmpf.toFilesystemEncoding()),
696 FileName(owner_->filePath()));
698 ret = doVCCommand("svn propdel svn:needs-lock "
699 + quoteName(onlyFilename(owner_->absFileName()))
700 + " > " + quoteName(tmpf.toFilesystemEncoding()),
701 FileName(owner_->filePath()));
706 frontend::Alert::warning(_("VCN File Locking"),
707 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
708 + _("Do not forget to commit the locking property into the repository."),
711 return string("SVN: ") + N_("Locking property set.");
715 bool SVN::lockingToggleEnabled()
722 // Reverts to the version in CVS repository and
723 // gets the updated version from the repository.
724 string const fil = quoteName(onlyFilename(owner_->absFileName()));
726 doVCCommand("svn revert -q " + fil,
727 FileName(owner_->filePath()));
734 // merge the current with the previous version
735 // in a reverse patch kind of way, so that the
736 // result is to revert the last changes.
737 lyxerr << "Sorry, not implemented." << endl;
741 bool SVN::undoLastEnabled()
747 void SVN::getLog(FileName const & tmpf)
749 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
750 + " > " + quoteName(tmpf.toFilesystemEncoding()),
751 FileName(owner_->filePath()));
755 bool SVN::toggleReadOnlyEnabled()