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 "FuncRequest.h"
18 #include "frontends/alert.h"
19 #include "frontends/Application.h"
21 #include "support/debug.h"
22 #include "support/filetools.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25 #include "support/Path.h"
26 #include "support/Systemcall.h"
28 #include <boost/regex.hpp>
33 using namespace lyx::support;
36 using boost::regex_match;
42 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
44 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
46 support::PathChanger p(path);
47 return one.startscript(Systemcall::Wait, cmd, false);
51 int VCS::doVCCommand(string const & cmd, FileName const & path)
54 owner_->setBusy(true);
56 int const ret = doVCCommandCall(cmd, path);
59 owner_->setBusy(false);
61 frontend::Alert::error(_("Revision control error."),
62 bformat(_("Some problem occured while running the command:\n"
69 /////////////////////////////////////////////////////////////////////
73 /////////////////////////////////////////////////////////////////////
75 RCS::RCS(FileName const & m)
82 FileName const RCS::findFile(FileName const & file)
84 // Check if *,v exists.
85 FileName tmp(file.absFilename() + ",v");
86 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
87 if (tmp.isReadableFile()) {
88 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
92 // Check if RCS/*,v exists.
93 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
94 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
95 if (tmp.isReadableFile()) {
96 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
104 void RCS::retrieve(FileName const & file)
106 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
107 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
112 void RCS::scanMaster()
117 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
119 ifstream ifs(master_.toFilesystemEncoding().c_str());
122 bool read_enough = false;
124 while (!read_enough && ifs >> token) {
125 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
130 else if (token == "head") {
134 tmv = rtrim(tmv, ";");
136 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
137 } else if (contains(token, "access")
138 || contains(token, "symbols")
139 || contains(token, "strict")) {
141 } else if (contains(token, "locks")) {
143 if (contains(token, ';')) {
144 locker_ = "Unlocked";
153 s1 = rtrim(tmpt, ";");
154 // tmp is now in the format <user>:<version>
155 s1 = split(s1, s2, ':');
156 // s2 is user, and s1 is version
157 if (s1 == version_) {
162 } while (!contains(tmpt, ';'));
164 } else if (token == "comment") {
165 // we don't need to read any further than this.
169 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
175 void RCS::registrer(string const & msg)
177 string cmd = "ci -q -u -i -t-\"";
180 cmd += quoteName(onlyFilename(owner_->absFileName()));
181 doVCCommand(cmd, FileName(owner_->filePath()));
185 string RCS::checkIn(string const & msg)
187 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
188 + quoteName(onlyFilename(owner_->absFileName())),
189 FileName(owner_->filePath()));
190 return ret ? string() : "RCS: Proceeded";
194 bool RCS::checkInEnabled()
196 return owner_ && !owner_->isReadonly();
200 string RCS::checkOut()
203 int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
204 FileName(owner_->filePath()));
205 return ret ? string() : "RCS: Proceeded";
209 bool RCS::checkOutEnabled()
211 return owner_ && owner_->isReadonly();
215 string RCS::repoUpdate()
217 lyxerr << "Sorry, not implemented." << endl;
222 bool RCS::repoUpdateEnabled()
228 string RCS::lockingToggle()
230 lyxerr << "Sorry, not implemented." << endl;
235 bool RCS::lockingToggleEnabled()
243 doVCCommand("co -f -u" + version() + " "
244 + quoteName(onlyFilename(owner_->absFileName())),
245 FileName(owner_->filePath()));
246 // We ignore changes and just reload!
253 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
254 doVCCommand("rcs -o" + version() + " "
255 + quoteName(onlyFilename(owner_->absFileName())),
256 FileName(owner_->filePath()));
260 bool RCS::undoLastEnabled()
266 void RCS::getLog(FileName const & tmpf)
268 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
269 + " > " + quoteName(tmpf.toFilesystemEncoding()),
270 FileName(owner_->filePath()));
274 bool RCS::toggleReadOnlyEnabled()
280 /////////////////////////////////////////////////////////////////////
284 /////////////////////////////////////////////////////////////////////
286 CVS::CVS(FileName const & m, FileName const & f)
294 FileName const CVS::findFile(FileName const & file)
296 // First we look for the CVS/Entries in the same dir
297 // where we have file.
298 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
299 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
300 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
301 << "' for `" << tmpf << '\'');
302 if (entries.isReadableFile()) {
303 // Ok we are at least in a CVS dir. Parse the CVS/Entries
304 // and see if we can find this file. We do a fast and
306 ifstream ifs(entries.toFilesystemEncoding().c_str());
308 while (getline(ifs, line)) {
309 LYXERR(Debug::LYXVC, "\tEntries: " << line);
310 if (contains(line, tmpf))
318 void CVS::scanMaster()
320 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
321 // Ok now we do the real scan...
322 ifstream ifs(master_.toFilesystemEncoding().c_str());
323 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
324 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
326 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
327 while (getline(ifs, line)) {
328 LYXERR(Debug::LYXVC, "\t line: " << line);
329 if (contains(line, tmpf)) {
330 // Ok extract the fields.
333 regex_match(line, sm, reg);
335 //sm[0]; // whole matched string
337 version_ = sm.str(2);
338 string const file_date = sm.str(3);
341 //sm[5]; // tag or tagdate
342 // FIXME: must double check file is stattable/existing
343 time_t mod = file_.lastModified();
344 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
345 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
346 << "'\nModification date of file: `" << mod_date << '\'');
347 //FIXME this whole locking bussiness is not working under cvs and the machinery
348 // conforms to the ci usage, not cvs.
349 if (file_date == mod_date) {
350 locker_ = "Unlocked";
353 // Here we should also to some more checking
354 // to see if there are conflicts or not.
364 void CVS::registrer(string const & msg)
366 doVCCommand("cvs -q add -m \"" + msg + "\" "
367 + quoteName(onlyFilename(owner_->absFileName())),
368 FileName(owner_->filePath()));
372 string CVS::checkIn(string const & msg)
374 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
375 + quoteName(onlyFilename(owner_->absFileName())),
376 FileName(owner_->filePath()));
377 return ret ? string() : "CVS: Proceeded";
381 bool CVS::checkInEnabled()
387 string CVS::checkOut()
389 // cvs update or perhaps for cvs this should be a noop
390 // we need to detect conflict (eg "C" in output)
391 // before we can do this.
392 lyxerr << "Sorry, not implemented." << endl;
397 bool CVS::checkOutEnabled()
403 string CVS::repoUpdate()
405 lyxerr << "Sorry, not implemented." << endl;
410 bool CVS::repoUpdateEnabled()
416 string CVS::lockingToggle()
418 lyxerr << "Sorry, not implemented." << endl;
423 bool CVS::lockingToggleEnabled()
431 // Reverts to the version in CVS repository and
432 // gets the updated version from the repository.
433 string const fil = quoteName(onlyFilename(owner_->absFileName()));
434 // This is sensitive operation, so at lest some check about
435 // existence of cvs program and its file
436 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
438 FileName f(owner_->absFileName());
440 doVCCommand("cvs update " + fil,
441 FileName(owner_->filePath()));
448 // merge the current with the previous version
449 // in a reverse patch kind of way, so that the
450 // result is to revert the last changes.
451 lyxerr << "Sorry, not implemented." << endl;
455 bool CVS::undoLastEnabled()
461 void CVS::getLog(FileName const & tmpf)
463 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
464 + " > " + quoteName(tmpf.toFilesystemEncoding()),
465 FileName(owner_->filePath()));
469 bool CVS::toggleReadOnlyEnabled()
474 /////////////////////////////////////////////////////////////////////
478 /////////////////////////////////////////////////////////////////////
480 SVN::SVN(FileName const & m, FileName const & f)
490 FileName const SVN::findFile(FileName const & file)
492 // First we look for the .svn/entries in the same dir
493 // where we have file.
494 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
495 string const tmpf = onlyFilename(file.absFilename());
496 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
497 << "' for `" << tmpf << '\'');
498 if (entries.isReadableFile()) {
499 // Ok we are at least in a SVN dir. Parse the .svn/entries
500 // and see if we can find this file. We do a fast and
502 ifstream ifs(entries.toFilesystemEncoding().c_str());
503 string line, oldline;
504 while (getline(ifs, line)) {
505 if (line == "dir" || line == "file")
506 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
507 if (oldline == tmpf && line == "file")
516 void SVN::scanMaster()
519 vcstatus = NOLOCKING;
520 if (checkLockMode()) {
525 locker_ = "Unlocked";
532 bool SVN::checkLockMode()
534 FileName tmpf = FileName::tempName("lyxvcout");
536 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
537 return N_("Error: Could not generate logfile.");
540 LYXERR(Debug::LYXVC, "Detecting locking mode...");
541 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
542 + " > " + quoteName(tmpf.toFilesystemEncoding()),
546 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
552 LYXERR(Debug::LYXVC, line);
553 if (contains(line, "svn:needs-lock"))
556 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
564 bool SVN::isLocked() const
567 FileName file(file_.absFilename());
568 return !file.isReadOnly();
572 void SVN::registrer(string const & /*msg*/)
574 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
575 FileName(owner_->filePath()));
579 string SVN::checkIn(string const & msg)
581 FileName tmpf = FileName::tempName("lyxvcout");
583 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
584 return N_("Error: Could not generate logfile.");
587 doVCCommand("svn commit -m \"" + msg + "\" "
588 + quoteName(onlyFilename(owner_->absFileName()))
589 + " > " + quoteName(tmpf.toFilesystemEncoding()),
590 FileName(owner_->filePath()));
593 string res = scanLogFile(tmpf, log);
595 frontend::Alert::error(_("Revision control error."),
596 _("Error when committing to repository.\n"
597 "You have to manually resolve the problem.\n"
598 "After pressing OK, LyX will reopen the document."));
600 fileLock(false, tmpf, log);
603 return "SVN: " + log;
607 bool SVN::checkInEnabled()
616 // FIXME Correctly return code should be checked instead of this.
617 // This would need another solution than just plain startscript.
618 // Hint from Andre': QProcess::readAllStandardError()...
619 string SVN::scanLogFile(FileName const & f, string & status)
621 ifstream ifs(f.toFilesystemEncoding().c_str());
626 lyxerr << line << "\n";
627 if (!line.empty()) status += line + "; ";
628 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
632 if (contains(line, "svn:needs-lock")) {
642 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
644 if (!locked_mode_ || (isLocked() == lock))
647 string arg = lock ? "lock " : "unlock ";
648 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
649 + " > " + quoteName(tmpf.toFilesystemEncoding()),
650 FileName(owner_->filePath()));
652 // Lock error messages go unfortunately on stderr and are unreachible this way.
653 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
657 if (!line.empty()) status += line + "; ";
661 if (!isLocked() && lock)
662 frontend::Alert::error(_("Revision control error."),
663 _("Error when acquiring write lock.\n"
664 "Most probably another user is editing\n"
665 "the current document now!\n"
666 "Also check the access to the repository."));
667 if (isLocked() && !lock)
668 frontend::Alert::error(_("Revision control error."),
669 _("Error when releasing write lock.\n"
670 "Check the access to the repository."));
674 string SVN::checkOut()
676 FileName tmpf = FileName::tempName("lyxvcout");
678 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
679 return N_("Error: Could not generate logfile.");
682 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
683 + " > " + quoteName(tmpf.toFilesystemEncoding()),
684 FileName(owner_->filePath()));
687 string res = scanLogFile(tmpf, log);
689 frontend::Alert::error(_("Revision control error."),
690 bformat(_("Error when updating from repository.\n"
691 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
692 "After pressing OK, LyX will try to reopen resolved document."),
693 from_local8bit(res)));
695 fileLock(true, tmpf, log);
698 return "SVN: " + log;
702 bool SVN::checkOutEnabled()
711 string SVN::repoUpdate()
713 FileName tmpf = FileName::tempName("lyxvcout");
715 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
716 return N_("Error: Could not generate logfile.");
719 doVCCommand("svn diff " + quoteName(owner_->filePath())
720 + " > " + quoteName(tmpf.toFilesystemEncoding()),
721 FileName(owner_->filePath()));
722 docstring res = tmpf.fileContents("UTF-8");
724 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
725 docstring const file = from_utf8(owner_->filePath());
726 docstring text = bformat(_("There were detected changes "
727 "in the working directory:\n%1$s\n\n"
728 "In case of file conflict version of the local directory files "
730 "\n\nContinue?"), file);
731 int ret = frontend::Alert::prompt(_("Changes detected"),
732 text, 0, 1, _("&Yes"), _("&No"), _("See &Log"));
734 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFilename()));
735 ret = frontend::Alert::prompt(_("Changes detected"),
736 text, 0, 1, _("&Yes"), _("&No"));
737 hideDialogs("file", 0);
745 // Reverting looks too harsh, see bug #6255.
746 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
747 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
748 // FileName(owner_->filePath()));
749 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
750 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
751 + " > " + quoteName(tmpf.toFilesystemEncoding()),
752 FileName(owner_->filePath()));
753 res += "Update log:\n" + tmpf.fileContents("UTF-8");
755 LYXERR(Debug::LYXVC, res);
761 bool SVN::repoUpdateEnabled()
767 string SVN::lockingToggle()
769 FileName tmpf = FileName::tempName("lyxvcout");
771 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
772 return N_("Error: Could not generate logfile.");
775 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
776 + " > " + quoteName(tmpf.toFilesystemEncoding()),
777 FileName(owner_->filePath()));
782 string res = scanLogFile(tmpf, log);
783 bool locking = contains(res, "svn:needs-lock");
785 ret = doVCCommand("svn propset svn:needs-lock ON "
786 + quoteName(onlyFilename(owner_->absFileName()))
787 + " > " + quoteName(tmpf.toFilesystemEncoding()),
788 FileName(owner_->filePath()));
790 ret = doVCCommand("svn propdel svn:needs-lock "
791 + quoteName(onlyFilename(owner_->absFileName()))
792 + " > " + quoteName(tmpf.toFilesystemEncoding()),
793 FileName(owner_->filePath()));
798 frontend::Alert::warning(_("VCN File Locking"),
799 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
800 + _("Do not forget to commit the locking property into the repository."),
803 return string("SVN: ") + N_("Locking property set.");
807 bool SVN::lockingToggleEnabled()
815 // Reverts to the version in CVS repository and
816 // gets the updated version from the repository.
817 string const fil = quoteName(onlyFilename(owner_->absFileName()));
819 doVCCommand("svn revert -q " + fil,
820 FileName(owner_->filePath()));
827 // merge the current with the previous version
828 // in a reverse patch kind of way, so that the
829 // result is to revert the last changes.
830 lyxerr << "Sorry, not implemented." << endl;
834 bool SVN::undoLastEnabled()
840 void SVN::getLog(FileName const & tmpf)
842 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
843 + " > " + quoteName(tmpf.toFilesystemEncoding()),
844 FileName(owner_->filePath()));
848 bool SVN::toggleReadOnlyEnabled()