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 /////////////////////////////////////////////////////////////////////
297 /////////////////////////////////////////////////////////////////////
299 CVS::CVS(FileName const & m, FileName const & f)
307 FileName const CVS::findFile(FileName const & file)
309 // First we look for the CVS/Entries in the same dir
310 // where we have file.
311 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
312 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
313 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
314 << "' for `" << tmpf << '\'');
315 if (entries.isReadableFile()) {
316 // Ok we are at least in a CVS dir. Parse the CVS/Entries
317 // and see if we can find this file. We do a fast and
319 ifstream ifs(entries.toFilesystemEncoding().c_str());
321 while (getline(ifs, line)) {
322 LYXERR(Debug::LYXVC, "\tEntries: " << line);
323 if (contains(line, tmpf))
331 void CVS::scanMaster()
333 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
334 // Ok now we do the real scan...
335 ifstream ifs(master_.toFilesystemEncoding().c_str());
336 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
337 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
339 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
340 while (getline(ifs, line)) {
341 LYXERR(Debug::LYXVC, "\t line: " << line);
342 if (contains(line, tmpf)) {
343 // Ok extract the fields.
346 regex_match(line, sm, reg);
348 //sm[0]; // whole matched string
350 version_ = sm.str(2);
351 string const file_date = sm.str(3);
354 //sm[5]; // tag or tagdate
355 // FIXME: must double check file is stattable/existing
356 time_t mod = file_.lastModified();
357 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
358 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
359 << "'\nModification date of file: `" << mod_date << '\'');
360 //FIXME this whole locking bussiness is not working under cvs and the machinery
361 // conforms to the ci usage, not cvs.
362 if (file_date == mod_date) {
363 locker_ = "Unlocked";
366 // Here we should also to some more checking
367 // to see if there are conflicts or not.
377 void CVS::registrer(string const & msg)
379 doVCCommand("cvs -q add -m \"" + msg + "\" "
380 + quoteName(onlyFilename(owner_->absFileName())),
381 FileName(owner_->filePath()));
385 string CVS::checkIn(string const & msg)
387 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
388 + quoteName(onlyFilename(owner_->absFileName())),
389 FileName(owner_->filePath()));
390 return ret ? string() : "CVS: Proceeded";
394 bool CVS::checkInEnabled()
400 string CVS::checkOut()
402 // cvs update or perhaps for cvs this should be a noop
403 // we need to detect conflict (eg "C" in output)
404 // before we can do this.
405 lyxerr << "Sorry, not implemented." << endl;
410 bool CVS::checkOutEnabled()
416 string CVS::repoUpdate()
418 lyxerr << "Sorry, not implemented." << endl;
423 bool CVS::repoUpdateEnabled()
429 string CVS::lockingToggle()
431 lyxerr << "Sorry, not implemented." << endl;
436 bool CVS::lockingToggleEnabled()
444 // Reverts to the version in CVS repository and
445 // gets the updated version from the repository.
446 string const fil = quoteName(onlyFilename(owner_->absFileName()));
447 // This is sensitive operation, so at lest some check about
448 // existence of cvs program and its file
449 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
451 FileName f(owner_->absFileName());
453 doVCCommand("cvs update " + fil,
454 FileName(owner_->filePath()));
461 // merge the current with the previous version
462 // in a reverse patch kind of way, so that the
463 // result is to revert the last changes.
464 lyxerr << "Sorry, not implemented." << endl;
468 bool CVS::undoLastEnabled()
474 void CVS::getLog(FileName const & tmpf)
476 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
477 + " > " + quoteName(tmpf.toFilesystemEncoding()),
478 FileName(owner_->filePath()));
482 bool CVS::toggleReadOnlyEnabled()
488 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
490 if (info == LyXVC::File)
496 /////////////////////////////////////////////////////////////////////
500 /////////////////////////////////////////////////////////////////////
502 SVN::SVN(FileName const & m, FileName const & f)
512 FileName const SVN::findFile(FileName const & file)
514 // First we look for the .svn/entries in the same dir
515 // where we have file.
516 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
517 string const tmpf = onlyFilename(file.absFilename());
518 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
519 << "' for `" << tmpf << '\'');
520 if (entries.isReadableFile()) {
521 // Ok we are at least in a SVN dir. Parse the .svn/entries
522 // and see if we can find this file. We do a fast and
524 ifstream ifs(entries.toFilesystemEncoding().c_str());
525 string line, oldline;
526 while (getline(ifs, line)) {
527 if (line == "dir" || line == "file")
528 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
529 if (oldline == tmpf && line == "file")
538 void SVN::scanMaster()
540 // vcstatus code is somewhat superflous, until we want
541 // to implement read-only toggle for svn.
542 vcstatus = NOLOCKING;
543 if (checkLockMode()) {
553 bool SVN::checkLockMode()
555 FileName tmpf = FileName::tempName("lyxvcout");
557 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
558 return N_("Error: Could not generate logfile.");
561 LYXERR(Debug::LYXVC, "Detecting locking mode...");
562 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
563 + " > " + quoteName(tmpf.toFilesystemEncoding()),
567 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
573 LYXERR(Debug::LYXVC, line);
574 if (contains(line, "svn:needs-lock"))
577 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
585 bool SVN::isLocked() const
588 return !file_.isReadOnly();
592 void SVN::registrer(string const & /*msg*/)
594 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
595 FileName(owner_->filePath()));
599 string SVN::checkIn(string const & msg)
601 FileName tmpf = FileName::tempName("lyxvcout");
603 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
604 return N_("Error: Could not generate logfile.");
607 doVCCommand("svn commit -m \"" + msg + "\" "
608 + quoteName(onlyFilename(owner_->absFileName()))
609 + " > " + quoteName(tmpf.toFilesystemEncoding()),
610 FileName(owner_->filePath()));
613 string res = scanLogFile(tmpf, log);
615 frontend::Alert::error(_("Revision control error."),
616 _("Error when committing to repository.\n"
617 "You have to manually resolve the problem.\n"
618 "LyX will reopen the document after you press OK."));
620 fileLock(false, tmpf, log);
623 return log.empty() ? string() : "SVN: " + log;
627 bool SVN::checkInEnabled()
636 // FIXME Correctly return code should be checked instead of this.
637 // This would need another solution than just plain startscript.
638 // Hint from Andre': QProcess::readAllStandardError()...
639 string SVN::scanLogFile(FileName const & f, string & status)
641 ifstream ifs(f.toFilesystemEncoding().c_str());
646 LYXERR(Debug::LYXVC, line << "\n");
648 status += line + "; ";
649 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
650 || contains(line, "Commit failed")) {
654 if (contains(line, "svn:needs-lock")) {
664 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
666 if (!locked_mode_ || (isLocked() == lock))
669 string const arg = lock ? "lock " : "unlock ";
670 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
671 + " > " + quoteName(tmpf.toFilesystemEncoding()),
672 FileName(owner_->filePath()));
674 // Lock error messages go unfortunately on stderr and are unreachible this way.
675 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
679 if (!line.empty()) status += line + "; ";
683 if (!isLocked() && lock)
684 frontend::Alert::error(_("Revision control error."),
685 _("Error when acquiring write lock.\n"
686 "Most probably another user is editing\n"
687 "the current document now!\n"
688 "Also check the access to the repository."));
689 if (isLocked() && !lock)
690 frontend::Alert::error(_("Revision control error."),
691 _("Error when releasing write lock.\n"
692 "Check the access to the repository."));
696 string SVN::checkOut()
698 FileName tmpf = FileName::tempName("lyxvcout");
700 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
701 return N_("Error: Could not generate logfile.");
704 doVCCommand("svn update --non-interactive " + quoteName(onlyFilename(owner_->absFileName()))
705 + " > " + quoteName(tmpf.toFilesystemEncoding()),
706 FileName(owner_->filePath()));
709 string res = scanLogFile(tmpf, log);
711 frontend::Alert::error(_("Revision control error."),
712 bformat(_("Error when updating from repository.\n"
713 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
714 "After pressing OK, LyX will try to reopen the resolved document."),
715 from_local8bit(res)));
717 fileLock(true, tmpf, log);
720 return log.empty() ? string() : "SVN: " + log;
724 bool SVN::checkOutEnabled()
733 string SVN::repoUpdate()
735 FileName tmpf = FileName::tempName("lyxvcout");
737 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
738 return N_("Error: Could not generate logfile.");
741 doVCCommand("svn diff " + quoteName(owner_->filePath())
742 + " > " + quoteName(tmpf.toFilesystemEncoding()),
743 FileName(owner_->filePath()));
744 docstring res = tmpf.fileContents("UTF-8");
746 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
747 docstring const file = from_utf8(owner_->filePath());
748 docstring text = bformat(_("There were detected changes "
749 "in the working directory:\n%1$s\n\n"
750 "In case of file conflict version of the local directory files "
752 "\n\nContinue?"), file);
753 int ret = frontend::Alert::prompt(_("Changes detected"),
754 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
756 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFilename()));
757 ret = frontend::Alert::prompt(_("Changes detected"),
758 text, 0, 1, _("&Yes"), _("&No"));
759 hideDialogs("file", 0);
767 // Reverting looks too harsh, see bug #6255.
768 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
769 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
770 // FileName(owner_->filePath()));
771 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
772 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
773 + " > " + quoteName(tmpf.toFilesystemEncoding()),
774 FileName(owner_->filePath()));
775 res += "Update log:\n" + tmpf.fileContents("UTF-8");
777 LYXERR(Debug::LYXVC, res);
783 bool SVN::repoUpdateEnabled()
789 string SVN::lockingToggle()
791 FileName tmpf = FileName::tempName("lyxvcout");
793 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
794 return N_("Error: Could not generate logfile.");
797 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
798 + " > " + quoteName(tmpf.toFilesystemEncoding()),
799 FileName(owner_->filePath()));
804 string res = scanLogFile(tmpf, log);
805 bool locking = contains(res, "svn:needs-lock");
807 ret = doVCCommand("svn propset svn:needs-lock ON "
808 + quoteName(onlyFilename(owner_->absFileName()))
809 + " > " + quoteName(tmpf.toFilesystemEncoding()),
810 FileName(owner_->filePath()));
812 ret = doVCCommand("svn propdel svn:needs-lock "
813 + quoteName(onlyFilename(owner_->absFileName()))
814 + " > " + quoteName(tmpf.toFilesystemEncoding()),
815 FileName(owner_->filePath()));
820 frontend::Alert::warning(_("VCN File Locking"),
821 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
822 + _("Do not forget to commit the locking property into the repository."),
825 return string("SVN: ") + N_("Locking property set.");
829 bool SVN::lockingToggleEnabled()
837 // Reverts to the version in CVS repository and
838 // gets the updated version from the repository.
839 string const fil = quoteName(onlyFilename(owner_->absFileName()));
841 doVCCommand("svn revert -q " + fil,
842 FileName(owner_->filePath()));
849 // merge the current with the previous version
850 // in a reverse patch kind of way, so that the
851 // result is to revert the last changes.
852 lyxerr << "Sorry, not implemented." << endl;
856 bool SVN::undoLastEnabled()
862 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
864 if (info == LyXVC::Tree) {
865 if (rev_tree_cache_.empty())
866 if (!getTreeRevisionInfo())
867 rev_tree_cache_ = "?";
868 if (rev_tree_cache_ == "?")
871 return rev_tree_cache_;
874 // fill the rest of the attributes for a single file
875 if (rev_file_cache_.empty())
876 if (!getFileRevisionInfo())
877 rev_file_cache_ = "?";
881 if (rev_file_cache_ == "?")
883 return rev_file_cache_;
885 return rev_author_cache_;
887 return rev_date_cache_;
889 return rev_time_cache_;
898 bool SVN::getFileRevisionInfo()
900 FileName tmpf = FileName::tempName("lyxvcout");
902 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
903 return N_("Error: Could not generate logfile.");
906 doVCCommand("svn info --xml " + quoteName(onlyFilename(owner_->absFileName()))
907 + " > " + quoteName(tmpf.toFilesystemEncoding()),
908 FileName(owner_->filePath()));
913 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
921 LYXERR(Debug::LYXVC, line);
922 if (prefixIs(line, "<commit"))
924 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
925 string l1 = subst(line, "revision=\"", "");
926 string l2 = trim(subst(l1, "\">", ""));
928 rev_file_cache_ = rev = l2;
930 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
931 string l1 = subst(line, "<author>", "");
932 string l2 = subst(l1, "</author>", "");
933 rev_author_cache_ = l2;
935 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
936 string l1 = subst(line, "<date>", "");
937 string l2 = subst(l1, "</date>", "");
938 l2 = split(l2, l1, 'T');
939 rev_date_cache_ = l1;
940 l2 = split(l2, l1, '.');
941 rev_time_cache_ = l1;
951 bool SVN::getTreeRevisionInfo()
953 FileName tmpf = FileName::tempName("lyxvcout");
955 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
956 return N_("Error: Could not generate logfile.");
959 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
960 FileName(owner_->filePath()));
965 // only first line in case something bad happens.
966 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
972 rev_tree_cache_ = line;
973 return !line.empty();
977 void SVN::getLog(FileName const & tmpf)
979 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
980 + " > " + quoteName(tmpf.toFilesystemEncoding()),
981 FileName(owner_->filePath()));
985 bool SVN::prepareFileRevision(int rev, string & f)
988 if (!getFileRevisionInfo())
991 rev = convert<int>(rev_file_cache_);
992 // go back for minus rev
994 rev = rev + convert<int>(rev_file_cache_);
999 FileName tmpf = FileName::tempName("lyxvcrev");
1001 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1002 return N_("Error: Could not generate logfile.");
1005 doVCCommand("svn cat -r " + convert<string>(rev) + " "
1006 + quoteName(onlyFilename(owner_->absFileName()))
1007 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1008 FileName(owner_->filePath()));
1009 if (tmpf.isFileEmpty())
1012 f = tmpf.absFilename();
1017 bool SVN::toggleReadOnlyEnabled()