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();
208 string RCS::repoSynchro()
210 lyxerr << "Sorry, not implemented." << endl;
215 bool RCS::repoSynchroEnabled()
221 string RCS::lockingToggle()
223 lyxerr << "Sorry, not implemented." << endl;
228 bool RCS::lockingToggleEnabled()
236 doVCCommand("co -f -u" + version() + " "
237 + quoteName(onlyFilename(owner_->absFileName())),
238 FileName(owner_->filePath()));
239 // We ignore changes and just reload!
246 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
247 doVCCommand("rcs -o" + version() + " "
248 + quoteName(onlyFilename(owner_->absFileName())),
249 FileName(owner_->filePath()));
253 bool RCS::undoLastEnabled()
259 void RCS::getLog(FileName const & tmpf)
261 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
262 + " > " + quoteName(tmpf.toFilesystemEncoding()),
263 FileName(owner_->filePath()));
267 bool RCS::toggleReadOnlyEnabled()
273 /////////////////////////////////////////////////////////////////////
277 /////////////////////////////////////////////////////////////////////
279 CVS::CVS(FileName const & m, FileName const & f)
287 FileName const CVS::findFile(FileName const & file)
289 // First we look for the CVS/Entries in the same dir
290 // where we have file.
291 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
292 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
293 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
294 << "' for `" << tmpf << '\'');
295 if (entries.isReadableFile()) {
296 // Ok we are at least in a CVS dir. Parse the CVS/Entries
297 // and see if we can find this file. We do a fast and
299 ifstream ifs(entries.toFilesystemEncoding().c_str());
301 while (getline(ifs, line)) {
302 LYXERR(Debug::LYXVC, "\tEntries: " << line);
303 if (contains(line, tmpf))
311 void CVS::scanMaster()
313 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
314 // Ok now we do the real scan...
315 ifstream ifs(master_.toFilesystemEncoding().c_str());
316 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
317 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
319 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
320 while (getline(ifs, line)) {
321 LYXERR(Debug::LYXVC, "\t line: " << line);
322 if (contains(line, tmpf)) {
323 // Ok extract the fields.
326 regex_match(line, sm, reg);
328 //sm[0]; // whole matched string
330 version_ = sm.str(2);
331 string const file_date = sm.str(3);
334 //sm[5]; // tag or tagdate
335 // FIXME: must double check file is stattable/existing
336 time_t mod = file_.lastModified();
337 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
338 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
339 << "'\nModification date of file: `" << mod_date << '\'');
340 //FIXME this whole locking bussiness is not working under cvs and the machinery
341 // conforms to the ci usage, not cvs.
342 if (file_date == mod_date) {
343 locker_ = "Unlocked";
346 // Here we should also to some more checking
347 // to see if there are conflicts or not.
357 void CVS::registrer(string const & msg)
359 doVCCommand("cvs -q add -m \"" + msg + "\" "
360 + quoteName(onlyFilename(owner_->absFileName())),
361 FileName(owner_->filePath()));
365 string CVS::checkIn(string const & msg)
367 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
368 + quoteName(onlyFilename(owner_->absFileName())),
369 FileName(owner_->filePath()));
370 return ret ? string() : "CVS: Proceeded";
374 bool CVS::checkInEnabled()
380 string CVS::checkOut()
382 // cvs update or perhaps for cvs this should be a noop
383 // we need to detect conflict (eg "C" in output)
384 // before we can do this.
385 lyxerr << "Sorry, not implemented." << endl;
390 bool CVS::checkOutEnabled()
396 string CVS::repoSynchro()
398 lyxerr << "Sorry, not implemented." << endl;
403 bool CVS::repoSynchroEnabled()
409 string CVS::lockingToggle()
411 lyxerr << "Sorry, not implemented." << endl;
416 bool CVS::lockingToggleEnabled()
424 // Reverts to the version in CVS repository and
425 // gets the updated version from the repository.
426 string const fil = quoteName(onlyFilename(owner_->absFileName()));
427 // This is sensitive operation, so at lest some check about
428 // existence of cvs program and its file
429 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
431 FileName f(owner_->absFileName());
433 doVCCommand("cvs update " + fil,
434 FileName(owner_->filePath()));
441 // merge the current with the previous version
442 // in a reverse patch kind of way, so that the
443 // result is to revert the last changes.
444 lyxerr << "Sorry, not implemented." << endl;
448 bool CVS::undoLastEnabled()
454 void CVS::getLog(FileName const & tmpf)
456 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
457 + " > " + quoteName(tmpf.toFilesystemEncoding()),
458 FileName(owner_->filePath()));
461 bool CVS::toggleReadOnlyEnabled()
466 /////////////////////////////////////////////////////////////////////
470 /////////////////////////////////////////////////////////////////////
472 SVN::SVN(FileName const & m, FileName const & f)
482 FileName const SVN::findFile(FileName const & file)
484 // First we look for the .svn/entries in the same dir
485 // where we have file.
486 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
487 string const tmpf = onlyFilename(file.absFilename());
488 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
489 << "' for `" << tmpf << '\'');
490 if (entries.isReadableFile()) {
491 // Ok we are at least in a SVN dir. Parse the .svn/entries
492 // and see if we can find this file. We do a fast and
494 ifstream ifs(entries.toFilesystemEncoding().c_str());
495 string line, oldline;
496 while (getline(ifs, line)) {
497 if (line == "dir" || line == "file")
498 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
499 if (oldline == tmpf && line == "file")
508 void SVN::scanMaster()
511 vcstatus = NOLOCKING;
512 if (checkLockMode()) {
517 locker_ = "Unlocked";
524 bool SVN::checkLockMode()
526 FileName tmpf = FileName::tempName("lyxvcout");
528 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
529 return N_("Error: Could not generate logfile.");
532 LYXERR(Debug::LYXVC, "Detecting locking mode...");
533 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
534 + " > " + quoteName(tmpf.toFilesystemEncoding()),
538 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
544 LYXERR(Debug::LYXVC, line);
545 if (contains(line, "svn:needs-lock"))
548 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
556 bool SVN::isLocked() const
559 FileName file(file_.absFilename());
560 return !file.isReadOnly();
564 void SVN::registrer(string const & /*msg*/)
566 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
567 FileName(owner_->filePath()));
571 string SVN::checkIn(string const & msg)
573 FileName tmpf = FileName::tempName("lyxvcout");
575 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
576 return N_("Error: Could not generate logfile.");
579 doVCCommand("svn commit -m \"" + msg + "\" "
580 + quoteName(onlyFilename(owner_->absFileName()))
581 + " > " + quoteName(tmpf.toFilesystemEncoding()),
582 FileName(owner_->filePath()));
585 string res = scanLogFile(tmpf, log);
587 frontend::Alert::error(_("Revision control error."),
588 _("Error when committing to repository.\n"
589 "You have to manually resolve the problem.\n"
590 "After pressing OK, LyX will reopen the document."));
592 fileLock(false, tmpf, log);
595 return "SVN: " + log;
599 bool SVN::checkInEnabled()
608 // FIXME Correctly return code should be checked instead of this.
609 // This would need another solution than just plain startscript.
610 // Hint from Andre': QProcess::readAllStandardError()...
611 string SVN::scanLogFile(FileName const & f, string & status)
613 ifstream ifs(f.toFilesystemEncoding().c_str());
618 lyxerr << line << "\n";
619 if (!line.empty()) status += line + "; ";
620 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
624 if (contains(line, "svn:needs-lock")) {
634 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
636 if (!locked_mode_ || (isLocked() == lock))
639 string arg = lock ? "lock " : "unlock ";
640 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
641 + " > " + quoteName(tmpf.toFilesystemEncoding()),
642 FileName(owner_->filePath()));
644 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
648 if (!line.empty()) status += line + "; ";
652 if (!isLocked() && lock)
653 frontend::Alert::error(_("Revision control error."),
654 _("Error when acquiring write lock.\n"
655 "Most probably another user is editing\n"
656 "the current document now!\n"
657 "Also check the access to the repository."));
658 if (isLocked() && !lock)
659 frontend::Alert::error(_("Revision control error."),
660 _("Error when releasing write lock.\n"
661 "Check the access to the repository."));
665 string SVN::checkOut()
667 FileName tmpf = FileName::tempName("lyxvcout");
669 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
670 return N_("Error: Could not generate logfile.");
673 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
674 + " > " + quoteName(tmpf.toFilesystemEncoding()),
675 FileName(owner_->filePath()));
678 string res = scanLogFile(tmpf, log);
680 frontend::Alert::error(_("Revision control error."),
681 bformat(_("Error when updating from repository.\n"
682 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
683 "After pressing OK, LyX will try to reopen resolved document."),
684 from_local8bit(res)));
686 fileLock(true, tmpf, log);
689 return "SVN: " + log;
693 bool SVN::checkOutEnabled()
702 string SVN::repoSynchro()
704 FileName tmpf = FileName::tempName("lyxvcout");
706 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
707 return N_("Error: Could not generate logfile.");
710 doVCCommand("svn diff " + quoteName(owner_->filePath())
711 + " > " + quoteName(tmpf.toFilesystemEncoding()),
712 FileName(owner_->filePath()));
713 docstring res = tmpf.fileContents("UTF-8");
715 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
716 docstring const file = from_utf8(owner_->filePath());
717 docstring text = bformat(_("There were detected changes"
718 "in the working directory.\n"
719 "Synchronizing with repository will discard "
720 "any uncommitted changes in the directory:\n%1$s"
721 "\n\nContinue?"), file);
722 int const ret = frontend::Alert::prompt(_("Changes detected"),
723 text, 0, 1, _("&Yes"), _("&No"));
730 doVCCommand("svn revert -R " + quoteName(owner_->filePath())
731 + " > " + quoteName(tmpf.toFilesystemEncoding()),
732 FileName(owner_->filePath()));
733 res = "Revert log:\n" + tmpf.fileContents("UTF-8");
734 doVCCommand("svn update " + quoteName(owner_->filePath())
735 + " > " + quoteName(tmpf.toFilesystemEncoding()),
736 FileName(owner_->filePath()));
737 res += "Update log:\n" + tmpf.fileContents("UTF-8");
739 LYXERR(Debug::LYXVC, res);
745 bool SVN::repoSynchroEnabled()
751 string SVN::lockingToggle()
753 FileName tmpf = FileName::tempName("lyxvcout");
755 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
756 return N_("Error: Could not generate logfile.");
759 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
760 + " > " + quoteName(tmpf.toFilesystemEncoding()),
761 FileName(owner_->filePath()));
766 string res = scanLogFile(tmpf, log);
767 bool locking = contains(res, "svn:needs-lock");
769 ret = doVCCommand("svn propset svn:needs-lock ON "
770 + quoteName(onlyFilename(owner_->absFileName()))
771 + " > " + quoteName(tmpf.toFilesystemEncoding()),
772 FileName(owner_->filePath()));
774 ret = doVCCommand("svn propdel svn:needs-lock "
775 + quoteName(onlyFilename(owner_->absFileName()))
776 + " > " + quoteName(tmpf.toFilesystemEncoding()),
777 FileName(owner_->filePath()));
782 frontend::Alert::warning(_("VCN File Locking"),
783 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
784 + _("Do not forget to commit the locking property into the repository."),
787 return string("SVN: ") + N_("Locking property set.");
791 bool SVN::lockingToggleEnabled()
798 // Reverts to the version in CVS repository and
799 // gets the updated version from the repository.
800 string const fil = quoteName(onlyFilename(owner_->absFileName()));
802 doVCCommand("svn revert -q " + fil,
803 FileName(owner_->filePath()));
810 // merge the current with the previous version
811 // in a reverse patch kind of way, so that the
812 // result is to revert the last changes.
813 lyxerr << "Sorry, not implemented." << endl;
817 bool SVN::undoLastEnabled()
823 void SVN::getLog(FileName const & tmpf)
825 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
826 + " > " + quoteName(tmpf.toFilesystemEncoding()),
827 FileName(owner_->filePath()));
831 bool SVN::toggleReadOnlyEnabled()