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);
101 bool VCS::checkparentdirs(FileName const & file, std::string const & pathname)
103 FileName dirname = FileName(file.absFileName()).onlyPath();
104 FileName tocheck = FileName(addName(dirname.absFileName(),pathname));
105 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
106 bool result = tocheck.exists();
107 while ( !result && !dirname.empty() ) {
108 dirname = dirname.parentPath();
109 LYXERR(Debug::LYXVC, "check directory: " << dirname.absFileName());
110 tocheck = FileName(addName(dirname.absFileName(),pathname));
111 result = tocheck.exists();
117 /////////////////////////////////////////////////////////////////////
121 /////////////////////////////////////////////////////////////////////
123 RCS::RCS(FileName const & m)
130 FileName const RCS::findFile(FileName const & file)
132 // Check if *,v exists.
133 FileName tmp(file.absFileName() + ",v");
134 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
135 if (tmp.isReadableFile()) {
136 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
140 // Check if RCS/*,v exists.
141 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
142 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
143 if (tmp.isReadableFile()) {
144 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
152 void RCS::retrieve(FileName const & file)
154 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
155 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
160 void RCS::scanMaster()
165 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
167 ifstream ifs(master_.toFilesystemEncoding().c_str());
170 bool read_enough = false;
172 while (!read_enough && ifs >> token) {
173 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
178 else if (token == "head") {
182 tmv = rtrim(tmv, ";");
184 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
185 } else if (contains(token, "access")
186 || contains(token, "symbols")
187 || contains(token, "strict")) {
189 } else if (contains(token, "locks")) {
191 if (contains(token, ';')) {
192 locker_ = "Unlocked";
201 s1 = rtrim(tmpt, ";");
202 // tmp is now in the format <user>:<version>
203 s1 = split(s1, s2, ':');
204 // s2 is user, and s1 is version
205 if (s1 == version_) {
210 } while (!contains(tmpt, ';'));
212 } else if (token == "comment") {
213 // we don't need to read any further than this.
217 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
223 void RCS::registrer(string const & msg)
225 string cmd = "ci -q -u -i -t-\"";
228 cmd += quoteName(onlyFileName(owner_->absFileName()));
229 doVCCommand(cmd, FileName(owner_->filePath()));
233 string RCS::checkIn(string const & msg)
235 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
236 + quoteName(onlyFileName(owner_->absFileName())),
237 FileName(owner_->filePath()));
238 return ret ? string() : "RCS: Proceeded";
242 bool RCS::checkInEnabled()
244 return owner_ && !owner_->isReadonly();
248 bool RCS::isCheckInWithConfirmation()
250 // FIXME one day common getDiff for all backends
252 // if (getDiff(file, diff) && diff.empty())
255 FileName tmpf = FileName::tempName("lyxvcout");
257 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
261 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
262 + " > " + quoteName(tmpf.toFilesystemEncoding()),
263 FileName(owner_->filePath()));
265 docstring diff = tmpf.fileContents("UTF-8");
275 string RCS::checkOut()
278 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
279 FileName(owner_->filePath()));
280 return ret ? string() : "RCS: Proceeded";
284 bool RCS::checkOutEnabled()
286 return owner_ && owner_->isReadonly();
290 string RCS::repoUpdate()
292 lyxerr << "Sorry, not implemented." << endl;
297 bool RCS::repoUpdateEnabled()
303 string RCS::lockingToggle()
305 //FIXME this might be actually possible, study rcs -U, rcs -L.
306 //State should be easy to get inside scanMaster.
307 //It would fix #4370 and make rcs/svn usage even more closer.
308 lyxerr << "Sorry, not implemented." << endl;
313 bool RCS::lockingToggleEnabled()
321 if (doVCCommand("co -f -u" + version_ + " "
322 + quoteName(onlyFileName(owner_->absFileName())),
323 FileName(owner_->filePath())))
325 // We ignore changes and just reload!
331 bool RCS::isRevertWithConfirmation()
333 //FIXME owner && diff ?
340 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
341 doVCCommand("rcs -o" + version_ + " "
342 + quoteName(onlyFileName(owner_->absFileName())),
343 FileName(owner_->filePath()));
347 bool RCS::undoLastEnabled()
353 void RCS::getLog(FileName const & tmpf)
355 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
356 + " > " + quoteName(tmpf.toFilesystemEncoding()),
357 FileName(owner_->filePath()));
361 bool RCS::toggleReadOnlyEnabled()
363 // This got broken somewhere along lfuns dispatch reorganization.
364 // reloadBuffer would be needed after this, but thats problematic
365 // since we are inside Buffer::dispatch.
371 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
373 if (info == LyXVC::File)
375 // fill the rest of the attributes for a single file
376 if (rev_date_cache_.empty())
377 if (!getRevisionInfo())
382 return rev_author_cache_;
384 return rev_date_cache_;
386 return rev_time_cache_;
394 bool RCS::getRevisionInfo()
396 FileName tmpf = FileName::tempName("lyxvcout");
398 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
401 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
402 + " > " + quoteName(tmpf.toFilesystemEncoding()),
403 FileName(owner_->filePath()));
408 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
411 // we reached to the entry, i.e. after initial log message
413 // line with critical info, e.g:
414 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
419 LYXERR(Debug::LYXVC, line);
420 if (entry && prefixIs(line, "date:")) {
424 if (prefixIs(line, "revision"))
430 rev_date_cache_ = token(result, ' ', 1);
431 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
432 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
434 return !rev_author_cache_.empty();
437 bool RCS::prepareFileRevision(string const &revis, string & f)
440 if (!VCS::makeRCSRevision(version_, rev))
443 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
445 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
449 doVCCommand("co -p" + rev + " "
450 + quoteName(onlyFileName(owner_->absFileName()))
451 + " > " + quoteName(tmpf.toFilesystemEncoding()),
452 FileName(owner_->filePath()));
453 if (tmpf.isFileEmpty())
456 f = tmpf.absFileName();
461 bool RCS::prepareFileRevisionEnabled()
467 /////////////////////////////////////////////////////////////////////
471 /////////////////////////////////////////////////////////////////////
473 CVS::CVS(FileName const & m, FileName const & f)
477 have_rev_info_ = false;
482 FileName const CVS::findFile(FileName const & file)
484 // First we look for the CVS/Entries in the same dir
485 // where we have file.
486 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
487 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
488 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
489 << "' for `" << tmpf << '\'');
490 if (entries.isReadableFile()) {
491 // Ok we are at least in a CVS dir. Parse the CVS/Entries
492 // and see if we can find this file. We do a fast and
494 ifstream ifs(entries.toFilesystemEncoding().c_str());
496 while (getline(ifs, line)) {
497 LYXERR(Debug::LYXVC, "\tEntries: " << line);
498 if (contains(line, tmpf))
506 void CVS::scanMaster()
508 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
509 // Ok now we do the real scan...
510 ifstream ifs(master_.toFilesystemEncoding().c_str());
511 string name = onlyFileName(file_.absFileName());
512 string tmpf = '/' + name + '/';
513 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
515 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
516 while (getline(ifs, line)) {
517 LYXERR(Debug::LYXVC, "\t line: " << line);
518 if (contains(line, tmpf)) {
519 // Ok extract the fields.
522 regex_match(line, sm, reg);
524 //sm[0]; // whole matched string
526 version_ = sm.str(2);
527 string const file_date = sm.str(3);
530 //sm[5]; // tag or tagdate
531 if (file_.isReadableFile()) {
532 time_t mod = file_.lastModified();
533 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
534 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
535 << "'\nModification date of file: `" << mod_date << '\'');
536 if (file_.isReadOnly()) {
537 // readonly checkout is unlocked
540 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
541 FileName base(addName(bdir.absFileName(),name));
542 // if base version is existent "cvs edit" was used to lock
543 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
546 vcstatus = NOLOCKING;
554 string const CVS::getTarget(OperationMode opmode) const
558 // in client server mode CVS does not like full path operand for directory operation
559 // since LyX switches to the repo dir "." is good enough as target
562 return quoteName(onlyFileName(owner_->absFileName()));
568 docstring CVS::toString(CvsStatus status) const
572 return _("Up-to-date");
573 case LocallyModified:
574 return _("Locally Modified");
576 return _("Locally Added");
578 return _("Needs Merge");
580 return _("Needs Checkout");
582 return _("No CVS file");
584 return _("Cannot retrieve CVS status");
590 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
591 FileName const & output, bool reportError)
593 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
594 return doVCCommand(cmd + redirection, path, reportError);
598 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
599 support::FileName const & path,
600 support::FileName const & output)
602 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
603 return doVCCommandCall(cmd + redirection, path);
607 CVS::CvsStatus CVS::getStatus()
609 FileName tmpf = FileName::tempName("lyxvcout");
611 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
615 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
616 FileName(owner_->filePath()), tmpf)) {
621 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
622 CvsStatus status = NoCvsFile;
627 LYXERR(Debug::LYXVC, line << "\n");
628 if (prefixIs(line, "File:")) {
629 if (contains(line, "Up-to-date"))
631 else if (contains(line, "Locally Modified"))
632 status = LocallyModified;
633 else if (contains(line, "Locally Added"))
634 status = LocallyAdded;
635 else if (contains(line, "Needs Merge"))
637 else if (contains(line, "Needs Checkout"))
638 status = NeedsCheckout;
645 void CVS::getRevisionInfo()
649 have_rev_info_ = true;
650 FileName tmpf = FileName::tempName("lyxvcout");
652 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
656 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
657 + " " + getTarget(File),
658 FileName(owner_->filePath()), tmpf);
661 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
665 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
666 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
671 LYXERR(Debug::LYXVC, line << "\n");
672 if (prefixIs(line, "date:")) {
674 regex_match(line, sm, reg);
675 //sm[0]; // whole matched string
676 rev_date_cache_ = sm[1];
677 rev_time_cache_ = sm[2];
678 //sm[3]; // GMT offset
679 rev_author_cache_ = sm[4];
684 if (rev_author_cache_.empty())
686 "Could not retrieve revision info for " << version_ <<
687 " of " << getTarget(File));
691 void CVS::registrer(string const & msg)
693 doVCCommand("cvs -q add -m \"" + msg + "\" "
695 FileName(owner_->filePath()));
699 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
701 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
702 FileName(owner_->filePath()), tmpf, false);
709 return doVCCommand("cvs -q edit " + getTarget(File),
710 FileName(owner_->filePath()));
717 return doVCCommand("cvs -q unedit " + getTarget(File),
718 FileName(owner_->filePath()));
722 int CVS::update(OperationMode opmode, FileName const & tmpf)
724 return doVCCommandWithOutput("cvs -q update "
726 FileName(owner_->filePath()), tmpf, false);
730 string CVS::scanLogFile(FileName const & f, string & status)
732 ifstream ifs(f.toFilesystemEncoding().c_str());
737 LYXERR(Debug::LYXVC, line << "\n");
739 status += line + "; ";
740 if (prefixIs(line, "C ")) {
750 string CVS::checkIn(string const & msg)
752 CvsStatus status = getStatus();
755 if (vcstatus != NOLOCKING)
757 return "CVS: Proceeded";
758 case LocallyModified:
760 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
762 FileName(owner_->filePath()));
763 return rc ? string() : "CVS: Proceeded";
767 frontend::Alert::error(_("Revision control error."),
768 _("The repository version is newer then the current check out.\n"
769 "You have to update from repository first or revert your changes.")) ;
772 frontend::Alert::error(_("Revision control error."),
773 bformat(_("Bad status when checking in changes.\n"
782 bool CVS::isLocked() const
784 FileName fn(owner_->absFileName());
786 return !fn.isReadOnly();
790 bool CVS::checkInEnabled()
792 if (vcstatus != NOLOCKING)
799 bool CVS::isCheckInWithConfirmation()
801 CvsStatus status = getStatus();
802 return status == LocallyModified || status == LocallyAdded;
806 string CVS::checkOut()
808 if (vcstatus != NOLOCKING && edit())
810 FileName tmpf = FileName::tempName("lyxvcout");
812 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
816 int rc = update(File, tmpf);
818 string const res = scanLogFile(tmpf, log);
820 frontend::Alert::error(_("Revision control error."),
821 bformat(_("Error when updating from repository.\n"
822 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
823 "After pressing OK, LyX will try to reopen the resolved document."),
824 from_local8bit(res)));
829 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
833 bool CVS::checkOutEnabled()
835 if (vcstatus != NOLOCKING)
842 string CVS::repoUpdate()
844 FileName tmpf = FileName::tempName("lyxvcout");
846 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
850 getDiff(Directory, tmpf);
851 docstring res = tmpf.fileContents("UTF-8");
853 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
854 docstring const file = from_utf8(owner_->filePath());
855 docstring text = bformat(_("There were detected changes "
856 "in the working directory:\n%1$s\n\n"
857 "Possible file conflicts must be then resolved manually "
858 "or you will need to revert back to the repository version."), file);
859 int ret = frontend::Alert::prompt(_("Changes detected"),
860 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
862 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
863 ret = frontend::Alert::prompt(_("Changes detected"),
864 text, 0, 1, _("&Continue"), _("&Abort"));
865 hideDialogs("file", 0);
873 int rc = update(Directory, tmpf);
874 res += "Update log:\n" + tmpf.fileContents("UTF-8");
875 LYXERR(Debug::LYXVC, res);
878 string sres = scanLogFile(tmpf, log);
880 docstring const file = owner_->fileName().displayName(20);
881 frontend::Alert::error(_("Revision control error."),
882 bformat(_("Error when updating document %1$s from repository.\n"
883 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
884 "After pressing OK, LyX will try to reopen the resolved document."),
885 file, from_local8bit(sres)));
891 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
895 bool CVS::repoUpdateEnabled()
901 string CVS::lockingToggle()
903 lyxerr << "Sorry, not implemented." << endl;
908 bool CVS::lockingToggleEnabled()
914 bool CVS::isRevertWithConfirmation()
916 CvsStatus status = getStatus();
917 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
923 // Reverts to the version in CVS repository and
924 // gets the updated version from the repository.
925 CvsStatus status = getStatus();
928 if (vcstatus != NOLOCKING)
929 return 0 == unedit();
933 case LocallyModified: {
934 FileName f(owner_->absFileName());
936 update(File, FileName());
941 docstring const file = owner_->fileName().displayName(20);
942 frontend::Alert::error(_("Revision control error."),
943 bformat(_("The document %1$s is not in repository.\n"
944 "You have to check in the first revision before you can revert."),
949 docstring const file = owner_->fileName().displayName(20);
950 frontend::Alert::error(_("Revision control error."),
951 bformat(_("Cannot revert document %1$s to repository version.\n"
952 "The status '%2$s' is unexpected."),
953 file, toString(status)));
963 // merge the current with the previous version
964 // in a reverse patch kind of way, so that the
965 // result is to revert the last changes.
966 lyxerr << "Sorry, not implemented." << endl;
970 bool CVS::undoLastEnabled()
976 void CVS::getLog(FileName const & tmpf)
978 doVCCommandWithOutput("cvs log " + getTarget(File),
979 FileName(owner_->filePath()),
984 bool CVS::toggleReadOnlyEnabled()
990 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
992 if (!version_.empty()) {
998 return rev_author_cache_;
1000 return rev_date_cache_;
1002 return rev_time_cache_;
1010 bool CVS::prepareFileRevision(string const & revis, string & f)
1013 if (!VCS::makeRCSRevision(version_, rev))
1016 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
1018 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1022 doVCCommandWithOutput("cvs update -p -r" + rev + " "
1024 FileName(owner_->filePath()), tmpf);
1025 if (tmpf.isFileEmpty())
1028 f = tmpf.absFileName();
1033 bool CVS::prepareFileRevisionEnabled()
1039 /////////////////////////////////////////////////////////////////////
1043 /////////////////////////////////////////////////////////////////////
1045 SVN::SVN(FileName const & m, FileName const & f)
1055 FileName const SVN::findFile(FileName const & file)
1057 // First we check the existence of repository meta data.
1058 if (!VCS::checkparentdirs(file, ".svn")) {
1059 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1063 // Now we check the status of the file.
1064 FileName tmpf = FileName::tempName("lyxvcout");
1066 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1070 string const fname = onlyFileName(file.absFileName());
1071 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1072 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1073 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1075 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1076 return found ? file : FileName();
1080 void SVN::scanMaster()
1082 // vcstatus code is somewhat superflous, until we want
1083 // to implement read-only toggle for svn.
1084 vcstatus = NOLOCKING;
1085 if (checkLockMode()) {
1089 vcstatus = UNLOCKED;
1095 bool SVN::checkLockMode()
1097 FileName tmpf = FileName::tempName("lyxvcout");
1099 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1103 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1104 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
1105 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1109 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1113 while (ifs && !ret) {
1115 LYXERR(Debug::LYXVC, line);
1116 if (contains(line, "svn:needs-lock"))
1119 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1127 bool SVN::isLocked() const
1130 return !file_.isReadOnly();
1134 void SVN::registrer(string const & /*msg*/)
1136 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1137 FileName(owner_->filePath()));
1141 string SVN::checkIn(string const & msg)
1143 FileName tmpf = FileName::tempName("lyxvcout");
1145 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1146 return N_("Error: Could not generate logfile.");
1149 doVCCommand("svn commit -m \"" + msg + "\" "
1150 + quoteName(onlyFileName(owner_->absFileName()))
1151 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1152 FileName(owner_->filePath()));
1155 string res = scanLogFile(tmpf, log);
1157 frontend::Alert::error(_("Revision control error."),
1158 _("Error when committing to repository.\n"
1159 "You have to manually resolve the problem.\n"
1160 "LyX will reopen the document after you press OK."));
1162 fileLock(false, tmpf, log);
1165 return log.empty() ? string() : "SVN: " + log;
1169 bool SVN::checkInEnabled()
1178 bool SVN::isCheckInWithConfirmation()
1180 // FIXME one day common getDiff and perhaps OpMode for all backends
1182 FileName tmpf = FileName::tempName("lyxvcout");
1184 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1188 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1189 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1190 FileName(owner_->filePath()));
1192 docstring diff = tmpf.fileContents("UTF-8");
1202 // FIXME Correctly return code should be checked instead of this.
1203 // This would need another solution than just plain startscript.
1204 // Hint from Andre': QProcess::readAllStandardError()...
1205 string SVN::scanLogFile(FileName const & f, string & status)
1207 ifstream ifs(f.toFilesystemEncoding().c_str());
1212 LYXERR(Debug::LYXVC, line << "\n");
1214 status += line + "; ";
1215 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1216 || contains(line, "Commit failed")) {
1220 if (contains(line, "svn:needs-lock")) {
1230 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1232 if (!locked_mode_ || (isLocked() == lock))
1235 string const arg = lock ? "lock " : "unlock ";
1236 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1237 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1238 FileName(owner_->filePath()));
1240 // Lock error messages go unfortunately on stderr and are unreachible this way.
1241 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1245 if (!line.empty()) status += line + "; ";
1249 if (!isLocked() && lock)
1250 frontend::Alert::error(_("Revision control error."),
1251 _("Error while acquiring write lock.\n"
1252 "Another user is most probably editing\n"
1253 "the current document now!\n"
1254 "Also check the access to the repository."));
1255 if (isLocked() && !lock)
1256 frontend::Alert::error(_("Revision control error."),
1257 _("Error while releasing write lock.\n"
1258 "Check the access to the repository."));
1262 string SVN::checkOut()
1264 FileName tmpf = FileName::tempName("lyxvcout");
1266 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1267 return N_("Error: Could not generate logfile.");
1270 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1271 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1272 FileName(owner_->filePath()));
1275 string const res = scanLogFile(tmpf, log);
1277 frontend::Alert::error(_("Revision control error."),
1278 bformat(_("Error when updating from repository.\n"
1279 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1280 "After pressing OK, LyX will try to reopen the resolved document."),
1281 from_local8bit(res)));
1283 fileLock(true, tmpf, log);
1286 return log.empty() ? string() : "SVN: " + log;
1290 bool SVN::checkOutEnabled()
1299 string SVN::repoUpdate()
1301 FileName tmpf = FileName::tempName("lyxvcout");
1303 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1304 return N_("Error: Could not generate logfile.");
1307 doVCCommand("svn diff " + quoteName(owner_->filePath())
1308 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1309 FileName(owner_->filePath()));
1310 docstring res = tmpf.fileContents("UTF-8");
1312 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1313 docstring const file = from_utf8(owner_->filePath());
1314 docstring text = bformat(_("There were detected changes "
1315 "in the working directory:\n%1$s\n\n"
1316 "In case of file conflict version of the local directory files "
1317 "will be preferred."
1318 "\n\nContinue?"), file);
1319 int ret = frontend::Alert::prompt(_("Changes detected"),
1320 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1322 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1323 ret = frontend::Alert::prompt(_("Changes detected"),
1324 text, 0, 1, _("&Yes"), _("&No"));
1325 hideDialogs("file", 0);
1333 // Reverting looks too harsh, see bug #6255.
1334 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1335 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1336 // FileName(owner_->filePath()));
1337 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1338 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1339 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1340 FileName(owner_->filePath()));
1341 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1343 LYXERR(Debug::LYXVC, res);
1345 return to_utf8(res);
1349 bool SVN::repoUpdateEnabled()
1355 string SVN::lockingToggle()
1357 FileName tmpf = FileName::tempName("lyxvcout");
1359 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1360 return N_("Error: Could not generate logfile.");
1363 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1364 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1365 FileName(owner_->filePath()));
1370 string res = scanLogFile(tmpf, log);
1371 bool locking = contains(res, "svn:needs-lock");
1373 ret = doVCCommand("svn propset svn:needs-lock ON "
1374 + quoteName(onlyFileName(owner_->absFileName()))
1375 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1376 FileName(owner_->filePath()));
1378 ret = doVCCommand("svn propdel svn:needs-lock "
1379 + quoteName(onlyFileName(owner_->absFileName()))
1380 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1381 FileName(owner_->filePath()));
1386 frontend::Alert::warning(_("VCN File Locking"),
1387 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1388 + _("Do not forget to commit the locking property into the repository."),
1391 return string("SVN: ") + N_("Locking property set.");
1395 bool SVN::lockingToggleEnabled()
1403 // Reverts to the version in SVN repository and
1404 // gets the updated version from the repository.
1405 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1407 if (doVCCommand("svn revert -q " + fil,
1408 FileName(owner_->filePath())))
1410 owner_->markClean();
1415 bool SVN::isRevertWithConfirmation()
1417 //FIXME owner && diff
1422 void SVN::undoLast()
1424 // merge the current with the previous version
1425 // in a reverse patch kind of way, so that the
1426 // result is to revert the last changes.
1427 lyxerr << "Sorry, not implemented." << endl;
1431 bool SVN::undoLastEnabled()
1437 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1439 if (info == LyXVC::Tree) {
1440 if (rev_tree_cache_.empty())
1441 if (!getTreeRevisionInfo())
1442 rev_tree_cache_ = "?";
1443 if (rev_tree_cache_ == "?")
1446 return rev_tree_cache_;
1449 // fill the rest of the attributes for a single file
1450 if (rev_file_cache_.empty())
1451 if (!getFileRevisionInfo())
1452 rev_file_cache_ = "?";
1456 if (rev_file_cache_ == "?")
1458 return rev_file_cache_;
1460 return rev_author_cache_;
1462 return rev_date_cache_;
1464 return rev_time_cache_;
1473 bool SVN::getFileRevisionInfo()
1475 FileName tmpf = FileName::tempName("lyxvcout");
1477 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1481 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1482 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1483 FileName(owner_->filePath()));
1488 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1496 LYXERR(Debug::LYXVC, line);
1497 if (prefixIs(line, "<commit"))
1499 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1500 string l1 = subst(line, "revision=\"", "");
1501 string l2 = trim(subst(l1, "\">", ""));
1503 rev_file_cache_ = rev = l2;
1505 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1506 string l1 = subst(line, "<author>", "");
1507 string l2 = subst(l1, "</author>", "");
1508 rev_author_cache_ = l2;
1510 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1511 string l1 = subst(line, "<date>", "");
1512 string l2 = subst(l1, "</date>", "");
1513 l2 = split(l2, l1, 'T');
1514 rev_date_cache_ = l1;
1515 l2 = split(l2, l1, '.');
1516 rev_time_cache_ = l1;
1522 return !rev.empty();
1526 bool SVN::getTreeRevisionInfo()
1528 FileName tmpf = FileName::tempName("lyxvcout");
1530 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1534 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1535 FileName(owner_->filePath()));
1540 // only first line in case something bad happens.
1541 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1547 rev_tree_cache_ = line;
1548 return !line.empty();
1552 void SVN::getLog(FileName const & tmpf)
1554 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1555 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1556 FileName(owner_->filePath()));
1560 bool SVN::prepareFileRevision(string const & revis, string & f)
1562 if (!isStrInt(revis))
1565 int rev = convert<int>(revis);
1567 if (!getFileRevisionInfo())
1570 rev = convert<int>(rev_file_cache_);
1571 // go back for minus rev
1573 rev = rev + convert<int>(rev_file_cache_);
1578 string revname = convert<string>(rev);
1579 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1581 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1585 doVCCommand("svn cat -r " + revname + " "
1586 + quoteName(onlyFileName(owner_->absFileName()))
1587 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1588 FileName(owner_->filePath()));
1589 if (tmpf.isFileEmpty())
1592 f = tmpf.absFileName();
1597 bool SVN::prepareFileRevisionEnabled()
1604 bool SVN::toggleReadOnlyEnabled()