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 &revis, string & f)
298 int back = convert<int>(rev);
303 // we care about the last number from revision string
304 // in case of backward indexing
307 cur = rsplit(version_, base , '.' );
310 int want = convert<int>(cur) + back;
314 rev = base + "." + convert<string>(want);
318 FileName tmpf = FileName::tempName("lyxvcrev");
320 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
321 return N_("Error: Could not generate logfile.");
324 doVCCommand("co -p" + rev + " "
325 + quoteName(onlyFilename(owner_->absFileName()))
326 + " > " + quoteName(tmpf.toFilesystemEncoding()),
327 FileName(owner_->filePath()));
328 if (tmpf.isFileEmpty())
331 f = tmpf.absFileName();
336 bool RCS::prepareFileRevisionEnabled()
342 /////////////////////////////////////////////////////////////////////
346 /////////////////////////////////////////////////////////////////////
348 CVS::CVS(FileName const & m, FileName const & f)
356 FileName const CVS::findFile(FileName const & file)
358 // First we look for the CVS/Entries in the same dir
359 // where we have file.
360 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
361 string const tmpf = '/' + onlyFilename(file.absFileName()) + '/';
362 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
363 << "' for `" << tmpf << '\'');
364 if (entries.isReadableFile()) {
365 // Ok we are at least in a CVS dir. Parse the CVS/Entries
366 // and see if we can find this file. We do a fast and
368 ifstream ifs(entries.toFilesystemEncoding().c_str());
370 while (getline(ifs, line)) {
371 LYXERR(Debug::LYXVC, "\tEntries: " << line);
372 if (contains(line, tmpf))
380 void CVS::scanMaster()
382 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
383 // Ok now we do the real scan...
384 ifstream ifs(master_.toFilesystemEncoding().c_str());
385 string tmpf = '/' + onlyFilename(file_.absFileName()) + '/';
386 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
388 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
389 while (getline(ifs, line)) {
390 LYXERR(Debug::LYXVC, "\t line: " << line);
391 if (contains(line, tmpf)) {
392 // Ok extract the fields.
395 regex_match(line, sm, reg);
397 //sm[0]; // whole matched string
399 version_ = sm.str(2);
400 string const file_date = sm.str(3);
403 //sm[5]; // tag or tagdate
404 // FIXME: must double check file is stattable/existing
405 time_t mod = file_.lastModified();
406 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
407 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
408 << "'\nModification date of file: `" << mod_date << '\'');
409 //FIXME this whole locking bussiness is not working under cvs and the machinery
410 // conforms to the ci usage, not cvs.
411 if (file_date == mod_date) {
412 locker_ = "Unlocked";
415 // Here we should also do some more checking
416 // to see if there are conflicts or not.
426 void CVS::registrer(string const & msg)
428 doVCCommand("cvs -q add -m \"" + msg + "\" "
429 + quoteName(onlyFilename(owner_->absFileName())),
430 FileName(owner_->filePath()));
434 string CVS::checkIn(string const & msg)
436 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
437 + quoteName(onlyFilename(owner_->absFileName())),
438 FileName(owner_->filePath()));
439 return ret ? string() : "CVS: Proceeded";
443 bool CVS::checkInEnabled()
449 string CVS::checkOut()
451 // cvs update or perhaps for cvs this should be a noop
452 // we need to detect conflict (eg "C" in output)
453 // before we can do this.
454 lyxerr << "Sorry, not implemented." << endl;
459 bool CVS::checkOutEnabled()
465 string CVS::repoUpdate()
467 lyxerr << "Sorry, not implemented." << endl;
472 bool CVS::repoUpdateEnabled()
478 string CVS::lockingToggle()
480 lyxerr << "Sorry, not implemented." << endl;
485 bool CVS::lockingToggleEnabled()
493 // Reverts to the version in CVS repository and
494 // gets the updated version from the repository.
495 string const fil = quoteName(onlyFilename(owner_->absFileName()));
496 // This is sensitive operation, so at lest some check about
497 // existence of cvs program and its file
498 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
500 FileName f(owner_->absFileName());
502 doVCCommand("cvs update " + fil,
503 FileName(owner_->filePath()));
510 // merge the current with the previous version
511 // in a reverse patch kind of way, so that the
512 // result is to revert the last changes.
513 lyxerr << "Sorry, not implemented." << endl;
517 bool CVS::undoLastEnabled()
523 void CVS::getLog(FileName const & tmpf)
525 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
526 + " > " + quoteName(tmpf.toFilesystemEncoding()),
527 FileName(owner_->filePath()));
531 bool CVS::toggleReadOnlyEnabled()
537 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
539 if (info == LyXVC::File)
545 bool CVS::prepareFileRevision(string const &, string &)
551 bool CVS::prepareFileRevisionEnabled()
557 /////////////////////////////////////////////////////////////////////
561 /////////////////////////////////////////////////////////////////////
563 SVN::SVN(FileName const & m, FileName const & f)
573 FileName const SVN::findFile(FileName const & file)
575 // First we look for the .svn/entries in the same dir
576 // where we have file.
577 FileName const entries(onlyPath(file.absFileName()) + "/.svn/entries");
578 string const tmpf = onlyFilename(file.absFileName());
579 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
580 << "' for `" << tmpf << '\'');
581 if (entries.isReadableFile()) {
582 // Ok we are at least in a SVN dir. Parse the .svn/entries
583 // and see if we can find this file. We do a fast and
585 ifstream ifs(entries.toFilesystemEncoding().c_str());
586 string line, oldline;
587 while (getline(ifs, line)) {
588 if (line == "dir" || line == "file")
589 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
590 if (oldline == tmpf && line == "file")
599 void SVN::scanMaster()
601 // vcstatus code is somewhat superflous, until we want
602 // to implement read-only toggle for svn.
603 vcstatus = NOLOCKING;
604 if (checkLockMode()) {
614 bool SVN::checkLockMode()
616 FileName tmpf = FileName::tempName("lyxvcout");
618 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
619 return N_("Error: Could not generate logfile.");
622 LYXERR(Debug::LYXVC, "Detecting locking mode...");
623 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
624 + " > " + quoteName(tmpf.toFilesystemEncoding()),
628 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
634 LYXERR(Debug::LYXVC, line);
635 if (contains(line, "svn:needs-lock"))
638 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
646 bool SVN::isLocked() const
649 return !file_.isReadOnly();
653 void SVN::registrer(string const & /*msg*/)
655 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
656 FileName(owner_->filePath()));
660 string SVN::checkIn(string const & msg)
662 FileName tmpf = FileName::tempName("lyxvcout");
664 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
665 return N_("Error: Could not generate logfile.");
668 doVCCommand("svn commit -m \"" + msg + "\" "
669 + quoteName(onlyFilename(owner_->absFileName()))
670 + " > " + quoteName(tmpf.toFilesystemEncoding()),
671 FileName(owner_->filePath()));
674 string res = scanLogFile(tmpf, log);
676 frontend::Alert::error(_("Revision control error."),
677 _("Error when committing to repository.\n"
678 "You have to manually resolve the problem.\n"
679 "LyX will reopen the document after you press OK."));
681 fileLock(false, tmpf, log);
684 return log.empty() ? string() : "SVN: " + log;
688 bool SVN::checkInEnabled()
697 // FIXME Correctly return code should be checked instead of this.
698 // This would need another solution than just plain startscript.
699 // Hint from Andre': QProcess::readAllStandardError()...
700 string SVN::scanLogFile(FileName const & f, string & status)
702 ifstream ifs(f.toFilesystemEncoding().c_str());
707 LYXERR(Debug::LYXVC, line << "\n");
709 status += line + "; ";
710 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
711 || contains(line, "Commit failed")) {
715 if (contains(line, "svn:needs-lock")) {
725 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
727 if (!locked_mode_ || (isLocked() == lock))
730 string const arg = lock ? "lock " : "unlock ";
731 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
732 + " > " + quoteName(tmpf.toFilesystemEncoding()),
733 FileName(owner_->filePath()));
735 // Lock error messages go unfortunately on stderr and are unreachible this way.
736 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
740 if (!line.empty()) status += line + "; ";
744 if (!isLocked() && lock)
745 frontend::Alert::error(_("Revision control error."),
746 _("Error while acquiring write lock.\n"
747 "Another user is most probably editing\n"
748 "the current document now!\n"
749 "Also check the access to the repository."));
750 if (isLocked() && !lock)
751 frontend::Alert::error(_("Revision control error."),
752 _("Error while releasing write lock.\n"
753 "Check the access to the repository."));
757 string SVN::checkOut()
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 update --non-interactive " + quoteName(onlyFilename(owner_->absFileName()))
766 + " > " + quoteName(tmpf.toFilesystemEncoding()),
767 FileName(owner_->filePath()));
770 string const res = scanLogFile(tmpf, log);
772 frontend::Alert::error(_("Revision control error."),
773 bformat(_("Error when updating from repository.\n"
774 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
775 "After pressing OK, LyX will try to reopen the resolved document."),
776 from_local8bit(res)));
778 fileLock(true, tmpf, log);
781 return log.empty() ? string() : "SVN: " + log;
785 bool SVN::checkOutEnabled()
794 string SVN::repoUpdate()
796 FileName tmpf = FileName::tempName("lyxvcout");
798 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
799 return N_("Error: Could not generate logfile.");
802 doVCCommand("svn diff " + quoteName(owner_->filePath())
803 + " > " + quoteName(tmpf.toFilesystemEncoding()),
804 FileName(owner_->filePath()));
805 docstring res = tmpf.fileContents("UTF-8");
807 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
808 docstring const file = from_utf8(owner_->filePath());
809 docstring text = bformat(_("There were detected changes "
810 "in the working directory:\n%1$s\n\n"
811 "In case of file conflict version of the local directory files "
813 "\n\nContinue?"), file);
814 int ret = frontend::Alert::prompt(_("Changes detected"),
815 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
817 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
818 ret = frontend::Alert::prompt(_("Changes detected"),
819 text, 0, 1, _("&Yes"), _("&No"));
820 hideDialogs("file", 0);
828 // Reverting looks too harsh, see bug #6255.
829 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
830 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
831 // FileName(owner_->filePath()));
832 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
833 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
834 + " > " + quoteName(tmpf.toFilesystemEncoding()),
835 FileName(owner_->filePath()));
836 res += "Update log:\n" + tmpf.fileContents("UTF-8");
838 LYXERR(Debug::LYXVC, res);
844 bool SVN::repoUpdateEnabled()
850 string SVN::lockingToggle()
852 FileName tmpf = FileName::tempName("lyxvcout");
854 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
855 return N_("Error: Could not generate logfile.");
858 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
859 + " > " + quoteName(tmpf.toFilesystemEncoding()),
860 FileName(owner_->filePath()));
865 string res = scanLogFile(tmpf, log);
866 bool locking = contains(res, "svn:needs-lock");
868 ret = doVCCommand("svn propset svn:needs-lock ON "
869 + quoteName(onlyFilename(owner_->absFileName()))
870 + " > " + quoteName(tmpf.toFilesystemEncoding()),
871 FileName(owner_->filePath()));
873 ret = doVCCommand("svn propdel svn:needs-lock "
874 + quoteName(onlyFilename(owner_->absFileName()))
875 + " > " + quoteName(tmpf.toFilesystemEncoding()),
876 FileName(owner_->filePath()));
881 frontend::Alert::warning(_("VCN File Locking"),
882 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
883 + _("Do not forget to commit the locking property into the repository."),
886 return string("SVN: ") + N_("Locking property set.");
890 bool SVN::lockingToggleEnabled()
898 // Reverts to the version in CVS repository and
899 // gets the updated version from the repository.
900 string const fil = quoteName(onlyFilename(owner_->absFileName()));
902 doVCCommand("svn revert -q " + fil,
903 FileName(owner_->filePath()));
910 // merge the current with the previous version
911 // in a reverse patch kind of way, so that the
912 // result is to revert the last changes.
913 lyxerr << "Sorry, not implemented." << endl;
917 bool SVN::undoLastEnabled()
923 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
925 if (info == LyXVC::Tree) {
926 if (rev_tree_cache_.empty())
927 if (!getTreeRevisionInfo())
928 rev_tree_cache_ = "?";
929 if (rev_tree_cache_ == "?")
932 return rev_tree_cache_;
935 // fill the rest of the attributes for a single file
936 if (rev_file_cache_.empty())
937 if (!getFileRevisionInfo())
938 rev_file_cache_ = "?";
942 if (rev_file_cache_ == "?")
944 return rev_file_cache_;
946 return rev_author_cache_;
948 return rev_date_cache_;
950 return rev_time_cache_;
959 bool SVN::getFileRevisionInfo()
961 FileName tmpf = FileName::tempName("lyxvcout");
963 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
964 return N_("Error: Could not generate logfile.");
967 doVCCommand("svn info --xml " + quoteName(onlyFilename(owner_->absFileName()))
968 + " > " + quoteName(tmpf.toFilesystemEncoding()),
969 FileName(owner_->filePath()));
974 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
982 LYXERR(Debug::LYXVC, line);
983 if (prefixIs(line, "<commit"))
985 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
986 string l1 = subst(line, "revision=\"", "");
987 string l2 = trim(subst(l1, "\">", ""));
989 rev_file_cache_ = rev = l2;
991 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
992 string l1 = subst(line, "<author>", "");
993 string l2 = subst(l1, "</author>", "");
994 rev_author_cache_ = l2;
996 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
997 string l1 = subst(line, "<date>", "");
998 string l2 = subst(l1, "</date>", "");
999 l2 = split(l2, l1, 'T');
1000 rev_date_cache_ = l1;
1001 l2 = split(l2, l1, '.');
1002 rev_time_cache_ = l1;
1008 return !rev.empty();
1012 bool SVN::getTreeRevisionInfo()
1014 FileName tmpf = FileName::tempName("lyxvcout");
1016 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1017 return N_("Error: Could not generate logfile.");
1020 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1021 FileName(owner_->filePath()));
1026 // only first line in case something bad happens.
1027 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1033 rev_tree_cache_ = line;
1034 return !line.empty();
1038 void SVN::getLog(FileName const & tmpf)
1040 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
1041 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1042 FileName(owner_->filePath()));
1046 bool SVN::prepareFileRevision(string const & revis, string & f)
1048 if (!isStrInt(revis))
1051 int rev = convert<int>(revis);
1053 if (!getFileRevisionInfo())
1056 rev = convert<int>(rev_file_cache_);
1057 // go back for minus rev
1059 rev = rev + convert<int>(rev_file_cache_);
1064 FileName tmpf = FileName::tempName("lyxvcrev");
1066 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1067 return N_("Error: Could not generate logfile.");
1070 doVCCommand("svn cat -r " + convert<string>(rev) + " "
1071 + quoteName(onlyFilename(owner_->absFileName()))
1072 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1073 FileName(owner_->filePath()));
1074 if (tmpf.isFileEmpty())
1077 f = tmpf.absFileName();
1082 bool SVN::prepareFileRevisionEnabled()
1089 bool SVN::toggleReadOnlyEnabled()