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 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
285 if (info == LyXVC::File)
291 /////////////////////////////////////////////////////////////////////
295 /////////////////////////////////////////////////////////////////////
297 CVS::CVS(FileName const & m, FileName const & f)
305 FileName const CVS::findFile(FileName const & file)
307 // First we look for the CVS/Entries in the same dir
308 // where we have file.
309 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
310 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
311 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
312 << "' for `" << tmpf << '\'');
313 if (entries.isReadableFile()) {
314 // Ok we are at least in a CVS dir. Parse the CVS/Entries
315 // and see if we can find this file. We do a fast and
317 ifstream ifs(entries.toFilesystemEncoding().c_str());
319 while (getline(ifs, line)) {
320 LYXERR(Debug::LYXVC, "\tEntries: " << line);
321 if (contains(line, tmpf))
329 void CVS::scanMaster()
331 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
332 // Ok now we do the real scan...
333 ifstream ifs(master_.toFilesystemEncoding().c_str());
334 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
335 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
337 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
338 while (getline(ifs, line)) {
339 LYXERR(Debug::LYXVC, "\t line: " << line);
340 if (contains(line, tmpf)) {
341 // Ok extract the fields.
344 regex_match(line, sm, reg);
346 //sm[0]; // whole matched string
348 version_ = sm.str(2);
349 string const file_date = sm.str(3);
352 //sm[5]; // tag or tagdate
353 // FIXME: must double check file is stattable/existing
354 time_t mod = file_.lastModified();
355 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
356 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
357 << "'\nModification date of file: `" << mod_date << '\'');
358 //FIXME this whole locking bussiness is not working under cvs and the machinery
359 // conforms to the ci usage, not cvs.
360 if (file_date == mod_date) {
361 locker_ = "Unlocked";
364 // Here we should also to some more checking
365 // to see if there are conflicts or not.
375 void CVS::registrer(string const & msg)
377 doVCCommand("cvs -q add -m \"" + msg + "\" "
378 + quoteName(onlyFilename(owner_->absFileName())),
379 FileName(owner_->filePath()));
383 string CVS::checkIn(string const & msg)
385 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
386 + quoteName(onlyFilename(owner_->absFileName())),
387 FileName(owner_->filePath()));
388 return ret ? string() : "CVS: Proceeded";
392 bool CVS::checkInEnabled()
398 string CVS::checkOut()
400 // cvs update or perhaps for cvs this should be a noop
401 // we need to detect conflict (eg "C" in output)
402 // before we can do this.
403 lyxerr << "Sorry, not implemented." << endl;
408 bool CVS::checkOutEnabled()
414 string CVS::repoUpdate()
416 lyxerr << "Sorry, not implemented." << endl;
421 bool CVS::repoUpdateEnabled()
427 string CVS::lockingToggle()
429 lyxerr << "Sorry, not implemented." << endl;
434 bool CVS::lockingToggleEnabled()
442 // Reverts to the version in CVS repository and
443 // gets the updated version from the repository.
444 string const fil = quoteName(onlyFilename(owner_->absFileName()));
445 // This is sensitive operation, so at lest some check about
446 // existence of cvs program and its file
447 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
449 FileName f(owner_->absFileName());
451 doVCCommand("cvs update " + fil,
452 FileName(owner_->filePath()));
459 // merge the current with the previous version
460 // in a reverse patch kind of way, so that the
461 // result is to revert the last changes.
462 lyxerr << "Sorry, not implemented." << endl;
466 bool CVS::undoLastEnabled()
472 void CVS::getLog(FileName const & tmpf)
474 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
475 + " > " + quoteName(tmpf.toFilesystemEncoding()),
476 FileName(owner_->filePath()));
480 bool CVS::toggleReadOnlyEnabled()
486 string CVS::revisionInfo(LyXVC::RevisionInfo const)
492 /////////////////////////////////////////////////////////////////////
496 /////////////////////////////////////////////////////////////////////
498 SVN::SVN(FileName const & m, FileName const & f)
508 FileName const SVN::findFile(FileName const & file)
510 // First we look for the .svn/entries in the same dir
511 // where we have file.
512 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
513 string const tmpf = onlyFilename(file.absFilename());
514 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
515 << "' for `" << tmpf << '\'');
516 if (entries.isReadableFile()) {
517 // Ok we are at least in a SVN dir. Parse the .svn/entries
518 // and see if we can find this file. We do a fast and
520 ifstream ifs(entries.toFilesystemEncoding().c_str());
521 string line, oldline;
522 while (getline(ifs, line)) {
523 if (line == "dir" || line == "file")
524 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
525 if (oldline == tmpf && line == "file")
534 void SVN::scanMaster()
537 // vcstatus code is somewhat superflous, until we want
538 // to implement read-only toggle for svn.
539 vcstatus = NOLOCKING;
540 if (checkLockMode()) {
545 locker_ = "Unlocked";
552 bool SVN::checkLockMode()
554 FileName tmpf = FileName::tempName("lyxvcout");
556 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
557 return N_("Error: Could not generate logfile.");
560 LYXERR(Debug::LYXVC, "Detecting locking mode...");
561 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
562 + " > " + quoteName(tmpf.toFilesystemEncoding()),
566 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
572 LYXERR(Debug::LYXVC, line);
573 if (contains(line, "svn:needs-lock"))
576 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
584 bool SVN::isLocked() const
587 return !file_.isReadOnly();
591 void SVN::registrer(string const & /*msg*/)
593 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
594 FileName(owner_->filePath()));
598 string SVN::checkIn(string const & msg)
600 FileName tmpf = FileName::tempName("lyxvcout");
602 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
603 return N_("Error: Could not generate logfile.");
606 doVCCommand("svn commit -m \"" + msg + "\" "
607 + quoteName(onlyFilename(owner_->absFileName()))
608 + " > " + quoteName(tmpf.toFilesystemEncoding()),
609 FileName(owner_->filePath()));
612 string res = scanLogFile(tmpf, log);
614 frontend::Alert::error(_("Revision control error."),
615 _("Error when committing to repository.\n"
616 "You have to manually resolve the problem.\n"
617 "LyX will reopen the document after you press OK."));
619 fileLock(false, tmpf, log);
622 return log.empty() ? string() : "SVN: " + log;
626 bool SVN::checkInEnabled()
635 // FIXME Correctly return code should be checked instead of this.
636 // This would need another solution than just plain startscript.
637 // Hint from Andre': QProcess::readAllStandardError()...
638 string SVN::scanLogFile(FileName const & f, string & status)
640 ifstream ifs(f.toFilesystemEncoding().c_str());
645 LYXERR(Debug::LYXVC, line << "\n");
647 status += line + "; ";
648 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
649 || contains(line, "Commit failed")) {
653 if (contains(line, "svn:needs-lock")) {
663 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
665 if (!locked_mode_ || (isLocked() == lock))
668 string const arg = lock ? "lock " : "unlock ";
669 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
670 + " > " + quoteName(tmpf.toFilesystemEncoding()),
671 FileName(owner_->filePath()));
673 // Lock error messages go unfortunately on stderr and are unreachible this way.
674 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
678 if (!line.empty()) status += line + "; ";
682 if (!isLocked() && lock)
683 frontend::Alert::error(_("Revision control error."),
684 _("Error when acquiring write lock.\n"
685 "Most probably another user is editing\n"
686 "the current document now!\n"
687 "Also check the access to the repository."));
688 if (isLocked() && !lock)
689 frontend::Alert::error(_("Revision control error."),
690 _("Error when releasing write lock.\n"
691 "Check the access to the repository."));
695 string SVN::checkOut()
697 FileName tmpf = FileName::tempName("lyxvcout");
699 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
700 return N_("Error: Could not generate logfile.");
703 doVCCommand("svn update --non-interactive " + quoteName(onlyFilename(owner_->absFileName()))
704 + " > " + quoteName(tmpf.toFilesystemEncoding()),
705 FileName(owner_->filePath()));
708 string res = scanLogFile(tmpf, log);
710 frontend::Alert::error(_("Revision control error."),
711 bformat(_("Error when updating from repository.\n"
712 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
713 "After pressing OK, LyX will try to reopen the resolved document."),
714 from_local8bit(res)));
716 fileLock(true, tmpf, log);
719 return log.empty() ? string() : "SVN: " + log;
723 bool SVN::checkOutEnabled()
732 string SVN::repoUpdate()
734 FileName tmpf = FileName::tempName("lyxvcout");
736 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
737 return N_("Error: Could not generate logfile.");
740 doVCCommand("svn diff " + quoteName(owner_->filePath())
741 + " > " + quoteName(tmpf.toFilesystemEncoding()),
742 FileName(owner_->filePath()));
743 docstring res = tmpf.fileContents("UTF-8");
745 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
746 docstring const file = from_utf8(owner_->filePath());
747 docstring text = bformat(_("There were detected changes "
748 "in the working directory:\n%1$s\n\n"
749 "In case of file conflict version of the local directory files "
751 "\n\nContinue?"), file);
752 int ret = frontend::Alert::prompt(_("Changes detected"),
753 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
755 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFilename()));
756 ret = frontend::Alert::prompt(_("Changes detected"),
757 text, 0, 1, _("&Yes"), _("&No"));
758 hideDialogs("file", 0);
766 // Reverting looks too harsh, see bug #6255.
767 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
768 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
769 // FileName(owner_->filePath()));
770 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
771 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
772 + " > " + quoteName(tmpf.toFilesystemEncoding()),
773 FileName(owner_->filePath()));
774 res += "Update log:\n" + tmpf.fileContents("UTF-8");
776 LYXERR(Debug::LYXVC, res);
782 bool SVN::repoUpdateEnabled()
788 string SVN::lockingToggle()
790 FileName tmpf = FileName::tempName("lyxvcout");
792 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
793 return N_("Error: Could not generate logfile.");
796 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
797 + " > " + quoteName(tmpf.toFilesystemEncoding()),
798 FileName(owner_->filePath()));
803 string res = scanLogFile(tmpf, log);
804 bool locking = contains(res, "svn:needs-lock");
806 ret = doVCCommand("svn propset svn:needs-lock ON "
807 + quoteName(onlyFilename(owner_->absFileName()))
808 + " > " + quoteName(tmpf.toFilesystemEncoding()),
809 FileName(owner_->filePath()));
811 ret = doVCCommand("svn propdel svn:needs-lock "
812 + quoteName(onlyFilename(owner_->absFileName()))
813 + " > " + quoteName(tmpf.toFilesystemEncoding()),
814 FileName(owner_->filePath()));
819 frontend::Alert::warning(_("VCN File Locking"),
820 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
821 + _("Do not forget to commit the locking property into the repository."),
824 return string("SVN: ") + N_("Locking property set.");
828 bool SVN::lockingToggleEnabled()
836 // Reverts to the version in CVS repository and
837 // gets the updated version from the repository.
838 string const fil = quoteName(onlyFilename(owner_->absFileName()));
840 doVCCommand("svn revert -q " + fil,
841 FileName(owner_->filePath()));
848 // merge the current with the previous version
849 // in a reverse patch kind of way, so that the
850 // result is to revert the last changes.
851 lyxerr << "Sorry, not implemented." << endl;
855 bool SVN::undoLastEnabled()
861 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
865 if (rev_file_cache_.empty())
866 rev_file_cache_ = getFileRevisionInfo();
867 if (rev_file_cache_.empty())
868 rev_file_cache_ = "?";
869 if (rev_file_cache_ == "?")
872 return rev_file_cache_;
875 if (rev_tree_cache_.empty())
876 rev_tree_cache_ = getTreeRevisionInfo();
877 if (rev_tree_cache_.empty())
878 rev_tree_cache_ = "?";
879 if (rev_tree_cache_ == "?")
882 return rev_tree_cache_;
888 std::string SVN::getFileRevisionInfo()
890 FileName tmpf = FileName::tempName("lyxvcout");
892 doVCCommand("svn info --xml " + quoteName(onlyFilename(owner_->absFileName()))
893 + " > " + quoteName(tmpf.toFilesystemEncoding()),
894 FileName(owner_->filePath()));
899 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
907 LYXERR(Debug::LYXVC, line);
908 if (prefixIs(line, "<commit"))
910 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
911 string l1 = subst(line, "revision=\"", "");
912 string l2 = trim(subst(l1, "\">", ""));
923 std::string SVN::getTreeRevisionInfo()
925 FileName tmpf = FileName::tempName("lyxvcout");
927 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
928 FileName(owner_->filePath()));
933 // only first line in case something bad happens.
934 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
943 void SVN::getLog(FileName const & tmpf)
945 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
946 + " > " + quoteName(tmpf.toFilesystemEncoding()),
947 FileName(owner_->filePath()));
951 bool SVN::toggleReadOnlyEnabled()