3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "VCBackend.h"
17 #include "FuncRequest.h"
19 #include "frontends/alert.h"
20 #include "frontends/Application.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/Path.h"
28 #include "support/Systemcall.h"
29 #include "support/regex.h"
34 using namespace lyx::support;
41 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
43 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
45 support::PathChanger p(path);
46 return one.startscript(Systemcall::Wait, cmd, string(), false);
50 int VCS::doVCCommand(string const & cmd, FileName const & path, bool reportError)
53 owner_->setBusy(true);
55 int const ret = doVCCommandCall(cmd, path);
58 owner_->setBusy(false);
59 if (ret && reportError)
60 frontend::Alert::error(_("Revision control error."),
61 bformat(_("Some problem occured while running the command:\n"
68 bool VCS::makeRCSRevision(string const &version, string &revis) const
73 int back = convert<int>(rev);
74 // if positive use as the last number in the whole revision string
77 rsplit(version, base , '.' );
78 rev = base + "." + rev;
82 // we care about the last number from revision string
83 // in case of backward indexing
86 cur = rsplit(version, base , '.' );
89 int want = convert<int>(cur) + back;
93 rev = base + "." + convert<string>(want);
102 bool VCS::checkparentdirs(FileName const & file, std::string const & pathname)
104 FileName dirname = file.onlyPath();
105 FileName tocheck = FileName(addName(dirname.absFileName(),pathname));
106 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
107 bool result = tocheck.exists();
108 while ( !result && !dirname.empty() ) {
109 //this construct because of #8295
110 dirname = FileName(dirname.absFileName()).parentPath();
111 LYXERR(Debug::LYXVC, "check directory: " << dirname.absFileName());
112 tocheck = FileName(addName(dirname.absFileName(),pathname));
113 result = tocheck.exists();
119 /////////////////////////////////////////////////////////////////////
123 /////////////////////////////////////////////////////////////////////
125 RCS::RCS(FileName const & m, Buffer * b) : VCS(b)
132 FileName const RCS::findFile(FileName const & file)
134 // Check if *,v exists.
135 FileName tmp(file.absFileName() + ",v");
136 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
137 if (tmp.isReadableFile()) {
138 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
142 // Check if RCS/*,v exists.
143 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
144 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
145 if (tmp.isReadableFile()) {
146 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
154 bool RCS::retrieve(FileName const & file)
156 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
157 // The caller ensures that file does not exist, so no need to check that.
158 return doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
163 void RCS::scanMaster()
168 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
170 ifstream ifs(master_.toFilesystemEncoding().c_str());
173 bool read_enough = false;
175 while (!read_enough && ifs >> token) {
176 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
181 else if (token == "head") {
185 tmv = rtrim(tmv, ";");
187 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
188 } else if (contains(token, "access")
189 || contains(token, "symbols")
190 || contains(token, "strict")) {
192 } else if (contains(token, "locks")) {
194 if (contains(token, ';')) {
195 locker_ = "Unlocked";
204 s1 = rtrim(tmpt, ";");
205 // tmp is now in the format <user>:<version>
206 s1 = split(s1, s2, ':');
207 // s2 is user, and s1 is version
208 if (s1 == version_) {
213 } while (!contains(tmpt, ';'));
215 } else if (token == "comment") {
216 // we don't need to read any further than this.
220 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
226 void RCS::registrer(string const & msg)
228 string cmd = "ci -q -u -i -t-\"";
231 cmd += quoteName(onlyFileName(owner_->absFileName()));
232 doVCCommand(cmd, FileName(owner_->filePath()));
236 string RCS::checkIn(string const & msg)
238 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
239 + quoteName(onlyFileName(owner_->absFileName())),
240 FileName(owner_->filePath()));
241 return ret ? string() : "RCS: Proceeded";
245 bool RCS::checkInEnabled()
247 return owner_ && !owner_->isReadonly();
251 bool RCS::isCheckInWithConfirmation()
253 // FIXME one day common getDiff for all backends
255 // if (getDiff(file, diff) && diff.empty())
258 FileName tmpf = FileName::tempName("lyxvcout");
260 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
264 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
265 + " > " + quoteName(tmpf.toFilesystemEncoding()),
266 FileName(owner_->filePath()));
268 docstring diff = tmpf.fileContents("UTF-8");
278 string RCS::checkOut()
281 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
282 FileName(owner_->filePath()));
283 return ret ? string() : "RCS: Proceeded";
287 bool RCS::checkOutEnabled()
289 return owner_ && owner_->isReadonly();
293 string RCS::repoUpdate()
295 lyxerr << "Sorry, not implemented." << endl;
300 bool RCS::repoUpdateEnabled()
306 string RCS::lockingToggle()
308 //FIXME this might be actually possible, study rcs -U, rcs -L.
309 //State should be easy to get inside scanMaster.
310 //It would fix #4370 and make rcs/svn usage even more closer.
311 lyxerr << "Sorry, not implemented." << endl;
316 bool RCS::lockingToggleEnabled()
324 if (doVCCommand("co -f -u" + version_ + " "
325 + quoteName(onlyFileName(owner_->absFileName())),
326 FileName(owner_->filePath())))
328 // We ignore changes and just reload!
334 bool RCS::isRevertWithConfirmation()
336 //FIXME owner && diff ?
343 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
344 doVCCommand("rcs -o" + version_ + " "
345 + quoteName(onlyFileName(owner_->absFileName())),
346 FileName(owner_->filePath()));
350 bool RCS::undoLastEnabled()
356 void RCS::getLog(FileName const & tmpf)
358 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
359 + " > " + quoteName(tmpf.toFilesystemEncoding()),
360 FileName(owner_->filePath()));
364 bool RCS::toggleReadOnlyEnabled()
366 // This got broken somewhere along lfuns dispatch reorganization.
367 // reloadBuffer would be needed after this, but thats problematic
368 // since we are inside Buffer::dispatch.
374 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
376 if (info == LyXVC::File)
378 // fill the rest of the attributes for a single file
379 if (rev_date_cache_.empty())
380 if (!getRevisionInfo())
385 return rev_author_cache_;
387 return rev_date_cache_;
389 return rev_time_cache_;
397 bool RCS::getRevisionInfo()
399 FileName tmpf = FileName::tempName("lyxvcout");
401 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
404 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
405 + " > " + quoteName(tmpf.toFilesystemEncoding()),
406 FileName(owner_->filePath()));
411 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
414 // we reached to the entry, i.e. after initial log message
416 // line with critical info, e.g:
417 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
422 LYXERR(Debug::LYXVC, line);
423 if (entry && prefixIs(line, "date:")) {
427 if (prefixIs(line, "revision"))
433 rev_date_cache_ = token(result, ' ', 1);
434 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
435 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
437 return !rev_author_cache_.empty();
440 bool RCS::prepareFileRevision(string const &revis, string & f)
443 if (!VCS::makeRCSRevision(version_, rev))
446 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
448 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
452 doVCCommand("co -p" + rev + " "
453 + quoteName(onlyFileName(owner_->absFileName()))
454 + " > " + quoteName(tmpf.toFilesystemEncoding()),
455 FileName(owner_->filePath()));
456 if (tmpf.isFileEmpty())
459 f = tmpf.absFileName();
464 bool RCS::prepareFileRevisionEnabled()
470 /////////////////////////////////////////////////////////////////////
474 /////////////////////////////////////////////////////////////////////
476 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
479 have_rev_info_ = false;
484 FileName const CVS::findFile(FileName const & file)
486 // First we look for the CVS/Entries in the same dir
487 // where we have file.
488 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
489 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
490 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
491 << "' for `" << tmpf << '\'');
492 if (entries.isReadableFile()) {
493 // Ok we are at least in a CVS dir. Parse the CVS/Entries
494 // and see if we can find this file. We do a fast and
496 ifstream ifs(entries.toFilesystemEncoding().c_str());
498 while (getline(ifs, line)) {
499 LYXERR(Debug::LYXVC, "\tEntries: " << line);
500 if (contains(line, tmpf))
508 void CVS::scanMaster()
510 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
511 // Ok now we do the real scan...
512 ifstream ifs(master_.toFilesystemEncoding().c_str());
513 string name = onlyFileName(owner_->absFileName());
514 string tmpf = '/' + name + '/';
515 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
517 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
518 while (getline(ifs, line)) {
519 LYXERR(Debug::LYXVC, "\t line: " << line);
520 if (contains(line, tmpf)) {
521 // Ok extract the fields.
524 regex_match(line, sm, reg);
526 //sm[0]; // whole matched string
528 version_ = sm.str(2);
529 string const file_date = sm.str(3);
532 //sm[5]; // tag or tagdate
533 FileName file(owner_->absFileName());
534 if (file.isReadableFile()) {
535 time_t mod = file.lastModified();
536 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
537 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
538 << "'\nModification date of file: `" << mod_date << '\'');
539 if (file.isReadOnly()) {
540 // readonly checkout is unlocked
543 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
544 FileName base(addName(bdir.absFileName(),name));
545 // if base version is existent "cvs edit" was used to lock
546 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
549 vcstatus = NOLOCKING;
557 bool CVS::retrieve(FileName const & file)
559 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
560 // The caller ensures that file does not exist, so no need to check that.
561 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
562 file.onlyPath()) == 0;
566 string const CVS::getTarget(OperationMode opmode) const
570 // in client server mode CVS does not like full path operand for directory operation
571 // since LyX switches to the repo dir "." is good enough as target
574 return quoteName(onlyFileName(owner_->absFileName()));
580 docstring CVS::toString(CvsStatus status) const
584 return _("Up-to-date");
585 case LocallyModified:
586 return _("Locally Modified");
588 return _("Locally Added");
590 return _("Needs Merge");
592 return _("Needs Checkout");
594 return _("No CVS file");
596 return _("Cannot retrieve CVS status");
602 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
603 FileName const & output, bool reportError)
605 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
606 return doVCCommand(cmd + redirection, path, reportError);
610 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
611 support::FileName const & path,
612 support::FileName const & output)
614 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
615 return doVCCommandCall(cmd + redirection, path);
619 CVS::CvsStatus CVS::getStatus()
621 FileName tmpf = FileName::tempName("lyxvcout");
623 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
627 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
628 FileName(owner_->filePath()), tmpf)) {
633 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
634 CvsStatus status = NoCvsFile;
639 LYXERR(Debug::LYXVC, line << "\n");
640 if (prefixIs(line, "File:")) {
641 if (contains(line, "Up-to-date"))
643 else if (contains(line, "Locally Modified"))
644 status = LocallyModified;
645 else if (contains(line, "Locally Added"))
646 status = LocallyAdded;
647 else if (contains(line, "Needs Merge"))
649 else if (contains(line, "Needs Checkout"))
650 status = NeedsCheckout;
657 void CVS::getRevisionInfo()
661 have_rev_info_ = true;
662 FileName tmpf = FileName::tempName("lyxvcout");
664 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
668 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
669 + " " + getTarget(File),
670 FileName(owner_->filePath()), tmpf);
673 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
677 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
678 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
683 LYXERR(Debug::LYXVC, line << "\n");
684 if (prefixIs(line, "date:")) {
686 regex_match(line, sm, reg);
687 //sm[0]; // whole matched string
688 rev_date_cache_ = sm[1];
689 rev_time_cache_ = sm[2];
690 //sm[3]; // GMT offset
691 rev_author_cache_ = sm[4];
696 if (rev_author_cache_.empty())
698 "Could not retrieve revision info for " << version_ <<
699 " of " << getTarget(File));
703 void CVS::registrer(string const & msg)
705 doVCCommand("cvs -q add -m \"" + msg + "\" "
707 FileName(owner_->filePath()));
711 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
713 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
714 FileName(owner_->filePath()), tmpf, false);
721 return doVCCommand("cvs -q edit " + getTarget(File),
722 FileName(owner_->filePath()));
729 return doVCCommand("cvs -q unedit " + getTarget(File),
730 FileName(owner_->filePath()));
734 int CVS::update(OperationMode opmode, FileName const & tmpf)
736 return doVCCommandWithOutput("cvs -q update "
738 FileName(owner_->filePath()), tmpf, false);
742 string CVS::scanLogFile(FileName const & f, string & status)
744 ifstream ifs(f.toFilesystemEncoding().c_str());
749 LYXERR(Debug::LYXVC, line << "\n");
751 status += line + "; ";
752 if (prefixIs(line, "C ")) {
762 string CVS::checkIn(string const & msg)
764 CvsStatus status = getStatus();
767 if (vcstatus != NOLOCKING)
769 return "CVS: Proceeded";
770 case LocallyModified:
772 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
774 FileName(owner_->filePath()));
775 return rc ? string() : "CVS: Proceeded";
779 frontend::Alert::error(_("Revision control error."),
780 _("The repository version is newer then the current check out.\n"
781 "You have to update from repository first or revert your changes.")) ;
784 frontend::Alert::error(_("Revision control error."),
785 bformat(_("Bad status when checking in changes.\n"
794 bool CVS::isLocked() const
796 FileName fn(owner_->absFileName());
798 return !fn.isReadOnly();
802 bool CVS::checkInEnabled()
804 if (vcstatus != NOLOCKING)
811 bool CVS::isCheckInWithConfirmation()
813 CvsStatus status = getStatus();
814 return status == LocallyModified || status == LocallyAdded;
818 string CVS::checkOut()
820 if (vcstatus != NOLOCKING && edit())
822 FileName tmpf = FileName::tempName("lyxvcout");
824 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
828 int rc = update(File, tmpf);
830 string const res = scanLogFile(tmpf, log);
832 frontend::Alert::error(_("Revision control error."),
833 bformat(_("Error when updating from repository.\n"
834 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
835 "After pressing OK, LyX will try to reopen the resolved document."),
836 from_local8bit(res)));
841 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
845 bool CVS::checkOutEnabled()
847 if (vcstatus != NOLOCKING)
854 string CVS::repoUpdate()
856 FileName tmpf = FileName::tempName("lyxvcout");
858 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
862 getDiff(Directory, tmpf);
863 docstring res = tmpf.fileContents("UTF-8");
865 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
866 docstring const file = from_utf8(owner_->filePath());
867 docstring text = bformat(_("There were detected changes "
868 "in the working directory:\n%1$s\n\n"
869 "Possible file conflicts must be then resolved manually "
870 "or you will need to revert back to the repository version."), file);
871 int ret = frontend::Alert::prompt(_("Changes detected"),
872 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
874 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
875 ret = frontend::Alert::prompt(_("Changes detected"),
876 text, 0, 1, _("&Continue"), _("&Abort"));
877 hideDialogs("file", 0);
885 int rc = update(Directory, tmpf);
886 res += "Update log:\n" + tmpf.fileContents("UTF-8");
887 LYXERR(Debug::LYXVC, res);
890 string sres = scanLogFile(tmpf, log);
892 docstring const file = owner_->fileName().displayName(20);
893 frontend::Alert::error(_("Revision control error."),
894 bformat(_("Error when updating document %1$s from repository.\n"
895 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
896 "After pressing OK, LyX will try to reopen the resolved document."),
897 file, from_local8bit(sres)));
903 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
907 bool CVS::repoUpdateEnabled()
913 string CVS::lockingToggle()
915 lyxerr << "Sorry, not implemented." << endl;
920 bool CVS::lockingToggleEnabled()
926 bool CVS::isRevertWithConfirmation()
928 CvsStatus status = getStatus();
929 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
935 // Reverts to the version in CVS repository and
936 // gets the updated version from the repository.
937 CvsStatus status = getStatus();
940 if (vcstatus != NOLOCKING)
941 return 0 == unedit();
945 case LocallyModified: {
946 FileName f(owner_->absFileName());
948 update(File, FileName());
953 docstring const file = owner_->fileName().displayName(20);
954 frontend::Alert::error(_("Revision control error."),
955 bformat(_("The document %1$s is not in repository.\n"
956 "You have to check in the first revision before you can revert."),
961 docstring const file = owner_->fileName().displayName(20);
962 frontend::Alert::error(_("Revision control error."),
963 bformat(_("Cannot revert document %1$s to repository version.\n"
964 "The status '%2$s' is unexpected."),
965 file, toString(status)));
975 // merge the current with the previous version
976 // in a reverse patch kind of way, so that the
977 // result is to revert the last changes.
978 lyxerr << "Sorry, not implemented." << endl;
982 bool CVS::undoLastEnabled()
988 void CVS::getLog(FileName const & tmpf)
990 doVCCommandWithOutput("cvs log " + getTarget(File),
991 FileName(owner_->filePath()),
996 bool CVS::toggleReadOnlyEnabled()
1002 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1004 if (!version_.empty()) {
1010 return rev_author_cache_;
1012 return rev_date_cache_;
1014 return rev_time_cache_;
1022 bool CVS::prepareFileRevision(string const & revis, string & f)
1025 if (!VCS::makeRCSRevision(version_, rev))
1028 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
1030 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1034 doVCCommandWithOutput("cvs update -p -r" + rev + " "
1036 FileName(owner_->filePath()), tmpf);
1037 if (tmpf.isFileEmpty())
1040 f = tmpf.absFileName();
1045 bool CVS::prepareFileRevisionEnabled()
1051 /////////////////////////////////////////////////////////////////////
1055 /////////////////////////////////////////////////////////////////////
1057 SVN::SVN(FileName const & m, Buffer * b) : VCS(b)
1065 FileName const SVN::findFile(FileName const & file)
1067 // First we check the existence of repository meta data.
1068 if (!VCS::checkparentdirs(file, ".svn")) {
1069 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1073 // Now we check the status of the file.
1074 FileName tmpf = FileName::tempName("lyxvcout");
1076 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1080 string const fname = onlyFileName(file.absFileName());
1081 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1082 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1083 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1085 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1086 return found ? file : FileName();
1090 void SVN::scanMaster()
1092 // vcstatus code is somewhat superflous, until we want
1093 // to implement read-only toggle for svn.
1094 vcstatus = NOLOCKING;
1095 if (checkLockMode()) {
1099 vcstatus = UNLOCKED;
1105 bool SVN::checkLockMode()
1107 FileName tmpf = FileName::tempName("lyxvcout");
1109 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1113 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1114 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1115 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1116 FileName(owner_->filePath())))
1119 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1123 while (ifs && !ret) {
1125 LYXERR(Debug::LYXVC, line);
1126 if (contains(line, "svn:needs-lock"))
1129 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1137 bool SVN::isLocked() const
1139 FileName file(owner_->absFileName());
1141 return !file.isReadOnly();
1145 bool SVN::retrieve(FileName const & file)
1147 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1148 // The caller ensures that file does not exist, so no need to check that.
1149 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1150 file.onlyPath()) == 0;
1154 void SVN::registrer(string const & /*msg*/)
1156 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1157 FileName(owner_->filePath()));
1161 string SVN::checkIn(string const & msg)
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 commit -m \"" + msg + "\" "
1170 + quoteName(onlyFileName(owner_->absFileName()))
1171 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1172 FileName(owner_->filePath()));
1175 string res = scanLogFile(tmpf, log);
1177 frontend::Alert::error(_("Revision control error."),
1178 _("Error when committing to repository.\n"
1179 "You have to manually resolve the problem.\n"
1180 "LyX will reopen the document after you press OK."));
1182 fileLock(false, tmpf, log);
1185 return log.empty() ? string() : "SVN: " + log;
1189 bool SVN::checkInEnabled()
1198 bool SVN::isCheckInWithConfirmation()
1200 // FIXME one day common getDiff and perhaps OpMode for all backends
1202 FileName tmpf = FileName::tempName("lyxvcout");
1204 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1208 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1209 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1210 FileName(owner_->filePath()));
1212 docstring diff = tmpf.fileContents("UTF-8");
1222 // FIXME Correctly return code should be checked instead of this.
1223 // This would need another solution than just plain startscript.
1224 // Hint from Andre': QProcess::readAllStandardError()...
1225 string SVN::scanLogFile(FileName const & f, string & status)
1227 ifstream ifs(f.toFilesystemEncoding().c_str());
1232 LYXERR(Debug::LYXVC, line << "\n");
1234 status += line + "; ";
1235 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1236 || contains(line, "Commit failed")) {
1240 if (contains(line, "svn:needs-lock")) {
1250 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1252 if (!locked_mode_ || (isLocked() == lock))
1255 string const arg = lock ? "lock " : "unlock ";
1256 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1257 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1258 FileName(owner_->filePath()));
1260 // Lock error messages go unfortunately on stderr and are unreachible this way.
1261 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1265 if (!line.empty()) status += line + "; ";
1269 if (!isLocked() && lock)
1270 frontend::Alert::error(_("Revision control error."),
1271 _("Error while acquiring write lock.\n"
1272 "Another user is most probably editing\n"
1273 "the current document now!\n"
1274 "Also check the access to the repository."));
1275 if (isLocked() && !lock)
1276 frontend::Alert::error(_("Revision control error."),
1277 _("Error while releasing write lock.\n"
1278 "Check the access to the repository."));
1282 string SVN::checkOut()
1284 FileName tmpf = FileName::tempName("lyxvcout");
1286 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1287 return N_("Error: Could not generate logfile.");
1290 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1291 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1292 FileName(owner_->filePath()));
1295 string const res = scanLogFile(tmpf, log);
1297 frontend::Alert::error(_("Revision control error."),
1298 bformat(_("Error when updating from repository.\n"
1299 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1300 "After pressing OK, LyX will try to reopen the resolved document."),
1301 from_local8bit(res)));
1303 fileLock(true, tmpf, log);
1306 return log.empty() ? string() : "SVN: " + log;
1310 bool SVN::checkOutEnabled()
1319 string SVN::repoUpdate()
1321 FileName tmpf = FileName::tempName("lyxvcout");
1323 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1324 return N_("Error: Could not generate logfile.");
1327 doVCCommand("svn diff " + quoteName(owner_->filePath())
1328 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1329 FileName(owner_->filePath()));
1330 docstring res = tmpf.fileContents("UTF-8");
1332 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1333 docstring const file = from_utf8(owner_->filePath());
1334 docstring text = bformat(_("There were detected changes "
1335 "in the working directory:\n%1$s\n\n"
1336 "In case of file conflict version of the local directory files "
1337 "will be preferred."
1338 "\n\nContinue?"), file);
1339 int ret = frontend::Alert::prompt(_("Changes detected"),
1340 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1342 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1343 ret = frontend::Alert::prompt(_("Changes detected"),
1344 text, 0, 1, _("&Yes"), _("&No"));
1345 hideDialogs("file", 0);
1353 // Reverting looks too harsh, see bug #6255.
1354 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1355 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1356 // FileName(owner_->filePath()));
1357 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1358 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1359 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1360 FileName(owner_->filePath()));
1361 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1363 LYXERR(Debug::LYXVC, res);
1365 return to_utf8(res);
1369 bool SVN::repoUpdateEnabled()
1375 string SVN::lockingToggle()
1377 FileName tmpf = FileName::tempName("lyxvcout");
1379 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1380 return N_("Error: Could not generate logfile.");
1383 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1384 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1385 FileName(owner_->filePath()));
1390 string res = scanLogFile(tmpf, log);
1391 bool locking = contains(res, "svn:needs-lock");
1393 ret = doVCCommand("svn propset svn:needs-lock ON "
1394 + quoteName(onlyFileName(owner_->absFileName()))
1395 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1396 FileName(owner_->filePath()));
1398 ret = doVCCommand("svn propdel svn:needs-lock "
1399 + quoteName(onlyFileName(owner_->absFileName()))
1400 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1401 FileName(owner_->filePath()));
1406 frontend::Alert::warning(_("SVN File Locking"),
1407 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1408 + _("Do not forget to commit the locking property into the repository."),
1411 return string("SVN: ") + (locking ?
1412 N_("Locking property unset.") : N_("Locking property set."));
1416 bool SVN::lockingToggleEnabled()
1424 // Reverts to the version in SVN repository and
1425 // gets the updated version from the repository.
1426 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1428 if (doVCCommand("svn revert -q " + fil,
1429 FileName(owner_->filePath())))
1431 owner_->markClean();
1436 bool SVN::isRevertWithConfirmation()
1438 //FIXME owner && diff
1443 void SVN::undoLast()
1445 // merge the current with the previous version
1446 // in a reverse patch kind of way, so that the
1447 // result is to revert the last changes.
1448 lyxerr << "Sorry, not implemented." << endl;
1452 bool SVN::undoLastEnabled()
1458 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1460 if (info == LyXVC::Tree) {
1461 if (rev_tree_cache_.empty())
1462 if (!getTreeRevisionInfo())
1463 rev_tree_cache_ = "?";
1464 if (rev_tree_cache_ == "?")
1467 return rev_tree_cache_;
1470 // fill the rest of the attributes for a single file
1471 if (rev_file_cache_.empty())
1472 if (!getFileRevisionInfo())
1473 rev_file_cache_ = "?";
1477 if (rev_file_cache_ == "?")
1479 return rev_file_cache_;
1481 return rev_author_cache_;
1483 return rev_date_cache_;
1485 return rev_time_cache_;
1494 bool SVN::getFileRevisionInfo()
1496 FileName tmpf = FileName::tempName("lyxvcout");
1498 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1502 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1503 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1504 FileName(owner_->filePath()));
1509 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1517 LYXERR(Debug::LYXVC, line);
1518 if (prefixIs(line, "<commit"))
1520 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1521 string l1 = subst(line, "revision=\"", "");
1522 string l2 = trim(subst(l1, "\">", ""));
1524 rev_file_cache_ = rev = l2;
1526 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1527 string l1 = subst(line, "<author>", "");
1528 string l2 = subst(l1, "</author>", "");
1529 rev_author_cache_ = l2;
1531 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1532 string l1 = subst(line, "<date>", "");
1533 string l2 = subst(l1, "</date>", "");
1534 l2 = split(l2, l1, 'T');
1535 rev_date_cache_ = l1;
1536 l2 = split(l2, l1, '.');
1537 rev_time_cache_ = l1;
1543 return !rev.empty();
1547 bool SVN::getTreeRevisionInfo()
1549 FileName tmpf = FileName::tempName("lyxvcout");
1551 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1555 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1556 FileName(owner_->filePath()));
1561 // only first line in case something bad happens.
1562 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1568 rev_tree_cache_ = line;
1569 return !line.empty();
1573 void SVN::getLog(FileName const & tmpf)
1575 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1576 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1577 FileName(owner_->filePath()));
1581 bool SVN::prepareFileRevision(string const & revis, string & f)
1583 if (!isStrInt(revis))
1586 int rev = convert<int>(revis);
1588 if (!getFileRevisionInfo())
1591 rev = convert<int>(rev_file_cache_);
1592 // go back for minus rev
1594 rev = rev + convert<int>(rev_file_cache_);
1599 string revname = convert<string>(rev);
1600 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1602 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1606 doVCCommand("svn cat -r " + revname + " "
1607 + quoteName(onlyFileName(owner_->absFileName()))
1608 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1609 FileName(owner_->filePath()));
1610 if (tmpf.isFileEmpty())
1613 f = tmpf.absFileName();
1618 bool SVN::prepareFileRevisionEnabled()
1625 bool SVN::toggleReadOnlyEnabled()