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 = file.onlyPath();
104 FileName tocheck = FileName(addName(dirname.absFileName(),pathname));
105 bool result = tocheck.exists();
106 while ( !result && !dirname.empty() ) {
107 dirname = dirname.parentPath();
108 tocheck = FileName(addName(dirname.absFileName(),pathname));
109 result = tocheck.exists();
115 /////////////////////////////////////////////////////////////////////
119 /////////////////////////////////////////////////////////////////////
121 RCS::RCS(FileName const & m)
128 FileName const RCS::findFile(FileName const & file)
130 // Check if *,v exists.
131 FileName tmp(file.absFileName() + ",v");
132 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
133 if (tmp.isReadableFile()) {
134 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
138 // Check if RCS/*,v exists.
139 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
140 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
141 if (tmp.isReadableFile()) {
142 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
150 void RCS::retrieve(FileName const & file)
152 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
153 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
158 void RCS::scanMaster()
163 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
165 ifstream ifs(master_.toFilesystemEncoding().c_str());
168 bool read_enough = false;
170 while (!read_enough && ifs >> token) {
171 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
176 else if (token == "head") {
180 tmv = rtrim(tmv, ";");
182 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
183 } else if (contains(token, "access")
184 || contains(token, "symbols")
185 || contains(token, "strict")) {
187 } else if (contains(token, "locks")) {
189 if (contains(token, ';')) {
190 locker_ = "Unlocked";
199 s1 = rtrim(tmpt, ";");
200 // tmp is now in the format <user>:<version>
201 s1 = split(s1, s2, ':');
202 // s2 is user, and s1 is version
203 if (s1 == version_) {
208 } while (!contains(tmpt, ';'));
210 } else if (token == "comment") {
211 // we don't need to read any further than this.
215 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
221 void RCS::registrer(string const & msg)
223 string cmd = "ci -q -u -i -t-\"";
226 cmd += quoteName(onlyFileName(owner_->absFileName()));
227 doVCCommand(cmd, FileName(owner_->filePath()));
231 string RCS::checkIn(string const & msg)
233 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
234 + quoteName(onlyFileName(owner_->absFileName())),
235 FileName(owner_->filePath()));
236 return ret ? string() : "RCS: Proceeded";
240 bool RCS::checkInEnabled()
242 return owner_ && !owner_->isReadonly();
246 bool RCS::isCheckInWithConfirmation()
248 // FIXME one day common getDiff for all backends
250 // if (getDiff(file, diff) && diff.empty())
253 FileName tmpf = FileName::tempName("lyxvcout");
255 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
259 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
260 + " > " + quoteName(tmpf.toFilesystemEncoding()),
261 FileName(owner_->filePath()));
263 docstring diff = tmpf.fileContents("UTF-8");
273 string RCS::checkOut()
276 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
277 FileName(owner_->filePath()));
278 return ret ? string() : "RCS: Proceeded";
282 bool RCS::checkOutEnabled()
284 return owner_ && owner_->isReadonly();
288 string RCS::repoUpdate()
290 lyxerr << "Sorry, not implemented." << endl;
295 bool RCS::repoUpdateEnabled()
301 string RCS::lockingToggle()
303 //FIXME this might be actually possible, study rcs -U, rcs -L.
304 //State should be easy to get inside scanMaster.
305 //It would fix #4370 and make rcs/svn usage even more closer.
306 lyxerr << "Sorry, not implemented." << endl;
311 bool RCS::lockingToggleEnabled()
319 if (doVCCommand("co -f -u" + version_ + " "
320 + quoteName(onlyFileName(owner_->absFileName())),
321 FileName(owner_->filePath())))
323 // We ignore changes and just reload!
329 bool RCS::isRevertWithConfirmation()
331 //FIXME owner && diff ?
338 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
339 doVCCommand("rcs -o" + version_ + " "
340 + quoteName(onlyFileName(owner_->absFileName())),
341 FileName(owner_->filePath()));
345 bool RCS::undoLastEnabled()
351 void RCS::getLog(FileName const & tmpf)
353 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
354 + " > " + quoteName(tmpf.toFilesystemEncoding()),
355 FileName(owner_->filePath()));
359 bool RCS::toggleReadOnlyEnabled()
361 // This got broken somewhere along lfuns dispatch reorganization.
362 // reloadBuffer would be needed after this, but thats problematic
363 // since we are inside Buffer::dispatch.
369 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
371 if (info == LyXVC::File)
373 // fill the rest of the attributes for a single file
374 if (rev_date_cache_.empty())
375 if (!getRevisionInfo())
380 return rev_author_cache_;
382 return rev_date_cache_;
384 return rev_time_cache_;
392 bool RCS::getRevisionInfo()
394 FileName tmpf = FileName::tempName("lyxvcout");
396 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
399 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
400 + " > " + quoteName(tmpf.toFilesystemEncoding()),
401 FileName(owner_->filePath()));
406 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
409 // we reached to the entry, i.e. after initial log message
411 // line with critical info, e.g:
412 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
417 LYXERR(Debug::LYXVC, line);
418 if (entry && prefixIs(line, "date:")) {
422 if (prefixIs(line, "revision"))
428 rev_date_cache_ = token(result, ' ', 1);
429 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
430 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
432 return !rev_author_cache_.empty();
435 bool RCS::prepareFileRevision(string const &revis, string & f)
438 if (!VCS::makeRCSRevision(version_, rev))
441 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
443 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
447 doVCCommand("co -p" + rev + " "
448 + quoteName(onlyFileName(owner_->absFileName()))
449 + " > " + quoteName(tmpf.toFilesystemEncoding()),
450 FileName(owner_->filePath()));
451 if (tmpf.isFileEmpty())
454 f = tmpf.absFileName();
459 bool RCS::prepareFileRevisionEnabled()
465 /////////////////////////////////////////////////////////////////////
469 /////////////////////////////////////////////////////////////////////
471 CVS::CVS(FileName const & m, FileName const & f)
475 have_rev_info_ = false;
480 FileName const CVS::findFile(FileName const & file)
482 // First we look for the CVS/Entries in the same dir
483 // where we have file.
484 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
485 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
486 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
487 << "' for `" << tmpf << '\'');
488 if (entries.isReadableFile()) {
489 // Ok we are at least in a CVS dir. Parse the CVS/Entries
490 // and see if we can find this file. We do a fast and
492 ifstream ifs(entries.toFilesystemEncoding().c_str());
494 while (getline(ifs, line)) {
495 LYXERR(Debug::LYXVC, "\tEntries: " << line);
496 if (contains(line, tmpf))
504 void CVS::scanMaster()
506 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
507 // Ok now we do the real scan...
508 ifstream ifs(master_.toFilesystemEncoding().c_str());
509 string name = onlyFileName(file_.absFileName());
510 string tmpf = '/' + name + '/';
511 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
513 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
514 while (getline(ifs, line)) {
515 LYXERR(Debug::LYXVC, "\t line: " << line);
516 if (contains(line, tmpf)) {
517 // Ok extract the fields.
520 regex_match(line, sm, reg);
522 //sm[0]; // whole matched string
524 version_ = sm.str(2);
525 string const file_date = sm.str(3);
528 //sm[5]; // tag or tagdate
529 if (file_.isReadableFile()) {
530 time_t mod = file_.lastModified();
531 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
532 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
533 << "'\nModification date of file: `" << mod_date << '\'');
534 if (file_.isReadOnly()) {
535 // readonly checkout is unlocked
538 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
539 FileName base(addName(bdir.absFileName(),name));
540 // if base version is existent "cvs edit" was used to lock
541 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
544 vcstatus = NOLOCKING;
552 string const CVS::getTarget(OperationMode opmode) const
556 // in client server mode CVS does not like full path operand for directory operation
557 // since LyX switches to the repo dir "." is good enough as target
560 return quoteName(onlyFileName(owner_->absFileName()));
566 docstring CVS::toString(CvsStatus status) const
570 return _("Up-to-date");
571 case LocallyModified:
572 return _("Locally Modified");
574 return _("Locally Added");
576 return _("Needs Merge");
578 return _("Needs Checkout");
580 return _("No CVS file");
582 return _("Cannot retrieve CVS status");
588 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
589 FileName const & output, bool reportError)
591 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
592 return doVCCommand(cmd + redirection, path, reportError);
596 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
597 support::FileName const & path,
598 support::FileName const & output)
600 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
601 return doVCCommandCall(cmd + redirection, path);
605 CVS::CvsStatus CVS::getStatus()
607 FileName tmpf = FileName::tempName("lyxvcout");
609 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
613 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
614 FileName(owner_->filePath()), tmpf)) {
619 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
620 CvsStatus status = NoCvsFile;
625 LYXERR(Debug::LYXVC, line << "\n");
626 if (prefixIs(line, "File:")) {
627 if (contains(line, "Up-to-date"))
629 else if (contains(line, "Locally Modified"))
630 status = LocallyModified;
631 else if (contains(line, "Locally Added"))
632 status = LocallyAdded;
633 else if (contains(line, "Needs Merge"))
635 else if (contains(line, "Needs Checkout"))
636 status = NeedsCheckout;
643 void CVS::getRevisionInfo()
647 have_rev_info_ = true;
648 FileName tmpf = FileName::tempName("lyxvcout");
650 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
654 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
655 + " " + getTarget(File),
656 FileName(owner_->filePath()), tmpf);
659 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
663 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
664 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
669 LYXERR(Debug::LYXVC, line << "\n");
670 if (prefixIs(line, "date:")) {
672 regex_match(line, sm, reg);
673 //sm[0]; // whole matched string
674 rev_date_cache_ = sm[1];
675 rev_time_cache_ = sm[2];
676 //sm[3]; // GMT offset
677 rev_author_cache_ = sm[4];
682 if (rev_author_cache_.empty())
684 "Could not retrieve revision info for " << version_ <<
685 " of " << getTarget(File));
689 void CVS::registrer(string const & msg)
691 doVCCommand("cvs -q add -m \"" + msg + "\" "
693 FileName(owner_->filePath()));
697 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
699 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
700 FileName(owner_->filePath()), tmpf, false);
707 return doVCCommand("cvs -q edit " + getTarget(File),
708 FileName(owner_->filePath()));
715 return doVCCommand("cvs -q unedit " + getTarget(File),
716 FileName(owner_->filePath()));
720 int CVS::update(OperationMode opmode, FileName const & tmpf)
722 return doVCCommandWithOutput("cvs -q update "
724 FileName(owner_->filePath()), tmpf, false);
728 string CVS::scanLogFile(FileName const & f, string & status)
730 ifstream ifs(f.toFilesystemEncoding().c_str());
735 LYXERR(Debug::LYXVC, line << "\n");
737 status += line + "; ";
738 if (prefixIs(line, "C ")) {
748 string CVS::checkIn(string const & msg)
750 CvsStatus status = getStatus();
753 if (vcstatus != NOLOCKING)
755 return "CVS: Proceeded";
756 case LocallyModified:
758 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
760 FileName(owner_->filePath()));
761 return rc ? string() : "CVS: Proceeded";
765 frontend::Alert::error(_("Revision control error."),
766 _("The repository version is newer then the current check out.\n"
767 "You have to update from repository first or revert your changes.")) ;
770 frontend::Alert::error(_("Revision control error."),
771 bformat(_("Bad status when checking in changes.\n"
780 bool CVS::isLocked() const
782 FileName fn(owner_->absFileName());
784 return !fn.isReadOnly();
788 bool CVS::checkInEnabled()
790 if (vcstatus != NOLOCKING)
797 bool CVS::isCheckInWithConfirmation()
799 CvsStatus status = getStatus();
800 return status == LocallyModified || status == LocallyAdded;
804 string CVS::checkOut()
806 if (vcstatus != NOLOCKING && edit())
808 FileName tmpf = FileName::tempName("lyxvcout");
810 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
814 int rc = update(File, tmpf);
816 string const res = scanLogFile(tmpf, log);
818 frontend::Alert::error(_("Revision control error."),
819 bformat(_("Error when updating from repository.\n"
820 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
821 "After pressing OK, LyX will try to reopen the resolved document."),
822 from_local8bit(res)));
827 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
831 bool CVS::checkOutEnabled()
833 if (vcstatus != NOLOCKING)
840 string CVS::repoUpdate()
842 FileName tmpf = FileName::tempName("lyxvcout");
844 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
848 getDiff(Directory, tmpf);
849 docstring res = tmpf.fileContents("UTF-8");
851 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
852 docstring const file = from_utf8(owner_->filePath());
853 docstring text = bformat(_("There were detected changes "
854 "in the working directory:\n%1$s\n\n"
855 "Possible file conflicts must be then resolved manually "
856 "or you will need to revert back to the repository version."), file);
857 int ret = frontend::Alert::prompt(_("Changes detected"),
858 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
860 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
861 ret = frontend::Alert::prompt(_("Changes detected"),
862 text, 0, 1, _("&Continue"), _("&Abort"));
863 hideDialogs("file", 0);
871 int rc = update(Directory, tmpf);
872 res += "Update log:\n" + tmpf.fileContents("UTF-8");
873 LYXERR(Debug::LYXVC, res);
876 string sres = scanLogFile(tmpf, log);
878 docstring const file = owner_->fileName().displayName(20);
879 frontend::Alert::error(_("Revision control error."),
880 bformat(_("Error when updating document %1$s from repository.\n"
881 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
882 "After pressing OK, LyX will try to reopen the resolved document."),
883 file, from_local8bit(sres)));
889 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
893 bool CVS::repoUpdateEnabled()
899 string CVS::lockingToggle()
901 lyxerr << "Sorry, not implemented." << endl;
906 bool CVS::lockingToggleEnabled()
912 bool CVS::isRevertWithConfirmation()
914 CvsStatus status = getStatus();
915 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
921 // Reverts to the version in CVS repository and
922 // gets the updated version from the repository.
923 CvsStatus status = getStatus();
926 if (vcstatus != NOLOCKING)
927 return 0 == unedit();
931 case LocallyModified: {
932 FileName f(owner_->absFileName());
934 update(File, FileName());
939 docstring const file = owner_->fileName().displayName(20);
940 frontend::Alert::error(_("Revision control error."),
941 bformat(_("The document %1$s is not in repository.\n"
942 "You have to check in the first revision before you can revert."),
947 docstring const file = owner_->fileName().displayName(20);
948 frontend::Alert::error(_("Revision control error."),
949 bformat(_("Cannot revert document %1$s to repository version.\n"
950 "The status '%2$s' is unexpected."),
951 file, toString(status)));
961 // merge the current with the previous version
962 // in a reverse patch kind of way, so that the
963 // result is to revert the last changes.
964 lyxerr << "Sorry, not implemented." << endl;
968 bool CVS::undoLastEnabled()
974 void CVS::getLog(FileName const & tmpf)
976 doVCCommandWithOutput("cvs log " + getTarget(File),
977 FileName(owner_->filePath()),
982 bool CVS::toggleReadOnlyEnabled()
988 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
990 if (!version_.empty()) {
996 return rev_author_cache_;
998 return rev_date_cache_;
1000 return rev_time_cache_;
1008 bool CVS::prepareFileRevision(string const & revis, string & f)
1011 if (!VCS::makeRCSRevision(version_, rev))
1014 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
1016 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1020 doVCCommandWithOutput("cvs update -p -r" + rev + " "
1022 FileName(owner_->filePath()), tmpf);
1023 if (tmpf.isFileEmpty())
1026 f = tmpf.absFileName();
1031 bool CVS::prepareFileRevisionEnabled()
1037 /////////////////////////////////////////////////////////////////////
1041 /////////////////////////////////////////////////////////////////////
1043 SVN::SVN(FileName const & m, FileName const & f)
1053 FileName const SVN::findFile(FileName const & file)
1055 // First we check the existence of repository meta data.
1056 if (!VCS::checkparentdirs(file, ".svn")) {
1057 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1061 // Now we check the status of the file.
1062 FileName tmpf = FileName::tempName("lyxvcout");
1064 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1068 string const fname = onlyFileName(file.absFileName());
1069 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1070 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1071 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1073 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1074 return found ? file : FileName();
1078 void SVN::scanMaster()
1080 // vcstatus code is somewhat superflous, until we want
1081 // to implement read-only toggle for svn.
1082 vcstatus = NOLOCKING;
1083 if (checkLockMode()) {
1087 vcstatus = UNLOCKED;
1093 bool SVN::checkLockMode()
1095 FileName tmpf = FileName::tempName("lyxvcout");
1097 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1101 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1102 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
1103 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1107 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1111 while (ifs && !ret) {
1113 LYXERR(Debug::LYXVC, line);
1114 if (contains(line, "svn:needs-lock"))
1117 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1125 bool SVN::isLocked() const
1128 return !file_.isReadOnly();
1132 void SVN::registrer(string const & /*msg*/)
1134 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1135 FileName(owner_->filePath()));
1139 string SVN::checkIn(string const & msg)
1141 FileName tmpf = FileName::tempName("lyxvcout");
1143 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1144 return N_("Error: Could not generate logfile.");
1147 doVCCommand("svn commit -m \"" + msg + "\" "
1148 + quoteName(onlyFileName(owner_->absFileName()))
1149 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1150 FileName(owner_->filePath()));
1153 string res = scanLogFile(tmpf, log);
1155 frontend::Alert::error(_("Revision control error."),
1156 _("Error when committing to repository.\n"
1157 "You have to manually resolve the problem.\n"
1158 "LyX will reopen the document after you press OK."));
1160 fileLock(false, tmpf, log);
1163 return log.empty() ? string() : "SVN: " + log;
1167 bool SVN::checkInEnabled()
1176 bool SVN::isCheckInWithConfirmation()
1178 // FIXME one day common getDiff and perhaps OpMode for all backends
1180 FileName tmpf = FileName::tempName("lyxvcout");
1182 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1186 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1187 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1188 FileName(owner_->filePath()));
1190 docstring diff = tmpf.fileContents("UTF-8");
1200 // FIXME Correctly return code should be checked instead of this.
1201 // This would need another solution than just plain startscript.
1202 // Hint from Andre': QProcess::readAllStandardError()...
1203 string SVN::scanLogFile(FileName const & f, string & status)
1205 ifstream ifs(f.toFilesystemEncoding().c_str());
1210 LYXERR(Debug::LYXVC, line << "\n");
1212 status += line + "; ";
1213 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1214 || contains(line, "Commit failed")) {
1218 if (contains(line, "svn:needs-lock")) {
1228 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1230 if (!locked_mode_ || (isLocked() == lock))
1233 string const arg = lock ? "lock " : "unlock ";
1234 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1235 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1236 FileName(owner_->filePath()));
1238 // Lock error messages go unfortunately on stderr and are unreachible this way.
1239 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1243 if (!line.empty()) status += line + "; ";
1247 if (!isLocked() && lock)
1248 frontend::Alert::error(_("Revision control error."),
1249 _("Error while acquiring write lock.\n"
1250 "Another user is most probably editing\n"
1251 "the current document now!\n"
1252 "Also check the access to the repository."));
1253 if (isLocked() && !lock)
1254 frontend::Alert::error(_("Revision control error."),
1255 _("Error while releasing write lock.\n"
1256 "Check the access to the repository."));
1260 string SVN::checkOut()
1262 FileName tmpf = FileName::tempName("lyxvcout");
1264 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1265 return N_("Error: Could not generate logfile.");
1268 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1269 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1270 FileName(owner_->filePath()));
1273 string const res = scanLogFile(tmpf, log);
1275 frontend::Alert::error(_("Revision control error."),
1276 bformat(_("Error when updating from repository.\n"
1277 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1278 "After pressing OK, LyX will try to reopen the resolved document."),
1279 from_local8bit(res)));
1281 fileLock(true, tmpf, log);
1284 return log.empty() ? string() : "SVN: " + log;
1288 bool SVN::checkOutEnabled()
1297 string SVN::repoUpdate()
1299 FileName tmpf = FileName::tempName("lyxvcout");
1301 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1302 return N_("Error: Could not generate logfile.");
1305 doVCCommand("svn diff " + quoteName(owner_->filePath())
1306 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1307 FileName(owner_->filePath()));
1308 docstring res = tmpf.fileContents("UTF-8");
1310 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1311 docstring const file = from_utf8(owner_->filePath());
1312 docstring text = bformat(_("There were detected changes "
1313 "in the working directory:\n%1$s\n\n"
1314 "In case of file conflict version of the local directory files "
1315 "will be preferred."
1316 "\n\nContinue?"), file);
1317 int ret = frontend::Alert::prompt(_("Changes detected"),
1318 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1320 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1321 ret = frontend::Alert::prompt(_("Changes detected"),
1322 text, 0, 1, _("&Yes"), _("&No"));
1323 hideDialogs("file", 0);
1331 // Reverting looks too harsh, see bug #6255.
1332 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1333 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1334 // FileName(owner_->filePath()));
1335 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1336 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1337 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1338 FileName(owner_->filePath()));
1339 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1341 LYXERR(Debug::LYXVC, res);
1343 return to_utf8(res);
1347 bool SVN::repoUpdateEnabled()
1353 string SVN::lockingToggle()
1355 FileName tmpf = FileName::tempName("lyxvcout");
1357 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1358 return N_("Error: Could not generate logfile.");
1361 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1362 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1363 FileName(owner_->filePath()));
1368 string res = scanLogFile(tmpf, log);
1369 bool locking = contains(res, "svn:needs-lock");
1371 ret = doVCCommand("svn propset svn:needs-lock ON "
1372 + quoteName(onlyFileName(owner_->absFileName()))
1373 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1374 FileName(owner_->filePath()));
1376 ret = doVCCommand("svn propdel svn:needs-lock "
1377 + quoteName(onlyFileName(owner_->absFileName()))
1378 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1379 FileName(owner_->filePath()));
1384 frontend::Alert::warning(_("VCN File Locking"),
1385 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1386 + _("Do not forget to commit the locking property into the repository."),
1389 return string("SVN: ") + N_("Locking property set.");
1393 bool SVN::lockingToggleEnabled()
1401 // Reverts to the version in SVN repository and
1402 // gets the updated version from the repository.
1403 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1405 if (doVCCommand("svn revert -q " + fil,
1406 FileName(owner_->filePath())))
1408 owner_->markClean();
1413 bool SVN::isRevertWithConfirmation()
1415 //FIXME owner && diff
1420 void SVN::undoLast()
1422 // merge the current with the previous version
1423 // in a reverse patch kind of way, so that the
1424 // result is to revert the last changes.
1425 lyxerr << "Sorry, not implemented." << endl;
1429 bool SVN::undoLastEnabled()
1435 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1437 if (info == LyXVC::Tree) {
1438 if (rev_tree_cache_.empty())
1439 if (!getTreeRevisionInfo())
1440 rev_tree_cache_ = "?";
1441 if (rev_tree_cache_ == "?")
1444 return rev_tree_cache_;
1447 // fill the rest of the attributes for a single file
1448 if (rev_file_cache_.empty())
1449 if (!getFileRevisionInfo())
1450 rev_file_cache_ = "?";
1454 if (rev_file_cache_ == "?")
1456 return rev_file_cache_;
1458 return rev_author_cache_;
1460 return rev_date_cache_;
1462 return rev_time_cache_;
1471 bool SVN::getFileRevisionInfo()
1473 FileName tmpf = FileName::tempName("lyxvcout");
1475 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1479 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1480 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1481 FileName(owner_->filePath()));
1486 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1494 LYXERR(Debug::LYXVC, line);
1495 if (prefixIs(line, "<commit"))
1497 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1498 string l1 = subst(line, "revision=\"", "");
1499 string l2 = trim(subst(l1, "\">", ""));
1501 rev_file_cache_ = rev = l2;
1503 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1504 string l1 = subst(line, "<author>", "");
1505 string l2 = subst(l1, "</author>", "");
1506 rev_author_cache_ = l2;
1508 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1509 string l1 = subst(line, "<date>", "");
1510 string l2 = subst(l1, "</date>", "");
1511 l2 = split(l2, l1, 'T');
1512 rev_date_cache_ = l1;
1513 l2 = split(l2, l1, '.');
1514 rev_time_cache_ = l1;
1520 return !rev.empty();
1524 bool SVN::getTreeRevisionInfo()
1526 FileName tmpf = FileName::tempName("lyxvcout");
1528 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1532 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1533 FileName(owner_->filePath()));
1538 // only first line in case something bad happens.
1539 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1545 rev_tree_cache_ = line;
1546 return !line.empty();
1550 void SVN::getLog(FileName const & tmpf)
1552 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1553 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1554 FileName(owner_->filePath()));
1558 bool SVN::prepareFileRevision(string const & revis, string & f)
1560 if (!isStrInt(revis))
1563 int rev = convert<int>(revis);
1565 if (!getFileRevisionInfo())
1568 rev = convert<int>(rev_file_cache_);
1569 // go back for minus rev
1571 rev = rev + convert<int>(rev_file_cache_);
1576 string revname = convert<string>(rev);
1577 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1579 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1583 doVCCommand("svn cat -r " + revname + " "
1584 + quoteName(onlyFileName(owner_->absFileName()))
1585 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1586 FileName(owner_->filePath()));
1587 if (tmpf.isFileEmpty())
1590 f = tmpf.absFileName();
1595 bool SVN::prepareFileRevisionEnabled()
1602 bool SVN::toggleReadOnlyEnabled()