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/PathChanger.h"
28 #include "support/Systemcall.h"
29 #include "support/regex.h"
34 using namespace lyx::support;
40 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
42 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
44 support::PathChanger p(path);
45 return one.startscript(Systemcall::Wait, cmd, string(), false);
49 int VCS::doVCCommand(string const & cmd, FileName const & path, bool reportError)
52 owner_->setBusy(true);
54 int const ret = doVCCommandCall(cmd, path);
57 owner_->setBusy(false);
58 if (ret && reportError)
59 frontend::Alert::error(_("Revision control error."),
60 bformat(_("Some problem occured while running the command:\n"
67 bool VCS::makeRCSRevision(string const &version, string &revis) const
72 int back = convert<int>(rev);
73 // if positive use as the last number in the whole revision string
76 rsplit(version, base , '.' );
77 rev = base + "." + rev;
81 // we care about the last number from revision string
82 // in case of backward indexing
85 cur = rsplit(version, base , '.' );
88 int want = convert<int>(cur) + back;
92 rev = base + "." + convert<string>(want);
101 bool VCS::checkparentdirs(FileName const & file, std::string const & vcsdir)
103 FileName dirname = file.onlyPath();
105 FileName tocheck = FileName(addName(dirname.absFileName(), vcsdir));
106 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
107 if (tocheck.exists())
109 //this construct because of #8295
110 dirname = FileName(dirname.absFileName()).parentPath();
111 } while (!dirname.empty());
116 /////////////////////////////////////////////////////////////////////
120 /////////////////////////////////////////////////////////////////////
122 RCS::RCS(FileName const & m, Buffer * b) : VCS(b)
124 // Here we know that the buffer file is either already in RCS or
125 // about to be registered
131 FileName const RCS::findFile(FileName const & file)
133 // Check if *,v exists.
134 FileName tmp(file.absFileName() + ",v");
135 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
136 if (tmp.isReadableFile()) {
137 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
141 // Check if RCS/*,v exists.
142 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
143 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
144 if (tmp.isReadableFile()) {
145 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
153 bool RCS::retrieve(FileName const & file)
155 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
156 // The caller ensures that file does not exist, so no need to check that.
157 return doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
162 void RCS::scanMaster()
167 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
169 ifstream ifs(master_.toFilesystemEncoding().c_str());
172 bool read_enough = false;
174 while (!read_enough && ifs >> token) {
175 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
180 else if (token == "head") {
184 tmv = rtrim(tmv, ";");
186 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
187 } else if (contains(token, "access")
188 || contains(token, "symbols")
189 || contains(token, "strict")) {
191 } else if (contains(token, "locks")) {
193 if (contains(token, ';')) {
194 locker_ = "Unlocked";
203 s1 = rtrim(tmpt, ";");
204 // tmp is now in the format <user>:<version>
205 s1 = split(s1, s2, ':');
206 // s2 is user, and s1 is version
207 if (s1 == version_) {
212 } while (!contains(tmpt, ';'));
214 } else if (token == "comment") {
215 // we don't need to read any further than this.
219 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
225 void RCS::registrer(string const & msg)
227 string cmd = "ci -q -u -i -t-\"";
230 cmd += quoteName(onlyFileName(owner_->absFileName()));
231 doVCCommand(cmd, FileName(owner_->filePath()));
235 bool RCS::renameEnabled()
241 string RCS::rename(support::FileName const & /*newFile*/, string const & /*msg*/)
243 // not implemented, since a left-over file.lyx,v would be confusing.
248 bool RCS::copyEnabled()
254 string RCS::copy(support::FileName const & newFile, string const & msg)
256 // RCS has no real copy command, so we create a poor mans version
257 support::FileName const oldFile(owner_->absFileName());
258 if (!oldFile.copyTo(newFile))
260 FileName path(oldFile.onlyPath());
261 string relFile(to_utf8(newFile.relPath(path.absFileName())));
262 string cmd = "ci -q -u -i -t-\"";
265 cmd += quoteName(relFile);
266 return doVCCommand(cmd, path) ? string() : "RCS: Proceeded";
270 LyXVC::CommandResult RCS::checkIn(string const & msg, string & log)
272 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
273 + quoteName(onlyFileName(owner_->absFileName())),
274 FileName(owner_->filePath()));
276 return LyXVC::ErrorCommand;
277 log = "RCS: Proceeded";
278 return LyXVC::VCSuccess;
282 bool RCS::checkInEnabled()
284 return owner_ && !owner_->isReadonly();
288 bool RCS::isCheckInWithConfirmation()
290 // FIXME one day common getDiff for all backends
292 // if (getDiff(file, diff) && diff.empty())
295 FileName tmpf = FileName::tempName("lyxvcout");
297 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
301 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
302 + " > " + quoteName(tmpf.toFilesystemEncoding()),
303 FileName(owner_->filePath()));
305 docstring diff = tmpf.fileContents("UTF-8");
315 string RCS::checkOut()
318 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
319 FileName(owner_->filePath()));
320 return ret ? string() : "RCS: Proceeded";
324 bool RCS::checkOutEnabled()
326 return owner_ && owner_->isReadonly();
330 string RCS::repoUpdate()
332 lyxerr << "Sorry, not implemented." << endl;
337 bool RCS::repoUpdateEnabled()
343 string RCS::lockingToggle()
345 //FIXME this might be actually possible, study rcs -U, rcs -L.
346 //State should be easy to get inside scanMaster.
347 //It would fix #4370 and make rcs/svn usage even more closer.
348 lyxerr << "Sorry, not implemented." << endl;
353 bool RCS::lockingToggleEnabled()
361 if (doVCCommand("co -f -u" + version_ + " "
362 + quoteName(onlyFileName(owner_->absFileName())),
363 FileName(owner_->filePath())))
365 // We ignore changes and just reload!
371 bool RCS::isRevertWithConfirmation()
373 //FIXME owner && diff ?
380 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
381 doVCCommand("rcs -o" + version_ + " "
382 + quoteName(onlyFileName(owner_->absFileName())),
383 FileName(owner_->filePath()));
387 bool RCS::undoLastEnabled()
393 void RCS::getLog(FileName const & tmpf)
395 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
396 + " > " + quoteName(tmpf.toFilesystemEncoding()),
397 FileName(owner_->filePath()));
401 bool RCS::toggleReadOnlyEnabled()
403 // This got broken somewhere along lfuns dispatch reorganization.
404 // reloadBuffer would be needed after this, but thats problematic
405 // since we are inside Buffer::dispatch.
411 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
413 if (info == LyXVC::File)
415 // fill the rest of the attributes for a single file
416 if (rev_date_cache_.empty())
417 if (!getRevisionInfo())
422 return rev_author_cache_;
424 return rev_date_cache_;
426 return rev_time_cache_;
434 bool RCS::getRevisionInfo()
436 FileName tmpf = FileName::tempName("lyxvcout");
438 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
441 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
442 + " > " + quoteName(tmpf.toFilesystemEncoding()),
443 FileName(owner_->filePath()));
448 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
451 // we reached to the entry, i.e. after initial log message
453 // line with critical info, e.g:
454 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
459 LYXERR(Debug::LYXVC, line);
460 if (entry && prefixIs(line, "date:")) {
464 if (prefixIs(line, "revision"))
470 rev_date_cache_ = token(result, ' ', 1);
471 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
472 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
474 return !rev_author_cache_.empty();
477 bool RCS::prepareFileRevision(string const &revis, string & f)
480 if (!VCS::makeRCSRevision(version_, rev))
483 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
485 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
489 doVCCommand("co -p" + rev + " "
490 + quoteName(onlyFileName(owner_->absFileName()))
491 + " > " + quoteName(tmpf.toFilesystemEncoding()),
492 FileName(owner_->filePath()));
493 if (tmpf.isFileEmpty())
496 f = tmpf.absFileName();
501 bool RCS::prepareFileRevisionEnabled()
507 /////////////////////////////////////////////////////////////////////
511 /////////////////////////////////////////////////////////////////////
513 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
515 // Here we know that the buffer file is either already in CVS or
516 // about to be registered
518 have_rev_info_ = false;
523 FileName const CVS::findFile(FileName const & file)
525 // First we look for the CVS/Entries in the same dir
526 // where we have file.
527 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
528 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
529 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
530 << "' for `" << tmpf << '\'');
531 if (entries.isReadableFile()) {
532 // Ok we are at least in a CVS dir. Parse the CVS/Entries
533 // and see if we can find this file. We do a fast and
535 ifstream ifs(entries.toFilesystemEncoding().c_str());
537 while (getline(ifs, line)) {
538 LYXERR(Debug::LYXVC, "\tEntries: " << line);
539 if (contains(line, tmpf))
547 void CVS::scanMaster()
549 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
550 // Ok now we do the real scan...
551 ifstream ifs(master_.toFilesystemEncoding().c_str());
552 string name = onlyFileName(owner_->absFileName());
553 string tmpf = '/' + name + '/';
554 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
556 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
557 while (getline(ifs, line)) {
558 LYXERR(Debug::LYXVC, "\t line: " << line);
559 if (contains(line, tmpf)) {
560 // Ok extract the fields.
563 regex_match(line, sm, reg);
565 //sm[0]; // whole matched string
567 version_ = sm.str(2);
568 string const file_date = sm.str(3);
571 //sm[5]; // tag or tagdate
572 FileName file(owner_->absFileName());
573 if (file.isReadableFile()) {
574 time_t mod = file.lastModified();
575 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
576 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
577 << "'\nModification date of file: `" << mod_date << '\'');
578 if (file.isReadOnly()) {
579 // readonly checkout is unlocked
582 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
583 FileName base(addName(bdir.absFileName(),name));
584 // if base version is existent "cvs edit" was used to lock
585 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
588 vcstatus = NOLOCKING;
596 bool CVS::retrieve(FileName const & file)
598 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
599 // The caller ensures that file does not exist, so no need to check that.
600 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
601 file.onlyPath()) == 0;
605 string const CVS::getTarget(OperationMode opmode) const
609 // in client server mode CVS does not like full path operand for directory operation
610 // since LyX switches to the repo dir "." is good enough as target
613 return quoteName(onlyFileName(owner_->absFileName()));
619 docstring CVS::toString(CvsStatus status) const
623 return _("Up-to-date");
624 case LocallyModified:
625 return _("Locally Modified");
627 return _("Locally Added");
629 return _("Needs Merge");
631 return _("Needs Checkout");
633 return _("No CVS file");
635 return _("Cannot retrieve CVS status");
641 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
642 FileName const & output, bool reportError)
644 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
645 return doVCCommand(cmd + redirection, path, reportError);
649 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
650 support::FileName const & path,
651 support::FileName const & output)
653 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
654 return doVCCommandCall(cmd + redirection, path);
658 CVS::CvsStatus CVS::getStatus()
660 FileName tmpf = FileName::tempName("lyxvcout");
662 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
666 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
667 FileName(owner_->filePath()), tmpf)) {
672 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
673 CvsStatus status = NoCvsFile;
678 LYXERR(Debug::LYXVC, line << "\n");
679 if (prefixIs(line, "File:")) {
680 if (contains(line, "Up-to-date"))
682 else if (contains(line, "Locally Modified"))
683 status = LocallyModified;
684 else if (contains(line, "Locally Added"))
685 status = LocallyAdded;
686 else if (contains(line, "Needs Merge"))
688 else if (contains(line, "Needs Checkout"))
689 status = NeedsCheckout;
696 void CVS::getRevisionInfo()
700 have_rev_info_ = true;
701 FileName tmpf = FileName::tempName("lyxvcout");
703 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
707 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
708 + " " + getTarget(File),
709 FileName(owner_->filePath()), tmpf);
712 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
716 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
717 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
722 LYXERR(Debug::LYXVC, line << "\n");
723 if (prefixIs(line, "date:")) {
725 regex_match(line, sm, reg);
726 //sm[0]; // whole matched string
727 rev_date_cache_ = sm[1];
728 rev_time_cache_ = sm[2];
729 //sm[3]; // GMT offset
730 rev_author_cache_ = sm[4];
735 if (rev_author_cache_.empty())
737 "Could not retrieve revision info for " << version_ <<
738 " of " << getTarget(File));
742 void CVS::registrer(string const & msg)
744 doVCCommand("cvs -q add -m \"" + msg + "\" "
746 FileName(owner_->filePath()));
750 bool CVS::renameEnabled()
756 string CVS::rename(support::FileName const & newFile, string const & msg)
758 // CVS has no real rename command, so we create a poor mans version
759 support::FileName const oldFile(owner_->absFileName());
760 string ret = copy(newFile, msg);
763 string cmd = "cvs -q remove -m \"" + msg + "\" " +
764 quoteName(oldFile.onlyFileName());
765 FileName path(oldFile.onlyPath());
766 return doVCCommand(cmd, path) ? string() : ret;
770 bool CVS::copyEnabled()
776 string CVS::copy(support::FileName const & newFile, string const & msg)
778 // CVS has no real copy command, so we create a poor mans version
779 support::FileName const oldFile(owner_->absFileName());
780 if (!oldFile.copyTo(newFile))
782 FileName path(oldFile.onlyPath());
783 string relFile(to_utf8(newFile.relPath(path.absFileName())));
784 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
785 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
789 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
791 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
792 FileName(owner_->filePath()), tmpf, false);
799 return doVCCommand("cvs -q edit " + getTarget(File),
800 FileName(owner_->filePath()));
807 return doVCCommand("cvs -q unedit " + getTarget(File),
808 FileName(owner_->filePath()));
812 int CVS::update(OperationMode opmode, FileName const & tmpf)
814 return doVCCommandWithOutput("cvs -q update "
816 FileName(owner_->filePath()), tmpf, false);
820 string CVS::scanLogFile(FileName const & f, string & status)
822 ifstream ifs(f.toFilesystemEncoding().c_str());
827 LYXERR(Debug::LYXVC, line << "\n");
829 status += line + "; ";
830 if (prefixIs(line, "C ")) {
840 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
842 CvsStatus status = getStatus();
845 if (vcstatus != NOLOCKING)
847 return LyXVC::ErrorCommand;
848 log = "CVS: Proceeded";
849 return LyXVC::VCSuccess;
850 case LocallyModified:
852 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
854 FileName(owner_->filePath()));
856 return LyXVC::ErrorCommand;
857 log = "CVS: Proceeded";
858 return LyXVC::VCSuccess;
862 frontend::Alert::error(_("Revision control error."),
863 _("The repository version is newer then the current check out.\n"
864 "You have to update from repository first or revert your changes.")) ;
867 frontend::Alert::error(_("Revision control error."),
868 bformat(_("Bad status when checking in changes.\n"
873 return LyXVC::ErrorBefore;
877 bool CVS::isLocked() const
879 FileName fn(owner_->absFileName());
881 return !fn.isReadOnly();
885 bool CVS::checkInEnabled()
887 if (vcstatus != NOLOCKING)
894 bool CVS::isCheckInWithConfirmation()
896 CvsStatus status = getStatus();
897 return status == LocallyModified || status == LocallyAdded;
901 string CVS::checkOut()
903 if (vcstatus != NOLOCKING && edit())
905 FileName tmpf = FileName::tempName("lyxvcout");
907 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
911 int rc = update(File, tmpf);
913 string const res = scanLogFile(tmpf, log);
915 frontend::Alert::error(_("Revision control error."),
916 bformat(_("Error when updating from repository.\n"
917 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
918 "After pressing OK, LyX will try to reopen the resolved document."),
919 from_local8bit(res)));
924 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
928 bool CVS::checkOutEnabled()
930 if (vcstatus != NOLOCKING)
937 string CVS::repoUpdate()
939 FileName tmpf = FileName::tempName("lyxvcout");
941 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
945 getDiff(Directory, tmpf);
946 docstring res = tmpf.fileContents("UTF-8");
948 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
949 docstring const file = from_utf8(owner_->filePath());
950 docstring text = bformat(_("There were detected changes "
951 "in the working directory:\n%1$s\n\n"
952 "Possible file conflicts must be then resolved manually "
953 "or you will need to revert back to the repository version."), file);
954 int ret = frontend::Alert::prompt(_("Changes detected"),
955 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
957 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
958 ret = frontend::Alert::prompt(_("Changes detected"),
959 text, 0, 1, _("&Continue"), _("&Abort"));
960 hideDialogs("file", 0);
968 int rc = update(Directory, tmpf);
969 res += "Update log:\n" + tmpf.fileContents("UTF-8");
970 LYXERR(Debug::LYXVC, res);
973 string sres = scanLogFile(tmpf, log);
975 docstring const file = owner_->fileName().displayName(20);
976 frontend::Alert::error(_("Revision control error."),
977 bformat(_("Error when updating document %1$s from repository.\n"
978 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
979 "After pressing OK, LyX will try to reopen the resolved document."),
980 file, from_local8bit(sres)));
986 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
990 bool CVS::repoUpdateEnabled()
996 string CVS::lockingToggle()
998 lyxerr << "Sorry, not implemented." << endl;
1003 bool CVS::lockingToggleEnabled()
1009 bool CVS::isRevertWithConfirmation()
1011 CvsStatus status = getStatus();
1012 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1018 // Reverts to the version in CVS repository and
1019 // gets the updated version from the repository.
1020 CvsStatus status = getStatus();
1023 if (vcstatus != NOLOCKING)
1024 return 0 == unedit();
1028 case LocallyModified: {
1029 FileName f(owner_->absFileName());
1031 update(File, FileName());
1032 owner_->markClean();
1035 case LocallyAdded: {
1036 docstring const file = owner_->fileName().displayName(20);
1037 frontend::Alert::error(_("Revision control error."),
1038 bformat(_("The document %1$s is not in repository.\n"
1039 "You have to check in the first revision before you can revert."),
1044 docstring const file = owner_->fileName().displayName(20);
1045 frontend::Alert::error(_("Revision control error."),
1046 bformat(_("Cannot revert document %1$s to repository version.\n"
1047 "The status '%2$s' is unexpected."),
1048 file, toString(status)));
1056 void CVS::undoLast()
1058 // merge the current with the previous version
1059 // in a reverse patch kind of way, so that the
1060 // result is to revert the last changes.
1061 lyxerr << "Sorry, not implemented." << endl;
1065 bool CVS::undoLastEnabled()
1071 void CVS::getLog(FileName const & tmpf)
1073 doVCCommandWithOutput("cvs log " + getTarget(File),
1074 FileName(owner_->filePath()),
1079 bool CVS::toggleReadOnlyEnabled()
1085 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1087 if (!version_.empty()) {
1093 return rev_author_cache_;
1095 return rev_date_cache_;
1097 return rev_time_cache_;
1105 bool CVS::prepareFileRevision(string const & revis, string & f)
1108 if (!VCS::makeRCSRevision(version_, rev))
1111 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
1113 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1117 doVCCommandWithOutput("cvs update -p -r" + rev + " "
1119 FileName(owner_->filePath()), tmpf);
1120 if (tmpf.isFileEmpty())
1123 f = tmpf.absFileName();
1128 bool CVS::prepareFileRevisionEnabled()
1134 /////////////////////////////////////////////////////////////////////
1138 /////////////////////////////////////////////////////////////////////
1140 SVN::SVN(FileName const & m, Buffer * b) : VCS(b)
1142 // Here we know that the buffer file is either already in SVN or
1143 // about to be registered
1150 FileName const SVN::findFile(FileName const & file)
1152 // First we check the existence of repository meta data.
1153 if (!VCS::checkparentdirs(file, ".svn")) {
1154 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1158 // Now we check the status of the file.
1159 FileName tmpf = FileName::tempName("lyxvcout");
1161 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1165 string const fname = onlyFileName(file.absFileName());
1166 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1167 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1168 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1171 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1172 return found ? file : FileName();
1176 void SVN::scanMaster()
1178 // vcstatus code is somewhat superflous,
1179 // until we want to implement read-only toggle for svn.
1180 vcstatus = NOLOCKING;
1181 if (checkLockMode()) {
1185 vcstatus = UNLOCKED;
1190 bool SVN::checkLockMode()
1192 FileName tmpf = FileName::tempName("lyxvcout");
1194 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1198 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1199 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1200 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1201 FileName(owner_->filePath())))
1204 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1208 while (ifs && !ret) {
1210 LYXERR(Debug::LYXVC, line);
1211 if (contains(line, "svn:needs-lock"))
1214 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1222 bool SVN::isLocked() const
1224 FileName file(owner_->absFileName());
1226 return !file.isReadOnly();
1230 bool SVN::retrieve(FileName const & file)
1232 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1233 // The caller ensures that file does not exist, so no need to check that.
1234 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1235 file.onlyPath()) == 0;
1239 void SVN::registrer(string const & /*msg*/)
1241 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1242 FileName(owner_->filePath()));
1246 bool SVN::renameEnabled()
1252 string SVN::rename(support::FileName const & newFile, string const & msg)
1254 // svn move does not require a log message, since it does not commit.
1255 // In LyX we commit immediately afterwards, otherwise it could be
1256 // confusing to the user to have two uncommitted files.
1257 FileName path(owner_->filePath());
1258 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1259 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1260 ' ' + quoteName(relFile));
1261 if (doVCCommand(cmd, path)) {
1262 cmd = "svn revert -q " +
1263 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1265 doVCCommand(cmd, path);
1266 if (newFile.exists())
1267 newFile.removeFile();
1270 vector<support::FileName> f;
1271 f.push_back(owner_->fileName());
1272 f.push_back(newFile);
1274 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1275 cmd = "svn revert -q " +
1276 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1278 doVCCommand(cmd, path);
1279 if (newFile.exists())
1280 newFile.removeFile();
1287 bool SVN::copyEnabled()
1293 string SVN::copy(support::FileName const & newFile, string const & msg)
1295 // svn copy does not require a log message, since it does not commit.
1296 // In LyX we commit immediately afterwards, otherwise it could be
1297 // confusing to the user to have an uncommitted file.
1298 FileName path(owner_->filePath());
1299 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1300 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1301 ' ' + quoteName(relFile));
1302 if (doVCCommand(cmd, path))
1304 vector<support::FileName> f(1, newFile);
1306 if (checkIn(f, msg, log) == LyXVC::VCSuccess)
1312 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1314 vector<support::FileName> f(1, owner_->fileName());
1315 return checkIn(f, msg, log);
1319 LyXVC::CommandResult
1320 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1322 FileName tmpf = FileName::tempName("lyxvcout");
1324 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1325 log = N_("Error: Could not generate logfile.");
1326 return LyXVC::ErrorBefore;
1330 os << "svn commit -m \"" << msg << '"';
1331 for (size_t i = 0; i < f.size(); ++i)
1332 os << ' ' << quoteName(f[i].onlyFileName());
1333 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1334 LyXVC::CommandResult ret =
1335 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1336 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1338 string res = scanLogFile(tmpf, log);
1340 frontend::Alert::error(_("Revision control error."),
1341 _("Error when committing to repository.\n"
1342 "You have to manually resolve the problem.\n"
1343 "LyX will reopen the document after you press OK."));
1344 ret = LyXVC::ErrorCommand;
1347 if (!fileLock(false, tmpf, log))
1348 ret = LyXVC::ErrorCommand;
1352 log.insert(0, "SVN: ");
1353 if (ret == LyXVC::VCSuccess && log.empty())
1354 log = "SVN: Proceeded";
1359 bool SVN::checkInEnabled()
1368 bool SVN::isCheckInWithConfirmation()
1370 // FIXME one day common getDiff and perhaps OpMode for all backends
1372 FileName tmpf = FileName::tempName("lyxvcout");
1374 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1378 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1379 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1380 FileName(owner_->filePath()));
1382 docstring diff = tmpf.fileContents("UTF-8");
1392 // FIXME Correctly return code should be checked instead of this.
1393 // This would need another solution than just plain startscript.
1394 // Hint from Andre': QProcess::readAllStandardError()...
1395 string SVN::scanLogFile(FileName const & f, string & status)
1397 ifstream ifs(f.toFilesystemEncoding().c_str());
1402 LYXERR(Debug::LYXVC, line << "\n");
1404 status += line + "; ";
1405 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1406 || contains(line, "Commit failed")) {
1410 if (contains(line, "svn:needs-lock")) {
1420 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1422 if (!locked_mode_ || (isLocked() == lock))
1425 string const arg = lock ? "lock " : "unlock ";
1426 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1427 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1428 FileName(owner_->filePath()));
1430 // Lock error messages go unfortunately on stderr and are unreachible this way.
1431 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1435 if (!line.empty()) status += line + "; ";
1439 if (isLocked() == lock)
1443 frontend::Alert::error(_("Revision control error."),
1444 _("Error while acquiring write lock.\n"
1445 "Another user is most probably editing\n"
1446 "the current document now!\n"
1447 "Also check the access to the repository."));
1449 frontend::Alert::error(_("Revision control error."),
1450 _("Error while releasing write lock.\n"
1451 "Check the access to the repository."));
1456 string SVN::checkOut()
1458 FileName tmpf = FileName::tempName("lyxvcout");
1460 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1461 return N_("Error: Could not generate logfile.");
1464 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1465 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1466 FileName(owner_->filePath()));
1469 string const res = scanLogFile(tmpf, log);
1471 frontend::Alert::error(_("Revision control error."),
1472 bformat(_("Error when updating from repository.\n"
1473 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1474 "After pressing OK, LyX will try to reopen the resolved document."),
1475 from_local8bit(res)));
1477 fileLock(true, tmpf, log);
1480 return log.empty() ? string() : "SVN: " + log;
1484 bool SVN::checkOutEnabled()
1493 string SVN::repoUpdate()
1495 FileName tmpf = FileName::tempName("lyxvcout");
1497 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1498 return N_("Error: Could not generate logfile.");
1501 doVCCommand("svn diff " + quoteName(owner_->filePath())
1502 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1503 FileName(owner_->filePath()));
1504 docstring res = tmpf.fileContents("UTF-8");
1506 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1507 docstring const file = from_utf8(owner_->filePath());
1508 docstring text = bformat(_("There were detected changes "
1509 "in the working directory:\n%1$s\n\n"
1510 "In case of file conflict version of the local directory files "
1511 "will be preferred."
1512 "\n\nContinue?"), file);
1513 int ret = frontend::Alert::prompt(_("Changes detected"),
1514 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1516 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1517 ret = frontend::Alert::prompt(_("Changes detected"),
1518 text, 0, 1, _("&Yes"), _("&No"));
1519 hideDialogs("file", 0);
1527 // Reverting looks too harsh, see bug #6255.
1528 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1529 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1530 // FileName(owner_->filePath()));
1531 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1532 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1533 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1534 FileName(owner_->filePath()));
1535 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1537 LYXERR(Debug::LYXVC, res);
1539 return to_utf8(res);
1543 bool SVN::repoUpdateEnabled()
1549 string SVN::lockingToggle()
1551 FileName tmpf = FileName::tempName("lyxvcout");
1553 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1554 return N_("Error: Could not generate logfile.");
1557 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1558 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1559 FileName(owner_->filePath()));
1564 string res = scanLogFile(tmpf, log);
1565 bool locking = contains(res, "svn:needs-lock");
1567 ret = doVCCommand("svn propset svn:needs-lock ON "
1568 + quoteName(onlyFileName(owner_->absFileName()))
1569 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1570 FileName(owner_->filePath()));
1572 ret = doVCCommand("svn propdel svn:needs-lock "
1573 + quoteName(onlyFileName(owner_->absFileName()))
1574 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1575 FileName(owner_->filePath()));
1580 frontend::Alert::warning(_("SVN File Locking"),
1581 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1582 + _("Do not forget to commit the locking property into the repository."),
1585 return string("SVN: ") + (locking ?
1586 N_("Locking property unset.") : N_("Locking property set."));
1590 bool SVN::lockingToggleEnabled()
1598 // Reverts to the version in SVN repository and
1599 // gets the updated version from the repository.
1600 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1602 if (doVCCommand("svn revert -q " + fil,
1603 FileName(owner_->filePath())))
1605 owner_->markClean();
1610 bool SVN::isRevertWithConfirmation()
1612 //FIXME owner && diff
1617 void SVN::undoLast()
1619 // merge the current with the previous version
1620 // in a reverse patch kind of way, so that the
1621 // result is to revert the last changes.
1622 lyxerr << "Sorry, not implemented." << endl;
1626 bool SVN::undoLastEnabled()
1632 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1634 if (info == LyXVC::Tree) {
1635 if (rev_tree_cache_.empty())
1636 if (!getTreeRevisionInfo())
1637 rev_tree_cache_ = "?";
1638 if (rev_tree_cache_ == "?")
1641 return rev_tree_cache_;
1644 // fill the rest of the attributes for a single file
1645 if (rev_file_cache_.empty())
1646 if (!getFileRevisionInfo())
1647 rev_file_cache_ = "?";
1651 if (rev_file_cache_ == "?")
1653 return rev_file_cache_;
1655 return rev_author_cache_;
1657 return rev_date_cache_;
1659 return rev_time_cache_;
1668 bool SVN::getFileRevisionInfo()
1670 FileName tmpf = FileName::tempName("lyxvcout");
1672 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1676 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1677 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1678 FileName(owner_->filePath()));
1683 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1691 LYXERR(Debug::LYXVC, line);
1692 if (prefixIs(line, "<commit"))
1694 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1695 string l1 = subst(line, "revision=\"", "");
1696 string l2 = trim(subst(l1, "\">", ""));
1698 rev_file_cache_ = rev = l2;
1700 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1701 string l1 = subst(line, "<author>", "");
1702 string l2 = subst(l1, "</author>", "");
1703 rev_author_cache_ = l2;
1705 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1706 string l1 = subst(line, "<date>", "");
1707 string l2 = subst(l1, "</date>", "");
1708 l2 = split(l2, l1, 'T');
1709 rev_date_cache_ = l1;
1710 l2 = split(l2, l1, '.');
1711 rev_time_cache_ = l1;
1717 return !rev.empty();
1721 bool SVN::getTreeRevisionInfo()
1723 FileName tmpf = FileName::tempName("lyxvcout");
1725 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1729 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1730 FileName(owner_->filePath()));
1735 // only first line in case something bad happens.
1736 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1742 rev_tree_cache_ = line;
1743 return !line.empty();
1747 void SVN::getLog(FileName const & tmpf)
1749 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1750 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1751 FileName(owner_->filePath()));
1755 bool SVN::prepareFileRevision(string const & revis, string & f)
1757 if (!isStrInt(revis))
1760 int rev = convert<int>(revis);
1762 if (!getFileRevisionInfo())
1765 rev = convert<int>(rev_file_cache_);
1766 // go back for minus rev
1768 rev = rev + convert<int>(rev_file_cache_);
1773 string revname = convert<string>(rev);
1774 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1776 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1780 doVCCommand("svn cat -r " + revname + " "
1781 + quoteName(onlyFileName(owner_->absFileName()))
1782 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1783 FileName(owner_->filePath()));
1784 if (tmpf.isFileEmpty())
1787 f = tmpf.absFileName();
1792 bool SVN::prepareFileRevisionEnabled()
1799 bool SVN::toggleReadOnlyEnabled()
1805 /////////////////////////////////////////////////////////////////////
1809 /////////////////////////////////////////////////////////////////////
1811 GIT::GIT(FileName const & m, Buffer * b) : VCS(b)
1813 // Here we know that the buffer file is either already in GIT or
1814 // about to be registered
1820 FileName const GIT::findFile(FileName const & file)
1822 // First we check the existence of repository meta data.
1823 if (!VCS::checkparentdirs(file, ".git")) {
1824 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1828 // Now we check the status of the file.
1829 FileName tmpf = FileName::tempName("lyxvcout");
1831 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1835 string const fname = onlyFileName(file.absFileName());
1836 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1838 doVCCommandCall("git ls-files " +
1839 quoteName(fname) + " > " +
1840 quoteName(tmpf.toFilesystemEncoding()),
1842 bool found = !tmpf.isFileEmpty();
1844 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1845 return found ? file : FileName();
1849 void GIT::scanMaster()
1851 // vcstatus code is somewhat superflous,
1852 // until we want to implement read-only toggle for git.
1853 vcstatus = NOLOCKING;
1857 bool GIT::retrieve(FileName const & file)
1859 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1860 // The caller ensures that file does not exist, so no need to check that.
1861 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1862 file.onlyPath()) == 0;
1866 void GIT::registrer(string const & /*msg*/)
1868 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1869 FileName(owner_->filePath()));
1873 bool GIT::renameEnabled()
1879 string GIT::rename(support::FileName const & newFile, string const & msg)
1881 // git mv does not require a log message, since it does not commit.
1882 // In LyX we commit immediately afterwards, otherwise it could be
1883 // confusing to the user to have two uncommitted files.
1884 FileName path(owner_->filePath());
1885 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1886 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1887 ' ' + quoteName(relFile));
1888 if (doVCCommand(cmd, path)) {
1889 cmd = "git checkout -q " +
1890 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1892 doVCCommand(cmd, path);
1893 if (newFile.exists())
1894 newFile.removeFile();
1897 vector<support::FileName> f;
1898 f.push_back(owner_->fileName());
1899 f.push_back(newFile);
1901 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1902 cmd = "git checkout -q " +
1903 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1905 doVCCommand(cmd, path);
1906 if (newFile.exists())
1907 newFile.removeFile();
1914 bool GIT::copyEnabled()
1920 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1922 // git does not support copy with history preservation
1927 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1929 vector<support::FileName> f(1, owner_->fileName());
1930 return checkIn(f, msg, log);
1934 LyXVC::CommandResult
1935 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1937 FileName tmpf = FileName::tempName("lyxvcout");
1939 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1940 log = N_("Error: Could not generate logfile.");
1941 return LyXVC::ErrorBefore;
1945 os << "git commit -m \"" << msg << '"';
1946 for (size_t i = 0; i < f.size(); ++i)
1947 os << ' ' << quoteName(f[i].onlyFileName());
1948 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1949 LyXVC::CommandResult ret =
1950 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1951 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1953 string res = scanLogFile(tmpf, log);
1955 frontend::Alert::error(_("Revision control error."),
1956 _("Error when committing to repository.\n"
1957 "You have to manually resolve the problem.\n"
1958 "LyX will reopen the document after you press OK."));
1959 ret = LyXVC::ErrorCommand;
1964 log.insert(0, "GIT: ");
1965 if (ret == LyXVC::VCSuccess && log.empty())
1966 log = "GIT: Proceeded";
1971 bool GIT::checkInEnabled()
1977 bool GIT::isCheckInWithConfirmation()
1979 // FIXME one day common getDiff and perhaps OpMode for all backends
1981 FileName tmpf = FileName::tempName("lyxvcout");
1983 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1987 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
1988 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1989 FileName(owner_->filePath()));
1991 docstring diff = tmpf.fileContents("UTF-8");
2001 // FIXME Correctly return code should be checked instead of this.
2002 // This would need another solution than just plain startscript.
2003 // Hint from Andre': QProcess::readAllStandardError()...
2004 string GIT::scanLogFile(FileName const & f, string & status)
2006 ifstream ifs(f.toFilesystemEncoding().c_str());
2011 LYXERR(Debug::LYXVC, line << "\n");
2013 status += line + "; ";
2014 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2015 || contains(line, "Commit failed")) {
2025 string GIT::checkOut()
2031 bool GIT::checkOutEnabled()
2037 string GIT::repoUpdate()
2043 bool GIT::repoUpdateEnabled()
2049 string GIT::lockingToggle()
2055 bool GIT::lockingToggleEnabled()
2063 // Reverts to the version in GIT repository and
2064 // gets the updated version from the repository.
2065 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2067 if (doVCCommand("git checkout -q " + fil,
2068 FileName(owner_->filePath())))
2070 owner_->markClean();
2075 bool GIT::isRevertWithConfirmation()
2077 //FIXME owner && diff
2082 void GIT::undoLast()
2084 // merge the current with the previous version
2085 // in a reverse patch kind of way, so that the
2086 // result is to revert the last changes.
2087 lyxerr << "Sorry, not implemented." << endl;
2091 bool GIT::undoLastEnabled()
2097 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2099 if (info == LyXVC::Tree) {
2100 if (rev_tree_cache_.empty())
2101 if (!getTreeRevisionInfo())
2102 rev_tree_cache_ = "?";
2103 if (rev_tree_cache_ == "?")
2106 return rev_tree_cache_;
2109 // fill the rest of the attributes for a single file
2110 if (rev_file_cache_.empty())
2111 if (!getFileRevisionInfo())
2112 rev_file_cache_ = "?";
2116 if (rev_file_cache_ == "?")
2118 return rev_file_cache_;
2120 return rev_author_cache_;
2122 return rev_date_cache_;
2124 return rev_time_cache_;
2133 bool GIT::getFileRevisionInfo()
2135 FileName tmpf = FileName::tempName("lyxvcout");
2137 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2141 doVCCommand("git log -n 1 --pretty=format:%H%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2142 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2143 FileName(owner_->filePath()));
2148 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2151 getline(ifs, rev_file_cache_);
2153 getline(ifs, rev_author_cache_);
2157 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2162 return !rev_file_cache_.empty();
2166 bool GIT::getTreeRevisionInfo()
2168 FileName tmpf = FileName::tempName("lyxvcout");
2170 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2174 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2175 FileName(owner_->filePath()));
2180 // only first line in case something bad happens.
2181 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2182 getline(ifs, rev_tree_cache_);
2186 return !rev_tree_cache_.empty();
2190 void GIT::getLog(FileName const & tmpf)
2192 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2193 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2194 FileName(owner_->filePath()));
2198 //at this moment we don't accept revision SHA, but just number of revision steps back
2199 //GUI and infrastucture needs to be changed first
2200 bool GIT::prepareFileRevision(string const & revis, string & f)
2202 // anything positive means we got hash, not "0" or minus revision
2205 // hash is rarely number and should be long
2206 if (isStrInt(revis) && revis.length()<20)
2207 rev = convert<int>(revis);
2209 // revision and filename
2212 // go back for "minus" revisions
2214 pointer = "HEAD~" + convert<string>(-rev);
2221 FileName tmpf = FileName::tempName("lyxvcrev_" + revis + "_");
2223 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2227 doVCCommand("git show " + pointer + "./"
2228 + quoteName(onlyFileName(owner_->absFileName()))
2229 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2230 FileName(owner_->filePath()));
2231 if (tmpf.isFileEmpty())
2234 f = tmpf.absFileName();
2239 bool GIT::prepareFileRevisionEnabled()
2245 bool GIT::toggleReadOnlyEnabled()