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/TempFile.h"
37 using namespace lyx::support;
43 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
45 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
47 support::PathChanger p(path);
48 return one.startscript(Systemcall::Wait, cmd, string(), string(), false);
52 int VCS::doVCCommand(string const & cmd, FileName const & path, bool reportError)
55 owner_->setBusy(true);
57 int const ret = doVCCommandCall(cmd, path);
60 owner_->setBusy(false);
61 if (ret && reportError) {
63 if (prefixIs(cmd, "ci "))
64 rcsmsg = "\n" + _("Perhaps the RCS package is not installed on your system?");
65 frontend::Alert::error(_("Revision control error."),
66 bformat(_("Some problem occurred while running the command:\n"
74 bool VCS::makeRCSRevision(string const &version, string &revis) const
79 int back = convert<int>(rev);
80 // if positive use as the last number in the whole revision string
83 rsplit(version, base , '.');
84 rev = base + '.' + rev;
88 // we care about the last number from revision string
89 // in case of backward indexing
92 cur = rsplit(version, base , '.');
95 int want = convert<int>(cur) + back;
99 rev = base + '.' + convert<string>(want);
108 FileName VCS::checkParentDirs(FileName const & start, std::string const & file)
110 FileName dirname = start.onlyPath();
112 FileName tocheck = FileName(addPathName(dirname.absFileName(), file));
113 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
114 if (tocheck.exists())
116 // this construct because of #8295
117 dirname = FileName(dirname.absFileName()).parentPath();
118 } while (!dirname.empty());
123 /////////////////////////////////////////////////////////////////////
127 /////////////////////////////////////////////////////////////////////
129 RCS::RCS(FileName const & m, Buffer * b) : VCS(b)
131 // Here we know that the buffer file is either already in RCS or
132 // about to be registered
138 FileName const RCS::findFile(FileName const & file)
140 // Check if *,v exists.
141 FileName tmp(file.absFileName() + ",v");
142 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
143 if (tmp.isReadableFile()) {
144 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
148 // Check if RCS/*,v exists.
149 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
150 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
151 if (tmp.isReadableFile()) {
152 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
160 bool RCS::retrieve(FileName const & file)
162 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
163 // The caller ensures that file does not exist, so no need to check that.
164 int const ret = doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()));
169 void RCS::scanMaster()
174 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
176 ifstream ifs(master_.toFilesystemEncoding().c_str());
177 // limit the size of strings we read to avoid memory problems
181 bool read_enough = false;
183 while (!read_enough && ifs >> token) {
184 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
189 else if (token == "head") {
193 tmv = rtrim(tmv, ";");
195 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
196 } else if (contains(token, "access")
197 || contains(token, "symbols")
198 || contains(token, "strict")) {
200 } else if (contains(token, "locks")) {
202 if (contains(token, ';')) {
203 locker_ = "Unlocked";
204 vcstatus_ = UNLOCKED;
212 s1 = rtrim(tmpt, ";");
213 // tmp is now in the format <user>:<version>
214 s1 = split(s1, s2, ':');
215 // s2 is user, and s1 is version
216 if (s1 == version_) {
221 } while (!contains(tmpt, ';'));
223 } else if (token == "comment") {
224 // we don't need to read any further than this.
228 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
234 void RCS::registrer(string const & msg)
236 string cmd = "ci -q -u -i -t-\"";
239 cmd += quoteName(onlyFileName(owner_->absFileName()));
240 doVCCommand(cmd, FileName(owner_->filePath()));
244 bool RCS::renameEnabled()
250 string RCS::rename(support::FileName const & /*newFile*/, string const & /*msg*/)
252 // not implemented, since a left-over file.lyx,v would be confusing.
257 bool RCS::copyEnabled()
263 string RCS::copy(support::FileName const & newFile, string const & msg)
265 // RCS has no real copy command, so we create a poor mans version
266 support::FileName const oldFile(owner_->absFileName());
267 if (!oldFile.copyTo(newFile))
269 FileName path(oldFile.onlyPath());
270 string relFile(to_utf8(newFile.relPath(path.absFileName())));
271 string cmd = "ci -q -u -i -t-\"";
274 cmd += quoteName(relFile);
275 return doVCCommand(cmd, path) ? string() : "RCS: Proceeded";
279 LyXVC::CommandResult RCS::checkIn(string const & msg, string & log)
281 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
282 + quoteName(onlyFileName(owner_->absFileName())),
283 FileName(owner_->filePath()));
285 return LyXVC::ErrorCommand;
286 log = "RCS: Proceeded";
287 return LyXVC::VCSuccess;
291 bool RCS::checkInEnabled()
293 return owner_ && !owner_->hasReadonlyFlag();
297 bool RCS::isCheckInWithConfirmation()
299 // FIXME one day common getDiff for all backends
301 // if (getDiff(file, diff) && diff.empty())
304 TempFile tempfile("lyxvcout");
305 FileName tmpf = tempfile.name();
307 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
311 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
312 + " > " + quoteName(tmpf.toFilesystemEncoding()),
313 FileName(owner_->filePath()));
315 docstring diff = tmpf.fileContents("UTF-8");
324 string RCS::checkOut()
327 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
328 FileName(owner_->filePath()));
329 return ret ? string() : "RCS: Proceeded";
333 bool RCS::checkOutEnabled()
335 return owner_ && owner_->hasReadonlyFlag();
339 string RCS::repoUpdate()
341 lyxerr << "Sorry, not implemented." << endl;
346 bool RCS::repoUpdateEnabled()
352 string RCS::lockingToggle()
354 //FIXME this might be actually possible, study rcs -U, rcs -L.
355 //State should be easy to get inside scanMaster.
356 //It would fix #4370 and make rcs/svn usage even more closer.
357 lyxerr << "Sorry, not implemented." << endl;
362 bool RCS::lockingToggleEnabled()
370 if (doVCCommand("co -f -u" + version_ + ' '
371 + quoteName(onlyFileName(owner_->absFileName())),
372 FileName(owner_->filePath())))
374 // We ignore changes and just reload!
380 bool RCS::isRevertWithConfirmation()
382 //FIXME owner && diff ?
389 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
390 doVCCommand("rcs -o" + version_ + ' '
391 + quoteName(onlyFileName(owner_->absFileName())),
392 FileName(owner_->filePath()));
396 bool RCS::undoLastEnabled()
398 return owner_->hasReadonlyFlag();
402 void RCS::getLog(FileName const & tmpf)
404 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
405 + " > " + quoteName(tmpf.toFilesystemEncoding()),
406 FileName(owner_->filePath()));
410 bool RCS::toggleReadOnlyEnabled()
412 // This got broken somewhere along lfuns dispatch reorganization.
413 // reloadBuffer would be needed after this, but thats problematic
414 // since we are inside Buffer::dispatch.
420 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
422 if (info == LyXVC::File)
424 // fill the rest of the attributes for a single file
425 if (rev_date_cache_.empty())
426 if (!getRevisionInfo())
431 return rev_author_cache_;
433 return rev_date_cache_;
435 return rev_time_cache_;
444 bool RCS::getRevisionInfo()
446 TempFile tempfile("lyxvcout");
447 FileName tmpf = tempfile.name();
449 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
452 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
453 + " > " + quoteName(tmpf.toFilesystemEncoding()),
454 FileName(owner_->filePath()));
459 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
462 // we reached to the entry, i.e. after initial log message
464 // line with critical info, e.g:
465 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
470 LYXERR(Debug::LYXVC, line);
471 if (entry && prefixIs(line, "date:")) {
475 if (prefixIs(line, "revision"))
481 rev_date_cache_ = token(result, ' ', 1);
482 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
483 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
485 return !rev_author_cache_.empty();
488 bool RCS::prepareFileRevision(string const &revis, string & f)
491 if (!VCS::makeRCSRevision(version_, rev))
494 TempFile tempfile("lyxvcrev_" + rev + '_');
495 tempfile.setAutoRemove(false);
496 FileName tmpf = tempfile.name();
498 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
502 doVCCommand("co -p" + rev + ' '
503 + quoteName(onlyFileName(owner_->absFileName()))
504 + " > " + quoteName(tmpf.toFilesystemEncoding()),
505 FileName(owner_->filePath()));
507 if (tmpf.isFileEmpty())
510 f = tmpf.absFileName();
515 bool RCS::prepareFileRevisionEnabled()
521 /////////////////////////////////////////////////////////////////////
525 /////////////////////////////////////////////////////////////////////
527 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
529 // Here we know that the buffer file is either already in CVS or
530 // about to be registered
532 have_rev_info_ = false;
537 FileName const CVS::findFile(FileName const & file)
539 LYXERR(Debug::LYXVC, "LyXVC: Checking if "
540 << onlyFileName(file.absFileName()) << "is under cvs");
541 // First we look for the CVS/Entries in the same dir where we have file.
542 // Note that it is not necessary to search parent directories, since
543 // there will be a CVS/Entries file in every subdirectory.
544 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
545 if (entries.isReadableFile()) {
546 // We are in a CVS-managed directory
547 // See if the file is known to CVS
548 string const cmd = "cvs log " + quoteName(file.toFilesystemEncoding());
549 int const ret = doVCCommandCall(cmd, file.onlyPath());
557 void CVS::scanMaster()
559 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
560 // Ok now we do the real scan...
561 ifstream ifs(master_.toFilesystemEncoding().c_str());
562 string const name = onlyFileName(owner_->absFileName());
563 string const tmpf = '/' + name + '/';
564 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
566 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
567 while (getline(ifs, line)) {
568 LYXERR(Debug::LYXVC, "\t line: " << line);
569 if (contains(line, tmpf)) {
570 // Ok extract the fields.
572 if (!regex_match(line, sm, reg)) {
573 LYXERR(Debug::LYXVC, "\t Cannot parse line. Skipping.");
577 //sm[0]; // whole matched string
579 version_ = sm.str(2);
580 string const file_date = sm.str(3);
583 //sm[5]; // tag or tagdate
584 FileName file(owner_->absFileName());
585 if (file.isReadableFile()) {
586 time_t const mod = file.lastModified();
587 string const mod_date = rtrim(asctime(gmtime(&mod)), "\n");
588 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
589 << "'\nModification date of file: `" << mod_date << '\'');
590 if (file.isReadOnly()) {
591 // readonly checkout is unlocked
592 vcstatus_ = UNLOCKED;
594 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
595 FileName base(addName(bdir.absFileName(),name));
596 // if base version is existent "cvs edit" was used to lock
597 vcstatus_ = base.isReadableFile() ? LOCKED : NOLOCKING;
600 vcstatus_ = NOLOCKING;
608 bool CVS::retrieve(FileName const & file)
610 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
611 // The caller ensures that file does not exist, so no need to check that.
612 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
613 file.onlyPath()) == 0;
617 string const CVS::getTarget(OperationMode opmode) const
621 // in client server mode CVS does not like full path operand for directory operation
622 // since LyX switches to the repo dir "." is good enough as target
625 return quoteName(onlyFileName(owner_->absFileName()));
631 docstring CVS::toString(CvsStatus status) const
635 return _("Up-to-date");
636 case LocallyModified:
637 return _("Locally Modified");
639 return _("Locally Added");
641 return _("Needs Merge");
643 return _("Needs Checkout");
645 return _("No CVS file");
647 return _("Cannot retrieve CVS status");
653 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
654 FileName const & output, bool reportError)
656 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
657 return doVCCommand(cmd + redirection, path, reportError);
661 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
662 support::FileName const & path,
663 support::FileName const & output)
665 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
666 return doVCCommandCall(cmd + redirection, path);
670 CVS::CvsStatus CVS::getStatus()
672 TempFile tempfile("lyxvout");
673 FileName tmpf = tempfile.name();
675 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
679 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
680 FileName(owner_->filePath()), tmpf)) {
684 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
685 CvsStatus status = NoCvsFile;
690 LYXERR(Debug::LYXVC, line << '\n');
691 if (prefixIs(line, "File:")) {
692 if (contains(line, "Up-to-date"))
694 else if (contains(line, "Locally Modified"))
695 status = LocallyModified;
696 else if (contains(line, "Locally Added"))
697 status = LocallyAdded;
698 else if (contains(line, "Needs Merge"))
700 else if (contains(line, "Needs Checkout"))
701 status = NeedsCheckout;
707 void CVS::getRevisionInfo()
711 have_rev_info_ = true;
712 TempFile tempfile("lyxvout");
713 FileName tmpf = tempfile.name();
715 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
719 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
720 + ' ' + getTarget(File),
721 FileName(owner_->filePath()), tmpf);
723 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
727 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
728 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
733 LYXERR(Debug::LYXVC, line << '\n');
734 if (prefixIs(line, "date:")) {
736 if (regex_match(line, sm, reg)) {
737 //sm[0]; // whole matched string
738 rev_date_cache_ = sm[1];
739 rev_time_cache_ = sm[2];
740 //sm[3]; // GMT offset
741 rev_author_cache_ = sm[4];
743 LYXERR(Debug::LYXVC, "\tCannot parse line. Skipping.");
747 if (rev_author_cache_.empty())
749 "Could not retrieve revision info for " << version_ <<
750 " of " << getTarget(File));
754 void CVS::registrer(string const & msg)
756 doVCCommand("cvs -q add -m \"" + msg + "\" "
758 FileName(owner_->filePath()));
762 bool CVS::renameEnabled()
768 string CVS::rename(support::FileName const & newFile, string const & msg)
770 // CVS has no real rename command, so we create a poor mans version
771 support::FileName const oldFile(owner_->absFileName());
772 string ret = copy(newFile, msg);
775 string cmd = "cvs -q remove -m \"" + msg + "\" " +
776 quoteName(oldFile.onlyFileName());
777 FileName path(oldFile.onlyPath());
778 return doVCCommand(cmd, path) ? string() : ret;
782 bool CVS::copyEnabled()
788 string CVS::copy(support::FileName const & newFile, string const & msg)
790 // CVS has no real copy command, so we create a poor mans version
791 support::FileName const oldFile(owner_->absFileName());
792 if (!oldFile.copyTo(newFile))
794 FileName path(oldFile.onlyPath());
795 string relFile(to_utf8(newFile.relPath(path.absFileName())));
796 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
797 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
801 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
803 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
804 FileName(owner_->filePath()), tmpf, false);
811 return doVCCommand("cvs -q edit " + getTarget(File),
812 FileName(owner_->filePath()));
818 vcstatus_ = UNLOCKED;
819 return doVCCommand("cvs -q unedit " + getTarget(File),
820 FileName(owner_->filePath()));
824 int CVS::update(OperationMode opmode, FileName const & tmpf)
826 return doVCCommandWithOutput("cvs -q update "
828 FileName(owner_->filePath()), tmpf, false);
832 string CVS::scanLogFile(FileName const & f, string & status)
834 ifstream ifs(f.toFilesystemEncoding().c_str());
839 LYXERR(Debug::LYXVC, line << '\n');
841 status += line + "; ";
842 if (prefixIs(line, "C ")) {
852 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
854 CvsStatus status = getStatus();
857 if (vcstatus_ != NOLOCKING)
859 return LyXVC::ErrorCommand;
860 log = "CVS: Proceeded";
861 return LyXVC::VCSuccess;
862 case LocallyModified:
864 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
866 FileName(owner_->filePath()));
868 return LyXVC::ErrorCommand;
869 log = "CVS: Proceeded";
870 return LyXVC::VCSuccess;
874 frontend::Alert::error(_("Revision control error."),
875 _("The repository version is newer then the current check out.\n"
876 "You have to update from repository first or revert your changes.")) ;
879 frontend::Alert::error(_("Revision control error."),
880 bformat(_("Bad status when checking in changes.\n"
885 return LyXVC::ErrorBefore;
889 bool CVS::isLocked() const
891 FileName fn(owner_->absFileName());
893 return !fn.isReadOnly();
897 bool CVS::checkInEnabled()
899 if (vcstatus_ != NOLOCKING)
906 bool CVS::isCheckInWithConfirmation()
908 CvsStatus status = getStatus();
909 return status == LocallyModified || status == LocallyAdded;
913 string CVS::checkOut()
915 if (vcstatus_ != NOLOCKING && edit())
917 TempFile tempfile("lyxvout");
918 FileName tmpf = tempfile.name();
920 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
924 int rc = update(File, tmpf);
926 string const res = scanLogFile(tmpf, log);
928 frontend::Alert::error(_("Revision control error."),
929 bformat(_("Error when updating from repository.\n"
930 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
931 "After pressing OK, LyX will try to reopen the resolved document."),
932 from_local8bit(res)));
936 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
940 bool CVS::checkOutEnabled()
942 if (vcstatus_ != NOLOCKING)
949 string CVS::repoUpdate()
951 TempFile tempfile("lyxvout");
952 FileName tmpf = tempfile.name();
954 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
958 getDiff(Directory, tmpf);
959 docstring res = tmpf.fileContents("UTF-8");
961 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
962 docstring const file = from_utf8(owner_->filePath());
963 docstring text = bformat(_("There were detected changes "
964 "in the working directory:\n%1$s\n\n"
965 "Possible file conflicts must be then resolved manually "
966 "or you will need to revert back to the repository version."), file);
967 int ret = frontend::Alert::prompt(_("Changes detected"),
968 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
970 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
971 ret = frontend::Alert::prompt(_("Changes detected"),
972 text, 0, 1, _("&Continue"), _("&Abort"));
973 hideDialogs("file", nullptr);
979 int rc = update(Directory, tmpf);
980 res += "Update log:\n" + tmpf.fileContents("UTF-8");
981 LYXERR(Debug::LYXVC, res);
984 string sres = scanLogFile(tmpf, log);
986 docstring const file = owner_->fileName().displayName(20);
987 frontend::Alert::error(_("Revision control error."),
988 bformat(_("Error when updating document %1$s from repository.\n"
989 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
990 "After pressing OK, LyX will try to reopen the resolved document."),
991 file, from_local8bit(sres)));
995 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
999 bool CVS::repoUpdateEnabled()
1005 string CVS::lockingToggle()
1007 lyxerr << "Sorry, not implemented." << endl;
1012 bool CVS::lockingToggleEnabled()
1018 bool CVS::isRevertWithConfirmation()
1020 CvsStatus status = getStatus();
1021 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1027 // Reverts to the version in CVS repository and
1028 // gets the updated version from the repository.
1029 CvsStatus status = getStatus();
1032 if (vcstatus_ != NOLOCKING)
1033 return 0 == unedit();
1037 case LocallyModified: {
1038 FileName f(owner_->absFileName());
1040 update(File, FileName());
1041 owner_->markClean();
1044 case LocallyAdded: {
1045 docstring const file = owner_->fileName().displayName(20);
1046 frontend::Alert::error(_("Revision control error."),
1047 bformat(_("The document %1$s is not in repository.\n"
1048 "You have to check in the first revision before you can revert."),
1053 docstring const file = owner_->fileName().displayName(20);
1054 frontend::Alert::error(_("Revision control error."),
1055 bformat(_("Cannot revert document %1$s to repository version.\n"
1056 "The status '%2$s' is unexpected."),
1057 file, toString(status)));
1065 void CVS::undoLast()
1067 // merge the current with the previous version
1068 // in a reverse patch kind of way, so that the
1069 // result is to revert the last changes.
1070 lyxerr << "Sorry, not implemented." << endl;
1074 bool CVS::undoLastEnabled()
1080 void CVS::getLog(FileName const & tmpf)
1082 doVCCommandWithOutput("cvs log " + getTarget(File),
1083 FileName(owner_->filePath()),
1088 bool CVS::toggleReadOnlyEnabled()
1094 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1096 if (!version_.empty()) {
1102 return rev_author_cache_;
1104 return rev_date_cache_;
1106 return rev_time_cache_;
1115 bool CVS::prepareFileRevision(string const & revis, string & f)
1118 if (!VCS::makeRCSRevision(version_, rev))
1121 TempFile tempfile("lyxvcrev_" + rev + '_');
1122 tempfile.setAutoRemove(false);
1123 FileName tmpf = tempfile.name();
1125 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1129 doVCCommandWithOutput("cvs update -p -r" + rev + ' '
1131 FileName(owner_->filePath()), tmpf);
1133 if (tmpf.isFileEmpty())
1136 f = tmpf.absFileName();
1141 bool CVS::prepareFileRevisionEnabled()
1147 /////////////////////////////////////////////////////////////////////
1151 /////////////////////////////////////////////////////////////////////
1153 SVN::SVN(Buffer * b) : VCS(b)
1155 // Here we know that the buffer file is either already in SVN or
1156 // about to be registered
1157 locked_mode_ = false;
1162 bool SVN::findFile(FileName const & file)
1164 // First we check the existence of repository meta data.
1165 if (VCS::checkParentDirs(file, ".svn").empty()) {
1166 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1170 // Now we check the status of the file.
1171 string const fname = onlyFileName(file.absFileName());
1172 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1173 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname),
1175 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1180 void SVN::scanMaster()
1182 // vcstatus code is somewhat superflous,
1183 // until we want to implement read-only toggle for svn.
1184 vcstatus_ = NOLOCKING;
1185 if (checkLockMode())
1186 vcstatus_ = isLocked() ? LOCKED : UNLOCKED;
1190 bool SVN::checkLockMode()
1192 TempFile tempfile("lyxvcout");
1193 FileName tmpf = tempfile.name();
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 --parents " + 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 TempFile tempfile("lyxvcout");
1324 FileName tmpf = tempfile.name();
1326 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1327 log = N_("Error: Could not generate logfile.");
1328 return LyXVC::ErrorBefore;
1332 os << "svn commit -m \"" << msg << '"';
1333 for (size_t i = 0; i < f.size(); ++i)
1334 os << ' ' << quoteName(f[i].onlyFileName());
1335 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1336 LyXVC::CommandResult ret =
1337 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1338 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1340 string res = scanLogFile(tmpf, log);
1342 frontend::Alert::error(_("Revision control error."),
1343 _("Error when committing to repository.\n"
1344 "You have to manually resolve the problem.\n"
1345 "LyX will reopen the document after you press OK."));
1346 ret = LyXVC::ErrorCommand;
1349 if (!fileLock(false, tmpf, log))
1350 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 TempFile tempfile("lyxvcout");
1374 FileName tmpf = tempfile.name();
1376 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1380 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1381 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1382 FileName(owner_->filePath()));
1384 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 unreachable 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 TempFile tempfile("lyxvcout");
1460 FileName tmpf = tempfile.name();
1462 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1463 return N_("Error: Could not generate logfile.");
1466 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1467 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1468 FileName(owner_->filePath()));
1471 string const res = scanLogFile(tmpf, log);
1473 frontend::Alert::error(_("Revision control error."),
1474 bformat(_("Error when updating from repository.\n"
1475 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1476 "After pressing OK, LyX will try to reopen the resolved document."),
1477 from_local8bit(res)));
1479 fileLock(true, tmpf, log);
1481 return log.empty() ? string() : "SVN: " + log;
1485 bool SVN::checkOutEnabled()
1494 string SVN::repoUpdate()
1496 TempFile tempfile("lyxvcout");
1497 FileName tmpf = tempfile.name();
1499 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1500 return N_("Error: Could not generate logfile.");
1503 doVCCommand("svn diff " + quoteName(owner_->filePath())
1504 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1505 FileName(owner_->filePath()));
1506 docstring res = tmpf.fileContents("UTF-8");
1508 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1509 docstring const file = from_utf8(owner_->filePath());
1510 docstring text = bformat(_("There were detected changes "
1511 "in the working directory:\n%1$s\n\n"
1512 "In case of file conflict version of the local directory files "
1513 "will be preferred."
1514 "\n\nContinue?"), file);
1515 int ret = frontend::Alert::prompt(_("Changes detected"),
1516 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1518 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1519 ret = frontend::Alert::prompt(_("Changes detected"),
1520 text, 0, 1, _("&Yes"), _("&No"));
1521 hideDialogs("file", nullptr);
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);
1538 return to_utf8(res);
1542 bool SVN::repoUpdateEnabled()
1548 string SVN::lockingToggle()
1550 TempFile tempfile("lyxvcout");
1551 FileName tmpf = tempfile.name();
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()));
1579 frontend::Alert::warning(_("SVN File Locking"),
1580 (locking ? _("Locking property unset.") : _("Locking property set.")) + '\n'
1581 + _("Do not forget to commit the locking property into the repository."),
1584 return string("SVN: ") + (locking ?
1585 N_("Locking property unset.") : N_("Locking property set."));
1589 bool SVN::lockingToggleEnabled()
1597 // Reverts to the version in SVN repository and
1598 // gets the updated version from the repository.
1599 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1601 if (doVCCommand("svn revert -q " + fil,
1602 FileName(owner_->filePath())))
1604 owner_->markClean();
1609 bool SVN::isRevertWithConfirmation()
1611 //FIXME owner && diff
1616 void SVN::undoLast()
1618 // merge the current with the previous version
1619 // in a reverse patch kind of way, so that the
1620 // result is to revert the last changes.
1621 lyxerr << "Sorry, not implemented." << endl;
1625 bool SVN::undoLastEnabled()
1631 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1633 if (info == LyXVC::Tree) {
1634 if (rev_tree_cache_.empty())
1635 if (!getTreeRevisionInfo())
1636 rev_tree_cache_ = "?";
1637 if (rev_tree_cache_ == "?")
1640 return rev_tree_cache_;
1643 // fill the rest of the attributes for a single file
1644 if (rev_file_cache_.empty())
1645 if (!getFileRevisionInfo())
1646 rev_file_cache_ = "?";
1650 if (rev_file_cache_ == "?")
1652 return rev_file_cache_;
1654 return rev_author_cache_;
1656 return rev_date_cache_;
1658 return rev_time_cache_;
1667 bool SVN::getFileRevisionInfo()
1669 TempFile tempfile("lyxvcout");
1670 FileName tmpf = tempfile.name();
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;
1716 return !rev.empty();
1720 bool SVN::getTreeRevisionInfo()
1722 TempFile tempfile("lyxvcout");
1723 FileName tmpf = tempfile.name();
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());
1741 rev_tree_cache_ = line;
1742 return !line.empty();
1746 void SVN::getLog(FileName const & tmpf)
1748 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1749 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1750 FileName(owner_->filePath()));
1754 bool SVN::prepareFileRevision(string const & revis, string & f)
1756 if (!isStrInt(revis))
1759 int rev = convert<int>(revis);
1761 if (!getFileRevisionInfo())
1764 rev = convert<int>(rev_file_cache_);
1765 // go back for minus rev
1767 rev = rev + convert<int>(rev_file_cache_);
1772 string revname = convert<string>(rev);
1773 TempFile tempfile("lyxvcrev_" + revname + '_');
1774 tempfile.setAutoRemove(false);
1775 FileName tmpf = tempfile.name();
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()));
1786 if (tmpf.isFileEmpty())
1789 f = tmpf.absFileName();
1794 bool SVN::prepareFileRevisionEnabled()
1801 bool SVN::toggleReadOnlyEnabled()
1807 /////////////////////////////////////////////////////////////////////
1811 /////////////////////////////////////////////////////////////////////
1813 GIT::GIT(Buffer * b) : VCS(b)
1815 // Here we know that the buffer file is either already in GIT or
1816 // about to be registered
1821 bool GIT::findFile(FileName const & file)
1823 // First we check the existence of repository meta data.
1824 if (VCS::checkParentDirs(file, ".git").empty()) {
1825 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1829 // Now we check if the file is known to git.
1830 string const fname = onlyFileName(file.absFileName());
1831 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1833 int const ret = doVCCommandCall("git log -n 0 " + quoteName(fname),
1835 bool const found = (ret == 0);
1836 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1841 void GIT::scanMaster()
1843 // vcstatus code is somewhat superflous,
1844 // until we want to implement read-only toggle for git.
1845 vcstatus_ = NOLOCKING;
1849 bool GIT::retrieve(FileName const & file)
1851 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1852 // The caller ensures that file does not exist, so no need to check that.
1853 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1854 file.onlyPath()) == 0;
1858 void GIT::registrer(string const & /*msg*/)
1860 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1861 FileName(owner_->filePath()));
1865 bool GIT::renameEnabled()
1871 string GIT::rename(support::FileName const & newFile, string const & msg)
1873 // git mv does not require a log message, since it does not commit.
1874 // In LyX we commit immediately afterwards, otherwise it could be
1875 // confusing to the user to have two uncommitted files.
1876 FileName path(owner_->filePath());
1877 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1878 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1879 ' ' + quoteName(relFile));
1880 if (doVCCommand(cmd, path)) {
1881 cmd = "git checkout -q " +
1882 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1884 doVCCommand(cmd, path);
1885 if (newFile.exists())
1886 newFile.removeFile();
1889 vector<support::FileName> f;
1890 f.push_back(owner_->fileName());
1891 f.push_back(newFile);
1893 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1894 cmd = "git checkout -q " +
1895 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1897 doVCCommand(cmd, path);
1898 if (newFile.exists())
1899 newFile.removeFile();
1906 bool GIT::copyEnabled()
1912 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1914 // git does not support copy with history preservation
1919 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1921 vector<support::FileName> f(1, owner_->fileName());
1922 return checkIn(f, msg, log);
1926 LyXVC::CommandResult
1927 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1929 TempFile tempfile("lyxvcout");
1930 FileName tmpf = tempfile.name();
1932 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1933 log = N_("Error: Could not generate logfile.");
1934 return LyXVC::ErrorBefore;
1938 os << "git commit -m \"" << msg << '"';
1939 for (size_t i = 0; i < f.size(); ++i)
1940 os << ' ' << quoteName(f[i].onlyFileName());
1941 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1942 LyXVC::CommandResult ret =
1943 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1944 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1946 string res = scanLogFile(tmpf, log);
1948 frontend::Alert::error(_("Revision control error."),
1949 _("Error when committing to repository.\n"
1950 "You have to manually resolve the problem.\n"
1951 "LyX will reopen the document after you press OK."));
1952 ret = LyXVC::ErrorCommand;
1956 log.insert(0, "GIT: ");
1957 if (ret == LyXVC::VCSuccess && log.empty())
1958 log = "GIT: Proceeded";
1963 bool GIT::checkInEnabled()
1969 bool GIT::isCheckInWithConfirmation()
1971 // FIXME one day common getDiff and perhaps OpMode for all backends
1973 TempFile tempfile("lyxvcout");
1974 FileName tmpf = tempfile.name();
1976 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1980 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
1981 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1982 FileName(owner_->filePath()));
1984 docstring diff = tmpf.fileContents("UTF-8");
1993 // FIXME Correctly return code should be checked instead of this.
1994 // This would need another solution than just plain startscript.
1995 // Hint from Andre': QProcess::readAllStandardError()...
1996 string GIT::scanLogFile(FileName const & f, string & status)
1998 ifstream ifs(f.toFilesystemEncoding().c_str());
2003 LYXERR(Debug::LYXVC, line << "\n");
2005 status += line + "; ";
2006 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2007 || contains(line, "Commit failed")) {
2017 string GIT::checkOut()
2023 bool GIT::checkOutEnabled()
2029 string GIT::repoUpdate()
2035 bool GIT::repoUpdateEnabled()
2041 string GIT::lockingToggle()
2047 bool GIT::lockingToggleEnabled()
2055 // Reverts to the version in GIT repository and
2056 // gets the updated version from the repository.
2057 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2059 if (doVCCommand("git checkout -q " + fil,
2060 FileName(owner_->filePath())))
2062 owner_->markClean();
2067 bool GIT::isRevertWithConfirmation()
2069 //FIXME owner && diff
2074 void GIT::undoLast()
2076 // merge the current with the previous version
2077 // in a reverse patch kind of way, so that the
2078 // result is to revert the last changes.
2079 lyxerr << "Sorry, not implemented." << endl;
2083 bool GIT::undoLastEnabled()
2089 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2091 if (info == LyXVC::Tree) {
2092 if (rev_tree_cache_.empty())
2093 if (!getTreeRevisionInfo())
2094 rev_tree_cache_ = "?";
2095 if (rev_tree_cache_ == "?")
2098 return rev_tree_cache_;
2101 // fill the rest of the attributes for a single file
2102 if (rev_file_cache_.empty())
2103 if (!getFileRevisionInfo()) {
2104 rev_file_cache_ = "?";
2105 rev_file_abbrev_cache_ = "?";
2110 if (rev_file_cache_ == "?")
2112 return rev_file_cache_;
2113 case LyXVC::FileAbbrev:
2114 if (rev_file_abbrev_cache_ == "?")
2116 return rev_file_abbrev_cache_;
2118 return rev_author_cache_;
2120 return rev_date_cache_;
2122 return rev_time_cache_;
2131 bool GIT::getFileRevisionInfo()
2133 TempFile tempfile("lyxvcout");
2134 FileName tmpf = tempfile.name();
2136 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2140 doVCCommand("git log -n 1 --pretty=format:%H%n%h%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2141 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2142 FileName(owner_->filePath()));
2147 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2150 getline(ifs, rev_file_cache_);
2152 getline(ifs, rev_file_abbrev_cache_);
2154 getline(ifs, rev_author_cache_);
2158 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2162 return !rev_file_cache_.empty();
2166 bool GIT::getTreeRevisionInfo()
2168 TempFile tempfile("lyxvcout");
2169 FileName tmpf = tempfile.name();
2171 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2175 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2176 FileName(owner_->filePath()),
2177 false); //git describe returns $?=128 when no tag found (but git repo still exists)
2182 // only first line in case something bad happens.
2183 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2184 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 TempFile tempfile("lyxvcrev_" + revis + '_');
2223 tempfile.setAutoRemove(false);
2224 FileName tmpf = tempfile.name();
2226 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2230 doVCCommand("git show " + pointer + "./"
2231 + quoteName(onlyFileName(owner_->absFileName()))
2232 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2233 FileName(owner_->filePath()));
2235 if (tmpf.isFileEmpty())
2238 f = tmpf.absFileName();
2243 bool GIT::prepareFileRevisionEnabled()
2249 bool GIT::toggleReadOnlyEnabled()