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()
276 // This got broken somewhere along lfuns dispatch reorganization.
277 // reloadBuffer would be needed after this, but thats problematic
278 // since we are inside Buffer::dispatch.
283 // FIXME This could be implemented with cache from scanMaster
284 string const RCS::revisionInfo(LyXVC::RevisionInfo const)
290 /////////////////////////////////////////////////////////////////////
294 /////////////////////////////////////////////////////////////////////
296 CVS::CVS(FileName const & m, FileName const & f)
304 FileName const CVS::findFile(FileName const & file)
306 // First we look for the CVS/Entries in the same dir
307 // where we have file.
308 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
309 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
310 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
311 << "' for `" << tmpf << '\'');
312 if (entries.isReadableFile()) {
313 // Ok we are at least in a CVS dir. Parse the CVS/Entries
314 // and see if we can find this file. We do a fast and
316 ifstream ifs(entries.toFilesystemEncoding().c_str());
318 while (getline(ifs, line)) {
319 LYXERR(Debug::LYXVC, "\tEntries: " << line);
320 if (contains(line, tmpf))
328 void CVS::scanMaster()
330 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
331 // Ok now we do the real scan...
332 ifstream ifs(master_.toFilesystemEncoding().c_str());
333 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
334 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
336 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
337 while (getline(ifs, line)) {
338 LYXERR(Debug::LYXVC, "\t line: " << line);
339 if (contains(line, tmpf)) {
340 // Ok extract the fields.
343 regex_match(line, sm, reg);
345 //sm[0]; // whole matched string
347 version_ = sm.str(2);
348 string const file_date = sm.str(3);
351 //sm[5]; // tag or tagdate
352 // FIXME: must double check file is stattable/existing
353 time_t mod = file_.lastModified();
354 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
355 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
356 << "'\nModification date of file: `" << mod_date << '\'');
357 //FIXME this whole locking bussiness is not working under cvs and the machinery
358 // conforms to the ci usage, not cvs.
359 if (file_date == mod_date) {
360 locker_ = "Unlocked";
363 // Here we should also to some more checking
364 // to see if there are conflicts or not.
374 void CVS::registrer(string const & msg)
376 doVCCommand("cvs -q add -m \"" + msg + "\" "
377 + quoteName(onlyFilename(owner_->absFileName())),
378 FileName(owner_->filePath()));
382 string CVS::checkIn(string const & msg)
384 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
385 + quoteName(onlyFilename(owner_->absFileName())),
386 FileName(owner_->filePath()));
387 return ret ? string() : "CVS: Proceeded";
391 bool CVS::checkInEnabled()
397 string CVS::checkOut()
399 // cvs update or perhaps for cvs this should be a noop
400 // we need to detect conflict (eg "C" in output)
401 // before we can do this.
402 lyxerr << "Sorry, not implemented." << endl;
407 bool CVS::checkOutEnabled()
413 string CVS::repoUpdate()
415 lyxerr << "Sorry, not implemented." << endl;
420 bool CVS::repoUpdateEnabled()
426 string CVS::lockingToggle()
428 lyxerr << "Sorry, not implemented." << endl;
433 bool CVS::lockingToggleEnabled()
441 // Reverts to the version in CVS repository and
442 // gets the updated version from the repository.
443 string const fil = quoteName(onlyFilename(owner_->absFileName()));
444 // This is sensitive operation, so at lest some check about
445 // existence of cvs program and its file
446 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
448 FileName f(owner_->absFileName());
450 doVCCommand("cvs update " + fil,
451 FileName(owner_->filePath()));
458 // merge the current with the previous version
459 // in a reverse patch kind of way, so that the
460 // result is to revert the last changes.
461 lyxerr << "Sorry, not implemented." << endl;
465 bool CVS::undoLastEnabled()
471 void CVS::getLog(FileName const & tmpf)
473 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
474 + " > " + quoteName(tmpf.toFilesystemEncoding()),
475 FileName(owner_->filePath()));
479 bool CVS::toggleReadOnlyEnabled()
485 string const CVS::revisionInfo(LyXVC::RevisionInfo const)
491 /////////////////////////////////////////////////////////////////////
495 /////////////////////////////////////////////////////////////////////
497 SVN::SVN(FileName const & m, FileName const & f)
507 FileName const SVN::findFile(FileName const & file)
509 // First we look for the .svn/entries in the same dir
510 // where we have file.
511 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
512 string const tmpf = onlyFilename(file.absFilename());
513 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
514 << "' for `" << tmpf << '\'');
515 if (entries.isReadableFile()) {
516 // Ok we are at least in a SVN dir. Parse the .svn/entries
517 // and see if we can find this file. We do a fast and
519 ifstream ifs(entries.toFilesystemEncoding().c_str());
520 string line, oldline;
521 while (getline(ifs, line)) {
522 if (line == "dir" || line == "file")
523 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
524 if (oldline == tmpf && line == "file")
533 void SVN::scanMaster()
536 // vcstatus code is somewhat superflous, until we want
537 // to implement read-only toggle for svn.
538 vcstatus = NOLOCKING;
539 if (checkLockMode()) {
544 locker_ = "Unlocked";
551 bool SVN::checkLockMode()
553 FileName tmpf = FileName::tempName("lyxvcout");
555 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
556 return N_("Error: Could not generate logfile.");
559 LYXERR(Debug::LYXVC, "Detecting locking mode...");
560 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
561 + " > " + quoteName(tmpf.toFilesystemEncoding()),
565 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
571 LYXERR(Debug::LYXVC, line);
572 if (contains(line, "svn:needs-lock"))
575 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
583 bool SVN::isLocked() const
586 return !file_.isReadOnly();
590 void SVN::registrer(string const & /*msg*/)
592 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
593 FileName(owner_->filePath()));
597 string SVN::checkIn(string const & msg)
599 FileName tmpf = FileName::tempName("lyxvcout");
601 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
602 return N_("Error: Could not generate logfile.");
605 doVCCommand("svn commit -m \"" + msg + "\" "
606 + quoteName(onlyFilename(owner_->absFileName()))
607 + " > " + quoteName(tmpf.toFilesystemEncoding()),
608 FileName(owner_->filePath()));
611 string res = scanLogFile(tmpf, log);
613 frontend::Alert::error(_("Revision control error."),
614 _("Error when committing to repository.\n"
615 "You have to manually resolve the problem.\n"
616 "LyX will reopen the document after you press OK."));
618 fileLock(false, tmpf, log);
621 return log.empty() ? string() : "SVN: " + log;
625 bool SVN::checkInEnabled()
634 // FIXME Correctly return code should be checked instead of this.
635 // This would need another solution than just plain startscript.
636 // Hint from Andre': QProcess::readAllStandardError()...
637 string SVN::scanLogFile(FileName const & f, string & status)
639 ifstream ifs(f.toFilesystemEncoding().c_str());
644 LYXERR(Debug::LYXVC, line << "\n");
646 status += line + "; ";
647 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
648 || contains(line, "Commit failed")) {
652 if (contains(line, "svn:needs-lock")) {
662 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
664 if (!locked_mode_ || (isLocked() == lock))
667 string const arg = lock ? "lock " : "unlock ";
668 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
669 + " > " + quoteName(tmpf.toFilesystemEncoding()),
670 FileName(owner_->filePath()));
672 // Lock error messages go unfortunately on stderr and are unreachible this way.
673 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
677 if (!line.empty()) status += line + "; ";
681 if (!isLocked() && lock)
682 frontend::Alert::error(_("Revision control error."),
683 _("Error when acquiring write lock.\n"
684 "Most probably another user is editing\n"
685 "the current document now!\n"
686 "Also check the access to the repository."));
687 if (isLocked() && !lock)
688 frontend::Alert::error(_("Revision control error."),
689 _("Error when releasing write lock.\n"
690 "Check the access to the repository."));
694 string SVN::checkOut()
696 FileName tmpf = FileName::tempName("lyxvcout");
698 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
699 return N_("Error: Could not generate logfile.");
702 doVCCommand("svn update --non-interactive " + quoteName(onlyFilename(owner_->absFileName()))
703 + " > " + quoteName(tmpf.toFilesystemEncoding()),
704 FileName(owner_->filePath()));
707 string res = scanLogFile(tmpf, log);
709 frontend::Alert::error(_("Revision control error."),
710 bformat(_("Error when updating from repository.\n"
711 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
712 "After pressing OK, LyX will try to reopen the resolved document."),
713 from_local8bit(res)));
715 fileLock(true, tmpf, log);
718 return log.empty() ? string() : "SVN: " + log;
722 bool SVN::checkOutEnabled()
731 string SVN::repoUpdate()
733 FileName tmpf = FileName::tempName("lyxvcout");
735 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
736 return N_("Error: Could not generate logfile.");
739 doVCCommand("svn diff " + quoteName(owner_->filePath())
740 + " > " + quoteName(tmpf.toFilesystemEncoding()),
741 FileName(owner_->filePath()));
742 docstring res = tmpf.fileContents("UTF-8");
744 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
745 docstring const file = from_utf8(owner_->filePath());
746 docstring text = bformat(_("There were detected changes "
747 "in the working directory:\n%1$s\n\n"
748 "In case of file conflict version of the local directory files "
750 "\n\nContinue?"), file);
751 int ret = frontend::Alert::prompt(_("Changes detected"),
752 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
754 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFilename()));
755 ret = frontend::Alert::prompt(_("Changes detected"),
756 text, 0, 1, _("&Yes"), _("&No"));
757 hideDialogs("file", 0);
765 // Reverting looks too harsh, see bug #6255.
766 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
767 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
768 // FileName(owner_->filePath()));
769 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
770 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
771 + " > " + quoteName(tmpf.toFilesystemEncoding()),
772 FileName(owner_->filePath()));
773 res += "Update log:\n" + tmpf.fileContents("UTF-8");
775 LYXERR(Debug::LYXVC, res);
781 bool SVN::repoUpdateEnabled()
787 string SVN::lockingToggle()
789 FileName tmpf = FileName::tempName("lyxvcout");
791 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
792 return N_("Error: Could not generate logfile.");
795 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
796 + " > " + quoteName(tmpf.toFilesystemEncoding()),
797 FileName(owner_->filePath()));
802 string res = scanLogFile(tmpf, log);
803 bool locking = contains(res, "svn:needs-lock");
805 ret = doVCCommand("svn propset svn:needs-lock ON "
806 + quoteName(onlyFilename(owner_->absFileName()))
807 + " > " + quoteName(tmpf.toFilesystemEncoding()),
808 FileName(owner_->filePath()));
810 ret = doVCCommand("svn propdel svn:needs-lock "
811 + quoteName(onlyFilename(owner_->absFileName()))
812 + " > " + quoteName(tmpf.toFilesystemEncoding()),
813 FileName(owner_->filePath()));
818 frontend::Alert::warning(_("VCN File Locking"),
819 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
820 + _("Do not forget to commit the locking property into the repository."),
823 return string("SVN: ") + N_("Locking property set.");
827 bool SVN::lockingToggleEnabled()
835 // Reverts to the version in CVS repository and
836 // gets the updated version from the repository.
837 string const fil = quoteName(onlyFilename(owner_->absFileName()));
839 doVCCommand("svn revert -q " + fil,
840 FileName(owner_->filePath()));
847 // merge the current with the previous version
848 // in a reverse patch kind of way, so that the
849 // result is to revert the last changes.
850 lyxerr << "Sorry, not implemented." << endl;
854 bool SVN::undoLastEnabled()
860 string const SVN::revisionInfo(LyXVC::RevisionInfo const info)
864 if (_rev_file_cache.empty())
865 _rev_file_cache = getFileRevisionInfo();
866 if (_rev_file_cache.empty())
867 _rev_file_cache = "?";
868 if (_rev_file_cache == "?")
871 return _rev_file_cache;
877 std::string SVN::getFileRevisionInfo()
879 FileName tmpf = FileName::tempName("lyxvcout");
881 doVCCommand("svn info --xml " + quoteName(onlyFilename(owner_->absFileName()))
882 + " > " + quoteName(tmpf.toFilesystemEncoding()),
883 FileName(owner_->filePath()));
888 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
896 LYXERR(Debug::LYXVC, line);
897 if (prefixIs(line, "<commit"))
899 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
900 string l1 = subst(line, "revision=\"", "");
901 string l2 = trim(subst(l1, "\">", ""));
912 void SVN::getLog(FileName const & tmpf)
914 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
915 + " > " + quoteName(tmpf.toFilesystemEncoding()),
916 FileName(owner_->filePath()));
920 bool SVN::toggleReadOnlyEnabled()