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;
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 & vcsdir)
104 FileName dirname = file.onlyPath();
106 FileName tocheck = FileName(addName(dirname.absFileName(), vcsdir));
107 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
108 if (tocheck.exists())
110 //this construct because of #8295
111 dirname = FileName(dirname.absFileName()).parentPath();
112 } while (!dirname.empty());
117 /////////////////////////////////////////////////////////////////////
121 /////////////////////////////////////////////////////////////////////
123 RCS::RCS(FileName const & m, Buffer * b) : VCS(b)
125 // Here we know that the buffer file is either already in RCS or
126 // about to be registered
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 bool RCS::renameEnabled()
242 string RCS::rename(support::FileName const & /*newFile*/, string const & /*msg*/)
244 // not implemented, since a left-over file.lyx,v would be confusing.
249 bool RCS::copyEnabled()
255 string RCS::copy(support::FileName const & newFile, string const & msg)
257 // RCS has no real copy command, so we create a poor mans version
258 support::FileName const oldFile(owner_->absFileName());
259 if (!oldFile.copyTo(newFile))
261 FileName path(oldFile.onlyPath());
262 string relFile(to_utf8(newFile.relPath(path.absFileName())));
263 string cmd = "ci -q -u -i -t-\"";
266 cmd += quoteName(relFile);
267 return doVCCommand(cmd, path) ? string() : "RCS: Proceeded";
271 LyXVC::CommandResult RCS::checkIn(string const & msg, string & log)
273 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
274 + quoteName(onlyFileName(owner_->absFileName())),
275 FileName(owner_->filePath()));
277 return LyXVC::ErrorCommand;
278 log = "RCS: Proceeded";
279 return LyXVC::VCSuccess;
283 bool RCS::checkInEnabled()
285 return owner_ && !owner_->isReadonly();
289 bool RCS::isCheckInWithConfirmation()
291 // FIXME one day common getDiff for all backends
293 // if (getDiff(file, diff) && diff.empty())
296 FileName tmpf = FileName::tempName("lyxvcout");
298 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
302 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
303 + " > " + quoteName(tmpf.toFilesystemEncoding()),
304 FileName(owner_->filePath()));
306 docstring diff = tmpf.fileContents("UTF-8");
316 string RCS::checkOut()
319 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
320 FileName(owner_->filePath()));
321 return ret ? string() : "RCS: Proceeded";
325 bool RCS::checkOutEnabled()
327 return owner_ && owner_->isReadonly();
331 string RCS::repoUpdate()
333 lyxerr << "Sorry, not implemented." << endl;
338 bool RCS::repoUpdateEnabled()
344 string RCS::lockingToggle()
346 //FIXME this might be actually possible, study rcs -U, rcs -L.
347 //State should be easy to get inside scanMaster.
348 //It would fix #4370 and make rcs/svn usage even more closer.
349 lyxerr << "Sorry, not implemented." << endl;
354 bool RCS::lockingToggleEnabled()
362 if (doVCCommand("co -f -u" + version_ + " "
363 + quoteName(onlyFileName(owner_->absFileName())),
364 FileName(owner_->filePath())))
366 // We ignore changes and just reload!
372 bool RCS::isRevertWithConfirmation()
374 //FIXME owner && diff ?
381 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
382 doVCCommand("rcs -o" + version_ + " "
383 + quoteName(onlyFileName(owner_->absFileName())),
384 FileName(owner_->filePath()));
388 bool RCS::undoLastEnabled()
394 void RCS::getLog(FileName const & tmpf)
396 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
397 + " > " + quoteName(tmpf.toFilesystemEncoding()),
398 FileName(owner_->filePath()));
402 bool RCS::toggleReadOnlyEnabled()
404 // This got broken somewhere along lfuns dispatch reorganization.
405 // reloadBuffer would be needed after this, but thats problematic
406 // since we are inside Buffer::dispatch.
412 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
414 if (info == LyXVC::File)
416 // fill the rest of the attributes for a single file
417 if (rev_date_cache_.empty())
418 if (!getRevisionInfo())
423 return rev_author_cache_;
425 return rev_date_cache_;
427 return rev_time_cache_;
435 bool RCS::getRevisionInfo()
437 FileName tmpf = FileName::tempName("lyxvcout");
439 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
442 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
443 + " > " + quoteName(tmpf.toFilesystemEncoding()),
444 FileName(owner_->filePath()));
449 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
452 // we reached to the entry, i.e. after initial log message
454 // line with critical info, e.g:
455 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
460 LYXERR(Debug::LYXVC, line);
461 if (entry && prefixIs(line, "date:")) {
465 if (prefixIs(line, "revision"))
471 rev_date_cache_ = token(result, ' ', 1);
472 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
473 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
475 return !rev_author_cache_.empty();
478 bool RCS::prepareFileRevision(string const &revis, string & f)
481 if (!VCS::makeRCSRevision(version_, rev))
484 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
486 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
490 doVCCommand("co -p" + rev + " "
491 + quoteName(onlyFileName(owner_->absFileName()))
492 + " > " + quoteName(tmpf.toFilesystemEncoding()),
493 FileName(owner_->filePath()));
494 if (tmpf.isFileEmpty())
497 f = tmpf.absFileName();
502 bool RCS::prepareFileRevisionEnabled()
508 /////////////////////////////////////////////////////////////////////
512 /////////////////////////////////////////////////////////////////////
514 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
516 // Here we know that the buffer file is either already in CVS or
517 // about to be registered
519 have_rev_info_ = false;
524 FileName const CVS::findFile(FileName const & file)
526 // First we look for the CVS/Entries in the same dir
527 // where we have file.
528 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
529 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
530 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
531 << "' for `" << tmpf << '\'');
532 if (entries.isReadableFile()) {
533 // Ok we are at least in a CVS dir. Parse the CVS/Entries
534 // and see if we can find this file. We do a fast and
536 ifstream ifs(entries.toFilesystemEncoding().c_str());
538 while (getline(ifs, line)) {
539 LYXERR(Debug::LYXVC, "\tEntries: " << line);
540 if (contains(line, tmpf))
548 void CVS::scanMaster()
550 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
551 // Ok now we do the real scan...
552 ifstream ifs(master_.toFilesystemEncoding().c_str());
553 string name = onlyFileName(owner_->absFileName());
554 string tmpf = '/' + name + '/';
555 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
557 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
558 while (getline(ifs, line)) {
559 LYXERR(Debug::LYXVC, "\t line: " << line);
560 if (contains(line, tmpf)) {
561 // Ok extract the fields.
564 regex_match(line, sm, reg);
566 //sm[0]; // whole matched string
568 version_ = sm.str(2);
569 string const file_date = sm.str(3);
572 //sm[5]; // tag or tagdate
573 FileName file(owner_->absFileName());
574 if (file.isReadableFile()) {
575 time_t mod = file.lastModified();
576 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
577 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
578 << "'\nModification date of file: `" << mod_date << '\'');
579 if (file.isReadOnly()) {
580 // readonly checkout is unlocked
583 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
584 FileName base(addName(bdir.absFileName(),name));
585 // if base version is existent "cvs edit" was used to lock
586 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
589 vcstatus = NOLOCKING;
597 bool CVS::retrieve(FileName const & file)
599 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
600 // The caller ensures that file does not exist, so no need to check that.
601 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
602 file.onlyPath()) == 0;
606 string const CVS::getTarget(OperationMode opmode) const
610 // in client server mode CVS does not like full path operand for directory operation
611 // since LyX switches to the repo dir "." is good enough as target
614 return quoteName(onlyFileName(owner_->absFileName()));
620 docstring CVS::toString(CvsStatus status) const
624 return _("Up-to-date");
625 case LocallyModified:
626 return _("Locally Modified");
628 return _("Locally Added");
630 return _("Needs Merge");
632 return _("Needs Checkout");
634 return _("No CVS file");
636 return _("Cannot retrieve CVS status");
642 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
643 FileName const & output, bool reportError)
645 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
646 return doVCCommand(cmd + redirection, path, reportError);
650 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
651 support::FileName const & path,
652 support::FileName const & output)
654 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
655 return doVCCommandCall(cmd + redirection, path);
659 CVS::CvsStatus CVS::getStatus()
661 FileName tmpf = FileName::tempName("lyxvcout");
663 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
667 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
668 FileName(owner_->filePath()), tmpf)) {
673 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
674 CvsStatus status = NoCvsFile;
679 LYXERR(Debug::LYXVC, line << "\n");
680 if (prefixIs(line, "File:")) {
681 if (contains(line, "Up-to-date"))
683 else if (contains(line, "Locally Modified"))
684 status = LocallyModified;
685 else if (contains(line, "Locally Added"))
686 status = LocallyAdded;
687 else if (contains(line, "Needs Merge"))
689 else if (contains(line, "Needs Checkout"))
690 status = NeedsCheckout;
697 void CVS::getRevisionInfo()
701 have_rev_info_ = true;
702 FileName tmpf = FileName::tempName("lyxvcout");
704 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
708 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
709 + " " + getTarget(File),
710 FileName(owner_->filePath()), tmpf);
713 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
717 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
718 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
723 LYXERR(Debug::LYXVC, line << "\n");
724 if (prefixIs(line, "date:")) {
726 regex_match(line, sm, reg);
727 //sm[0]; // whole matched string
728 rev_date_cache_ = sm[1];
729 rev_time_cache_ = sm[2];
730 //sm[3]; // GMT offset
731 rev_author_cache_ = sm[4];
736 if (rev_author_cache_.empty())
738 "Could not retrieve revision info for " << version_ <<
739 " of " << getTarget(File));
743 void CVS::registrer(string const & msg)
745 doVCCommand("cvs -q add -m \"" + msg + "\" "
747 FileName(owner_->filePath()));
751 bool CVS::renameEnabled()
757 string CVS::rename(support::FileName const & newFile, string const & msg)
759 // CVS has no real rename command, so we create a poor mans version
760 support::FileName const oldFile(owner_->absFileName());
761 string ret = copy(newFile, msg);
764 string cmd = "cvs -q remove -m \"" + msg + "\" " +
765 quoteName(oldFile.onlyFileName());
766 FileName path(oldFile.onlyPath());
767 return doVCCommand(cmd, path) ? string() : ret;
771 bool CVS::copyEnabled()
777 string CVS::copy(support::FileName const & newFile, string const & msg)
779 // CVS has no real copy command, so we create a poor mans version
780 support::FileName const oldFile(owner_->absFileName());
781 if (!oldFile.copyTo(newFile))
783 FileName path(oldFile.onlyPath());
784 string relFile(to_utf8(newFile.relPath(path.absFileName())));
785 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
786 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
790 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
792 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
793 FileName(owner_->filePath()), tmpf, false);
800 return doVCCommand("cvs -q edit " + getTarget(File),
801 FileName(owner_->filePath()));
808 return doVCCommand("cvs -q unedit " + getTarget(File),
809 FileName(owner_->filePath()));
813 int CVS::update(OperationMode opmode, FileName const & tmpf)
815 return doVCCommandWithOutput("cvs -q update "
817 FileName(owner_->filePath()), tmpf, false);
821 string CVS::scanLogFile(FileName const & f, string & status)
823 ifstream ifs(f.toFilesystemEncoding().c_str());
828 LYXERR(Debug::LYXVC, line << "\n");
830 status += line + "; ";
831 if (prefixIs(line, "C ")) {
841 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
843 CvsStatus status = getStatus();
846 if (vcstatus != NOLOCKING)
848 return LyXVC::ErrorCommand;
849 log = "CVS: Proceeded";
850 return LyXVC::VCSuccess;
851 case LocallyModified:
853 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
855 FileName(owner_->filePath()));
857 return LyXVC::ErrorCommand;
858 log = "CVS: Proceeded";
859 return LyXVC::VCSuccess;
863 frontend::Alert::error(_("Revision control error."),
864 _("The repository version is newer then the current check out.\n"
865 "You have to update from repository first or revert your changes.")) ;
868 frontend::Alert::error(_("Revision control error."),
869 bformat(_("Bad status when checking in changes.\n"
874 return LyXVC::ErrorBefore;
878 bool CVS::isLocked() const
880 FileName fn(owner_->absFileName());
882 return !fn.isReadOnly();
886 bool CVS::checkInEnabled()
888 if (vcstatus != NOLOCKING)
895 bool CVS::isCheckInWithConfirmation()
897 CvsStatus status = getStatus();
898 return status == LocallyModified || status == LocallyAdded;
902 string CVS::checkOut()
904 if (vcstatus != NOLOCKING && edit())
906 FileName tmpf = FileName::tempName("lyxvcout");
908 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
912 int rc = update(File, tmpf);
914 string const res = scanLogFile(tmpf, log);
916 frontend::Alert::error(_("Revision control error."),
917 bformat(_("Error when updating from repository.\n"
918 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
919 "After pressing OK, LyX will try to reopen the resolved document."),
920 from_local8bit(res)));
925 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
929 bool CVS::checkOutEnabled()
931 if (vcstatus != NOLOCKING)
938 string CVS::repoUpdate()
940 FileName tmpf = FileName::tempName("lyxvcout");
942 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
946 getDiff(Directory, tmpf);
947 docstring res = tmpf.fileContents("UTF-8");
949 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
950 docstring const file = from_utf8(owner_->filePath());
951 docstring text = bformat(_("There were detected changes "
952 "in the working directory:\n%1$s\n\n"
953 "Possible file conflicts must be then resolved manually "
954 "or you will need to revert back to the repository version."), file);
955 int ret = frontend::Alert::prompt(_("Changes detected"),
956 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
958 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
959 ret = frontend::Alert::prompt(_("Changes detected"),
960 text, 0, 1, _("&Continue"), _("&Abort"));
961 hideDialogs("file", 0);
969 int rc = update(Directory, tmpf);
970 res += "Update log:\n" + tmpf.fileContents("UTF-8");
971 LYXERR(Debug::LYXVC, res);
974 string sres = scanLogFile(tmpf, log);
976 docstring const file = owner_->fileName().displayName(20);
977 frontend::Alert::error(_("Revision control error."),
978 bformat(_("Error when updating document %1$s from repository.\n"
979 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
980 "After pressing OK, LyX will try to reopen the resolved document."),
981 file, from_local8bit(sres)));
987 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
991 bool CVS::repoUpdateEnabled()
997 string CVS::lockingToggle()
999 lyxerr << "Sorry, not implemented." << endl;
1004 bool CVS::lockingToggleEnabled()
1010 bool CVS::isRevertWithConfirmation()
1012 CvsStatus status = getStatus();
1013 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1019 // Reverts to the version in CVS repository and
1020 // gets the updated version from the repository.
1021 CvsStatus status = getStatus();
1024 if (vcstatus != NOLOCKING)
1025 return 0 == unedit();
1029 case LocallyModified: {
1030 FileName f(owner_->absFileName());
1032 update(File, FileName());
1033 owner_->markClean();
1036 case LocallyAdded: {
1037 docstring const file = owner_->fileName().displayName(20);
1038 frontend::Alert::error(_("Revision control error."),
1039 bformat(_("The document %1$s is not in repository.\n"
1040 "You have to check in the first revision before you can revert."),
1045 docstring const file = owner_->fileName().displayName(20);
1046 frontend::Alert::error(_("Revision control error."),
1047 bformat(_("Cannot revert document %1$s to repository version.\n"
1048 "The status '%2$s' is unexpected."),
1049 file, toString(status)));
1057 void CVS::undoLast()
1059 // merge the current with the previous version
1060 // in a reverse patch kind of way, so that the
1061 // result is to revert the last changes.
1062 lyxerr << "Sorry, not implemented." << endl;
1066 bool CVS::undoLastEnabled()
1072 void CVS::getLog(FileName const & tmpf)
1074 doVCCommandWithOutput("cvs log " + getTarget(File),
1075 FileName(owner_->filePath()),
1080 bool CVS::toggleReadOnlyEnabled()
1086 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1088 if (!version_.empty()) {
1094 return rev_author_cache_;
1096 return rev_date_cache_;
1098 return rev_time_cache_;
1106 bool CVS::prepareFileRevision(string const & revis, string & f)
1109 if (!VCS::makeRCSRevision(version_, rev))
1112 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
1114 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1118 doVCCommandWithOutput("cvs update -p -r" + rev + " "
1120 FileName(owner_->filePath()), tmpf);
1121 if (tmpf.isFileEmpty())
1124 f = tmpf.absFileName();
1129 bool CVS::prepareFileRevisionEnabled()
1135 /////////////////////////////////////////////////////////////////////
1139 /////////////////////////////////////////////////////////////////////
1141 SVN::SVN(FileName const & m, Buffer * b) : VCS(b)
1143 // Here we know that the buffer file is either already in SVN or
1144 // about to be registered
1151 FileName const SVN::findFile(FileName const & file)
1153 // First we check the existence of repository meta data.
1154 if (!VCS::checkparentdirs(file, ".svn")) {
1155 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1159 // Now we check the status of the file.
1160 FileName tmpf = FileName::tempName("lyxvcout");
1162 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1166 string const fname = onlyFileName(file.absFileName());
1167 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1168 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1169 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1172 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1173 return found ? file : FileName();
1177 void SVN::scanMaster()
1179 // vcstatus code is somewhat superflous,
1180 // until we want to implement read-only toggle for svn.
1181 vcstatus = NOLOCKING;
1182 if (checkLockMode()) {
1186 vcstatus = UNLOCKED;
1191 bool SVN::checkLockMode()
1193 FileName tmpf = FileName::tempName("lyxvcout");
1195 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1199 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1200 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1201 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1202 FileName(owner_->filePath())))
1205 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1209 while (ifs && !ret) {
1211 LYXERR(Debug::LYXVC, line);
1212 if (contains(line, "svn:needs-lock"))
1215 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1223 bool SVN::isLocked() const
1225 FileName file(owner_->absFileName());
1227 return !file.isReadOnly();
1231 bool SVN::retrieve(FileName const & file)
1233 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1234 // The caller ensures that file does not exist, so no need to check that.
1235 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1236 file.onlyPath()) == 0;
1240 void SVN::registrer(string const & /*msg*/)
1242 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1243 FileName(owner_->filePath()));
1247 bool SVN::renameEnabled()
1253 string SVN::rename(support::FileName const & newFile, string const & msg)
1255 // svn move does not require a log message, since it does not commit.
1256 // In LyX we commit immediately afterwards, otherwise it could be
1257 // confusing to the user to have two uncommitted files.
1258 FileName path(owner_->filePath());
1259 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1260 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1261 ' ' + quoteName(relFile));
1262 if (doVCCommand(cmd, path)) {
1263 cmd = "svn revert -q " +
1264 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1266 doVCCommand(cmd, path);
1267 if (newFile.exists())
1268 newFile.removeFile();
1271 vector<support::FileName> f;
1272 f.push_back(owner_->fileName());
1273 f.push_back(newFile);
1275 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1276 cmd = "svn revert -q " +
1277 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1279 doVCCommand(cmd, path);
1280 if (newFile.exists())
1281 newFile.removeFile();
1288 bool SVN::copyEnabled()
1294 string SVN::copy(support::FileName const & newFile, string const & msg)
1296 // svn copy does not require a log message, since it does not commit.
1297 // In LyX we commit immediately afterwards, otherwise it could be
1298 // confusing to the user to have an uncommitted file.
1299 FileName path(owner_->filePath());
1300 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1301 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1302 ' ' + quoteName(relFile));
1303 if (doVCCommand(cmd, path))
1305 vector<support::FileName> f(1, newFile);
1307 if (checkIn(f, msg, log) == LyXVC::VCSuccess)
1313 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1315 vector<support::FileName> f(1, owner_->fileName());
1316 return checkIn(f, msg, log);
1320 LyXVC::CommandResult
1321 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1323 FileName tmpf = FileName::tempName("lyxvcout");
1325 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1326 log = N_("Error: Could not generate logfile.");
1327 return LyXVC::ErrorBefore;
1331 os << "svn commit -m \"" << msg << '"';
1332 for (size_t i = 0; i < f.size(); ++i)
1333 os << ' ' << quoteName(f[i].onlyFileName());
1334 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1335 LyXVC::CommandResult ret =
1336 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1337 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1339 string res = scanLogFile(tmpf, log);
1341 frontend::Alert::error(_("Revision control error."),
1342 _("Error when committing to repository.\n"
1343 "You have to manually resolve the problem.\n"
1344 "LyX will reopen the document after you press OK."));
1345 ret = LyXVC::ErrorCommand;
1348 if (!fileLock(false, tmpf, log))
1349 ret = LyXVC::ErrorCommand;
1353 log.insert(0, "SVN: ");
1354 if (ret == LyXVC::VCSuccess && log.empty())
1355 log = "SVN: Proceeded";
1360 bool SVN::checkInEnabled()
1369 bool SVN::isCheckInWithConfirmation()
1371 // FIXME one day common getDiff and perhaps OpMode for all backends
1373 FileName tmpf = FileName::tempName("lyxvcout");
1375 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1379 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1380 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1381 FileName(owner_->filePath()));
1383 docstring diff = tmpf.fileContents("UTF-8");
1393 // FIXME Correctly return code should be checked instead of this.
1394 // This would need another solution than just plain startscript.
1395 // Hint from Andre': QProcess::readAllStandardError()...
1396 string SVN::scanLogFile(FileName const & f, string & status)
1398 ifstream ifs(f.toFilesystemEncoding().c_str());
1403 LYXERR(Debug::LYXVC, line << "\n");
1405 status += line + "; ";
1406 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1407 || contains(line, "Commit failed")) {
1411 if (contains(line, "svn:needs-lock")) {
1421 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1423 if (!locked_mode_ || (isLocked() == lock))
1426 string const arg = lock ? "lock " : "unlock ";
1427 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1428 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1429 FileName(owner_->filePath()));
1431 // Lock error messages go unfortunately on stderr and are unreachible this way.
1432 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1436 if (!line.empty()) status += line + "; ";
1440 if (isLocked() == lock)
1444 frontend::Alert::error(_("Revision control error."),
1445 _("Error while acquiring write lock.\n"
1446 "Another user is most probably editing\n"
1447 "the current document now!\n"
1448 "Also check the access to the repository."));
1450 frontend::Alert::error(_("Revision control error."),
1451 _("Error while releasing write lock.\n"
1452 "Check the access to the repository."));
1457 string SVN::checkOut()
1459 FileName tmpf = FileName::tempName("lyxvcout");
1461 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1462 return N_("Error: Could not generate logfile.");
1465 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1466 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1467 FileName(owner_->filePath()));
1470 string const res = scanLogFile(tmpf, log);
1472 frontend::Alert::error(_("Revision control error."),
1473 bformat(_("Error when updating from repository.\n"
1474 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1475 "After pressing OK, LyX will try to reopen the resolved document."),
1476 from_local8bit(res)));
1478 fileLock(true, tmpf, log);
1481 return log.empty() ? string() : "SVN: " + log;
1485 bool SVN::checkOutEnabled()
1494 string SVN::repoUpdate()
1496 FileName tmpf = FileName::tempName("lyxvcout");
1498 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1499 return N_("Error: Could not generate logfile.");
1502 doVCCommand("svn diff " + quoteName(owner_->filePath())
1503 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1504 FileName(owner_->filePath()));
1505 docstring res = tmpf.fileContents("UTF-8");
1507 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1508 docstring const file = from_utf8(owner_->filePath());
1509 docstring text = bformat(_("There were detected changes "
1510 "in the working directory:\n%1$s\n\n"
1511 "In case of file conflict version of the local directory files "
1512 "will be preferred."
1513 "\n\nContinue?"), file);
1514 int ret = frontend::Alert::prompt(_("Changes detected"),
1515 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1517 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1518 ret = frontend::Alert::prompt(_("Changes detected"),
1519 text, 0, 1, _("&Yes"), _("&No"));
1520 hideDialogs("file", 0);
1528 // Reverting looks too harsh, see bug #6255.
1529 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1530 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1531 // FileName(owner_->filePath()));
1532 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1533 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1534 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1535 FileName(owner_->filePath()));
1536 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1538 LYXERR(Debug::LYXVC, res);
1540 return to_utf8(res);
1544 bool SVN::repoUpdateEnabled()
1550 string SVN::lockingToggle()
1552 FileName tmpf = FileName::tempName("lyxvcout");
1554 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1555 return N_("Error: Could not generate logfile.");
1558 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1559 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1560 FileName(owner_->filePath()));
1565 string res = scanLogFile(tmpf, log);
1566 bool locking = contains(res, "svn:needs-lock");
1568 ret = doVCCommand("svn propset svn:needs-lock ON "
1569 + quoteName(onlyFileName(owner_->absFileName()))
1570 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1571 FileName(owner_->filePath()));
1573 ret = doVCCommand("svn propdel svn:needs-lock "
1574 + quoteName(onlyFileName(owner_->absFileName()))
1575 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1576 FileName(owner_->filePath()));
1581 frontend::Alert::warning(_("SVN File Locking"),
1582 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1583 + _("Do not forget to commit the locking property into the repository."),
1586 return string("SVN: ") + (locking ?
1587 N_("Locking property unset.") : N_("Locking property set."));
1591 bool SVN::lockingToggleEnabled()
1599 // Reverts to the version in SVN repository and
1600 // gets the updated version from the repository.
1601 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1603 if (doVCCommand("svn revert -q " + fil,
1604 FileName(owner_->filePath())))
1606 owner_->markClean();
1611 bool SVN::isRevertWithConfirmation()
1613 //FIXME owner && diff
1618 void SVN::undoLast()
1620 // merge the current with the previous version
1621 // in a reverse patch kind of way, so that the
1622 // result is to revert the last changes.
1623 lyxerr << "Sorry, not implemented." << endl;
1627 bool SVN::undoLastEnabled()
1633 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1635 if (info == LyXVC::Tree) {
1636 if (rev_tree_cache_.empty())
1637 if (!getTreeRevisionInfo())
1638 rev_tree_cache_ = "?";
1639 if (rev_tree_cache_ == "?")
1642 return rev_tree_cache_;
1645 // fill the rest of the attributes for a single file
1646 if (rev_file_cache_.empty())
1647 if (!getFileRevisionInfo())
1648 rev_file_cache_ = "?";
1652 if (rev_file_cache_ == "?")
1654 return rev_file_cache_;
1656 return rev_author_cache_;
1658 return rev_date_cache_;
1660 return rev_time_cache_;
1669 bool SVN::getFileRevisionInfo()
1671 FileName tmpf = FileName::tempName("lyxvcout");
1673 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1677 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1678 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1679 FileName(owner_->filePath()));
1684 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1692 LYXERR(Debug::LYXVC, line);
1693 if (prefixIs(line, "<commit"))
1695 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1696 string l1 = subst(line, "revision=\"", "");
1697 string l2 = trim(subst(l1, "\">", ""));
1699 rev_file_cache_ = rev = l2;
1701 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1702 string l1 = subst(line, "<author>", "");
1703 string l2 = subst(l1, "</author>", "");
1704 rev_author_cache_ = l2;
1706 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1707 string l1 = subst(line, "<date>", "");
1708 string l2 = subst(l1, "</date>", "");
1709 l2 = split(l2, l1, 'T');
1710 rev_date_cache_ = l1;
1711 l2 = split(l2, l1, '.');
1712 rev_time_cache_ = l1;
1718 return !rev.empty();
1722 bool SVN::getTreeRevisionInfo()
1724 FileName tmpf = FileName::tempName("lyxvcout");
1726 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1730 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1731 FileName(owner_->filePath()));
1736 // only first line in case something bad happens.
1737 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1743 rev_tree_cache_ = line;
1744 return !line.empty();
1748 void SVN::getLog(FileName const & tmpf)
1750 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1751 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1752 FileName(owner_->filePath()));
1756 bool SVN::prepareFileRevision(string const & revis, string & f)
1758 if (!isStrInt(revis))
1761 int rev = convert<int>(revis);
1763 if (!getFileRevisionInfo())
1766 rev = convert<int>(rev_file_cache_);
1767 // go back for minus rev
1769 rev = rev + convert<int>(rev_file_cache_);
1774 string revname = convert<string>(rev);
1775 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1777 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1781 doVCCommand("svn cat -r " + revname + " "
1782 + quoteName(onlyFileName(owner_->absFileName()))
1783 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1784 FileName(owner_->filePath()));
1785 if (tmpf.isFileEmpty())
1788 f = tmpf.absFileName();
1793 bool SVN::prepareFileRevisionEnabled()
1800 bool SVN::toggleReadOnlyEnabled()
1806 /////////////////////////////////////////////////////////////////////
1810 /////////////////////////////////////////////////////////////////////
1812 GIT::GIT(FileName const & m, Buffer * b) : VCS(b)
1814 // Here we know that the buffer file is either already in GIT or
1815 // about to be registered
1821 FileName const GIT::findFile(FileName const & file)
1823 // First we check the existence of repository meta data.
1824 if (!VCS::checkparentdirs(file, ".git")) {
1825 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1829 // Now we check the status of the file.
1830 FileName tmpf = FileName::tempName("lyxvcout");
1832 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1836 string const fname = onlyFileName(file.absFileName());
1837 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1839 doVCCommandCall("git ls-files " +
1840 quoteName(fname) + " > " +
1841 quoteName(tmpf.toFilesystemEncoding()),
1843 bool found = !tmpf.isFileEmpty();
1845 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1846 return found ? file : FileName();
1850 void GIT::scanMaster()
1852 // vcstatus code is somewhat superflous,
1853 // until we want to implement read-only toggle for git.
1854 vcstatus = NOLOCKING;
1858 bool GIT::retrieve(FileName const & file)
1860 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1861 // The caller ensures that file does not exist, so no need to check that.
1862 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1863 file.onlyPath()) == 0;
1867 void GIT::registrer(string const & /*msg*/)
1869 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1870 FileName(owner_->filePath()));
1874 bool GIT::renameEnabled()
1880 string GIT::rename(support::FileName const & newFile, string const & msg)
1882 // git mv does not require a log message, since it does not commit.
1883 // In LyX we commit immediately afterwards, otherwise it could be
1884 // confusing to the user to have two uncommitted files.
1885 FileName path(owner_->filePath());
1886 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1887 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1888 ' ' + quoteName(relFile));
1889 if (doVCCommand(cmd, path)) {
1890 cmd = "git checkout -q " +
1891 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1893 doVCCommand(cmd, path);
1894 if (newFile.exists())
1895 newFile.removeFile();
1898 vector<support::FileName> f;
1899 f.push_back(owner_->fileName());
1900 f.push_back(newFile);
1902 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1903 cmd = "git checkout -q " +
1904 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1906 doVCCommand(cmd, path);
1907 if (newFile.exists())
1908 newFile.removeFile();
1915 bool GIT::copyEnabled()
1921 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1923 // git does not support copy with history preservation
1928 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1930 vector<support::FileName> f(1, owner_->fileName());
1931 return checkIn(f, msg, log);
1935 LyXVC::CommandResult
1936 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1938 FileName tmpf = FileName::tempName("lyxvcout");
1940 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1941 log = N_("Error: Could not generate logfile.");
1942 return LyXVC::ErrorBefore;
1946 os << "git commit -m \"" << msg << '"';
1947 for (size_t i = 0; i < f.size(); ++i)
1948 os << ' ' << quoteName(f[i].onlyFileName());
1949 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1950 LyXVC::CommandResult ret =
1951 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1952 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1954 string res = scanLogFile(tmpf, log);
1956 frontend::Alert::error(_("Revision control error."),
1957 _("Error when committing to repository.\n"
1958 "You have to manually resolve the problem.\n"
1959 "LyX will reopen the document after you press OK."));
1960 ret = LyXVC::ErrorCommand;
1965 log.insert(0, "GIT: ");
1966 if (ret == LyXVC::VCSuccess && log.empty())
1967 log = "GIT: Proceeded";
1972 bool GIT::checkInEnabled()
1978 bool GIT::isCheckInWithConfirmation()
1980 // FIXME one day common getDiff and perhaps OpMode for all backends
1982 FileName tmpf = FileName::tempName("lyxvcout");
1984 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1988 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
1989 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1990 FileName(owner_->filePath()));
1992 docstring diff = tmpf.fileContents("UTF-8");
2002 // FIXME Correctly return code should be checked instead of this.
2003 // This would need another solution than just plain startscript.
2004 // Hint from Andre': QProcess::readAllStandardError()...
2005 string GIT::scanLogFile(FileName const & f, string & status)
2007 ifstream ifs(f.toFilesystemEncoding().c_str());
2012 LYXERR(Debug::LYXVC, line << "\n");
2014 status += line + "; ";
2015 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2016 || contains(line, "Commit failed")) {
2026 string GIT::checkOut()
2032 bool GIT::checkOutEnabled()
2038 string GIT::repoUpdate()
2044 bool GIT::repoUpdateEnabled()
2050 string GIT::lockingToggle()
2056 bool GIT::lockingToggleEnabled()
2064 // Reverts to the version in GIT repository and
2065 // gets the updated version from the repository.
2066 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2068 if (doVCCommand("git checkout -q " + fil,
2069 FileName(owner_->filePath())))
2071 owner_->markClean();
2076 bool GIT::isRevertWithConfirmation()
2078 //FIXME owner && diff
2083 void GIT::undoLast()
2085 // merge the current with the previous version
2086 // in a reverse patch kind of way, so that the
2087 // result is to revert the last changes.
2088 lyxerr << "Sorry, not implemented." << endl;
2092 bool GIT::undoLastEnabled()
2098 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2100 if (info == LyXVC::Tree) {
2101 if (rev_tree_cache_.empty())
2102 if (!getTreeRevisionInfo())
2103 rev_tree_cache_ = "?";
2104 if (rev_tree_cache_ == "?")
2107 return rev_tree_cache_;
2110 // fill the rest of the attributes for a single file
2111 if (rev_file_cache_.empty())
2112 if (!getFileRevisionInfo())
2113 rev_file_cache_ = "?";
2117 if (rev_file_cache_ == "?")
2119 return rev_file_cache_;
2121 return rev_author_cache_;
2123 return rev_date_cache_;
2125 return rev_time_cache_;
2134 bool GIT::getFileRevisionInfo()
2136 FileName tmpf = FileName::tempName("lyxvcout");
2138 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2142 doVCCommand("git log -n 1 --pretty=format:%H%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2143 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2144 FileName(owner_->filePath()));
2149 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2152 getline(ifs, rev_file_cache_);
2154 getline(ifs, rev_author_cache_);
2158 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2163 return !rev_file_cache_.empty();
2167 bool GIT::getTreeRevisionInfo()
2169 FileName tmpf = FileName::tempName("lyxvcout");
2171 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2175 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2176 FileName(owner_->filePath()));
2181 // only first line in case something bad happens.
2182 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2183 getline(ifs, rev_tree_cache_);
2187 return !rev_tree_cache_.empty();
2191 void GIT::getLog(FileName const & tmpf)
2193 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2194 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2195 FileName(owner_->filePath()));
2199 //at this moment we don't accept revision SHA, but just number of revision steps back
2200 //GUI and infrastucture needs to be changed first
2201 bool GIT::prepareFileRevision(string const & revis, string & f)
2203 // anything positive means we got hash, not "0" or minus revision
2206 // hash is rarely number and should be long
2207 if (isStrInt(revis) && revis.length()<20)
2208 rev = convert<int>(revis);
2210 // revision and filename
2213 // go back for "minus" revisions
2215 pointer = "HEAD~" + convert<string>(-rev);
2222 FileName tmpf = FileName::tempName("lyxvcrev_" + revis + "_");
2224 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2228 doVCCommand("git show " + pointer + "./"
2229 + quoteName(onlyFileName(owner_->absFileName()))
2230 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2231 FileName(owner_->filePath()));
2232 if (tmpf.isFileEmpty())
2235 f = tmpf.absFileName();
2240 bool GIT::prepareFileRevisionEnabled()
2246 bool GIT::toggleReadOnlyEnabled()