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"
29 #include "support/regex.h"
34 using namespace lyx::support;
41 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
43 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
45 support::PathChanger p(path);
46 return one.startscript(Systemcall::Wait, cmd, false);
50 int VCS::doVCCommand(string const & cmd, FileName const & path)
53 owner_->setBusy(true);
55 int const ret = doVCCommandCall(cmd, path);
58 owner_->setBusy(false);
60 frontend::Alert::error(_("Revision control error."),
61 bformat(_("Some problem occured while running the command:\n"
68 /////////////////////////////////////////////////////////////////////
72 /////////////////////////////////////////////////////////////////////
74 RCS::RCS(FileName const & m)
81 FileName const RCS::findFile(FileName const & file)
83 // Check if *,v exists.
84 FileName tmp(file.absFileName() + ",v");
85 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
86 if (tmp.isReadableFile()) {
87 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
91 // Check if RCS/*,v exists.
92 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
93 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
94 if (tmp.isReadableFile()) {
95 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
103 void RCS::retrieve(FileName const & file)
105 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
106 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
111 void RCS::scanMaster()
116 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
118 ifstream ifs(master_.toFilesystemEncoding().c_str());
121 bool read_enough = false;
123 while (!read_enough && ifs >> token) {
124 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
129 else if (token == "head") {
133 tmv = rtrim(tmv, ";");
135 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
136 } else if (contains(token, "access")
137 || contains(token, "symbols")
138 || contains(token, "strict")) {
140 } else if (contains(token, "locks")) {
142 if (contains(token, ';')) {
143 locker_ = "Unlocked";
152 s1 = rtrim(tmpt, ";");
153 // tmp is now in the format <user>:<version>
154 s1 = split(s1, s2, ':');
155 // s2 is user, and s1 is version
156 if (s1 == version_) {
161 } while (!contains(tmpt, ';'));
163 } else if (token == "comment") {
164 // we don't need to read any further than this.
168 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
174 void RCS::registrer(string const & msg)
176 string cmd = "ci -q -u -i -t-\"";
179 cmd += quoteName(onlyFileName(owner_->absFileName()));
180 doVCCommand(cmd, FileName(owner_->filePath()));
184 string RCS::checkIn(string const & msg)
186 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
187 + quoteName(onlyFileName(owner_->absFileName())),
188 FileName(owner_->filePath()));
189 return ret ? string() : "RCS: Proceeded";
193 bool RCS::checkInEnabled()
195 return owner_ && !owner_->isReadonly();
199 string RCS::checkOut()
202 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
203 FileName(owner_->filePath()));
204 return ret ? string() : "RCS: Proceeded";
208 bool RCS::checkOutEnabled()
210 return owner_ && owner_->isReadonly();
214 string RCS::repoUpdate()
216 lyxerr << "Sorry, not implemented." << endl;
221 bool RCS::repoUpdateEnabled()
227 string RCS::lockingToggle()
229 lyxerr << "Sorry, not implemented." << endl;
234 bool RCS::lockingToggleEnabled()
242 doVCCommand("co -f -u" + version_ + " "
243 + quoteName(onlyFileName(owner_->absFileName())),
244 FileName(owner_->filePath()));
245 // We ignore changes and just reload!
252 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
253 doVCCommand("rcs -o" + version_ + " "
254 + quoteName(onlyFileName(owner_->absFileName())),
255 FileName(owner_->filePath()));
259 bool RCS::undoLastEnabled()
265 void RCS::getLog(FileName const & tmpf)
267 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
268 + " > " + quoteName(tmpf.toFilesystemEncoding()),
269 FileName(owner_->filePath()));
273 bool RCS::toggleReadOnlyEnabled()
275 // This got broken somewhere along lfuns dispatch reorganization.
276 // reloadBuffer would be needed after this, but thats problematic
277 // since we are inside Buffer::dispatch.
282 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
284 if (info == LyXVC::File)
290 bool RCS::prepareFileRevision(string const &revis, string & f)
295 int back = convert<int>(rev);
300 // we care about the last number from revision string
301 // in case of backward indexing
304 cur = rsplit(version_, base , '.' );
307 int want = convert<int>(cur) + back;
311 rev = base + "." + convert<string>(want);
315 FileName tmpf = FileName::tempName("lyxvcrev");
317 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
318 return N_("Error: Could not generate logfile.");
321 doVCCommand("co -p" + rev + " "
322 + quoteName(onlyFileName(owner_->absFileName()))
323 + " > " + quoteName(tmpf.toFilesystemEncoding()),
324 FileName(owner_->filePath()));
325 if (tmpf.isFileEmpty())
328 f = tmpf.absFileName();
333 bool RCS::prepareFileRevisionEnabled()
339 /////////////////////////////////////////////////////////////////////
343 /////////////////////////////////////////////////////////////////////
345 CVS::CVS(FileName const & m, FileName const & f)
353 FileName const CVS::findFile(FileName const & file)
355 // First we look for the CVS/Entries in the same dir
356 // where we have file.
357 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
358 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
359 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
360 << "' for `" << tmpf << '\'');
361 if (entries.isReadableFile()) {
362 // Ok we are at least in a CVS dir. Parse the CVS/Entries
363 // and see if we can find this file. We do a fast and
365 ifstream ifs(entries.toFilesystemEncoding().c_str());
367 while (getline(ifs, line)) {
368 LYXERR(Debug::LYXVC, "\tEntries: " << line);
369 if (contains(line, tmpf))
377 void CVS::scanMaster()
379 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
380 // Ok now we do the real scan...
381 ifstream ifs(master_.toFilesystemEncoding().c_str());
382 string tmpf = '/' + onlyFileName(file_.absFileName()) + '/';
383 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
385 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
386 while (getline(ifs, line)) {
387 LYXERR(Debug::LYXVC, "\t line: " << line);
388 if (contains(line, tmpf)) {
389 // Ok extract the fields.
392 regex_match(line, sm, reg);
394 //sm[0]; // whole matched string
396 version_ = sm.str(2);
397 string const file_date = sm.str(3);
400 //sm[5]; // tag or tagdate
401 // FIXME: must double check file is stattable/existing
402 time_t mod = file_.lastModified();
403 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
404 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
405 << "'\nModification date of file: `" << mod_date << '\'');
406 //FIXME this whole locking bussiness is not working under cvs and the machinery
407 // conforms to the ci usage, not cvs.
408 if (file_date == mod_date) {
409 locker_ = "Unlocked";
412 // Here we should also do some more checking
413 // to see if there are conflicts or not.
423 void CVS::registrer(string const & msg)
425 doVCCommand("cvs -q add -m \"" + msg + "\" "
426 + quoteName(onlyFileName(owner_->absFileName())),
427 FileName(owner_->filePath()));
431 string CVS::checkIn(string const & msg)
433 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
434 + quoteName(onlyFileName(owner_->absFileName())),
435 FileName(owner_->filePath()));
436 return ret ? string() : "CVS: Proceeded";
440 bool CVS::checkInEnabled()
446 string CVS::checkOut()
448 // cvs update or perhaps for cvs this should be a noop
449 // we need to detect conflict (eg "C" in output)
450 // before we can do this.
451 lyxerr << "Sorry, not implemented." << endl;
456 bool CVS::checkOutEnabled()
462 string CVS::repoUpdate()
464 lyxerr << "Sorry, not implemented." << endl;
469 bool CVS::repoUpdateEnabled()
475 string CVS::lockingToggle()
477 lyxerr << "Sorry, not implemented." << endl;
482 bool CVS::lockingToggleEnabled()
490 // Reverts to the version in CVS repository and
491 // gets the updated version from the repository.
492 string const fil = quoteName(onlyFileName(owner_->absFileName()));
493 // This is sensitive operation, so at lest some check about
494 // existence of cvs program and its file
495 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
497 FileName f(owner_->absFileName());
499 doVCCommand("cvs update " + fil,
500 FileName(owner_->filePath()));
507 // merge the current with the previous version
508 // in a reverse patch kind of way, so that the
509 // result is to revert the last changes.
510 lyxerr << "Sorry, not implemented." << endl;
514 bool CVS::undoLastEnabled()
520 void CVS::getLog(FileName const & tmpf)
522 doVCCommand("cvs log " + quoteName(onlyFileName(owner_->absFileName()))
523 + " > " + quoteName(tmpf.toFilesystemEncoding()),
524 FileName(owner_->filePath()));
528 bool CVS::toggleReadOnlyEnabled()
534 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
536 if (info == LyXVC::File)
542 bool CVS::prepareFileRevision(string const &, string &)
548 bool CVS::prepareFileRevisionEnabled()
554 /////////////////////////////////////////////////////////////////////
558 /////////////////////////////////////////////////////////////////////
560 SVN::SVN(FileName const & m, FileName const & f)
570 FileName const SVN::findFile(FileName const & file)
572 // First we look for the .svn/entries in the same dir
573 // where we have file.
574 FileName const entries(onlyPath(file.absFileName()) + "/.svn/entries");
575 string const tmpf = onlyFileName(file.absFileName());
576 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
577 << "' for `" << tmpf << '\'');
578 if (entries.isReadableFile()) {
579 // Ok we are at least in a SVN dir. Parse the .svn/entries
580 // and see if we can find this file. We do a fast and
582 ifstream ifs(entries.toFilesystemEncoding().c_str());
583 string line, oldline;
584 while (getline(ifs, line)) {
585 if (line == "dir" || line == "file")
586 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
587 if (oldline == tmpf && line == "file")
596 void SVN::scanMaster()
598 // vcstatus code is somewhat superflous, until we want
599 // to implement read-only toggle for svn.
600 vcstatus = NOLOCKING;
601 if (checkLockMode()) {
611 bool SVN::checkLockMode()
613 FileName tmpf = FileName::tempName("lyxvcout");
615 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
616 return N_("Error: Could not generate logfile.");
619 LYXERR(Debug::LYXVC, "Detecting locking mode...");
620 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
621 + " > " + quoteName(tmpf.toFilesystemEncoding()),
625 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
631 LYXERR(Debug::LYXVC, line);
632 if (contains(line, "svn:needs-lock"))
635 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
643 bool SVN::isLocked() const
646 return !file_.isReadOnly();
650 void SVN::registrer(string const & /*msg*/)
652 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
653 FileName(owner_->filePath()));
657 string SVN::checkIn(string const & msg)
659 FileName tmpf = FileName::tempName("lyxvcout");
661 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
662 return N_("Error: Could not generate logfile.");
665 doVCCommand("svn commit -m \"" + msg + "\" "
666 + quoteName(onlyFileName(owner_->absFileName()))
667 + " > " + quoteName(tmpf.toFilesystemEncoding()),
668 FileName(owner_->filePath()));
671 string res = scanLogFile(tmpf, log);
673 frontend::Alert::error(_("Revision control error."),
674 _("Error when committing to repository.\n"
675 "You have to manually resolve the problem.\n"
676 "LyX will reopen the document after you press OK."));
678 fileLock(false, tmpf, log);
681 return log.empty() ? string() : "SVN: " + log;
685 bool SVN::checkInEnabled()
694 // FIXME Correctly return code should be checked instead of this.
695 // This would need another solution than just plain startscript.
696 // Hint from Andre': QProcess::readAllStandardError()...
697 string SVN::scanLogFile(FileName const & f, string & status)
699 ifstream ifs(f.toFilesystemEncoding().c_str());
704 LYXERR(Debug::LYXVC, line << "\n");
706 status += line + "; ";
707 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
708 || contains(line, "Commit failed")) {
712 if (contains(line, "svn:needs-lock")) {
722 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
724 if (!locked_mode_ || (isLocked() == lock))
727 string const arg = lock ? "lock " : "unlock ";
728 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
729 + " > " + quoteName(tmpf.toFilesystemEncoding()),
730 FileName(owner_->filePath()));
732 // Lock error messages go unfortunately on stderr and are unreachible this way.
733 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
737 if (!line.empty()) status += line + "; ";
741 if (!isLocked() && lock)
742 frontend::Alert::error(_("Revision control error."),
743 _("Error while acquiring write lock.\n"
744 "Another user is most probably editing\n"
745 "the current document now!\n"
746 "Also check the access to the repository."));
747 if (isLocked() && !lock)
748 frontend::Alert::error(_("Revision control error."),
749 _("Error while releasing write lock.\n"
750 "Check the access to the repository."));
754 string SVN::checkOut()
756 FileName tmpf = FileName::tempName("lyxvcout");
758 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
759 return N_("Error: Could not generate logfile.");
762 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
763 + " > " + quoteName(tmpf.toFilesystemEncoding()),
764 FileName(owner_->filePath()));
767 string const res = scanLogFile(tmpf, log);
769 frontend::Alert::error(_("Revision control error."),
770 bformat(_("Error when updating from repository.\n"
771 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
772 "After pressing OK, LyX will try to reopen the resolved document."),
773 from_local8bit(res)));
775 fileLock(true, tmpf, log);
778 return log.empty() ? string() : "SVN: " + log;
782 bool SVN::checkOutEnabled()
791 string SVN::repoUpdate()
793 FileName tmpf = FileName::tempName("lyxvcout");
795 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
796 return N_("Error: Could not generate logfile.");
799 doVCCommand("svn diff " + quoteName(owner_->filePath())
800 + " > " + quoteName(tmpf.toFilesystemEncoding()),
801 FileName(owner_->filePath()));
802 docstring res = tmpf.fileContents("UTF-8");
804 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
805 docstring const file = from_utf8(owner_->filePath());
806 docstring text = bformat(_("There were detected changes "
807 "in the working directory:\n%1$s\n\n"
808 "In case of file conflict version of the local directory files "
810 "\n\nContinue?"), file);
811 int ret = frontend::Alert::prompt(_("Changes detected"),
812 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
814 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
815 ret = frontend::Alert::prompt(_("Changes detected"),
816 text, 0, 1, _("&Yes"), _("&No"));
817 hideDialogs("file", 0);
825 // Reverting looks too harsh, see bug #6255.
826 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
827 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
828 // FileName(owner_->filePath()));
829 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
830 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
831 + " > " + quoteName(tmpf.toFilesystemEncoding()),
832 FileName(owner_->filePath()));
833 res += "Update log:\n" + tmpf.fileContents("UTF-8");
835 LYXERR(Debug::LYXVC, res);
841 bool SVN::repoUpdateEnabled()
847 string SVN::lockingToggle()
849 FileName tmpf = FileName::tempName("lyxvcout");
851 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
852 return N_("Error: Could not generate logfile.");
855 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
856 + " > " + quoteName(tmpf.toFilesystemEncoding()),
857 FileName(owner_->filePath()));
862 string res = scanLogFile(tmpf, log);
863 bool locking = contains(res, "svn:needs-lock");
865 ret = doVCCommand("svn propset svn:needs-lock ON "
866 + quoteName(onlyFileName(owner_->absFileName()))
867 + " > " + quoteName(tmpf.toFilesystemEncoding()),
868 FileName(owner_->filePath()));
870 ret = doVCCommand("svn propdel svn:needs-lock "
871 + quoteName(onlyFileName(owner_->absFileName()))
872 + " > " + quoteName(tmpf.toFilesystemEncoding()),
873 FileName(owner_->filePath()));
878 frontend::Alert::warning(_("VCN File Locking"),
879 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
880 + _("Do not forget to commit the locking property into the repository."),
883 return string("SVN: ") + N_("Locking property set.");
887 bool SVN::lockingToggleEnabled()
895 // Reverts to the version in CVS repository and
896 // gets the updated version from the repository.
897 string const fil = quoteName(onlyFileName(owner_->absFileName()));
899 doVCCommand("svn revert -q " + fil,
900 FileName(owner_->filePath()));
907 // merge the current with the previous version
908 // in a reverse patch kind of way, so that the
909 // result is to revert the last changes.
910 lyxerr << "Sorry, not implemented." << endl;
914 bool SVN::undoLastEnabled()
920 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
922 if (info == LyXVC::Tree) {
923 if (rev_tree_cache_.empty())
924 if (!getTreeRevisionInfo())
925 rev_tree_cache_ = "?";
926 if (rev_tree_cache_ == "?")
929 return rev_tree_cache_;
932 // fill the rest of the attributes for a single file
933 if (rev_file_cache_.empty())
934 if (!getFileRevisionInfo())
935 rev_file_cache_ = "?";
939 if (rev_file_cache_ == "?")
941 return rev_file_cache_;
943 return rev_author_cache_;
945 return rev_date_cache_;
947 return rev_time_cache_;
956 bool SVN::getFileRevisionInfo()
958 FileName tmpf = FileName::tempName("lyxvcout");
960 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
961 return N_("Error: Could not generate logfile.");
964 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
965 + " > " + quoteName(tmpf.toFilesystemEncoding()),
966 FileName(owner_->filePath()));
971 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
979 LYXERR(Debug::LYXVC, line);
980 if (prefixIs(line, "<commit"))
982 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
983 string l1 = subst(line, "revision=\"", "");
984 string l2 = trim(subst(l1, "\">", ""));
986 rev_file_cache_ = rev = l2;
988 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
989 string l1 = subst(line, "<author>", "");
990 string l2 = subst(l1, "</author>", "");
991 rev_author_cache_ = l2;
993 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
994 string l1 = subst(line, "<date>", "");
995 string l2 = subst(l1, "</date>", "");
996 l2 = split(l2, l1, 'T');
997 rev_date_cache_ = l1;
998 l2 = split(l2, l1, '.');
999 rev_time_cache_ = l1;
1005 return !rev.empty();
1009 bool SVN::getTreeRevisionInfo()
1011 FileName tmpf = FileName::tempName("lyxvcout");
1013 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1014 return N_("Error: Could not generate logfile.");
1017 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1018 FileName(owner_->filePath()));
1023 // only first line in case something bad happens.
1024 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1030 rev_tree_cache_ = line;
1031 return !line.empty();
1035 void SVN::getLog(FileName const & tmpf)
1037 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1038 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1039 FileName(owner_->filePath()));
1043 bool SVN::prepareFileRevision(string const & revis, string & f)
1045 if (!isStrInt(revis))
1048 int rev = convert<int>(revis);
1050 if (!getFileRevisionInfo())
1053 rev = convert<int>(rev_file_cache_);
1054 // go back for minus rev
1056 rev = rev + convert<int>(rev_file_cache_);
1061 FileName tmpf = FileName::tempName("lyxvcrev");
1063 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1064 return N_("Error: Could not generate logfile.");
1067 doVCCommand("svn cat -r " + convert<string>(rev) + " "
1068 + quoteName(onlyFileName(owner_->absFileName()))
1069 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1070 FileName(owner_->filePath()));
1071 if (tmpf.isFileEmpty())
1074 f = tmpf.absFileName();
1079 bool SVN::prepareFileRevisionEnabled()
1086 bool SVN::toggleReadOnlyEnabled()