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, string(), false);
50 int VCS::doVCCommand(string const & cmd, FileName const & path, bool reportError)
53 owner_->setBusy(true);
55 int const ret = doVCCommandCall(cmd, path);
58 owner_->setBusy(false);
59 if (ret && reportError)
60 frontend::Alert::error(_("Revision control error."),
61 bformat(_("Some problem occured while running the command:\n"
68 bool VCS::makeRCSRevision(string const &version, string &revis) const
73 int back = convert<int>(rev);
74 // if positive use as the last number in the whole revision string
77 rsplit(version, base , '.' );
78 rev = base + "." + rev;
82 // we care about the last number from revision string
83 // in case of backward indexing
86 cur = rsplit(version, base , '.' );
89 int want = convert<int>(cur) + back;
93 rev = base + "." + convert<string>(want);
102 bool VCS::checkparentdirs(FileName const & file, std::string const & pathname)
104 FileName dirname = file.onlyPath();
105 FileName tocheck = FileName(addName(dirname.absFileName(),pathname));
106 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
107 bool result = tocheck.exists();
108 while ( !result && !dirname.empty() ) {
109 //this construct because of #8295
110 dirname = FileName(dirname.absFileName()).parentPath();
111 LYXERR(Debug::LYXVC, "check directory: " << dirname.absFileName());
112 tocheck = FileName(addName(dirname.absFileName(),pathname));
113 result = tocheck.exists();
119 /////////////////////////////////////////////////////////////////////
123 /////////////////////////////////////////////////////////////////////
125 RCS::RCS(FileName const & m)
132 FileName const RCS::findFile(FileName const & file)
134 // Check if *,v exists.
135 FileName tmp(file.absFileName() + ",v");
136 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
137 if (tmp.isReadableFile()) {
138 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
142 // Check if RCS/*,v exists.
143 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
144 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
145 if (tmp.isReadableFile()) {
146 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
154 void RCS::retrieve(FileName const & file)
156 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
157 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
162 void RCS::scanMaster()
167 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
169 ifstream ifs(master_.toFilesystemEncoding().c_str());
172 bool read_enough = false;
174 while (!read_enough && ifs >> token) {
175 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
180 else if (token == "head") {
184 tmv = rtrim(tmv, ";");
186 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
187 } else if (contains(token, "access")
188 || contains(token, "symbols")
189 || contains(token, "strict")) {
191 } else if (contains(token, "locks")) {
193 if (contains(token, ';')) {
194 locker_ = "Unlocked";
203 s1 = rtrim(tmpt, ";");
204 // tmp is now in the format <user>:<version>
205 s1 = split(s1, s2, ':');
206 // s2 is user, and s1 is version
207 if (s1 == version_) {
212 } while (!contains(tmpt, ';'));
214 } else if (token == "comment") {
215 // we don't need to read any further than this.
219 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
225 void RCS::registrer(string const & msg)
227 string cmd = "ci -q -u -i -t-\"";
230 cmd += quoteName(onlyFileName(owner_->absFileName()));
231 doVCCommand(cmd, FileName(owner_->filePath()));
235 string RCS::checkIn(string const & msg)
237 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
238 + quoteName(onlyFileName(owner_->absFileName())),
239 FileName(owner_->filePath()));
240 return ret ? string() : "RCS: Proceeded";
244 bool RCS::checkInEnabled()
246 return owner_ && !owner_->isReadonly();
250 bool RCS::isCheckInWithConfirmation()
252 // FIXME one day common getDiff for all backends
254 // if (getDiff(file, diff) && diff.empty())
257 FileName tmpf = FileName::tempName("lyxvcout");
259 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
263 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
264 + " > " + quoteName(tmpf.toFilesystemEncoding()),
265 FileName(owner_->filePath()));
267 docstring diff = tmpf.fileContents("UTF-8");
277 string RCS::checkOut()
280 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
281 FileName(owner_->filePath()));
282 return ret ? string() : "RCS: Proceeded";
286 bool RCS::checkOutEnabled()
288 return owner_ && owner_->isReadonly();
292 string RCS::repoUpdate()
294 lyxerr << "Sorry, not implemented." << endl;
299 bool RCS::repoUpdateEnabled()
305 string RCS::lockingToggle()
307 //FIXME this might be actually possible, study rcs -U, rcs -L.
308 //State should be easy to get inside scanMaster.
309 //It would fix #4370 and make rcs/svn usage even more closer.
310 lyxerr << "Sorry, not implemented." << endl;
315 bool RCS::lockingToggleEnabled()
323 if (doVCCommand("co -f -u" + version_ + " "
324 + quoteName(onlyFileName(owner_->absFileName())),
325 FileName(owner_->filePath())))
327 // We ignore changes and just reload!
333 bool RCS::isRevertWithConfirmation()
335 //FIXME owner && diff ?
342 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
343 doVCCommand("rcs -o" + version_ + " "
344 + quoteName(onlyFileName(owner_->absFileName())),
345 FileName(owner_->filePath()));
349 bool RCS::undoLastEnabled()
355 void RCS::getLog(FileName const & tmpf)
357 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
358 + " > " + quoteName(tmpf.toFilesystemEncoding()),
359 FileName(owner_->filePath()));
363 bool RCS::toggleReadOnlyEnabled()
365 // This got broken somewhere along lfuns dispatch reorganization.
366 // reloadBuffer would be needed after this, but thats problematic
367 // since we are inside Buffer::dispatch.
373 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
375 if (info == LyXVC::File)
377 // fill the rest of the attributes for a single file
378 if (rev_date_cache_.empty())
379 if (!getRevisionInfo())
384 return rev_author_cache_;
386 return rev_date_cache_;
388 return rev_time_cache_;
396 bool RCS::getRevisionInfo()
398 FileName tmpf = FileName::tempName("lyxvcout");
400 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
403 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
404 + " > " + quoteName(tmpf.toFilesystemEncoding()),
405 FileName(owner_->filePath()));
410 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
413 // we reached to the entry, i.e. after initial log message
415 // line with critical info, e.g:
416 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
421 LYXERR(Debug::LYXVC, line);
422 if (entry && prefixIs(line, "date:")) {
426 if (prefixIs(line, "revision"))
432 rev_date_cache_ = token(result, ' ', 1);
433 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
434 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
436 return !rev_author_cache_.empty();
439 bool RCS::prepareFileRevision(string const &revis, string & f)
442 if (!VCS::makeRCSRevision(version_, rev))
445 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
447 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
451 doVCCommand("co -p" + rev + " "
452 + quoteName(onlyFileName(owner_->absFileName()))
453 + " > " + quoteName(tmpf.toFilesystemEncoding()),
454 FileName(owner_->filePath()));
455 if (tmpf.isFileEmpty())
458 f = tmpf.absFileName();
463 bool RCS::prepareFileRevisionEnabled()
469 /////////////////////////////////////////////////////////////////////
473 /////////////////////////////////////////////////////////////////////
475 CVS::CVS(FileName const & m, FileName const & f)
479 have_rev_info_ = false;
484 FileName const CVS::findFile(FileName const & file)
486 // First we look for the CVS/Entries in the same dir
487 // where we have file.
488 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
489 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
490 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
491 << "' for `" << tmpf << '\'');
492 if (entries.isReadableFile()) {
493 // Ok we are at least in a CVS dir. Parse the CVS/Entries
494 // and see if we can find this file. We do a fast and
496 ifstream ifs(entries.toFilesystemEncoding().c_str());
498 while (getline(ifs, line)) {
499 LYXERR(Debug::LYXVC, "\tEntries: " << line);
500 if (contains(line, tmpf))
508 void CVS::scanMaster()
510 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
511 // Ok now we do the real scan...
512 ifstream ifs(master_.toFilesystemEncoding().c_str());
513 string name = onlyFileName(file_.absFileName());
514 string tmpf = '/' + name + '/';
515 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
517 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
518 while (getline(ifs, line)) {
519 LYXERR(Debug::LYXVC, "\t line: " << line);
520 if (contains(line, tmpf)) {
521 // Ok extract the fields.
524 regex_match(line, sm, reg);
526 //sm[0]; // whole matched string
528 version_ = sm.str(2);
529 string const file_date = sm.str(3);
532 //sm[5]; // tag or tagdate
533 if (file_.isReadableFile()) {
534 time_t mod = file_.lastModified();
535 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
536 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
537 << "'\nModification date of file: `" << mod_date << '\'');
538 if (file_.isReadOnly()) {
539 // readonly checkout is unlocked
542 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
543 FileName base(addName(bdir.absFileName(),name));
544 // if base version is existent "cvs edit" was used to lock
545 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
548 vcstatus = NOLOCKING;
556 string const CVS::getTarget(OperationMode opmode) const
560 // in client server mode CVS does not like full path operand for directory operation
561 // since LyX switches to the repo dir "." is good enough as target
564 return quoteName(onlyFileName(owner_->absFileName()));
570 docstring CVS::toString(CvsStatus status) const
574 return _("Up-to-date");
575 case LocallyModified:
576 return _("Locally Modified");
578 return _("Locally Added");
580 return _("Needs Merge");
582 return _("Needs Checkout");
584 return _("No CVS file");
586 return _("Cannot retrieve CVS status");
592 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
593 FileName const & output, bool reportError)
595 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
596 return doVCCommand(cmd + redirection, path, reportError);
600 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
601 support::FileName const & path,
602 support::FileName const & output)
604 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
605 return doVCCommandCall(cmd + redirection, path);
609 CVS::CvsStatus CVS::getStatus()
611 FileName tmpf = FileName::tempName("lyxvcout");
613 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
617 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
618 FileName(owner_->filePath()), tmpf)) {
623 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
624 CvsStatus status = NoCvsFile;
629 LYXERR(Debug::LYXVC, line << "\n");
630 if (prefixIs(line, "File:")) {
631 if (contains(line, "Up-to-date"))
633 else if (contains(line, "Locally Modified"))
634 status = LocallyModified;
635 else if (contains(line, "Locally Added"))
636 status = LocallyAdded;
637 else if (contains(line, "Needs Merge"))
639 else if (contains(line, "Needs Checkout"))
640 status = NeedsCheckout;
647 void CVS::getRevisionInfo()
651 have_rev_info_ = true;
652 FileName tmpf = FileName::tempName("lyxvcout");
654 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
658 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
659 + " " + getTarget(File),
660 FileName(owner_->filePath()), tmpf);
663 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
667 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
668 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
673 LYXERR(Debug::LYXVC, line << "\n");
674 if (prefixIs(line, "date:")) {
676 regex_match(line, sm, reg);
677 //sm[0]; // whole matched string
678 rev_date_cache_ = sm[1];
679 rev_time_cache_ = sm[2];
680 //sm[3]; // GMT offset
681 rev_author_cache_ = sm[4];
686 if (rev_author_cache_.empty())
688 "Could not retrieve revision info for " << version_ <<
689 " of " << getTarget(File));
693 void CVS::registrer(string const & msg)
695 doVCCommand("cvs -q add -m \"" + msg + "\" "
697 FileName(owner_->filePath()));
701 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
703 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
704 FileName(owner_->filePath()), tmpf, false);
711 return doVCCommand("cvs -q edit " + getTarget(File),
712 FileName(owner_->filePath()));
719 return doVCCommand("cvs -q unedit " + getTarget(File),
720 FileName(owner_->filePath()));
724 int CVS::update(OperationMode opmode, FileName const & tmpf)
726 return doVCCommandWithOutput("cvs -q update "
728 FileName(owner_->filePath()), tmpf, false);
732 string CVS::scanLogFile(FileName const & f, string & status)
734 ifstream ifs(f.toFilesystemEncoding().c_str());
739 LYXERR(Debug::LYXVC, line << "\n");
741 status += line + "; ";
742 if (prefixIs(line, "C ")) {
752 string CVS::checkIn(string const & msg)
754 CvsStatus status = getStatus();
757 if (vcstatus != NOLOCKING)
759 return "CVS: Proceeded";
760 case LocallyModified:
762 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
764 FileName(owner_->filePath()));
765 return rc ? string() : "CVS: Proceeded";
769 frontend::Alert::error(_("Revision control error."),
770 _("The repository version is newer then the current check out.\n"
771 "You have to update from repository first or revert your changes.")) ;
774 frontend::Alert::error(_("Revision control error."),
775 bformat(_("Bad status when checking in changes.\n"
784 bool CVS::isLocked() const
786 FileName fn(owner_->absFileName());
788 return !fn.isReadOnly();
792 bool CVS::checkInEnabled()
794 if (vcstatus != NOLOCKING)
801 bool CVS::isCheckInWithConfirmation()
803 CvsStatus status = getStatus();
804 return status == LocallyModified || status == LocallyAdded;
808 string CVS::checkOut()
810 if (vcstatus != NOLOCKING && edit())
812 FileName tmpf = FileName::tempName("lyxvcout");
814 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
818 int rc = update(File, tmpf);
820 string const res = scanLogFile(tmpf, log);
822 frontend::Alert::error(_("Revision control error."),
823 bformat(_("Error when updating from repository.\n"
824 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
825 "After pressing OK, LyX will try to reopen the resolved document."),
826 from_local8bit(res)));
831 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
835 bool CVS::checkOutEnabled()
837 if (vcstatus != NOLOCKING)
844 string CVS::repoUpdate()
846 FileName tmpf = FileName::tempName("lyxvcout");
848 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
852 getDiff(Directory, tmpf);
853 docstring res = tmpf.fileContents("UTF-8");
855 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
856 docstring const file = from_utf8(owner_->filePath());
857 docstring text = bformat(_("There were detected changes "
858 "in the working directory:\n%1$s\n\n"
859 "Possible file conflicts must be then resolved manually "
860 "or you will need to revert back to the repository version."), file);
861 int ret = frontend::Alert::prompt(_("Changes detected"),
862 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
864 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
865 ret = frontend::Alert::prompt(_("Changes detected"),
866 text, 0, 1, _("&Continue"), _("&Abort"));
867 hideDialogs("file", 0);
875 int rc = update(Directory, tmpf);
876 res += "Update log:\n" + tmpf.fileContents("UTF-8");
877 LYXERR(Debug::LYXVC, res);
880 string sres = scanLogFile(tmpf, log);
882 docstring const file = owner_->fileName().displayName(20);
883 frontend::Alert::error(_("Revision control error."),
884 bformat(_("Error when updating document %1$s from repository.\n"
885 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
886 "After pressing OK, LyX will try to reopen the resolved document."),
887 file, from_local8bit(sres)));
893 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
897 bool CVS::repoUpdateEnabled()
903 string CVS::lockingToggle()
905 lyxerr << "Sorry, not implemented." << endl;
910 bool CVS::lockingToggleEnabled()
916 bool CVS::isRevertWithConfirmation()
918 CvsStatus status = getStatus();
919 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
925 // Reverts to the version in CVS repository and
926 // gets the updated version from the repository.
927 CvsStatus status = getStatus();
930 if (vcstatus != NOLOCKING)
931 return 0 == unedit();
935 case LocallyModified: {
936 FileName f(owner_->absFileName());
938 update(File, FileName());
943 docstring const file = owner_->fileName().displayName(20);
944 frontend::Alert::error(_("Revision control error."),
945 bformat(_("The document %1$s is not in repository.\n"
946 "You have to check in the first revision before you can revert."),
951 docstring const file = owner_->fileName().displayName(20);
952 frontend::Alert::error(_("Revision control error."),
953 bformat(_("Cannot revert document %1$s to repository version.\n"
954 "The status '%2$s' is unexpected."),
955 file, toString(status)));
965 // merge the current with the previous version
966 // in a reverse patch kind of way, so that the
967 // result is to revert the last changes.
968 lyxerr << "Sorry, not implemented." << endl;
972 bool CVS::undoLastEnabled()
978 void CVS::getLog(FileName const & tmpf)
980 doVCCommandWithOutput("cvs log " + getTarget(File),
981 FileName(owner_->filePath()),
986 bool CVS::toggleReadOnlyEnabled()
992 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
994 if (!version_.empty()) {
1000 return rev_author_cache_;
1002 return rev_date_cache_;
1004 return rev_time_cache_;
1012 bool CVS::prepareFileRevision(string const & revis, string & f)
1015 if (!VCS::makeRCSRevision(version_, rev))
1018 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
1020 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1024 doVCCommandWithOutput("cvs update -p -r" + rev + " "
1026 FileName(owner_->filePath()), tmpf);
1027 if (tmpf.isFileEmpty())
1030 f = tmpf.absFileName();
1035 bool CVS::prepareFileRevisionEnabled()
1041 /////////////////////////////////////////////////////////////////////
1045 /////////////////////////////////////////////////////////////////////
1047 SVN::SVN(FileName const & m, FileName const & f)
1057 FileName const SVN::findFile(FileName const & file)
1059 // First we check the existence of repository meta data.
1060 if (!VCS::checkparentdirs(file, ".svn")) {
1061 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1065 // Now we check the status of the file.
1066 FileName tmpf = FileName::tempName("lyxvcout");
1068 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1072 string const fname = onlyFileName(file.absFileName());
1073 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1074 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1075 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1077 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1078 return found ? file : FileName();
1082 void SVN::scanMaster()
1084 // vcstatus code is somewhat superflous, until we want
1085 // to implement read-only toggle for svn.
1086 vcstatus = NOLOCKING;
1087 if (checkLockMode()) {
1091 vcstatus = UNLOCKED;
1097 bool SVN::checkLockMode()
1099 FileName tmpf = FileName::tempName("lyxvcout");
1101 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1105 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1106 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
1107 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1111 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1115 while (ifs && !ret) {
1117 LYXERR(Debug::LYXVC, line);
1118 if (contains(line, "svn:needs-lock"))
1121 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1129 bool SVN::isLocked() const
1132 return !file_.isReadOnly();
1136 void SVN::registrer(string const & /*msg*/)
1138 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1139 FileName(owner_->filePath()));
1143 string SVN::checkIn(string const & msg)
1145 FileName tmpf = FileName::tempName("lyxvcout");
1147 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1148 return N_("Error: Could not generate logfile.");
1151 doVCCommand("svn commit -m \"" + msg + "\" "
1152 + quoteName(onlyFileName(owner_->absFileName()))
1153 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1154 FileName(owner_->filePath()));
1157 string res = scanLogFile(tmpf, log);
1159 frontend::Alert::error(_("Revision control error."),
1160 _("Error when committing to repository.\n"
1161 "You have to manually resolve the problem.\n"
1162 "LyX will reopen the document after you press OK."));
1164 fileLock(false, tmpf, log);
1167 return log.empty() ? string() : "SVN: " + log;
1171 bool SVN::checkInEnabled()
1180 bool SVN::isCheckInWithConfirmation()
1182 // FIXME one day common getDiff and perhaps OpMode for all backends
1184 FileName tmpf = FileName::tempName("lyxvcout");
1186 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1190 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1191 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1192 FileName(owner_->filePath()));
1194 docstring diff = tmpf.fileContents("UTF-8");
1204 // FIXME Correctly return code should be checked instead of this.
1205 // This would need another solution than just plain startscript.
1206 // Hint from Andre': QProcess::readAllStandardError()...
1207 string SVN::scanLogFile(FileName const & f, string & status)
1209 ifstream ifs(f.toFilesystemEncoding().c_str());
1214 LYXERR(Debug::LYXVC, line << "\n");
1216 status += line + "; ";
1217 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1218 || contains(line, "Commit failed")) {
1222 if (contains(line, "svn:needs-lock")) {
1232 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1234 if (!locked_mode_ || (isLocked() == lock))
1237 string const arg = lock ? "lock " : "unlock ";
1238 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1239 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1240 FileName(owner_->filePath()));
1242 // Lock error messages go unfortunately on stderr and are unreachible this way.
1243 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1247 if (!line.empty()) status += line + "; ";
1251 if (!isLocked() && lock)
1252 frontend::Alert::error(_("Revision control error."),
1253 _("Error while acquiring write lock.\n"
1254 "Another user is most probably editing\n"
1255 "the current document now!\n"
1256 "Also check the access to the repository."));
1257 if (isLocked() && !lock)
1258 frontend::Alert::error(_("Revision control error."),
1259 _("Error while releasing write lock.\n"
1260 "Check the access to the repository."));
1264 string SVN::checkOut()
1266 FileName tmpf = FileName::tempName("lyxvcout");
1268 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1269 return N_("Error: Could not generate logfile.");
1272 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1273 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1274 FileName(owner_->filePath()));
1277 string const res = scanLogFile(tmpf, log);
1279 frontend::Alert::error(_("Revision control error."),
1280 bformat(_("Error when updating from repository.\n"
1281 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1282 "After pressing OK, LyX will try to reopen the resolved document."),
1283 from_local8bit(res)));
1285 fileLock(true, tmpf, log);
1288 return log.empty() ? string() : "SVN: " + log;
1292 bool SVN::checkOutEnabled()
1301 string SVN::repoUpdate()
1303 FileName tmpf = FileName::tempName("lyxvcout");
1305 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1306 return N_("Error: Could not generate logfile.");
1309 doVCCommand("svn diff " + quoteName(owner_->filePath())
1310 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1311 FileName(owner_->filePath()));
1312 docstring res = tmpf.fileContents("UTF-8");
1314 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1315 docstring const file = from_utf8(owner_->filePath());
1316 docstring text = bformat(_("There were detected changes "
1317 "in the working directory:\n%1$s\n\n"
1318 "In case of file conflict version of the local directory files "
1319 "will be preferred."
1320 "\n\nContinue?"), file);
1321 int ret = frontend::Alert::prompt(_("Changes detected"),
1322 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1324 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1325 ret = frontend::Alert::prompt(_("Changes detected"),
1326 text, 0, 1, _("&Yes"), _("&No"));
1327 hideDialogs("file", 0);
1335 // Reverting looks too harsh, see bug #6255.
1336 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1337 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1338 // FileName(owner_->filePath()));
1339 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1340 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1341 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1342 FileName(owner_->filePath()));
1343 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1345 LYXERR(Debug::LYXVC, res);
1347 return to_utf8(res);
1351 bool SVN::repoUpdateEnabled()
1357 string SVN::lockingToggle()
1359 FileName tmpf = FileName::tempName("lyxvcout");
1361 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1362 return N_("Error: Could not generate logfile.");
1365 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1366 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1367 FileName(owner_->filePath()));
1372 string res = scanLogFile(tmpf, log);
1373 bool locking = contains(res, "svn:needs-lock");
1375 ret = doVCCommand("svn propset svn:needs-lock ON "
1376 + quoteName(onlyFileName(owner_->absFileName()))
1377 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1378 FileName(owner_->filePath()));
1380 ret = doVCCommand("svn propdel svn:needs-lock "
1381 + quoteName(onlyFileName(owner_->absFileName()))
1382 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1383 FileName(owner_->filePath()));
1388 frontend::Alert::warning(_("VCN File Locking"),
1389 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1390 + _("Do not forget to commit the locking property into the repository."),
1393 return string("SVN: ") + N_("Locking property set.");
1397 bool SVN::lockingToggleEnabled()
1405 // Reverts to the version in SVN repository and
1406 // gets the updated version from the repository.
1407 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1409 if (doVCCommand("svn revert -q " + fil,
1410 FileName(owner_->filePath())))
1412 owner_->markClean();
1417 bool SVN::isRevertWithConfirmation()
1419 //FIXME owner && diff
1424 void SVN::undoLast()
1426 // merge the current with the previous version
1427 // in a reverse patch kind of way, so that the
1428 // result is to revert the last changes.
1429 lyxerr << "Sorry, not implemented." << endl;
1433 bool SVN::undoLastEnabled()
1439 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1441 if (info == LyXVC::Tree) {
1442 if (rev_tree_cache_.empty())
1443 if (!getTreeRevisionInfo())
1444 rev_tree_cache_ = "?";
1445 if (rev_tree_cache_ == "?")
1448 return rev_tree_cache_;
1451 // fill the rest of the attributes for a single file
1452 if (rev_file_cache_.empty())
1453 if (!getFileRevisionInfo())
1454 rev_file_cache_ = "?";
1458 if (rev_file_cache_ == "?")
1460 return rev_file_cache_;
1462 return rev_author_cache_;
1464 return rev_date_cache_;
1466 return rev_time_cache_;
1475 bool SVN::getFileRevisionInfo()
1477 FileName tmpf = FileName::tempName("lyxvcout");
1479 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1483 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1484 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1485 FileName(owner_->filePath()));
1490 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1498 LYXERR(Debug::LYXVC, line);
1499 if (prefixIs(line, "<commit"))
1501 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1502 string l1 = subst(line, "revision=\"", "");
1503 string l2 = trim(subst(l1, "\">", ""));
1505 rev_file_cache_ = rev = l2;
1507 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1508 string l1 = subst(line, "<author>", "");
1509 string l2 = subst(l1, "</author>", "");
1510 rev_author_cache_ = l2;
1512 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1513 string l1 = subst(line, "<date>", "");
1514 string l2 = subst(l1, "</date>", "");
1515 l2 = split(l2, l1, 'T');
1516 rev_date_cache_ = l1;
1517 l2 = split(l2, l1, '.');
1518 rev_time_cache_ = l1;
1524 return !rev.empty();
1528 bool SVN::getTreeRevisionInfo()
1530 FileName tmpf = FileName::tempName("lyxvcout");
1532 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1536 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1537 FileName(owner_->filePath()));
1542 // only first line in case something bad happens.
1543 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1549 rev_tree_cache_ = line;
1550 return !line.empty();
1554 void SVN::getLog(FileName const & tmpf)
1556 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1557 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1558 FileName(owner_->filePath()));
1562 bool SVN::prepareFileRevision(string const & revis, string & f)
1564 if (!isStrInt(revis))
1567 int rev = convert<int>(revis);
1569 if (!getFileRevisionInfo())
1572 rev = convert<int>(rev_file_cache_);
1573 // go back for minus rev
1575 rev = rev + convert<int>(rev_file_cache_);
1580 string revname = convert<string>(rev);
1581 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1583 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1587 doVCCommand("svn cat -r " + revname + " "
1588 + quoteName(onlyFileName(owner_->absFileName()))
1589 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1590 FileName(owner_->filePath()));
1591 if (tmpf.isFileEmpty())
1594 f = tmpf.absFileName();
1599 bool SVN::prepareFileRevisionEnabled()
1606 bool SVN::toggleReadOnlyEnabled()