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 /////////////////////////////////////////////////////////////////////
106 /////////////////////////////////////////////////////////////////////
108 RCS::RCS(FileName const & m)
115 FileName const RCS::findFile(FileName const & file)
117 // Check if *,v exists.
118 FileName tmp(file.absFileName() + ",v");
119 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
120 if (tmp.isReadableFile()) {
121 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
125 // Check if RCS/*,v exists.
126 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
127 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
128 if (tmp.isReadableFile()) {
129 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
137 void RCS::retrieve(FileName const & file)
139 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
140 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
145 void RCS::scanMaster()
150 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
152 ifstream ifs(master_.toFilesystemEncoding().c_str());
155 bool read_enough = false;
157 while (!read_enough && ifs >> token) {
158 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
163 else if (token == "head") {
167 tmv = rtrim(tmv, ";");
169 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
170 } else if (contains(token, "access")
171 || contains(token, "symbols")
172 || contains(token, "strict")) {
174 } else if (contains(token, "locks")) {
176 if (contains(token, ';')) {
177 locker_ = "Unlocked";
186 s1 = rtrim(tmpt, ";");
187 // tmp is now in the format <user>:<version>
188 s1 = split(s1, s2, ':');
189 // s2 is user, and s1 is version
190 if (s1 == version_) {
195 } while (!contains(tmpt, ';'));
197 } else if (token == "comment") {
198 // we don't need to read any further than this.
202 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
208 void RCS::registrer(string const & msg)
210 string cmd = "ci -q -u -i -t-\"";
213 cmd += quoteName(onlyFileName(owner_->absFileName()));
214 doVCCommand(cmd, FileName(owner_->filePath()));
218 string RCS::checkIn(string const & msg)
220 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
221 + quoteName(onlyFileName(owner_->absFileName())),
222 FileName(owner_->filePath()));
223 return ret ? string() : "RCS: Proceeded";
227 bool RCS::checkInEnabled()
229 return owner_ && !owner_->isReadonly();
233 bool RCS::isCheckInWithConfirmation()
235 // FIXME one day common getDiff for all backends
237 // if (getDiff(file, diff) && diff.empty())
240 FileName tmpf = FileName::tempName("lyxvcout");
242 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
246 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
247 + " > " + quoteName(tmpf.toFilesystemEncoding()),
248 FileName(owner_->filePath()));
250 docstring diff = tmpf.fileContents("UTF-8");
260 string RCS::checkOut()
263 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
264 FileName(owner_->filePath()));
265 return ret ? string() : "RCS: Proceeded";
269 bool RCS::checkOutEnabled()
271 return owner_ && owner_->isReadonly();
275 string RCS::repoUpdate()
277 lyxerr << "Sorry, not implemented." << endl;
282 bool RCS::repoUpdateEnabled()
288 string RCS::lockingToggle()
290 //FIXME this might be actually possible, study rcs -U, rcs -L.
291 //State should be easy to get inside scanMaster.
292 //It would fix #4370 and make rcs/svn usage even more closer.
293 lyxerr << "Sorry, not implemented." << endl;
298 bool RCS::lockingToggleEnabled()
306 if (doVCCommand("co -f -u" + version_ + " "
307 + quoteName(onlyFileName(owner_->absFileName())),
308 FileName(owner_->filePath())))
310 // We ignore changes and just reload!
316 bool RCS::isRevertWithConfirmation()
318 //FIXME owner && diff ?
325 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
326 doVCCommand("rcs -o" + version_ + " "
327 + quoteName(onlyFileName(owner_->absFileName())),
328 FileName(owner_->filePath()));
332 bool RCS::undoLastEnabled()
338 void RCS::getLog(FileName const & tmpf)
340 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
341 + " > " + quoteName(tmpf.toFilesystemEncoding()),
342 FileName(owner_->filePath()));
346 bool RCS::toggleReadOnlyEnabled()
348 // This got broken somewhere along lfuns dispatch reorganization.
349 // reloadBuffer would be needed after this, but thats problematic
350 // since we are inside Buffer::dispatch.
356 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
358 if (info == LyXVC::File)
360 // fill the rest of the attributes for a single file
361 if (rev_date_cache_.empty())
362 if (!getRevisionInfo())
367 return rev_author_cache_;
369 return rev_date_cache_;
371 return rev_time_cache_;
379 bool RCS::getRevisionInfo()
381 FileName tmpf = FileName::tempName("lyxvcout");
383 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
386 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
387 + " > " + quoteName(tmpf.toFilesystemEncoding()),
388 FileName(owner_->filePath()));
393 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
396 // we reached to the entry, i.e. after initial log message
398 // line with critical info, e.g:
399 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
404 LYXERR(Debug::LYXVC, line);
405 if (entry && prefixIs(line, "date:")) {
409 if (prefixIs(line, "revision"))
415 rev_date_cache_ = token(result, ' ', 1);
416 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
417 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
419 return !rev_author_cache_.empty();
422 bool RCS::prepareFileRevision(string const &revis, string & f)
425 if (!VCS::makeRCSRevision(version_, rev))
428 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
430 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
434 doVCCommand("co -p" + rev + " "
435 + quoteName(onlyFileName(owner_->absFileName()))
436 + " > " + quoteName(tmpf.toFilesystemEncoding()),
437 FileName(owner_->filePath()));
438 if (tmpf.isFileEmpty())
441 f = tmpf.absFileName();
446 bool RCS::prepareFileRevisionEnabled()
452 /////////////////////////////////////////////////////////////////////
456 /////////////////////////////////////////////////////////////////////
458 CVS::CVS(FileName const & m, FileName const & f)
462 have_rev_info_ = false;
467 FileName const CVS::findFile(FileName const & file)
469 // First we look for the CVS/Entries in the same dir
470 // where we have file.
471 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
472 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
473 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
474 << "' for `" << tmpf << '\'');
475 if (entries.isReadableFile()) {
476 // Ok we are at least in a CVS dir. Parse the CVS/Entries
477 // and see if we can find this file. We do a fast and
479 ifstream ifs(entries.toFilesystemEncoding().c_str());
481 while (getline(ifs, line)) {
482 LYXERR(Debug::LYXVC, "\tEntries: " << line);
483 if (contains(line, tmpf))
491 void CVS::scanMaster()
493 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
494 // Ok now we do the real scan...
495 ifstream ifs(master_.toFilesystemEncoding().c_str());
496 string name = onlyFileName(file_.absFileName());
497 string tmpf = '/' + name + '/';
498 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
500 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
501 while (getline(ifs, line)) {
502 LYXERR(Debug::LYXVC, "\t line: " << line);
503 if (contains(line, tmpf)) {
504 // Ok extract the fields.
507 regex_match(line, sm, reg);
509 //sm[0]; // whole matched string
511 version_ = sm.str(2);
512 string const file_date = sm.str(3);
515 //sm[5]; // tag or tagdate
516 if (file_.isReadableFile()) {
517 time_t mod = file_.lastModified();
518 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
519 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
520 << "'\nModification date of file: `" << mod_date << '\'');
521 if (file_.isReadOnly()) {
522 // readonly checkout is unlocked
525 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
526 FileName base(addName(bdir.absFileName(),name));
527 // if base version is existent "cvs edit" was used to lock
528 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
531 vcstatus = NOLOCKING;
539 string const CVS::getTarget(OperationMode opmode) const
543 // in client server mode CVS does not like full path operand for directory operation
544 // since LyX switches to the repo dir "." is good enough as target
547 return quoteName(onlyFileName(owner_->absFileName()));
553 docstring CVS::toString(CvsStatus status) const
557 return _("Up-to-date");
558 case LocallyModified:
559 return _("Locally Modified");
561 return _("Locally Added");
563 return _("Needs Merge");
565 return _("Needs Checkout");
567 return _("No CVS file");
569 return _("Cannot retrieve CVS status");
575 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
576 FileName const & output, bool reportError)
578 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
579 return doVCCommand(cmd + redirection, path, reportError);
583 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
584 support::FileName const & path,
585 support::FileName const & output)
587 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
588 return doVCCommandCall(cmd + redirection, path);
592 CVS::CvsStatus CVS::getStatus()
594 FileName tmpf = FileName::tempName("lyxvcout");
596 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
600 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
601 FileName(owner_->filePath()), tmpf)) {
606 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
607 CvsStatus status = NoCvsFile;
612 LYXERR(Debug::LYXVC, line << "\n");
613 if (prefixIs(line, "File:")) {
614 if (contains(line, "Up-to-date"))
616 else if (contains(line, "Locally Modified"))
617 status = LocallyModified;
618 else if (contains(line, "Locally Added"))
619 status = LocallyAdded;
620 else if (contains(line, "Needs Merge"))
622 else if (contains(line, "Needs Checkout"))
623 status = NeedsCheckout;
630 void CVS::getRevisionInfo()
634 have_rev_info_ = true;
635 FileName tmpf = FileName::tempName("lyxvcout");
637 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
641 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
642 + " " + getTarget(File),
643 FileName(owner_->filePath()), tmpf);
646 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
650 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
651 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
656 LYXERR(Debug::LYXVC, line << "\n");
657 if (prefixIs(line, "date:")) {
659 regex_match(line, sm, reg);
660 //sm[0]; // whole matched string
661 rev_date_cache_ = sm[1];
662 rev_time_cache_ = sm[2];
663 //sm[3]; // GMT offset
664 rev_author_cache_ = sm[4];
669 if (rev_author_cache_.empty())
671 "Could not retrieve revision info for " << version_ <<
672 " of " << getTarget(File));
676 void CVS::registrer(string const & msg)
678 doVCCommand("cvs -q add -m \"" + msg + "\" "
680 FileName(owner_->filePath()));
684 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
686 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
687 FileName(owner_->filePath()), tmpf, false);
694 return doVCCommand("cvs -q edit " + getTarget(File),
695 FileName(owner_->filePath()));
702 return doVCCommand("cvs -q unedit " + getTarget(File),
703 FileName(owner_->filePath()));
707 int CVS::update(OperationMode opmode, FileName const & tmpf)
709 return doVCCommandWithOutput("cvs -q update "
711 FileName(owner_->filePath()), tmpf, false);
715 string CVS::scanLogFile(FileName const & f, string & status)
717 ifstream ifs(f.toFilesystemEncoding().c_str());
722 LYXERR(Debug::LYXVC, line << "\n");
724 status += line + "; ";
725 if (prefixIs(line, "C ")) {
735 string CVS::checkIn(string const & msg)
737 CvsStatus status = getStatus();
740 if (vcstatus != NOLOCKING)
742 return "CVS: Proceeded";
743 case LocallyModified:
745 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
747 FileName(owner_->filePath()));
748 return rc ? string() : "CVS: Proceeded";
752 frontend::Alert::error(_("Revision control error."),
753 _("The repository version is newer then the current check out.\n"
754 "You have to update from repository first or revert your changes.")) ;
757 frontend::Alert::error(_("Revision control error."),
758 bformat(_("Bad status when checking in changes.\n"
767 bool CVS::isLocked() const
769 FileName fn(owner_->absFileName());
771 return !fn.isReadOnly();
775 bool CVS::checkInEnabled()
777 if (vcstatus != NOLOCKING)
784 bool CVS::isCheckInWithConfirmation()
786 CvsStatus status = getStatus();
787 return status == LocallyModified || status == LocallyAdded;
791 string CVS::checkOut()
793 if (vcstatus != NOLOCKING && edit())
795 FileName tmpf = FileName::tempName("lyxvcout");
797 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
801 int rc = update(File, tmpf);
803 string const res = scanLogFile(tmpf, log);
805 frontend::Alert::error(_("Revision control error."),
806 bformat(_("Error when updating from repository.\n"
807 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
808 "After pressing OK, LyX will try to reopen the resolved document."),
809 from_local8bit(res)));
814 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
818 bool CVS::checkOutEnabled()
820 if (vcstatus != NOLOCKING)
827 string CVS::repoUpdate()
829 FileName tmpf = FileName::tempName("lyxvcout");
831 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
835 getDiff(Directory, tmpf);
836 docstring res = tmpf.fileContents("UTF-8");
838 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
839 docstring const file = from_utf8(owner_->filePath());
840 docstring text = bformat(_("There were detected changes "
841 "in the working directory:\n%1$s\n\n"
842 "Possible file conflicts must be then resolved manually "
843 "or you will need to revert back to the repository version."), file);
844 int ret = frontend::Alert::prompt(_("Changes detected"),
845 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
847 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
848 ret = frontend::Alert::prompt(_("Changes detected"),
849 text, 0, 1, _("&Continue"), _("&Abort"));
850 hideDialogs("file", 0);
858 int rc = update(Directory, tmpf);
859 res += "Update log:\n" + tmpf.fileContents("UTF-8");
860 LYXERR(Debug::LYXVC, res);
863 string sres = scanLogFile(tmpf, log);
865 docstring const file = owner_->fileName().displayName(20);
866 frontend::Alert::error(_("Revision control error."),
867 bformat(_("Error when updating document %1$s from repository.\n"
868 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
869 "After pressing OK, LyX will try to reopen the resolved document."),
870 file, from_local8bit(sres)));
876 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
880 bool CVS::repoUpdateEnabled()
886 string CVS::lockingToggle()
888 lyxerr << "Sorry, not implemented." << endl;
893 bool CVS::lockingToggleEnabled()
899 bool CVS::isRevertWithConfirmation()
901 CvsStatus status = getStatus();
902 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
908 // Reverts to the version in CVS repository and
909 // gets the updated version from the repository.
910 CvsStatus status = getStatus();
913 if (vcstatus != NOLOCKING)
914 return 0 == unedit();
918 case LocallyModified: {
919 FileName f(owner_->absFileName());
921 update(File, FileName());
926 docstring const file = owner_->fileName().displayName(20);
927 frontend::Alert::error(_("Revision control error."),
928 bformat(_("The document %1$s is not in repository.\n"
929 "You have to check in the first revision before you can revert."),
934 docstring const file = owner_->fileName().displayName(20);
935 frontend::Alert::error(_("Revision control error."),
936 bformat(_("Cannot revert document %1$s to repository version.\n"
937 "The status '%2$s' is unexpected."),
938 file, toString(status)));
948 // merge the current with the previous version
949 // in a reverse patch kind of way, so that the
950 // result is to revert the last changes.
951 lyxerr << "Sorry, not implemented." << endl;
955 bool CVS::undoLastEnabled()
961 void CVS::getLog(FileName const & tmpf)
963 doVCCommandWithOutput("cvs log " + getTarget(File),
964 FileName(owner_->filePath()),
969 bool CVS::toggleReadOnlyEnabled()
975 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
977 if (!version_.empty()) {
983 return rev_author_cache_;
985 return rev_date_cache_;
987 return rev_time_cache_;
995 bool CVS::prepareFileRevision(string const & revis, string & f)
998 if (!VCS::makeRCSRevision(version_, rev))
1001 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
1003 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1007 doVCCommandWithOutput("cvs update -p -r" + rev + " "
1009 FileName(owner_->filePath()), tmpf);
1010 if (tmpf.isFileEmpty())
1013 f = tmpf.absFileName();
1018 bool CVS::prepareFileRevisionEnabled()
1024 /////////////////////////////////////////////////////////////////////
1028 /////////////////////////////////////////////////////////////////////
1030 SVN::SVN(FileName const & m, FileName const & f)
1040 FileName const SVN::findFile(FileName const & file)
1042 // First we look for the .svn/entries in the same dir
1043 // where we have file.
1044 FileName const entries(onlyPath(file.absFileName()) + "/.svn/entries");
1045 string const tmpf = onlyFileName(file.absFileName());
1046 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
1047 << "' for `" << tmpf << '\'');
1048 if (entries.isReadableFile()) {
1049 // Ok we are at least in a SVN dir. Parse the .svn/entries
1050 // and see if we can find this file. We do a fast and
1051 // dirty parse here.
1052 ifstream ifs(entries.toFilesystemEncoding().c_str());
1053 string line, oldline;
1054 while (getline(ifs, line)) {
1055 if (line == "dir" || line == "file")
1056 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
1057 if (oldline == tmpf && line == "file")
1066 void SVN::scanMaster()
1068 // vcstatus code is somewhat superflous, until we want
1069 // to implement read-only toggle for svn.
1070 vcstatus = NOLOCKING;
1071 if (checkLockMode()) {
1075 vcstatus = UNLOCKED;
1081 bool SVN::checkLockMode()
1083 FileName tmpf = FileName::tempName("lyxvcout");
1085 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1089 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1090 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
1091 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1095 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1101 LYXERR(Debug::LYXVC, line);
1102 if (contains(line, "svn:needs-lock"))
1105 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1113 bool SVN::isLocked() const
1116 return !file_.isReadOnly();
1120 void SVN::registrer(string const & /*msg*/)
1122 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1123 FileName(owner_->filePath()));
1127 string SVN::checkIn(string const & msg)
1129 FileName tmpf = FileName::tempName("lyxvcout");
1131 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1132 return N_("Error: Could not generate logfile.");
1135 doVCCommand("svn commit -m \"" + msg + "\" "
1136 + quoteName(onlyFileName(owner_->absFileName()))
1137 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1138 FileName(owner_->filePath()));
1141 string res = scanLogFile(tmpf, log);
1143 frontend::Alert::error(_("Revision control error."),
1144 _("Error when committing to repository.\n"
1145 "You have to manually resolve the problem.\n"
1146 "LyX will reopen the document after you press OK."));
1148 fileLock(false, tmpf, log);
1151 return log.empty() ? string() : "SVN: " + log;
1155 bool SVN::checkInEnabled()
1164 bool SVN::isCheckInWithConfirmation()
1166 // FIXME one day common getDiff and perhaps OpMode for all backends
1168 FileName tmpf = FileName::tempName("lyxvcout");
1170 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1174 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1175 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1176 FileName(owner_->filePath()));
1178 docstring diff = tmpf.fileContents("UTF-8");
1188 // FIXME Correctly return code should be checked instead of this.
1189 // This would need another solution than just plain startscript.
1190 // Hint from Andre': QProcess::readAllStandardError()...
1191 string SVN::scanLogFile(FileName const & f, string & status)
1193 ifstream ifs(f.toFilesystemEncoding().c_str());
1198 LYXERR(Debug::LYXVC, line << "\n");
1200 status += line + "; ";
1201 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1202 || contains(line, "Commit failed")) {
1206 if (contains(line, "svn:needs-lock")) {
1216 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1218 if (!locked_mode_ || (isLocked() == lock))
1221 string const arg = lock ? "lock " : "unlock ";
1222 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1223 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1224 FileName(owner_->filePath()));
1226 // Lock error messages go unfortunately on stderr and are unreachible this way.
1227 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1231 if (!line.empty()) status += line + "; ";
1235 if (!isLocked() && lock)
1236 frontend::Alert::error(_("Revision control error."),
1237 _("Error while acquiring write lock.\n"
1238 "Another user is most probably editing\n"
1239 "the current document now!\n"
1240 "Also check the access to the repository."));
1241 if (isLocked() && !lock)
1242 frontend::Alert::error(_("Revision control error."),
1243 _("Error while releasing write lock.\n"
1244 "Check the access to the repository."));
1248 string SVN::checkOut()
1250 FileName tmpf = FileName::tempName("lyxvcout");
1252 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1253 return N_("Error: Could not generate logfile.");
1256 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1257 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1258 FileName(owner_->filePath()));
1261 string const res = scanLogFile(tmpf, log);
1263 frontend::Alert::error(_("Revision control error."),
1264 bformat(_("Error when updating from repository.\n"
1265 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1266 "After pressing OK, LyX will try to reopen the resolved document."),
1267 from_local8bit(res)));
1269 fileLock(true, tmpf, log);
1272 return log.empty() ? string() : "SVN: " + log;
1276 bool SVN::checkOutEnabled()
1285 string SVN::repoUpdate()
1287 FileName tmpf = FileName::tempName("lyxvcout");
1289 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1290 return N_("Error: Could not generate logfile.");
1293 doVCCommand("svn diff " + quoteName(owner_->filePath())
1294 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1295 FileName(owner_->filePath()));
1296 docstring res = tmpf.fileContents("UTF-8");
1298 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1299 docstring const file = from_utf8(owner_->filePath());
1300 docstring text = bformat(_("There were detected changes "
1301 "in the working directory:\n%1$s\n\n"
1302 "In case of file conflict version of the local directory files "
1303 "will be preferred."
1304 "\n\nContinue?"), file);
1305 int ret = frontend::Alert::prompt(_("Changes detected"),
1306 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1308 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1309 ret = frontend::Alert::prompt(_("Changes detected"),
1310 text, 0, 1, _("&Yes"), _("&No"));
1311 hideDialogs("file", 0);
1319 // Reverting looks too harsh, see bug #6255.
1320 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1321 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1322 // FileName(owner_->filePath()));
1323 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1324 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1325 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1326 FileName(owner_->filePath()));
1327 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1329 LYXERR(Debug::LYXVC, res);
1331 return to_utf8(res);
1335 bool SVN::repoUpdateEnabled()
1341 string SVN::lockingToggle()
1343 FileName tmpf = FileName::tempName("lyxvcout");
1345 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1346 return N_("Error: Could not generate logfile.");
1349 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1350 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1351 FileName(owner_->filePath()));
1356 string res = scanLogFile(tmpf, log);
1357 bool locking = contains(res, "svn:needs-lock");
1359 ret = doVCCommand("svn propset svn:needs-lock ON "
1360 + quoteName(onlyFileName(owner_->absFileName()))
1361 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1362 FileName(owner_->filePath()));
1364 ret = doVCCommand("svn propdel svn:needs-lock "
1365 + quoteName(onlyFileName(owner_->absFileName()))
1366 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1367 FileName(owner_->filePath()));
1372 frontend::Alert::warning(_("VCN File Locking"),
1373 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1374 + _("Do not forget to commit the locking property into the repository."),
1377 return string("SVN: ") + N_("Locking property set.");
1381 bool SVN::lockingToggleEnabled()
1389 // Reverts to the version in SVN repository and
1390 // gets the updated version from the repository.
1391 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1393 if (doVCCommand("svn revert -q " + fil,
1394 FileName(owner_->filePath())))
1396 owner_->markClean();
1401 bool SVN::isRevertWithConfirmation()
1403 //FIXME owner && diff
1408 void SVN::undoLast()
1410 // merge the current with the previous version
1411 // in a reverse patch kind of way, so that the
1412 // result is to revert the last changes.
1413 lyxerr << "Sorry, not implemented." << endl;
1417 bool SVN::undoLastEnabled()
1423 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1425 if (info == LyXVC::Tree) {
1426 if (rev_tree_cache_.empty())
1427 if (!getTreeRevisionInfo())
1428 rev_tree_cache_ = "?";
1429 if (rev_tree_cache_ == "?")
1432 return rev_tree_cache_;
1435 // fill the rest of the attributes for a single file
1436 if (rev_file_cache_.empty())
1437 if (!getFileRevisionInfo())
1438 rev_file_cache_ = "?";
1442 if (rev_file_cache_ == "?")
1444 return rev_file_cache_;
1446 return rev_author_cache_;
1448 return rev_date_cache_;
1450 return rev_time_cache_;
1459 bool SVN::getFileRevisionInfo()
1461 FileName tmpf = FileName::tempName("lyxvcout");
1463 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1467 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1468 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1469 FileName(owner_->filePath()));
1474 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1482 LYXERR(Debug::LYXVC, line);
1483 if (prefixIs(line, "<commit"))
1485 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1486 string l1 = subst(line, "revision=\"", "");
1487 string l2 = trim(subst(l1, "\">", ""));
1489 rev_file_cache_ = rev = l2;
1491 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1492 string l1 = subst(line, "<author>", "");
1493 string l2 = subst(l1, "</author>", "");
1494 rev_author_cache_ = l2;
1496 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1497 string l1 = subst(line, "<date>", "");
1498 string l2 = subst(l1, "</date>", "");
1499 l2 = split(l2, l1, 'T');
1500 rev_date_cache_ = l1;
1501 l2 = split(l2, l1, '.');
1502 rev_time_cache_ = l1;
1508 return !rev.empty();
1512 bool SVN::getTreeRevisionInfo()
1514 FileName tmpf = FileName::tempName("lyxvcout");
1516 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1520 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1521 FileName(owner_->filePath()));
1526 // only first line in case something bad happens.
1527 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1533 rev_tree_cache_ = line;
1534 return !line.empty();
1538 void SVN::getLog(FileName const & tmpf)
1540 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1541 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1542 FileName(owner_->filePath()));
1546 bool SVN::prepareFileRevision(string const & revis, string & f)
1548 if (!isStrInt(revis))
1551 int rev = convert<int>(revis);
1553 if (!getFileRevisionInfo())
1556 rev = convert<int>(rev_file_cache_);
1557 // go back for minus rev
1559 rev = rev + convert<int>(rev_file_cache_);
1564 string revname = convert<string>(rev);
1565 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1567 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1571 doVCCommand("svn cat -r " + revname + " "
1572 + quoteName(onlyFileName(owner_->absFileName()))
1573 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1574 FileName(owner_->filePath()));
1575 if (tmpf.isFileEmpty())
1578 f = tmpf.absFileName();
1583 bool SVN::prepareFileRevisionEnabled()
1590 bool SVN::toggleReadOnlyEnabled()