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/debug.h"
23 #include "support/filetools.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26 #include "support/Path.h"
27 #include "support/Systemcall.h"
29 #include <boost/regex.hpp>
34 using namespace lyx::support;
37 using boost::regex_match;
43 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
45 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
47 support::PathChanger p(path);
48 return one.startscript(Systemcall::Wait, cmd, false);
52 int VCS::doVCCommand(string const & cmd, FileName const & path)
55 owner_->setBusy(true);
57 int const ret = doVCCommandCall(cmd, path);
60 owner_->setBusy(false);
62 frontend::Alert::error(_("Revision control error."),
63 bformat(_("Some problem occured while running the command:\n"
70 /////////////////////////////////////////////////////////////////////
74 /////////////////////////////////////////////////////////////////////
76 RCS::RCS(FileName const & m)
83 FileName const RCS::findFile(FileName const & file)
85 // Check if *,v exists.
86 FileName tmp(file.absFilename() + ",v");
87 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
88 if (tmp.isReadableFile()) {
89 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
93 // Check if RCS/*,v exists.
94 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
95 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
96 if (tmp.isReadableFile()) {
97 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
105 void RCS::retrieve(FileName const & file)
107 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
108 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
113 void RCS::scanMaster()
118 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
120 ifstream ifs(master_.toFilesystemEncoding().c_str());
123 bool read_enough = false;
125 while (!read_enough && ifs >> token) {
126 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
131 else if (token == "head") {
135 tmv = rtrim(tmv, ";");
137 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
138 } else if (contains(token, "access")
139 || contains(token, "symbols")
140 || contains(token, "strict")) {
142 } else if (contains(token, "locks")) {
144 if (contains(token, ';')) {
145 locker_ = "Unlocked";
154 s1 = rtrim(tmpt, ";");
155 // tmp is now in the format <user>:<version>
156 s1 = split(s1, s2, ':');
157 // s2 is user, and s1 is version
158 if (s1 == version_) {
163 } while (!contains(tmpt, ';'));
165 } else if (token == "comment") {
166 // we don't need to read any further than this.
170 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
176 void RCS::registrer(string const & msg)
178 string cmd = "ci -q -u -i -t-\"";
181 cmd += quoteName(onlyFilename(owner_->absFileName()));
182 doVCCommand(cmd, FileName(owner_->filePath()));
186 string RCS::checkIn(string const & msg)
188 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
189 + quoteName(onlyFilename(owner_->absFileName())),
190 FileName(owner_->filePath()));
191 return ret ? string() : "RCS: Proceeded";
195 bool RCS::checkInEnabled()
197 return owner_ && !owner_->isReadonly();
201 string RCS::checkOut()
204 int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
205 FileName(owner_->filePath()));
206 return ret ? string() : "RCS: Proceeded";
210 bool RCS::checkOutEnabled()
212 return owner_ && owner_->isReadonly();
216 string RCS::repoUpdate()
218 lyxerr << "Sorry, not implemented." << endl;
223 bool RCS::repoUpdateEnabled()
229 string RCS::lockingToggle()
231 lyxerr << "Sorry, not implemented." << endl;
236 bool RCS::lockingToggleEnabled()
244 doVCCommand("co -f -u" + version_ + " "
245 + quoteName(onlyFilename(owner_->absFileName())),
246 FileName(owner_->filePath()));
247 // We ignore changes and just reload!
254 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
255 doVCCommand("rcs -o" + version_ + " "
256 + quoteName(onlyFilename(owner_->absFileName())),
257 FileName(owner_->filePath()));
261 bool RCS::undoLastEnabled()
267 void RCS::getLog(FileName const & tmpf)
269 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
270 + " > " + quoteName(tmpf.toFilesystemEncoding()),
271 FileName(owner_->filePath()));
275 bool RCS::toggleReadOnlyEnabled()
277 // This got broken somewhere along lfuns dispatch reorganization.
278 // reloadBuffer would be needed after this, but thats problematic
279 // since we are inside Buffer::dispatch.
284 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
286 if (info == LyXVC::File)
292 /////////////////////////////////////////////////////////////////////
296 /////////////////////////////////////////////////////////////////////
298 CVS::CVS(FileName const & m, FileName const & f)
306 FileName const CVS::findFile(FileName const & file)
308 // First we look for the CVS/Entries in the same dir
309 // where we have file.
310 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
311 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
312 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
313 << "' for `" << tmpf << '\'');
314 if (entries.isReadableFile()) {
315 // Ok we are at least in a CVS dir. Parse the CVS/Entries
316 // and see if we can find this file. We do a fast and
318 ifstream ifs(entries.toFilesystemEncoding().c_str());
320 while (getline(ifs, line)) {
321 LYXERR(Debug::LYXVC, "\tEntries: " << line);
322 if (contains(line, tmpf))
330 void CVS::scanMaster()
332 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
333 // Ok now we do the real scan...
334 ifstream ifs(master_.toFilesystemEncoding().c_str());
335 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
336 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
338 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
339 while (getline(ifs, line)) {
340 LYXERR(Debug::LYXVC, "\t line: " << line);
341 if (contains(line, tmpf)) {
342 // Ok extract the fields.
345 regex_match(line, sm, reg);
347 //sm[0]; // whole matched string
349 version_ = sm.str(2);
350 string const file_date = sm.str(3);
353 //sm[5]; // tag or tagdate
354 // FIXME: must double check file is stattable/existing
355 time_t mod = file_.lastModified();
356 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
357 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
358 << "'\nModification date of file: `" << mod_date << '\'');
359 //FIXME this whole locking bussiness is not working under cvs and the machinery
360 // conforms to the ci usage, not cvs.
361 if (file_date == mod_date) {
362 locker_ = "Unlocked";
365 // Here we should also to some more checking
366 // to see if there are conflicts or not.
376 void CVS::registrer(string const & msg)
378 doVCCommand("cvs -q add -m \"" + msg + "\" "
379 + quoteName(onlyFilename(owner_->absFileName())),
380 FileName(owner_->filePath()));
384 string CVS::checkIn(string const & msg)
386 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
387 + quoteName(onlyFilename(owner_->absFileName())),
388 FileName(owner_->filePath()));
389 return ret ? string() : "CVS: Proceeded";
393 bool CVS::checkInEnabled()
399 string CVS::checkOut()
401 // cvs update or perhaps for cvs this should be a noop
402 // we need to detect conflict (eg "C" in output)
403 // before we can do this.
404 lyxerr << "Sorry, not implemented." << endl;
409 bool CVS::checkOutEnabled()
415 string CVS::repoUpdate()
417 lyxerr << "Sorry, not implemented." << endl;
422 bool CVS::repoUpdateEnabled()
428 string CVS::lockingToggle()
430 lyxerr << "Sorry, not implemented." << endl;
435 bool CVS::lockingToggleEnabled()
443 // Reverts to the version in CVS repository and
444 // gets the updated version from the repository.
445 string const fil = quoteName(onlyFilename(owner_->absFileName()));
446 // This is sensitive operation, so at lest some check about
447 // existence of cvs program and its file
448 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
450 FileName f(owner_->absFileName());
452 doVCCommand("cvs update " + fil,
453 FileName(owner_->filePath()));
460 // merge the current with the previous version
461 // in a reverse patch kind of way, so that the
462 // result is to revert the last changes.
463 lyxerr << "Sorry, not implemented." << endl;
467 bool CVS::undoLastEnabled()
473 void CVS::getLog(FileName const & tmpf)
475 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
476 + " > " + quoteName(tmpf.toFilesystemEncoding()),
477 FileName(owner_->filePath()));
481 bool CVS::toggleReadOnlyEnabled()
487 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
489 if (info == LyXVC::File)
495 /////////////////////////////////////////////////////////////////////
499 /////////////////////////////////////////////////////////////////////
501 SVN::SVN(FileName const & m, FileName const & f)
511 FileName const SVN::findFile(FileName const & file)
513 // First we look for the .svn/entries in the same dir
514 // where we have file.
515 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
516 string const tmpf = onlyFilename(file.absFilename());
517 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
518 << "' for `" << tmpf << '\'');
519 if (entries.isReadableFile()) {
520 // Ok we are at least in a SVN dir. Parse the .svn/entries
521 // and see if we can find this file. We do a fast and
523 ifstream ifs(entries.toFilesystemEncoding().c_str());
524 string line, oldline;
525 while (getline(ifs, line)) {
526 if (line == "dir" || line == "file")
527 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
528 if (oldline == tmpf && line == "file")
537 void SVN::scanMaster()
539 // vcstatus code is somewhat superflous, until we want
540 // to implement read-only toggle for svn.
541 vcstatus = NOLOCKING;
542 if (checkLockMode()) {
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)
863 if (info == LyXVC::Tree) {
864 if (rev_tree_cache_.empty())
865 if (!getTreeRevisionInfo())
866 rev_tree_cache_ = "?";
867 if (rev_tree_cache_ == "?")
870 return rev_tree_cache_;
873 // fill the rest of the attributes for a single file
874 if (rev_file_cache_.empty())
875 if (!getFileRevisionInfo())
876 rev_file_cache_ = "?";
880 if (rev_file_cache_ == "?")
882 return rev_file_cache_;
884 return rev_author_cache_;
886 return rev_date_cache_;
888 return rev_time_cache_;
897 bool SVN::getFileRevisionInfo()
899 FileName tmpf = FileName::tempName("lyxvcout");
901 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
902 return N_("Error: Could not generate logfile.");
905 doVCCommand("svn info --xml " + quoteName(onlyFilename(owner_->absFileName()))
906 + " > " + quoteName(tmpf.toFilesystemEncoding()),
907 FileName(owner_->filePath()));
912 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
920 LYXERR(Debug::LYXVC, line);
921 if (prefixIs(line, "<commit"))
923 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
924 string l1 = subst(line, "revision=\"", "");
925 string l2 = trim(subst(l1, "\">", ""));
927 rev_file_cache_ = rev = l2;
929 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
930 string l1 = subst(line, "<author>", "");
931 string l2 = subst(l1, "</author>", "");
932 rev_author_cache_ = l2;
934 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
935 string l1 = subst(line, "<date>", "");
936 string l2 = subst(l1, "</date>", "");
937 l2 = split(l2, l1, 'T');
938 rev_date_cache_ = l1;
939 l2 = split(l2, l1, '.');
940 rev_time_cache_ = l1;
950 bool SVN::getTreeRevisionInfo()
952 FileName tmpf = FileName::tempName("lyxvcout");
954 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
955 return N_("Error: Could not generate logfile.");
958 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
959 FileName(owner_->filePath()));
964 // only first line in case something bad happens.
965 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
971 rev_tree_cache_ = line;
972 return !line.empty();
976 void SVN::getLog(FileName const & tmpf)
978 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
979 + " > " + quoteName(tmpf.toFilesystemEncoding()),
980 FileName(owner_->filePath()));
984 bool SVN::toggleReadOnlyEnabled()