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 /////////////////////////////////////////////////////////////////////
72 /////////////////////////////////////////////////////////////////////
74 RCS::RCS(FileName const & m)
81 FileName const RCS::findFile(FileName const & file)
83 // Check if *,v exists.
84 FileName tmp(file.absFileName() + ",v");
85 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
86 if (tmp.isReadableFile()) {
87 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
91 // Check if RCS/*,v exists.
92 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
93 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
94 if (tmp.isReadableFile()) {
95 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
103 void RCS::retrieve(FileName const & file)
105 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
106 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
111 void RCS::scanMaster()
116 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
118 ifstream ifs(master_.toFilesystemEncoding().c_str());
121 bool read_enough = false;
123 while (!read_enough && ifs >> token) {
124 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
129 else if (token == "head") {
133 tmv = rtrim(tmv, ";");
135 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
136 } else if (contains(token, "access")
137 || contains(token, "symbols")
138 || contains(token, "strict")) {
140 } else if (contains(token, "locks")) {
142 if (contains(token, ';')) {
143 locker_ = "Unlocked";
152 s1 = rtrim(tmpt, ";");
153 // tmp is now in the format <user>:<version>
154 s1 = split(s1, s2, ':');
155 // s2 is user, and s1 is version
156 if (s1 == version_) {
161 } while (!contains(tmpt, ';'));
163 } else if (token == "comment") {
164 // we don't need to read any further than this.
168 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
174 void RCS::registrer(string const & msg)
176 string cmd = "ci -q -u -i -t-\"";
179 cmd += quoteName(onlyFileName(owner_->absFileName()));
180 doVCCommand(cmd, FileName(owner_->filePath()));
184 string RCS::checkIn(string const & msg)
186 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
187 + quoteName(onlyFileName(owner_->absFileName())),
188 FileName(owner_->filePath()));
189 return ret ? string() : "RCS: Proceeded";
193 bool RCS::checkInEnabled()
195 return owner_ && !owner_->isReadonly();
199 bool RCS::isCheckInWithConfirmation()
201 // FIXME one day common getDiff for all backends
203 // if (getDiff(file, diff) && diff.empty())
206 FileName tmpf = FileName::tempName("lyxvcout");
208 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
212 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
213 + " > " + quoteName(tmpf.toFilesystemEncoding()),
214 FileName(owner_->filePath()));
216 docstring diff = tmpf.fileContents("UTF-8");
226 string RCS::checkOut()
229 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
230 FileName(owner_->filePath()));
231 return ret ? string() : "RCS: Proceeded";
235 bool RCS::checkOutEnabled()
237 return owner_ && owner_->isReadonly();
241 string RCS::repoUpdate()
243 lyxerr << "Sorry, not implemented." << endl;
248 bool RCS::repoUpdateEnabled()
254 string RCS::lockingToggle()
256 //FIXME this might be actually possible, study rcs -U, rcs -L.
257 //State should be easy to get inside scanMaster.
258 //It would fix #4370 and make rcs/svn usage even more closer.
259 lyxerr << "Sorry, not implemented." << endl;
264 bool RCS::lockingToggleEnabled()
272 if (doVCCommand("co -f -u" + version_ + " "
273 + quoteName(onlyFileName(owner_->absFileName())),
274 FileName(owner_->filePath())))
276 // We ignore changes and just reload!
282 bool RCS::isRevertWithConfirmation()
284 //FIXME owner && diff ?
291 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
292 doVCCommand("rcs -o" + version_ + " "
293 + quoteName(onlyFileName(owner_->absFileName())),
294 FileName(owner_->filePath()));
298 bool RCS::undoLastEnabled()
304 void RCS::getLog(FileName const & tmpf)
306 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
307 + " > " + quoteName(tmpf.toFilesystemEncoding()),
308 FileName(owner_->filePath()));
312 bool RCS::toggleReadOnlyEnabled()
314 // This got broken somewhere along lfuns dispatch reorganization.
315 // reloadBuffer would be needed after this, but thats problematic
316 // since we are inside Buffer::dispatch.
321 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
323 if (info == LyXVC::File)
329 bool RCS::prepareFileRevision(string const &revis, string & f)
334 int back = convert<int>(rev);
335 // if positive use as the last number in the whole revision string
338 rsplit(version_, base , '.' );
339 rev = base + "." + rev;
343 // we care about the last number from revision string
344 // in case of backward indexing
347 cur = rsplit(version_, base , '.' );
350 int want = convert<int>(cur) + back;
354 rev = base + "." + convert<string>(want);
358 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
360 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
361 return N_("Error: Could not generate logfile.");
364 doVCCommand("co -p" + rev + " "
365 + quoteName(onlyFileName(owner_->absFileName()))
366 + " > " + quoteName(tmpf.toFilesystemEncoding()),
367 FileName(owner_->filePath()));
368 if (tmpf.isFileEmpty())
371 f = tmpf.absFileName();
376 bool RCS::prepareFileRevisionEnabled()
382 /////////////////////////////////////////////////////////////////////
386 /////////////////////////////////////////////////////////////////////
388 CVS::CVS(FileName const & m, FileName const & f)
392 have_rev_info_ = false;
397 FileName const CVS::findFile(FileName const & file)
399 // First we look for the CVS/Entries in the same dir
400 // where we have file.
401 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
402 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
403 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
404 << "' for `" << tmpf << '\'');
405 if (entries.isReadableFile()) {
406 // Ok we are at least in a CVS dir. Parse the CVS/Entries
407 // and see if we can find this file. We do a fast and
409 ifstream ifs(entries.toFilesystemEncoding().c_str());
411 while (getline(ifs, line)) {
412 LYXERR(Debug::LYXVC, "\tEntries: " << line);
413 if (contains(line, tmpf))
421 void CVS::scanMaster()
423 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
424 // Ok now we do the real scan...
425 ifstream ifs(master_.toFilesystemEncoding().c_str());
426 string name = onlyFileName(file_.absFileName());
427 string tmpf = '/' + name + '/';
428 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
430 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
431 while (getline(ifs, line)) {
432 LYXERR(Debug::LYXVC, "\t line: " << line);
433 if (contains(line, tmpf)) {
434 // Ok extract the fields.
437 regex_match(line, sm, reg);
439 //sm[0]; // whole matched string
441 version_ = sm.str(2);
442 string const file_date = sm.str(3);
445 //sm[5]; // tag or tagdate
446 if (file_.isReadableFile()) {
447 time_t mod = file_.lastModified();
448 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
449 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
450 << "'\nModification date of file: `" << mod_date << '\'');
451 if (file_.isReadOnly()) {
452 // readonly checkout is unlocked
455 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
456 FileName base(addName(bdir.absFileName(),name));
457 // if base version is existent "cvs edit" was used to lock
458 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
461 vcstatus = NOLOCKING;
469 string const CVS::getTarget(OperationMode opmode) const
473 // in client server mode CVS does not like full path operand for directory operation
474 // since LyX switches to the repo dir "." is good enough as target
477 return quoteName(onlyFileName(owner_->absFileName()));
483 docstring CVS::toString(CvsStatus status) const
487 return _("Up-to-date");
488 case LocallyModified:
489 return _("Locally Modified");
491 return _("Locally Added");
493 return _("Needs Merge");
495 return _("Needs Checkout");
497 return _("No CVS file");
499 return _("Cannot retrieve CVS status");
505 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
506 FileName const & output, bool reportError)
508 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
509 return doVCCommand(cmd + redirection, path, reportError);
513 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
514 support::FileName const & path,
515 support::FileName const & output)
517 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
518 return doVCCommandCall(cmd + redirection, path);
522 CVS::CvsStatus CVS::getStatus()
524 FileName tmpf = FileName::tempName("lyxvcout");
526 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
530 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
531 FileName(owner_->filePath()), tmpf)) {
536 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
537 CvsStatus status = NoCvsFile;
542 LYXERR(Debug::LYXVC, line << "\n");
543 if (prefixIs(line, "File:")) {
544 if (contains(line, "Up-to-date"))
546 else if (contains(line, "Locally Modified"))
547 status = LocallyModified;
548 else if (contains(line, "Locally Added"))
549 status = LocallyAdded;
550 else if (contains(line, "Needs Merge"))
552 else if (contains(line, "Needs Checkout"))
553 status = NeedsCheckout;
560 void CVS::getRevisionInfo()
564 have_rev_info_ = true;
565 FileName tmpf = FileName::tempName("lyxvcout");
567 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
571 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
572 + " " + getTarget(File),
573 FileName(owner_->filePath()), tmpf);
576 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
580 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
581 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
586 LYXERR(Debug::LYXVC, line << "\n");
587 if (prefixIs(line, "date:")) {
589 regex_match(line, sm, reg);
590 //sm[0]; // whole matched string
591 rev_date_cache_ = sm[1];
592 rev_time_cache_ = sm[2];
593 //sm[3]; // GMT offset
594 rev_author_cache_ = sm[4];
599 if (rev_author_cache_.empty())
601 "Could not retrieve revision info for " << version_ <<
602 " of " << getTarget(File));
606 void CVS::registrer(string const & msg)
608 doVCCommand("cvs -q add -m \"" + msg + "\" "
610 FileName(owner_->filePath()));
614 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
616 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
617 FileName(owner_->filePath()), tmpf, false);
624 return doVCCommand("cvs -q edit " + getTarget(File),
625 FileName(owner_->filePath()));
632 return doVCCommand("cvs -q unedit " + getTarget(File),
633 FileName(owner_->filePath()));
637 int CVS::update(OperationMode opmode, FileName const & tmpf)
639 return doVCCommandWithOutput("cvs -q update "
641 FileName(owner_->filePath()), tmpf, false);
645 string CVS::scanLogFile(FileName const & f, string & status)
647 ifstream ifs(f.toFilesystemEncoding().c_str());
652 LYXERR(Debug::LYXVC, line << "\n");
654 status += line + "; ";
655 if (prefixIs(line, "C ")) {
665 string CVS::checkIn(string const & msg)
667 CvsStatus status = getStatus();
670 if (vcstatus != NOLOCKING)
672 return "CVS: Proceeded";
673 case LocallyModified:
675 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
677 FileName(owner_->filePath()));
678 return rc ? string() : "CVS: Proceeded";
682 frontend::Alert::error(_("Revision control error."),
683 _("The repository version is newer then the current check out.\n"
684 "You have to update from repository first or revert your changes.")) ;
687 frontend::Alert::error(_("Revision control error."),
688 bformat(_("Bad status when checking in changes.\n"
697 bool CVS::isLocked() const
699 FileName fn(owner_->absFileName());
701 return !fn.isReadOnly();
705 bool CVS::checkInEnabled()
707 if (vcstatus != NOLOCKING)
714 bool CVS::isCheckInWithConfirmation()
716 CvsStatus status = getStatus();
717 return status == LocallyModified || status == LocallyAdded;
721 string CVS::checkOut()
723 if (vcstatus != NOLOCKING && edit())
725 FileName tmpf = FileName::tempName("lyxvcout");
727 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
731 int rc = update(File, tmpf);
733 string const res = scanLogFile(tmpf, log);
735 frontend::Alert::error(_("Revision control error."),
736 bformat(_("Error when updating from repository.\n"
737 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
738 "After pressing OK, LyX will try to reopen the resolved document."),
739 from_local8bit(res)));
744 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
748 bool CVS::checkOutEnabled()
750 if (vcstatus != NOLOCKING)
757 string CVS::repoUpdate()
759 FileName tmpf = FileName::tempName("lyxvcout");
761 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
765 getDiff(Directory, tmpf);
766 docstring res = tmpf.fileContents("UTF-8");
768 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
769 docstring const file = from_utf8(owner_->filePath());
770 docstring text = bformat(_("There were detected changes "
771 "in the working directory:\n%1$s\n\n"
772 "In case of file conflict you have to resolve them "
773 "manually or revert to repository version later."), file);
774 int ret = frontend::Alert::prompt(_("Changes detected"),
775 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
777 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
778 ret = frontend::Alert::prompt(_("Changes detected"),
779 text, 0, 1, _("&Continue"), _("&Abort"));
780 hideDialogs("file", 0);
788 int rc = update(Directory, tmpf);
789 res += "Update log:\n" + tmpf.fileContents("UTF-8");
790 LYXERR(Debug::LYXVC, res);
793 string sres = scanLogFile(tmpf, log);
795 docstring const file = owner_->fileName().displayName(20);
796 frontend::Alert::error(_("Revision control error."),
797 bformat(_("Error when updating document %1$s from repository.\n"
798 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
799 "After pressing OK, LyX will try to reopen the resolved document."),
800 file, from_local8bit(sres)));
806 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
810 bool CVS::repoUpdateEnabled()
816 string CVS::lockingToggle()
818 lyxerr << "Sorry, not implemented." << endl;
823 bool CVS::lockingToggleEnabled()
829 bool CVS::isRevertWithConfirmation()
831 CvsStatus status = getStatus();
832 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
838 // Reverts to the version in CVS repository and
839 // gets the updated version from the repository.
840 CvsStatus status = getStatus();
843 if (vcstatus != NOLOCKING)
844 return 0 == unedit();
848 case LocallyModified: {
849 FileName f(owner_->absFileName());
851 update(File, FileName());
856 docstring const file = owner_->fileName().displayName(20);
857 frontend::Alert::error(_("Revision control error."),
858 bformat(_("The document %1$s is not in repository.\n"
859 "You have to check in the first revision before you can revert."),
864 docstring const file = owner_->fileName().displayName(20);
865 frontend::Alert::error(_("Revision control error."),
866 bformat(_("Cannot revert document %1$s to repository version.\n"
867 "The status '%2$s' is unexpected."),
868 file, toString(status)));
878 // merge the current with the previous version
879 // in a reverse patch kind of way, so that the
880 // result is to revert the last changes.
881 lyxerr << "Sorry, not implemented." << endl;
885 bool CVS::undoLastEnabled()
891 void CVS::getLog(FileName const & tmpf)
893 doVCCommandWithOutput("cvs log " + getTarget(File),
894 FileName(owner_->filePath()),
899 bool CVS::toggleReadOnlyEnabled()
905 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
907 if (!version_.empty()) {
913 return rev_author_cache_;
915 return rev_date_cache_;
917 return rev_time_cache_;
925 bool CVS::prepareFileRevision(string const &, string &)
931 bool CVS::prepareFileRevisionEnabled()
937 /////////////////////////////////////////////////////////////////////
941 /////////////////////////////////////////////////////////////////////
943 SVN::SVN(FileName const & m, FileName const & f)
953 FileName const SVN::findFile(FileName const & file)
955 // First we look for the .svn/entries in the same dir
956 // where we have file.
957 FileName const entries(onlyPath(file.absFileName()) + "/.svn/entries");
958 string const tmpf = onlyFileName(file.absFileName());
959 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
960 << "' for `" << tmpf << '\'');
961 if (entries.isReadableFile()) {
962 // Ok we are at least in a SVN dir. Parse the .svn/entries
963 // and see if we can find this file. We do a fast and
965 ifstream ifs(entries.toFilesystemEncoding().c_str());
966 string line, oldline;
967 while (getline(ifs, line)) {
968 if (line == "dir" || line == "file")
969 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
970 if (oldline == tmpf && line == "file")
979 void SVN::scanMaster()
981 // vcstatus code is somewhat superflous, until we want
982 // to implement read-only toggle for svn.
983 vcstatus = NOLOCKING;
984 if (checkLockMode()) {
994 bool SVN::checkLockMode()
996 FileName tmpf = FileName::tempName("lyxvcout");
998 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
999 return N_("Error: Could not generate logfile.");
1002 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1003 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
1004 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1008 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1014 LYXERR(Debug::LYXVC, line);
1015 if (contains(line, "svn:needs-lock"))
1018 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1026 bool SVN::isLocked() const
1029 return !file_.isReadOnly();
1033 void SVN::registrer(string const & /*msg*/)
1035 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1036 FileName(owner_->filePath()));
1040 string SVN::checkIn(string const & msg)
1042 FileName tmpf = FileName::tempName("lyxvcout");
1044 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1045 return N_("Error: Could not generate logfile.");
1048 doVCCommand("svn commit -m \"" + msg + "\" "
1049 + quoteName(onlyFileName(owner_->absFileName()))
1050 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1051 FileName(owner_->filePath()));
1054 string res = scanLogFile(tmpf, log);
1056 frontend::Alert::error(_("Revision control error."),
1057 _("Error when committing to repository.\n"
1058 "You have to manually resolve the problem.\n"
1059 "LyX will reopen the document after you press OK."));
1061 fileLock(false, tmpf, log);
1064 return log.empty() ? string() : "SVN: " + log;
1068 bool SVN::checkInEnabled()
1077 bool SVN::isCheckInWithConfirmation()
1079 // FIXME one day common getDiff and perhaps OpMode for all backends
1081 FileName tmpf = FileName::tempName("lyxvcout");
1083 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1087 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1088 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1089 FileName(owner_->filePath()));
1091 docstring diff = tmpf.fileContents("UTF-8");
1101 // FIXME Correctly return code should be checked instead of this.
1102 // This would need another solution than just plain startscript.
1103 // Hint from Andre': QProcess::readAllStandardError()...
1104 string SVN::scanLogFile(FileName const & f, string & status)
1106 ifstream ifs(f.toFilesystemEncoding().c_str());
1111 LYXERR(Debug::LYXVC, line << "\n");
1113 status += line + "; ";
1114 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1115 || contains(line, "Commit failed")) {
1119 if (contains(line, "svn:needs-lock")) {
1129 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1131 if (!locked_mode_ || (isLocked() == lock))
1134 string const arg = lock ? "lock " : "unlock ";
1135 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1136 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1137 FileName(owner_->filePath()));
1139 // Lock error messages go unfortunately on stderr and are unreachible this way.
1140 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1144 if (!line.empty()) status += line + "; ";
1148 if (!isLocked() && lock)
1149 frontend::Alert::error(_("Revision control error."),
1150 _("Error while acquiring write lock.\n"
1151 "Another user is most probably editing\n"
1152 "the current document now!\n"
1153 "Also check the access to the repository."));
1154 if (isLocked() && !lock)
1155 frontend::Alert::error(_("Revision control error."),
1156 _("Error while releasing write lock.\n"
1157 "Check the access to the repository."));
1161 string SVN::checkOut()
1163 FileName tmpf = FileName::tempName("lyxvcout");
1165 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1166 return N_("Error: Could not generate logfile.");
1169 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1170 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1171 FileName(owner_->filePath()));
1174 string const res = scanLogFile(tmpf, log);
1176 frontend::Alert::error(_("Revision control error."),
1177 bformat(_("Error when updating from repository.\n"
1178 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1179 "After pressing OK, LyX will try to reopen the resolved document."),
1180 from_local8bit(res)));
1182 fileLock(true, tmpf, log);
1185 return log.empty() ? string() : "SVN: " + log;
1189 bool SVN::checkOutEnabled()
1198 string SVN::repoUpdate()
1200 FileName tmpf = FileName::tempName("lyxvcout");
1202 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1203 return N_("Error: Could not generate logfile.");
1206 doVCCommand("svn diff " + quoteName(owner_->filePath())
1207 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1208 FileName(owner_->filePath()));
1209 docstring res = tmpf.fileContents("UTF-8");
1211 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1212 docstring const file = from_utf8(owner_->filePath());
1213 docstring text = bformat(_("There were detected changes "
1214 "in the working directory:\n%1$s\n\n"
1215 "In case of file conflict version of the local directory files "
1216 "will be preferred."
1217 "\n\nContinue?"), file);
1218 int ret = frontend::Alert::prompt(_("Changes detected"),
1219 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1221 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1222 ret = frontend::Alert::prompt(_("Changes detected"),
1223 text, 0, 1, _("&Yes"), _("&No"));
1224 hideDialogs("file", 0);
1232 // Reverting looks too harsh, see bug #6255.
1233 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1234 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1235 // FileName(owner_->filePath()));
1236 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1237 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1238 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1239 FileName(owner_->filePath()));
1240 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1242 LYXERR(Debug::LYXVC, res);
1244 return to_utf8(res);
1248 bool SVN::repoUpdateEnabled()
1254 string SVN::lockingToggle()
1256 FileName tmpf = FileName::tempName("lyxvcout");
1258 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1259 return N_("Error: Could not generate logfile.");
1262 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1263 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1264 FileName(owner_->filePath()));
1269 string res = scanLogFile(tmpf, log);
1270 bool locking = contains(res, "svn:needs-lock");
1272 ret = doVCCommand("svn propset svn:needs-lock ON "
1273 + quoteName(onlyFileName(owner_->absFileName()))
1274 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1275 FileName(owner_->filePath()));
1277 ret = doVCCommand("svn propdel svn:needs-lock "
1278 + quoteName(onlyFileName(owner_->absFileName()))
1279 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1280 FileName(owner_->filePath()));
1285 frontend::Alert::warning(_("VCN File Locking"),
1286 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1287 + _("Do not forget to commit the locking property into the repository."),
1290 return string("SVN: ") + N_("Locking property set.");
1294 bool SVN::lockingToggleEnabled()
1302 // Reverts to the version in SVN repository and
1303 // gets the updated version from the repository.
1304 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1306 if (doVCCommand("svn revert -q " + fil,
1307 FileName(owner_->filePath())))
1309 owner_->markClean();
1314 bool SVN::isRevertWithConfirmation()
1316 //FIXME owner && diff
1321 void SVN::undoLast()
1323 // merge the current with the previous version
1324 // in a reverse patch kind of way, so that the
1325 // result is to revert the last changes.
1326 lyxerr << "Sorry, not implemented." << endl;
1330 bool SVN::undoLastEnabled()
1336 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1338 if (info == LyXVC::Tree) {
1339 if (rev_tree_cache_.empty())
1340 if (!getTreeRevisionInfo())
1341 rev_tree_cache_ = "?";
1342 if (rev_tree_cache_ == "?")
1345 return rev_tree_cache_;
1348 // fill the rest of the attributes for a single file
1349 if (rev_file_cache_.empty())
1350 if (!getFileRevisionInfo())
1351 rev_file_cache_ = "?";
1355 if (rev_file_cache_ == "?")
1357 return rev_file_cache_;
1359 return rev_author_cache_;
1361 return rev_date_cache_;
1363 return rev_time_cache_;
1372 bool SVN::getFileRevisionInfo()
1374 FileName tmpf = FileName::tempName("lyxvcout");
1376 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1377 return N_("Error: Could not generate logfile.");
1380 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1381 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1382 FileName(owner_->filePath()));
1387 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1395 LYXERR(Debug::LYXVC, line);
1396 if (prefixIs(line, "<commit"))
1398 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1399 string l1 = subst(line, "revision=\"", "");
1400 string l2 = trim(subst(l1, "\">", ""));
1402 rev_file_cache_ = rev = l2;
1404 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1405 string l1 = subst(line, "<author>", "");
1406 string l2 = subst(l1, "</author>", "");
1407 rev_author_cache_ = l2;
1409 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1410 string l1 = subst(line, "<date>", "");
1411 string l2 = subst(l1, "</date>", "");
1412 l2 = split(l2, l1, 'T');
1413 rev_date_cache_ = l1;
1414 l2 = split(l2, l1, '.');
1415 rev_time_cache_ = l1;
1421 return !rev.empty();
1425 bool SVN::getTreeRevisionInfo()
1427 FileName tmpf = FileName::tempName("lyxvcout");
1429 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1430 return N_("Error: Could not generate logfile.");
1433 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1434 FileName(owner_->filePath()));
1439 // only first line in case something bad happens.
1440 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1446 rev_tree_cache_ = line;
1447 return !line.empty();
1451 void SVN::getLog(FileName const & tmpf)
1453 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1454 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1455 FileName(owner_->filePath()));
1459 bool SVN::prepareFileRevision(string const & revis, string & f)
1461 if (!isStrInt(revis))
1464 int rev = convert<int>(revis);
1466 if (!getFileRevisionInfo())
1469 rev = convert<int>(rev_file_cache_);
1470 // go back for minus rev
1472 rev = rev + convert<int>(rev_file_cache_);
1477 string revname = convert<string>(rev);
1478 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1480 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1481 return N_("Error: Could not generate logfile.");
1484 doVCCommand("svn cat -r " + revname + " "
1485 + quoteName(onlyFileName(owner_->absFileName()))
1486 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1487 FileName(owner_->filePath()));
1488 if (tmpf.isFileEmpty())
1491 f = tmpf.absFileName();
1496 bool SVN::prepareFileRevisionEnabled()
1503 bool SVN::toggleReadOnlyEnabled()