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
9 * Full author contact details are available in file CREDITS.
14 #include "VCBackend.h"
17 #include "FuncRequest.h"
19 #include "frontends/alert.h"
20 #include "frontends/Application.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/Path.h"
28 #include "support/Systemcall.h"
30 #include <boost/regex.hpp>
35 using namespace lyx::support;
38 using boost::regex_match;
44 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
46 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
48 support::PathChanger p(path);
49 return one.startscript(Systemcall::Wait, cmd, false);
53 int VCS::doVCCommand(string const & cmd, FileName const & path)
56 owner_->setBusy(true);
58 int const ret = doVCCommandCall(cmd, path);
61 owner_->setBusy(false);
63 frontend::Alert::error(_("Revision control error."),
64 bformat(_("Some problem occured while running the command:\n"
71 /////////////////////////////////////////////////////////////////////
75 /////////////////////////////////////////////////////////////////////
77 RCS::RCS(FileName const & m)
84 FileName const RCS::findFile(FileName const & file)
86 // Check if *,v exists.
87 FileName tmp(file.absFilename() + ",v");
88 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
89 if (tmp.isReadableFile()) {
90 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
94 // Check if RCS/*,v exists.
95 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
96 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
97 if (tmp.isReadableFile()) {
98 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
106 void RCS::retrieve(FileName const & file)
108 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
109 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
114 void RCS::scanMaster()
119 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
121 ifstream ifs(master_.toFilesystemEncoding().c_str());
124 bool read_enough = false;
126 while (!read_enough && ifs >> token) {
127 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
132 else if (token == "head") {
136 tmv = rtrim(tmv, ";");
138 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
139 } else if (contains(token, "access")
140 || contains(token, "symbols")
141 || contains(token, "strict")) {
143 } else if (contains(token, "locks")) {
145 if (contains(token, ';')) {
146 locker_ = "Unlocked";
155 s1 = rtrim(tmpt, ";");
156 // tmp is now in the format <user>:<version>
157 s1 = split(s1, s2, ':');
158 // s2 is user, and s1 is version
159 if (s1 == version_) {
164 } while (!contains(tmpt, ';'));
166 } else if (token == "comment") {
167 // we don't need to read any further than this.
171 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
177 void RCS::registrer(string const & msg)
179 string cmd = "ci -q -u -i -t-\"";
182 cmd += quoteName(onlyFilename(owner_->absFileName()));
183 doVCCommand(cmd, FileName(owner_->filePath()));
187 string RCS::checkIn(string const & msg)
189 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
190 + quoteName(onlyFilename(owner_->absFileName())),
191 FileName(owner_->filePath()));
192 return ret ? string() : "RCS: Proceeded";
196 bool RCS::checkInEnabled()
198 return owner_ && !owner_->isReadonly();
202 string RCS::checkOut()
205 int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
206 FileName(owner_->filePath()));
207 return ret ? string() : "RCS: Proceeded";
211 bool RCS::checkOutEnabled()
213 return owner_ && owner_->isReadonly();
217 string RCS::repoUpdate()
219 lyxerr << "Sorry, not implemented." << endl;
224 bool RCS::repoUpdateEnabled()
230 string RCS::lockingToggle()
232 lyxerr << "Sorry, not implemented." << endl;
237 bool RCS::lockingToggleEnabled()
245 doVCCommand("co -f -u" + version_ + " "
246 + quoteName(onlyFilename(owner_->absFileName())),
247 FileName(owner_->filePath()));
248 // We ignore changes and just reload!
255 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
256 doVCCommand("rcs -o" + version_ + " "
257 + quoteName(onlyFilename(owner_->absFileName())),
258 FileName(owner_->filePath()));
262 bool RCS::undoLastEnabled()
268 void RCS::getLog(FileName const & tmpf)
270 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
271 + " > " + quoteName(tmpf.toFilesystemEncoding()),
272 FileName(owner_->filePath()));
276 bool RCS::toggleReadOnlyEnabled()
278 // This got broken somewhere along lfuns dispatch reorganization.
279 // reloadBuffer would be needed after this, but thats problematic
280 // since we are inside Buffer::dispatch.
285 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
287 if (info == LyXVC::File)
293 bool RCS::prepareFileRevision(string const &, string &)
299 bool RCS::prepareFileRevisionEnabled()
305 /////////////////////////////////////////////////////////////////////
309 /////////////////////////////////////////////////////////////////////
311 CVS::CVS(FileName const & m, FileName const & f)
319 FileName const CVS::findFile(FileName const & file)
321 // First we look for the CVS/Entries in the same dir
322 // where we have file.
323 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
324 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
325 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
326 << "' for `" << tmpf << '\'');
327 if (entries.isReadableFile()) {
328 // Ok we are at least in a CVS dir. Parse the CVS/Entries
329 // and see if we can find this file. We do a fast and
331 ifstream ifs(entries.toFilesystemEncoding().c_str());
333 while (getline(ifs, line)) {
334 LYXERR(Debug::LYXVC, "\tEntries: " << line);
335 if (contains(line, tmpf))
343 void CVS::scanMaster()
345 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
346 // Ok now we do the real scan...
347 ifstream ifs(master_.toFilesystemEncoding().c_str());
348 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
349 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
351 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
352 while (getline(ifs, line)) {
353 LYXERR(Debug::LYXVC, "\t line: " << line);
354 if (contains(line, tmpf)) {
355 // Ok extract the fields.
358 regex_match(line, sm, reg);
360 //sm[0]; // whole matched string
362 version_ = sm.str(2);
363 string const file_date = sm.str(3);
366 //sm[5]; // tag or tagdate
367 // FIXME: must double check file is stattable/existing
368 time_t mod = file_.lastModified();
369 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
370 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
371 << "'\nModification date of file: `" << mod_date << '\'');
372 //FIXME this whole locking bussiness is not working under cvs and the machinery
373 // conforms to the ci usage, not cvs.
374 if (file_date == mod_date) {
375 locker_ = "Unlocked";
378 // Here we should also to some more checking
379 // to see if there are conflicts or not.
389 void CVS::registrer(string const & msg)
391 doVCCommand("cvs -q add -m \"" + msg + "\" "
392 + quoteName(onlyFilename(owner_->absFileName())),
393 FileName(owner_->filePath()));
397 string CVS::checkIn(string const & msg)
399 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
400 + quoteName(onlyFilename(owner_->absFileName())),
401 FileName(owner_->filePath()));
402 return ret ? string() : "CVS: Proceeded";
406 bool CVS::checkInEnabled()
412 string CVS::checkOut()
414 // cvs update or perhaps for cvs this should be a noop
415 // we need to detect conflict (eg "C" in output)
416 // before we can do this.
417 lyxerr << "Sorry, not implemented." << endl;
422 bool CVS::checkOutEnabled()
428 string CVS::repoUpdate()
430 lyxerr << "Sorry, not implemented." << endl;
435 bool CVS::repoUpdateEnabled()
441 string CVS::lockingToggle()
443 lyxerr << "Sorry, not implemented." << endl;
448 bool CVS::lockingToggleEnabled()
456 // Reverts to the version in CVS repository and
457 // gets the updated version from the repository.
458 string const fil = quoteName(onlyFilename(owner_->absFileName()));
459 // This is sensitive operation, so at lest some check about
460 // existence of cvs program and its file
461 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
463 FileName f(owner_->absFileName());
465 doVCCommand("cvs update " + fil,
466 FileName(owner_->filePath()));
473 // merge the current with the previous version
474 // in a reverse patch kind of way, so that the
475 // result is to revert the last changes.
476 lyxerr << "Sorry, not implemented." << endl;
480 bool CVS::undoLastEnabled()
486 void CVS::getLog(FileName const & tmpf)
488 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
489 + " > " + quoteName(tmpf.toFilesystemEncoding()),
490 FileName(owner_->filePath()));
494 bool CVS::toggleReadOnlyEnabled()
500 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
502 if (info == LyXVC::File)
508 bool CVS::prepareFileRevision(string const &, string &)
514 bool CVS::prepareFileRevisionEnabled()
520 /////////////////////////////////////////////////////////////////////
524 /////////////////////////////////////////////////////////////////////
526 SVN::SVN(FileName const & m, FileName const & f)
536 FileName const SVN::findFile(FileName const & file)
538 // First we look for the .svn/entries in the same dir
539 // where we have file.
540 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
541 string const tmpf = onlyFilename(file.absFilename());
542 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
543 << "' for `" << tmpf << '\'');
544 if (entries.isReadableFile()) {
545 // Ok we are at least in a SVN dir. Parse the .svn/entries
546 // and see if we can find this file. We do a fast and
548 ifstream ifs(entries.toFilesystemEncoding().c_str());
549 string line, oldline;
550 while (getline(ifs, line)) {
551 if (line == "dir" || line == "file")
552 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
553 if (oldline == tmpf && line == "file")
562 void SVN::scanMaster()
564 // vcstatus code is somewhat superflous, until we want
565 // to implement read-only toggle for svn.
566 vcstatus = NOLOCKING;
567 if (checkLockMode()) {
577 bool SVN::checkLockMode()
579 FileName tmpf = FileName::tempName("lyxvcout");
581 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
582 return N_("Error: Could not generate logfile.");
585 LYXERR(Debug::LYXVC, "Detecting locking mode...");
586 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
587 + " > " + quoteName(tmpf.toFilesystemEncoding()),
591 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
597 LYXERR(Debug::LYXVC, line);
598 if (contains(line, "svn:needs-lock"))
601 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
609 bool SVN::isLocked() const
612 return !file_.isReadOnly();
616 void SVN::registrer(string const & /*msg*/)
618 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
619 FileName(owner_->filePath()));
623 string SVN::checkIn(string const & msg)
625 FileName tmpf = FileName::tempName("lyxvcout");
627 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
628 return N_("Error: Could not generate logfile.");
631 doVCCommand("svn commit -m \"" + msg + "\" "
632 + quoteName(onlyFilename(owner_->absFileName()))
633 + " > " + quoteName(tmpf.toFilesystemEncoding()),
634 FileName(owner_->filePath()));
637 string res = scanLogFile(tmpf, log);
639 frontend::Alert::error(_("Revision control error."),
640 _("Error when committing to repository.\n"
641 "You have to manually resolve the problem.\n"
642 "LyX will reopen the document after you press OK."));
644 fileLock(false, tmpf, log);
647 return log.empty() ? string() : "SVN: " + log;
651 bool SVN::checkInEnabled()
660 // FIXME Correctly return code should be checked instead of this.
661 // This would need another solution than just plain startscript.
662 // Hint from Andre': QProcess::readAllStandardError()...
663 string SVN::scanLogFile(FileName const & f, string & status)
665 ifstream ifs(f.toFilesystemEncoding().c_str());
670 LYXERR(Debug::LYXVC, line << "\n");
672 status += line + "; ";
673 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
674 || contains(line, "Commit failed")) {
678 if (contains(line, "svn:needs-lock")) {
688 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
690 if (!locked_mode_ || (isLocked() == lock))
693 string const arg = lock ? "lock " : "unlock ";
694 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
695 + " > " + quoteName(tmpf.toFilesystemEncoding()),
696 FileName(owner_->filePath()));
698 // Lock error messages go unfortunately on stderr and are unreachible this way.
699 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
703 if (!line.empty()) status += line + "; ";
707 if (!isLocked() && lock)
708 frontend::Alert::error(_("Revision control error."),
709 _("Error when acquiring write lock.\n"
710 "Most probably another user is editing\n"
711 "the current document now!\n"
712 "Also check the access to the repository."));
713 if (isLocked() && !lock)
714 frontend::Alert::error(_("Revision control error."),
715 _("Error when releasing write lock.\n"
716 "Check the access to the repository."));
720 string SVN::checkOut()
722 FileName tmpf = FileName::tempName("lyxvcout");
724 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
725 return N_("Error: Could not generate logfile.");
728 doVCCommand("svn update --non-interactive " + quoteName(onlyFilename(owner_->absFileName()))
729 + " > " + quoteName(tmpf.toFilesystemEncoding()),
730 FileName(owner_->filePath()));
733 string res = scanLogFile(tmpf, log);
735 frontend::Alert::error(_("Revision control error."),
736 bformat(_("Error when updating from repository.\n"
737 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
738 "After pressing OK, LyX will try to reopen the resolved document."),
739 from_local8bit(res)));
741 fileLock(true, tmpf, log);
744 return log.empty() ? string() : "SVN: " + log;
748 bool SVN::checkOutEnabled()
757 string SVN::repoUpdate()
759 FileName tmpf = FileName::tempName("lyxvcout");
761 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
762 return N_("Error: Could not generate logfile.");
765 doVCCommand("svn diff " + quoteName(owner_->filePath())
766 + " > " + quoteName(tmpf.toFilesystemEncoding()),
767 FileName(owner_->filePath()));
768 docstring res = tmpf.fileContents("UTF-8");
770 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
771 docstring const file = from_utf8(owner_->filePath());
772 docstring text = bformat(_("There were detected changes "
773 "in the working directory:\n%1$s\n\n"
774 "In case of file conflict version of the local directory files "
776 "\n\nContinue?"), file);
777 int ret = frontend::Alert::prompt(_("Changes detected"),
778 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
780 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFilename()));
781 ret = frontend::Alert::prompt(_("Changes detected"),
782 text, 0, 1, _("&Yes"), _("&No"));
783 hideDialogs("file", 0);
791 // Reverting looks too harsh, see bug #6255.
792 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
793 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
794 // FileName(owner_->filePath()));
795 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
796 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
797 + " > " + quoteName(tmpf.toFilesystemEncoding()),
798 FileName(owner_->filePath()));
799 res += "Update log:\n" + tmpf.fileContents("UTF-8");
801 LYXERR(Debug::LYXVC, res);
807 bool SVN::repoUpdateEnabled()
813 string SVN::lockingToggle()
815 FileName tmpf = FileName::tempName("lyxvcout");
817 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
818 return N_("Error: Could not generate logfile.");
821 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
822 + " > " + quoteName(tmpf.toFilesystemEncoding()),
823 FileName(owner_->filePath()));
828 string res = scanLogFile(tmpf, log);
829 bool locking = contains(res, "svn:needs-lock");
831 ret = doVCCommand("svn propset svn:needs-lock ON "
832 + quoteName(onlyFilename(owner_->absFileName()))
833 + " > " + quoteName(tmpf.toFilesystemEncoding()),
834 FileName(owner_->filePath()));
836 ret = doVCCommand("svn propdel svn:needs-lock "
837 + quoteName(onlyFilename(owner_->absFileName()))
838 + " > " + quoteName(tmpf.toFilesystemEncoding()),
839 FileName(owner_->filePath()));
844 frontend::Alert::warning(_("VCN File Locking"),
845 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
846 + _("Do not forget to commit the locking property into the repository."),
849 return string("SVN: ") + N_("Locking property set.");
853 bool SVN::lockingToggleEnabled()
861 // Reverts to the version in CVS repository and
862 // gets the updated version from the repository.
863 string const fil = quoteName(onlyFilename(owner_->absFileName()));
865 doVCCommand("svn revert -q " + fil,
866 FileName(owner_->filePath()));
873 // merge the current with the previous version
874 // in a reverse patch kind of way, so that the
875 // result is to revert the last changes.
876 lyxerr << "Sorry, not implemented." << endl;
880 bool SVN::undoLastEnabled()
886 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
888 if (info == LyXVC::Tree) {
889 if (rev_tree_cache_.empty())
890 if (!getTreeRevisionInfo())
891 rev_tree_cache_ = "?";
892 if (rev_tree_cache_ == "?")
895 return rev_tree_cache_;
898 // fill the rest of the attributes for a single file
899 if (rev_file_cache_.empty())
900 if (!getFileRevisionInfo())
901 rev_file_cache_ = "?";
905 if (rev_file_cache_ == "?")
907 return rev_file_cache_;
909 return rev_author_cache_;
911 return rev_date_cache_;
913 return rev_time_cache_;
922 bool SVN::getFileRevisionInfo()
924 FileName tmpf = FileName::tempName("lyxvcout");
926 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
927 return N_("Error: Could not generate logfile.");
930 doVCCommand("svn info --xml " + quoteName(onlyFilename(owner_->absFileName()))
931 + " > " + quoteName(tmpf.toFilesystemEncoding()),
932 FileName(owner_->filePath()));
937 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
945 LYXERR(Debug::LYXVC, line);
946 if (prefixIs(line, "<commit"))
948 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
949 string l1 = subst(line, "revision=\"", "");
950 string l2 = trim(subst(l1, "\">", ""));
952 rev_file_cache_ = rev = l2;
954 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
955 string l1 = subst(line, "<author>", "");
956 string l2 = subst(l1, "</author>", "");
957 rev_author_cache_ = l2;
959 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
960 string l1 = subst(line, "<date>", "");
961 string l2 = subst(l1, "</date>", "");
962 l2 = split(l2, l1, 'T');
963 rev_date_cache_ = l1;
964 l2 = split(l2, l1, '.');
965 rev_time_cache_ = l1;
975 bool SVN::getTreeRevisionInfo()
977 FileName tmpf = FileName::tempName("lyxvcout");
979 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
980 return N_("Error: Could not generate logfile.");
983 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
984 FileName(owner_->filePath()));
989 // only first line in case something bad happens.
990 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
996 rev_tree_cache_ = line;
997 return !line.empty();
1001 void SVN::getLog(FileName const & tmpf)
1003 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
1004 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1005 FileName(owner_->filePath()));
1009 bool SVN::prepareFileRevision(string const & revis, string & f)
1011 if (!isStrInt(revis))
1014 int rev = convert<int>(revis);
1016 if (!getFileRevisionInfo())
1019 rev = convert<int>(rev_file_cache_);
1020 // go back for minus rev
1022 rev = rev + convert<int>(rev_file_cache_);
1027 FileName tmpf = FileName::tempName("lyxvcrev");
1029 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1030 return N_("Error: Could not generate logfile.");
1033 doVCCommand("svn cat -r " + convert<string>(rev) + " "
1034 + quoteName(onlyFilename(owner_->absFileName()))
1035 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1036 FileName(owner_->filePath()));
1037 if (tmpf.isFileEmpty())
1040 f = tmpf.absFilename();
1045 bool SVN::prepareFileRevisionEnabled()
1052 bool SVN::toggleReadOnlyEnabled()