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, 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.
355 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
357 if (info == LyXVC::File)
363 bool RCS::prepareFileRevision(string const &revis, string & f)
366 if (!VCS::makeRCSRevision(version_, rev))
369 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
371 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
372 return N_("Error: Could not generate logfile.");
375 doVCCommand("co -p" + rev + " "
376 + quoteName(onlyFileName(owner_->absFileName()))
377 + " > " + quoteName(tmpf.toFilesystemEncoding()),
378 FileName(owner_->filePath()));
379 if (tmpf.isFileEmpty())
382 f = tmpf.absFileName();
387 bool RCS::prepareFileRevisionEnabled()
393 /////////////////////////////////////////////////////////////////////
397 /////////////////////////////////////////////////////////////////////
399 CVS::CVS(FileName const & m, FileName const & f)
403 have_rev_info_ = false;
408 FileName const CVS::findFile(FileName const & file)
410 // First we look for the CVS/Entries in the same dir
411 // where we have file.
412 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
413 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
414 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
415 << "' for `" << tmpf << '\'');
416 if (entries.isReadableFile()) {
417 // Ok we are at least in a CVS dir. Parse the CVS/Entries
418 // and see if we can find this file. We do a fast and
420 ifstream ifs(entries.toFilesystemEncoding().c_str());
422 while (getline(ifs, line)) {
423 LYXERR(Debug::LYXVC, "\tEntries: " << line);
424 if (contains(line, tmpf))
432 void CVS::scanMaster()
434 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
435 // Ok now we do the real scan...
436 ifstream ifs(master_.toFilesystemEncoding().c_str());
437 string name = onlyFileName(file_.absFileName());
438 string tmpf = '/' + name + '/';
439 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
441 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
442 while (getline(ifs, line)) {
443 LYXERR(Debug::LYXVC, "\t line: " << line);
444 if (contains(line, tmpf)) {
445 // Ok extract the fields.
448 regex_match(line, sm, reg);
450 //sm[0]; // whole matched string
452 version_ = sm.str(2);
453 string const file_date = sm.str(3);
456 //sm[5]; // tag or tagdate
457 if (file_.isReadableFile()) {
458 time_t mod = file_.lastModified();
459 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
460 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
461 << "'\nModification date of file: `" << mod_date << '\'');
462 if (file_.isReadOnly()) {
463 // readonly checkout is unlocked
466 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
467 FileName base(addName(bdir.absFileName(),name));
468 // if base version is existent "cvs edit" was used to lock
469 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
472 vcstatus = NOLOCKING;
480 string const CVS::getTarget(OperationMode opmode) const
484 // in client server mode CVS does not like full path operand for directory operation
485 // since LyX switches to the repo dir "." is good enough as target
488 return quoteName(onlyFileName(owner_->absFileName()));
494 docstring CVS::toString(CvsStatus status) const
498 return _("Up-to-date");
499 case LocallyModified:
500 return _("Locally Modified");
502 return _("Locally Added");
504 return _("Needs Merge");
506 return _("Needs Checkout");
508 return _("No CVS file");
510 return _("Cannot retrieve CVS status");
516 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
517 FileName const & output, bool reportError)
519 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
520 return doVCCommand(cmd + redirection, path, reportError);
524 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
525 support::FileName const & path,
526 support::FileName const & output)
528 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
529 return doVCCommandCall(cmd + redirection, path);
533 CVS::CvsStatus CVS::getStatus()
535 FileName tmpf = FileName::tempName("lyxvcout");
537 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
541 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
542 FileName(owner_->filePath()), tmpf)) {
547 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
548 CvsStatus status = NoCvsFile;
553 LYXERR(Debug::LYXVC, line << "\n");
554 if (prefixIs(line, "File:")) {
555 if (contains(line, "Up-to-date"))
557 else if (contains(line, "Locally Modified"))
558 status = LocallyModified;
559 else if (contains(line, "Locally Added"))
560 status = LocallyAdded;
561 else if (contains(line, "Needs Merge"))
563 else if (contains(line, "Needs Checkout"))
564 status = NeedsCheckout;
571 void CVS::getRevisionInfo()
575 have_rev_info_ = true;
576 FileName tmpf = FileName::tempName("lyxvcout");
578 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
582 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
583 + " " + getTarget(File),
584 FileName(owner_->filePath()), tmpf);
587 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
591 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
592 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
597 LYXERR(Debug::LYXVC, line << "\n");
598 if (prefixIs(line, "date:")) {
600 regex_match(line, sm, reg);
601 //sm[0]; // whole matched string
602 rev_date_cache_ = sm[1];
603 rev_time_cache_ = sm[2];
604 //sm[3]; // GMT offset
605 rev_author_cache_ = sm[4];
610 if (rev_author_cache_.empty())
612 "Could not retrieve revision info for " << version_ <<
613 " of " << getTarget(File));
617 void CVS::registrer(string const & msg)
619 doVCCommand("cvs -q add -m \"" + msg + "\" "
621 FileName(owner_->filePath()));
625 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
627 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
628 FileName(owner_->filePath()), tmpf, false);
635 return doVCCommand("cvs -q edit " + getTarget(File),
636 FileName(owner_->filePath()));
643 return doVCCommand("cvs -q unedit " + getTarget(File),
644 FileName(owner_->filePath()));
648 int CVS::update(OperationMode opmode, FileName const & tmpf)
650 return doVCCommandWithOutput("cvs -q update "
652 FileName(owner_->filePath()), tmpf, false);
656 string CVS::scanLogFile(FileName const & f, string & status)
658 ifstream ifs(f.toFilesystemEncoding().c_str());
663 LYXERR(Debug::LYXVC, line << "\n");
665 status += line + "; ";
666 if (prefixIs(line, "C ")) {
676 string CVS::checkIn(string const & msg)
678 CvsStatus status = getStatus();
681 if (vcstatus != NOLOCKING)
683 return "CVS: Proceeded";
684 case LocallyModified:
686 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
688 FileName(owner_->filePath()));
689 return rc ? string() : "CVS: Proceeded";
693 frontend::Alert::error(_("Revision control error."),
694 _("The repository version is newer then the current check out.\n"
695 "You have to update from repository first or revert your changes.")) ;
698 frontend::Alert::error(_("Revision control error."),
699 bformat(_("Bad status when checking in changes.\n"
708 bool CVS::isLocked() const
710 FileName fn(owner_->absFileName());
712 return !fn.isReadOnly();
716 bool CVS::checkInEnabled()
718 if (vcstatus != NOLOCKING)
725 bool CVS::isCheckInWithConfirmation()
727 CvsStatus status = getStatus();
728 return status == LocallyModified || status == LocallyAdded;
732 string CVS::checkOut()
734 if (vcstatus != NOLOCKING && edit())
736 FileName tmpf = FileName::tempName("lyxvcout");
738 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
742 int rc = update(File, tmpf);
744 string const res = scanLogFile(tmpf, log);
746 frontend::Alert::error(_("Revision control error."),
747 bformat(_("Error when updating from repository.\n"
748 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
749 "After pressing OK, LyX will try to reopen the resolved document."),
750 from_local8bit(res)));
755 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
759 bool CVS::checkOutEnabled()
761 if (vcstatus != NOLOCKING)
768 string CVS::repoUpdate()
770 FileName tmpf = FileName::tempName("lyxvcout");
772 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
776 getDiff(Directory, tmpf);
777 docstring res = tmpf.fileContents("UTF-8");
779 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
780 docstring const file = from_utf8(owner_->filePath());
781 docstring text = bformat(_("There were detected changes "
782 "in the working directory:\n%1$s\n\n"
783 "Possible file conflicts must be then resolved manually "
784 "or you will need to revert back to the repository version."), file);
785 int ret = frontend::Alert::prompt(_("Changes detected"),
786 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
788 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
789 ret = frontend::Alert::prompt(_("Changes detected"),
790 text, 0, 1, _("&Continue"), _("&Abort"));
791 hideDialogs("file", 0);
799 int rc = update(Directory, tmpf);
800 res += "Update log:\n" + tmpf.fileContents("UTF-8");
801 LYXERR(Debug::LYXVC, res);
804 string sres = scanLogFile(tmpf, log);
806 docstring const file = owner_->fileName().displayName(20);
807 frontend::Alert::error(_("Revision control error."),
808 bformat(_("Error when updating document %1$s from repository.\n"
809 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
810 "After pressing OK, LyX will try to reopen the resolved document."),
811 file, from_local8bit(sres)));
817 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
821 bool CVS::repoUpdateEnabled()
827 string CVS::lockingToggle()
829 lyxerr << "Sorry, not implemented." << endl;
834 bool CVS::lockingToggleEnabled()
840 bool CVS::isRevertWithConfirmation()
842 CvsStatus status = getStatus();
843 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
849 // Reverts to the version in CVS repository and
850 // gets the updated version from the repository.
851 CvsStatus status = getStatus();
854 if (vcstatus != NOLOCKING)
855 return 0 == unedit();
859 case LocallyModified: {
860 FileName f(owner_->absFileName());
862 update(File, FileName());
867 docstring const file = owner_->fileName().displayName(20);
868 frontend::Alert::error(_("Revision control error."),
869 bformat(_("The document %1$s is not in repository.\n"
870 "You have to check in the first revision before you can revert."),
875 docstring const file = owner_->fileName().displayName(20);
876 frontend::Alert::error(_("Revision control error."),
877 bformat(_("Cannot revert document %1$s to repository version.\n"
878 "The status '%2$s' is unexpected."),
879 file, toString(status)));
889 // merge the current with the previous version
890 // in a reverse patch kind of way, so that the
891 // result is to revert the last changes.
892 lyxerr << "Sorry, not implemented." << endl;
896 bool CVS::undoLastEnabled()
902 void CVS::getLog(FileName const & tmpf)
904 doVCCommandWithOutput("cvs log " + getTarget(File),
905 FileName(owner_->filePath()),
910 bool CVS::toggleReadOnlyEnabled()
916 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
918 if (!version_.empty()) {
924 return rev_author_cache_;
926 return rev_date_cache_;
928 return rev_time_cache_;
936 bool CVS::prepareFileRevision(string const & revis, string & f)
939 if (!VCS::makeRCSRevision(version_, rev))
942 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
944 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
948 doVCCommandWithOutput("cvs update -p -r" + rev + " "
950 FileName(owner_->filePath()), tmpf);
951 if (tmpf.isFileEmpty())
954 f = tmpf.absFileName();
959 bool CVS::prepareFileRevisionEnabled()
965 /////////////////////////////////////////////////////////////////////
969 /////////////////////////////////////////////////////////////////////
971 SVN::SVN(FileName const & m, FileName const & f)
981 FileName const SVN::findFile(FileName const & file)
983 // First we look for the .svn/entries in the same dir
984 // where we have file.
985 FileName const entries(onlyPath(file.absFileName()) + "/.svn/entries");
986 string const tmpf = onlyFileName(file.absFileName());
987 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
988 << "' for `" << tmpf << '\'');
989 if (entries.isReadableFile()) {
990 // Ok we are at least in a SVN dir. Parse the .svn/entries
991 // and see if we can find this file. We do a fast and
993 ifstream ifs(entries.toFilesystemEncoding().c_str());
994 string line, oldline;
995 while (getline(ifs, line)) {
996 if (line == "dir" || line == "file")
997 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
998 if (oldline == tmpf && line == "file")
1007 void SVN::scanMaster()
1009 // vcstatus code is somewhat superflous, until we want
1010 // to implement read-only toggle for svn.
1011 vcstatus = NOLOCKING;
1012 if (checkLockMode()) {
1016 vcstatus = UNLOCKED;
1022 bool SVN::checkLockMode()
1024 FileName tmpf = FileName::tempName("lyxvcout");
1026 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1027 return N_("Error: Could not generate logfile.");
1030 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1031 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
1032 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1036 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1042 LYXERR(Debug::LYXVC, line);
1043 if (contains(line, "svn:needs-lock"))
1046 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1054 bool SVN::isLocked() const
1057 return !file_.isReadOnly();
1061 void SVN::registrer(string const & /*msg*/)
1063 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1064 FileName(owner_->filePath()));
1068 string SVN::checkIn(string const & msg)
1070 FileName tmpf = FileName::tempName("lyxvcout");
1072 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1073 return N_("Error: Could not generate logfile.");
1076 doVCCommand("svn commit -m \"" + msg + "\" "
1077 + quoteName(onlyFileName(owner_->absFileName()))
1078 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1079 FileName(owner_->filePath()));
1082 string res = scanLogFile(tmpf, log);
1084 frontend::Alert::error(_("Revision control error."),
1085 _("Error when committing to repository.\n"
1086 "You have to manually resolve the problem.\n"
1087 "LyX will reopen the document after you press OK."));
1089 fileLock(false, tmpf, log);
1092 return log.empty() ? string() : "SVN: " + log;
1096 bool SVN::checkInEnabled()
1105 bool SVN::isCheckInWithConfirmation()
1107 // FIXME one day common getDiff and perhaps OpMode for all backends
1109 FileName tmpf = FileName::tempName("lyxvcout");
1111 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1115 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1116 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1117 FileName(owner_->filePath()));
1119 docstring diff = tmpf.fileContents("UTF-8");
1129 // FIXME Correctly return code should be checked instead of this.
1130 // This would need another solution than just plain startscript.
1131 // Hint from Andre': QProcess::readAllStandardError()...
1132 string SVN::scanLogFile(FileName const & f, string & status)
1134 ifstream ifs(f.toFilesystemEncoding().c_str());
1139 LYXERR(Debug::LYXVC, line << "\n");
1141 status += line + "; ";
1142 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1143 || contains(line, "Commit failed")) {
1147 if (contains(line, "svn:needs-lock")) {
1157 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1159 if (!locked_mode_ || (isLocked() == lock))
1162 string const arg = lock ? "lock " : "unlock ";
1163 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1164 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1165 FileName(owner_->filePath()));
1167 // Lock error messages go unfortunately on stderr and are unreachible this way.
1168 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1172 if (!line.empty()) status += line + "; ";
1176 if (!isLocked() && lock)
1177 frontend::Alert::error(_("Revision control error."),
1178 _("Error while acquiring write lock.\n"
1179 "Another user is most probably editing\n"
1180 "the current document now!\n"
1181 "Also check the access to the repository."));
1182 if (isLocked() && !lock)
1183 frontend::Alert::error(_("Revision control error."),
1184 _("Error while releasing write lock.\n"
1185 "Check the access to the repository."));
1189 string SVN::checkOut()
1191 FileName tmpf = FileName::tempName("lyxvcout");
1193 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1194 return N_("Error: Could not generate logfile.");
1197 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1198 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1199 FileName(owner_->filePath()));
1202 string const res = scanLogFile(tmpf, log);
1204 frontend::Alert::error(_("Revision control error."),
1205 bformat(_("Error when updating from repository.\n"
1206 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1207 "After pressing OK, LyX will try to reopen the resolved document."),
1208 from_local8bit(res)));
1210 fileLock(true, tmpf, log);
1213 return log.empty() ? string() : "SVN: " + log;
1217 bool SVN::checkOutEnabled()
1226 string SVN::repoUpdate()
1228 FileName tmpf = FileName::tempName("lyxvcout");
1230 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1231 return N_("Error: Could not generate logfile.");
1234 doVCCommand("svn diff " + quoteName(owner_->filePath())
1235 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1236 FileName(owner_->filePath()));
1237 docstring res = tmpf.fileContents("UTF-8");
1239 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1240 docstring const file = from_utf8(owner_->filePath());
1241 docstring text = bformat(_("There were detected changes "
1242 "in the working directory:\n%1$s\n\n"
1243 "In case of file conflict version of the local directory files "
1244 "will be preferred."
1245 "\n\nContinue?"), file);
1246 int ret = frontend::Alert::prompt(_("Changes detected"),
1247 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1249 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1250 ret = frontend::Alert::prompt(_("Changes detected"),
1251 text, 0, 1, _("&Yes"), _("&No"));
1252 hideDialogs("file", 0);
1260 // Reverting looks too harsh, see bug #6255.
1261 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1262 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1263 // FileName(owner_->filePath()));
1264 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1265 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1266 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1267 FileName(owner_->filePath()));
1268 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1270 LYXERR(Debug::LYXVC, res);
1272 return to_utf8(res);
1276 bool SVN::repoUpdateEnabled()
1282 string SVN::lockingToggle()
1284 FileName tmpf = FileName::tempName("lyxvcout");
1286 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1287 return N_("Error: Could not generate logfile.");
1290 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1291 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1292 FileName(owner_->filePath()));
1297 string res = scanLogFile(tmpf, log);
1298 bool locking = contains(res, "svn:needs-lock");
1300 ret = doVCCommand("svn propset svn:needs-lock ON "
1301 + quoteName(onlyFileName(owner_->absFileName()))
1302 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1303 FileName(owner_->filePath()));
1305 ret = doVCCommand("svn propdel svn:needs-lock "
1306 + quoteName(onlyFileName(owner_->absFileName()))
1307 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1308 FileName(owner_->filePath()));
1313 frontend::Alert::warning(_("VCN File Locking"),
1314 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1315 + _("Do not forget to commit the locking property into the repository."),
1318 return string("SVN: ") + N_("Locking property set.");
1322 bool SVN::lockingToggleEnabled()
1330 // Reverts to the version in SVN repository and
1331 // gets the updated version from the repository.
1332 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1334 if (doVCCommand("svn revert -q " + fil,
1335 FileName(owner_->filePath())))
1337 owner_->markClean();
1342 bool SVN::isRevertWithConfirmation()
1344 //FIXME owner && diff
1349 void SVN::undoLast()
1351 // merge the current with the previous version
1352 // in a reverse patch kind of way, so that the
1353 // result is to revert the last changes.
1354 lyxerr << "Sorry, not implemented." << endl;
1358 bool SVN::undoLastEnabled()
1364 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1366 if (info == LyXVC::Tree) {
1367 if (rev_tree_cache_.empty())
1368 if (!getTreeRevisionInfo())
1369 rev_tree_cache_ = "?";
1370 if (rev_tree_cache_ == "?")
1373 return rev_tree_cache_;
1376 // fill the rest of the attributes for a single file
1377 if (rev_file_cache_.empty())
1378 if (!getFileRevisionInfo())
1379 rev_file_cache_ = "?";
1383 if (rev_file_cache_ == "?")
1385 return rev_file_cache_;
1387 return rev_author_cache_;
1389 return rev_date_cache_;
1391 return rev_time_cache_;
1400 bool SVN::getFileRevisionInfo()
1402 FileName tmpf = FileName::tempName("lyxvcout");
1404 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1405 return N_("Error: Could not generate logfile.");
1408 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1409 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1410 FileName(owner_->filePath()));
1415 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1423 LYXERR(Debug::LYXVC, line);
1424 if (prefixIs(line, "<commit"))
1426 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1427 string l1 = subst(line, "revision=\"", "");
1428 string l2 = trim(subst(l1, "\">", ""));
1430 rev_file_cache_ = rev = l2;
1432 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1433 string l1 = subst(line, "<author>", "");
1434 string l2 = subst(l1, "</author>", "");
1435 rev_author_cache_ = l2;
1437 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1438 string l1 = subst(line, "<date>", "");
1439 string l2 = subst(l1, "</date>", "");
1440 l2 = split(l2, l1, 'T');
1441 rev_date_cache_ = l1;
1442 l2 = split(l2, l1, '.');
1443 rev_time_cache_ = l1;
1449 return !rev.empty();
1453 bool SVN::getTreeRevisionInfo()
1455 FileName tmpf = FileName::tempName("lyxvcout");
1457 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1458 return N_("Error: Could not generate logfile.");
1461 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1462 FileName(owner_->filePath()));
1467 // only first line in case something bad happens.
1468 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1474 rev_tree_cache_ = line;
1475 return !line.empty();
1479 void SVN::getLog(FileName const & tmpf)
1481 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1482 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1483 FileName(owner_->filePath()));
1487 bool SVN::prepareFileRevision(string const & revis, string & f)
1489 if (!isStrInt(revis))
1492 int rev = convert<int>(revis);
1494 if (!getFileRevisionInfo())
1497 rev = convert<int>(rev_file_cache_);
1498 // go back for minus rev
1500 rev = rev + convert<int>(rev_file_cache_);
1505 string revname = convert<string>(rev);
1506 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1508 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1512 doVCCommand("svn cat -r " + revname + " "
1513 + quoteName(onlyFileName(owner_->absFileName()))
1514 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1515 FileName(owner_->filePath()));
1516 if (tmpf.isFileEmpty())
1519 f = tmpf.absFileName();
1524 bool SVN::prepareFileRevisionEnabled()
1531 bool SVN::toggleReadOnlyEnabled()