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"
30 #include "support/TempFile.h"
35 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 occurred 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 TempFile tempfile("lyxvcout");
297 FileName tmpf = tempfile.name();
299 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
303 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
304 + " > " + quoteName(tmpf.toFilesystemEncoding()),
305 FileName(owner_->filePath()));
307 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 TempFile tempfile("lyxvcout");
438 FileName tmpf = tempfile.name();
440 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
443 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
444 + " > " + quoteName(tmpf.toFilesystemEncoding()),
445 FileName(owner_->filePath()));
450 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
453 // we reached to the entry, i.e. after initial log message
455 // line with critical info, e.g:
456 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
461 LYXERR(Debug::LYXVC, line);
462 if (entry && prefixIs(line, "date:")) {
466 if (prefixIs(line, "revision"))
472 rev_date_cache_ = token(result, ' ', 1);
473 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
474 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
476 return !rev_author_cache_.empty();
479 bool RCS::prepareFileRevision(string const &revis, string & f)
482 if (!VCS::makeRCSRevision(version_, rev))
485 TempFile tempfile("lyxvcrev_" + rev + '_');
486 tempfile.setAutoRemove(false);
487 FileName tmpf = tempfile.name();
489 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
493 doVCCommand("co -p" + rev + ' '
494 + quoteName(onlyFileName(owner_->absFileName()))
495 + " > " + quoteName(tmpf.toFilesystemEncoding()),
496 FileName(owner_->filePath()));
497 if (tmpf.isFileEmpty())
500 f = tmpf.absFileName();
505 bool RCS::prepareFileRevisionEnabled()
511 /////////////////////////////////////////////////////////////////////
515 /////////////////////////////////////////////////////////////////////
517 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
519 // Here we know that the buffer file is either already in CVS or
520 // about to be registered
522 have_rev_info_ = false;
527 FileName const CVS::findFile(FileName const & file)
529 // First we look for the CVS/Entries in the same dir
530 // where we have file.
531 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
532 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
533 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
534 << "' for `" << tmpf << '\'');
535 if (entries.isReadableFile()) {
536 // Ok we are at least in a CVS dir. Parse the CVS/Entries
537 // and see if we can find this file. We do a fast and
539 ifstream ifs(entries.toFilesystemEncoding().c_str());
541 while (getline(ifs, line)) {
542 LYXERR(Debug::LYXVC, "\tEntries: " << line);
543 if (contains(line, tmpf))
551 void CVS::scanMaster()
553 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
554 // Ok now we do the real scan...
555 ifstream ifs(master_.toFilesystemEncoding().c_str());
556 string name = onlyFileName(owner_->absFileName());
557 string tmpf = '/' + name + '/';
558 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
560 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
561 while (getline(ifs, line)) {
562 LYXERR(Debug::LYXVC, "\t line: " << line);
563 if (contains(line, tmpf)) {
564 // Ok extract the fields.
567 regex_match(line, sm, reg);
569 //sm[0]; // whole matched string
571 version_ = sm.str(2);
572 string const file_date = sm.str(3);
575 //sm[5]; // tag or tagdate
576 FileName file(owner_->absFileName());
577 if (file.isReadableFile()) {
578 time_t mod = file.lastModified();
579 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
580 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
581 << "'\nModification date of file: `" << mod_date << '\'');
582 if (file.isReadOnly()) {
583 // readonly checkout is unlocked
586 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
587 FileName base(addName(bdir.absFileName(),name));
588 // if base version is existent "cvs edit" was used to lock
589 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
592 vcstatus = NOLOCKING;
600 bool CVS::retrieve(FileName const & file)
602 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
603 // The caller ensures that file does not exist, so no need to check that.
604 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
605 file.onlyPath()) == 0;
609 string const CVS::getTarget(OperationMode opmode) const
613 // in client server mode CVS does not like full path operand for directory operation
614 // since LyX switches to the repo dir "." is good enough as target
617 return quoteName(onlyFileName(owner_->absFileName()));
623 docstring CVS::toString(CvsStatus status) const
627 return _("Up-to-date");
628 case LocallyModified:
629 return _("Locally Modified");
631 return _("Locally Added");
633 return _("Needs Merge");
635 return _("Needs Checkout");
637 return _("No CVS file");
639 return _("Cannot retrieve CVS status");
645 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
646 FileName const & output, bool reportError)
648 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
649 return doVCCommand(cmd + redirection, path, reportError);
653 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
654 support::FileName const & path,
655 support::FileName const & output)
657 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
658 return doVCCommandCall(cmd + redirection, path);
662 CVS::CvsStatus CVS::getStatus()
664 TempFile tempfile("lyxvout");
665 FileName tmpf = tempfile.name();
667 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
671 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
672 FileName(owner_->filePath()), tmpf)) {
676 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
677 CvsStatus status = NoCvsFile;
682 LYXERR(Debug::LYXVC, line << '\n');
683 if (prefixIs(line, "File:")) {
684 if (contains(line, "Up-to-date"))
686 else if (contains(line, "Locally Modified"))
687 status = LocallyModified;
688 else if (contains(line, "Locally Added"))
689 status = LocallyAdded;
690 else if (contains(line, "Needs Merge"))
692 else if (contains(line, "Needs Checkout"))
693 status = NeedsCheckout;
699 void CVS::getRevisionInfo()
703 have_rev_info_ = true;
704 TempFile tempfile("lyxvout");
705 FileName tmpf = tempfile.name();
707 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
711 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
712 + ' ' + getTarget(File),
713 FileName(owner_->filePath()), tmpf);
715 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
719 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
720 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
725 LYXERR(Debug::LYXVC, line << '\n');
726 if (prefixIs(line, "date:")) {
728 regex_match(line, sm, reg);
729 //sm[0]; // whole matched string
730 rev_date_cache_ = sm[1];
731 rev_time_cache_ = sm[2];
732 //sm[3]; // GMT offset
733 rev_author_cache_ = sm[4];
737 if (rev_author_cache_.empty())
739 "Could not retrieve revision info for " << version_ <<
740 " of " << getTarget(File));
744 void CVS::registrer(string const & msg)
746 doVCCommand("cvs -q add -m \"" + msg + "\" "
748 FileName(owner_->filePath()));
752 bool CVS::renameEnabled()
758 string CVS::rename(support::FileName const & newFile, string const & msg)
760 // CVS has no real rename command, so we create a poor mans version
761 support::FileName const oldFile(owner_->absFileName());
762 string ret = copy(newFile, msg);
765 string cmd = "cvs -q remove -m \"" + msg + "\" " +
766 quoteName(oldFile.onlyFileName());
767 FileName path(oldFile.onlyPath());
768 return doVCCommand(cmd, path) ? string() : ret;
772 bool CVS::copyEnabled()
778 string CVS::copy(support::FileName const & newFile, string const & msg)
780 // CVS has no real copy command, so we create a poor mans version
781 support::FileName const oldFile(owner_->absFileName());
782 if (!oldFile.copyTo(newFile))
784 FileName path(oldFile.onlyPath());
785 string relFile(to_utf8(newFile.relPath(path.absFileName())));
786 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
787 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
791 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
793 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
794 FileName(owner_->filePath()), tmpf, false);
801 return doVCCommand("cvs -q edit " + getTarget(File),
802 FileName(owner_->filePath()));
809 return doVCCommand("cvs -q unedit " + getTarget(File),
810 FileName(owner_->filePath()));
814 int CVS::update(OperationMode opmode, FileName const & tmpf)
816 return doVCCommandWithOutput("cvs -q update "
818 FileName(owner_->filePath()), tmpf, false);
822 string CVS::scanLogFile(FileName const & f, string & status)
824 ifstream ifs(f.toFilesystemEncoding().c_str());
829 LYXERR(Debug::LYXVC, line << '\n');
831 status += line + "; ";
832 if (prefixIs(line, "C ")) {
842 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
844 CvsStatus status = getStatus();
847 if (vcstatus != NOLOCKING)
849 return LyXVC::ErrorCommand;
850 log = "CVS: Proceeded";
851 return LyXVC::VCSuccess;
852 case LocallyModified:
854 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
856 FileName(owner_->filePath()));
858 return LyXVC::ErrorCommand;
859 log = "CVS: Proceeded";
860 return LyXVC::VCSuccess;
864 frontend::Alert::error(_("Revision control error."),
865 _("The repository version is newer then the current check out.\n"
866 "You have to update from repository first or revert your changes.")) ;
869 frontend::Alert::error(_("Revision control error."),
870 bformat(_("Bad status when checking in changes.\n"
875 return LyXVC::ErrorBefore;
879 bool CVS::isLocked() const
881 FileName fn(owner_->absFileName());
883 return !fn.isReadOnly();
887 bool CVS::checkInEnabled()
889 if (vcstatus != NOLOCKING)
896 bool CVS::isCheckInWithConfirmation()
898 CvsStatus status = getStatus();
899 return status == LocallyModified || status == LocallyAdded;
903 string CVS::checkOut()
905 if (vcstatus != NOLOCKING && edit())
907 TempFile tempfile("lyxvout");
908 FileName tmpf = tempfile.name();
910 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
914 int rc = update(File, tmpf);
916 string const res = scanLogFile(tmpf, log);
918 frontend::Alert::error(_("Revision control error."),
919 bformat(_("Error when updating from repository.\n"
920 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
921 "After pressing OK, LyX will try to reopen the resolved document."),
922 from_local8bit(res)));
926 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
930 bool CVS::checkOutEnabled()
932 if (vcstatus != NOLOCKING)
939 string CVS::repoUpdate()
941 TempFile tempfile("lyxvout");
942 FileName tmpf = tempfile.name();
944 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
948 getDiff(Directory, tmpf);
949 docstring res = tmpf.fileContents("UTF-8");
951 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
952 docstring const file = from_utf8(owner_->filePath());
953 docstring text = bformat(_("There were detected changes "
954 "in the working directory:\n%1$s\n\n"
955 "Possible file conflicts must be then resolved manually "
956 "or you will need to revert back to the repository version."), file);
957 int ret = frontend::Alert::prompt(_("Changes detected"),
958 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
960 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
961 ret = frontend::Alert::prompt(_("Changes detected"),
962 text, 0, 1, _("&Continue"), _("&Abort"));
963 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)));
985 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
989 bool CVS::repoUpdateEnabled()
995 string CVS::lockingToggle()
997 lyxerr << "Sorry, not implemented." << endl;
1002 bool CVS::lockingToggleEnabled()
1008 bool CVS::isRevertWithConfirmation()
1010 CvsStatus status = getStatus();
1011 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1017 // Reverts to the version in CVS repository and
1018 // gets the updated version from the repository.
1019 CvsStatus status = getStatus();
1022 if (vcstatus != NOLOCKING)
1023 return 0 == unedit();
1027 case LocallyModified: {
1028 FileName f(owner_->absFileName());
1030 update(File, FileName());
1031 owner_->markClean();
1034 case LocallyAdded: {
1035 docstring const file = owner_->fileName().displayName(20);
1036 frontend::Alert::error(_("Revision control error."),
1037 bformat(_("The document %1$s is not in repository.\n"
1038 "You have to check in the first revision before you can revert."),
1043 docstring const file = owner_->fileName().displayName(20);
1044 frontend::Alert::error(_("Revision control error."),
1045 bformat(_("Cannot revert document %1$s to repository version.\n"
1046 "The status '%2$s' is unexpected."),
1047 file, toString(status)));
1055 void CVS::undoLast()
1057 // merge the current with the previous version
1058 // in a reverse patch kind of way, so that the
1059 // result is to revert the last changes.
1060 lyxerr << "Sorry, not implemented." << endl;
1064 bool CVS::undoLastEnabled()
1070 void CVS::getLog(FileName const & tmpf)
1072 doVCCommandWithOutput("cvs log " + getTarget(File),
1073 FileName(owner_->filePath()),
1078 bool CVS::toggleReadOnlyEnabled()
1084 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1086 if (!version_.empty()) {
1092 return rev_author_cache_;
1094 return rev_date_cache_;
1096 return rev_time_cache_;
1104 bool CVS::prepareFileRevision(string const & revis, string & f)
1107 if (!VCS::makeRCSRevision(version_, rev))
1110 TempFile tempfile("lyxvcrev_" + rev + '_');
1111 tempfile.setAutoRemove(false);
1112 FileName tmpf = tempfile.name();
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 TempFile tempfile("lyxvcout");
1161 FileName tmpf = tempfile.name();
1163 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1167 string const fname = onlyFileName(file.absFileName());
1168 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1169 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1170 + " > " + 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 TempFile tempfile("lyxvcout");
1194 FileName tmpf = tempfile.name();
1196 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1200 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1201 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1202 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1203 FileName(owner_->filePath())))
1206 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1210 while (ifs && !ret) {
1212 LYXERR(Debug::LYXVC, line);
1213 if (contains(line, "svn:needs-lock"))
1216 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1224 bool SVN::isLocked() const
1226 FileName file(owner_->absFileName());
1228 return !file.isReadOnly();
1232 bool SVN::retrieve(FileName const & file)
1234 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1235 // The caller ensures that file does not exist, so no need to check that.
1236 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1237 file.onlyPath()) == 0;
1241 void SVN::registrer(string const & /*msg*/)
1243 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1244 FileName(owner_->filePath()));
1248 bool SVN::renameEnabled()
1254 string SVN::rename(support::FileName const & newFile, string const & msg)
1256 // svn move does not require a log message, since it does not commit.
1257 // In LyX we commit immediately afterwards, otherwise it could be
1258 // confusing to the user to have two uncommitted files.
1259 FileName path(owner_->filePath());
1260 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1261 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1262 ' ' + quoteName(relFile));
1263 if (doVCCommand(cmd, path)) {
1264 cmd = "svn revert -q " +
1265 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1267 doVCCommand(cmd, path);
1268 if (newFile.exists())
1269 newFile.removeFile();
1272 vector<support::FileName> f;
1273 f.push_back(owner_->fileName());
1274 f.push_back(newFile);
1276 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1277 cmd = "svn revert -q " +
1278 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1280 doVCCommand(cmd, path);
1281 if (newFile.exists())
1282 newFile.removeFile();
1289 bool SVN::copyEnabled()
1295 string SVN::copy(support::FileName const & newFile, string const & msg)
1297 // svn copy does not require a log message, since it does not commit.
1298 // In LyX we commit immediately afterwards, otherwise it could be
1299 // confusing to the user to have an uncommitted file.
1300 FileName path(owner_->filePath());
1301 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1302 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1303 ' ' + quoteName(relFile));
1304 if (doVCCommand(cmd, path))
1306 vector<support::FileName> f(1, newFile);
1308 if (checkIn(f, msg, log) == LyXVC::VCSuccess)
1314 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1316 vector<support::FileName> f(1, owner_->fileName());
1317 return checkIn(f, msg, log);
1321 LyXVC::CommandResult
1322 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1324 TempFile tempfile("lyxvcout");
1325 FileName tmpf = tempfile.name();
1327 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1328 log = N_("Error: Could not generate logfile.");
1329 return LyXVC::ErrorBefore;
1333 os << "svn commit -m \"" << msg << '"';
1334 for (size_t i = 0; i < f.size(); ++i)
1335 os << ' ' << quoteName(f[i].onlyFileName());
1336 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1337 LyXVC::CommandResult ret =
1338 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1339 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1341 string res = scanLogFile(tmpf, log);
1343 frontend::Alert::error(_("Revision control error."),
1344 _("Error when committing to repository.\n"
1345 "You have to manually resolve the problem.\n"
1346 "LyX will reopen the document after you press OK."));
1347 ret = LyXVC::ErrorCommand;
1350 if (!fileLock(false, tmpf, log))
1351 ret = LyXVC::ErrorCommand;
1354 log.insert(0, "SVN: ");
1355 if (ret == LyXVC::VCSuccess && log.empty())
1356 log = "SVN: Proceeded";
1361 bool SVN::checkInEnabled()
1370 bool SVN::isCheckInWithConfirmation()
1372 // FIXME one day common getDiff and perhaps OpMode for all backends
1374 TempFile tempfile("lyxvcout");
1375 FileName tmpf = tempfile.name();
1377 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1381 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1382 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1383 FileName(owner_->filePath()));
1385 docstring diff = tmpf.fileContents("UTF-8");
1394 // FIXME Correctly return code should be checked instead of this.
1395 // This would need another solution than just plain startscript.
1396 // Hint from Andre': QProcess::readAllStandardError()...
1397 string SVN::scanLogFile(FileName const & f, string & status)
1399 ifstream ifs(f.toFilesystemEncoding().c_str());
1404 LYXERR(Debug::LYXVC, line << '\n');
1406 status += line + "; ";
1407 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1408 || contains(line, "Commit failed")) {
1412 if (contains(line, "svn:needs-lock")) {
1422 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1424 if (!locked_mode_ || (isLocked() == lock))
1427 string const arg = lock ? "lock " : "unlock ";
1428 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1429 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1430 FileName(owner_->filePath()));
1432 // Lock error messages go unfortunately on stderr and are unreachible this way.
1433 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1437 if (!line.empty()) status += line + "; ";
1441 if (isLocked() == lock)
1445 frontend::Alert::error(_("Revision control error."),
1446 _("Error while acquiring write lock.\n"
1447 "Another user is most probably editing\n"
1448 "the current document now!\n"
1449 "Also check the access to the repository."));
1451 frontend::Alert::error(_("Revision control error."),
1452 _("Error while releasing write lock.\n"
1453 "Check the access to the repository."));
1458 string SVN::checkOut()
1460 TempFile tempfile("lyxvcout");
1461 FileName tmpf = tempfile.name();
1463 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1464 return N_("Error: Could not generate logfile.");
1467 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1468 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1469 FileName(owner_->filePath()));
1472 string const res = scanLogFile(tmpf, log);
1474 frontend::Alert::error(_("Revision control error."),
1475 bformat(_("Error when updating from repository.\n"
1476 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1477 "After pressing OK, LyX will try to reopen the resolved document."),
1478 from_local8bit(res)));
1480 fileLock(true, tmpf, log);
1482 return log.empty() ? string() : "SVN: " + log;
1486 bool SVN::checkOutEnabled()
1495 string SVN::repoUpdate()
1497 TempFile tempfile("lyxvcout");
1498 FileName tmpf = tempfile.name();
1500 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1501 return N_("Error: Could not generate logfile.");
1504 doVCCommand("svn diff " + quoteName(owner_->filePath())
1505 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1506 FileName(owner_->filePath()));
1507 docstring res = tmpf.fileContents("UTF-8");
1509 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1510 docstring const file = from_utf8(owner_->filePath());
1511 docstring text = bformat(_("There were detected changes "
1512 "in the working directory:\n%1$s\n\n"
1513 "In case of file conflict version of the local directory files "
1514 "will be preferred."
1515 "\n\nContinue?"), file);
1516 int ret = frontend::Alert::prompt(_("Changes detected"),
1517 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1519 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1520 ret = frontend::Alert::prompt(_("Changes detected"),
1521 text, 0, 1, _("&Yes"), _("&No"));
1522 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);
1539 return to_utf8(res);
1543 bool SVN::repoUpdateEnabled()
1549 string SVN::lockingToggle()
1551 TempFile tempfile("lyxvcout");
1552 FileName tmpf = tempfile.name();
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()));
1580 frontend::Alert::warning(_("SVN File Locking"),
1581 (locking ? _("Locking property unset.") : _("Locking property set.")) + '\n'
1582 + _("Do not forget to commit the locking property into the repository."),
1585 return string("SVN: ") + (locking ?
1586 N_("Locking property unset.") : N_("Locking property set."));
1590 bool SVN::lockingToggleEnabled()
1598 // Reverts to the version in SVN repository and
1599 // gets the updated version from the repository.
1600 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1602 if (doVCCommand("svn revert -q " + fil,
1603 FileName(owner_->filePath())))
1605 owner_->markClean();
1610 bool SVN::isRevertWithConfirmation()
1612 //FIXME owner && diff
1617 void SVN::undoLast()
1619 // merge the current with the previous version
1620 // in a reverse patch kind of way, so that the
1621 // result is to revert the last changes.
1622 lyxerr << "Sorry, not implemented." << endl;
1626 bool SVN::undoLastEnabled()
1632 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1634 if (info == LyXVC::Tree) {
1635 if (rev_tree_cache_.empty())
1636 if (!getTreeRevisionInfo())
1637 rev_tree_cache_ = "?";
1638 if (rev_tree_cache_ == "?")
1641 return rev_tree_cache_;
1644 // fill the rest of the attributes for a single file
1645 if (rev_file_cache_.empty())
1646 if (!getFileRevisionInfo())
1647 rev_file_cache_ = "?";
1651 if (rev_file_cache_ == "?")
1653 return rev_file_cache_;
1655 return rev_author_cache_;
1657 return rev_date_cache_;
1659 return rev_time_cache_;
1668 bool SVN::getFileRevisionInfo()
1670 TempFile tempfile("lyxvcout");
1671 FileName tmpf = tempfile.name();
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;
1717 return !rev.empty();
1721 bool SVN::getTreeRevisionInfo()
1723 TempFile tempfile("lyxvcout");
1724 FileName tmpf = tempfile.name();
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());
1742 rev_tree_cache_ = line;
1743 return !line.empty();
1747 void SVN::getLog(FileName const & tmpf)
1749 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1750 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1751 FileName(owner_->filePath()));
1755 bool SVN::prepareFileRevision(string const & revis, string & f)
1757 if (!isStrInt(revis))
1760 int rev = convert<int>(revis);
1762 if (!getFileRevisionInfo())
1765 rev = convert<int>(rev_file_cache_);
1766 // go back for minus rev
1768 rev = rev + convert<int>(rev_file_cache_);
1773 string revname = convert<string>(rev);
1774 TempFile tempfile("lyxvcrev_" + revname + '_');
1775 tempfile.setAutoRemove(false);
1776 FileName tmpf = tempfile.name();
1778 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1782 doVCCommand("svn cat -r " + revname + ' '
1783 + quoteName(onlyFileName(owner_->absFileName()))
1784 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1785 FileName(owner_->filePath()));
1786 if (tmpf.isFileEmpty())
1789 f = tmpf.absFileName();
1794 bool SVN::prepareFileRevisionEnabled()
1801 bool SVN::toggleReadOnlyEnabled()
1807 /////////////////////////////////////////////////////////////////////
1811 /////////////////////////////////////////////////////////////////////
1813 GIT::GIT(FileName const & m, Buffer * b) : VCS(b)
1815 // Here we know that the buffer file is either already in GIT or
1816 // about to be registered
1822 FileName const GIT::findFile(FileName const & file)
1824 // First we check the existence of repository meta data.
1825 if (!VCS::checkparentdirs(file, ".git")) {
1826 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1830 // Now we check the status of the file.
1831 TempFile tempfile("lyxvcout");
1832 FileName tmpf = tempfile.name();
1834 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1838 string const fname = onlyFileName(file.absFileName());
1839 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1841 doVCCommandCall("git ls-files " +
1842 quoteName(fname) + " > " +
1843 quoteName(tmpf.toFilesystemEncoding()),
1845 bool found = !tmpf.isFileEmpty();
1846 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1847 return found ? file : FileName();
1851 void GIT::scanMaster()
1853 // vcstatus code is somewhat superflous,
1854 // until we want to implement read-only toggle for git.
1855 vcstatus = NOLOCKING;
1859 bool GIT::retrieve(FileName const & file)
1861 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1862 // The caller ensures that file does not exist, so no need to check that.
1863 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1864 file.onlyPath()) == 0;
1868 void GIT::registrer(string const & /*msg*/)
1870 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1871 FileName(owner_->filePath()));
1875 bool GIT::renameEnabled()
1881 string GIT::rename(support::FileName const & newFile, string const & msg)
1883 // git mv does not require a log message, since it does not commit.
1884 // In LyX we commit immediately afterwards, otherwise it could be
1885 // confusing to the user to have two uncommitted files.
1886 FileName path(owner_->filePath());
1887 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1888 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1889 ' ' + quoteName(relFile));
1890 if (doVCCommand(cmd, path)) {
1891 cmd = "git checkout -q " +
1892 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1894 doVCCommand(cmd, path);
1895 if (newFile.exists())
1896 newFile.removeFile();
1899 vector<support::FileName> f;
1900 f.push_back(owner_->fileName());
1901 f.push_back(newFile);
1903 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1904 cmd = "git checkout -q " +
1905 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1907 doVCCommand(cmd, path);
1908 if (newFile.exists())
1909 newFile.removeFile();
1916 bool GIT::copyEnabled()
1922 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1924 // git does not support copy with history preservation
1929 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1931 vector<support::FileName> f(1, owner_->fileName());
1932 return checkIn(f, msg, log);
1936 LyXVC::CommandResult
1937 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1939 TempFile tempfile("lyxvcout");
1940 FileName tmpf = tempfile.name();
1942 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1943 log = N_("Error: Could not generate logfile.");
1944 return LyXVC::ErrorBefore;
1948 os << "git commit -m \"" << msg << '"';
1949 for (size_t i = 0; i < f.size(); ++i)
1950 os << ' ' << quoteName(f[i].onlyFileName());
1951 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1952 LyXVC::CommandResult ret =
1953 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1954 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1956 string res = scanLogFile(tmpf, log);
1958 frontend::Alert::error(_("Revision control error."),
1959 _("Error when committing to repository.\n"
1960 "You have to manually resolve the problem.\n"
1961 "LyX will reopen the document after you press OK."));
1962 ret = LyXVC::ErrorCommand;
1966 log.insert(0, "GIT: ");
1967 if (ret == LyXVC::VCSuccess && log.empty())
1968 log = "GIT: Proceeded";
1973 bool GIT::checkInEnabled()
1979 bool GIT::isCheckInWithConfirmation()
1981 // FIXME one day common getDiff and perhaps OpMode for all backends
1983 TempFile tempfile("lyxvcout");
1984 FileName tmpf = tempfile.name();
1986 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1990 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
1991 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1992 FileName(owner_->filePath()));
1994 docstring diff = tmpf.fileContents("UTF-8");
2003 // FIXME Correctly return code should be checked instead of this.
2004 // This would need another solution than just plain startscript.
2005 // Hint from Andre': QProcess::readAllStandardError()...
2006 string GIT::scanLogFile(FileName const & f, string & status)
2008 ifstream ifs(f.toFilesystemEncoding().c_str());
2013 LYXERR(Debug::LYXVC, line << "\n");
2015 status += line + "; ";
2016 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2017 || contains(line, "Commit failed")) {
2027 string GIT::checkOut()
2033 bool GIT::checkOutEnabled()
2039 string GIT::repoUpdate()
2045 bool GIT::repoUpdateEnabled()
2051 string GIT::lockingToggle()
2057 bool GIT::lockingToggleEnabled()
2065 // Reverts to the version in GIT repository and
2066 // gets the updated version from the repository.
2067 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2069 if (doVCCommand("git checkout -q " + fil,
2070 FileName(owner_->filePath())))
2072 owner_->markClean();
2077 bool GIT::isRevertWithConfirmation()
2079 //FIXME owner && diff
2084 void GIT::undoLast()
2086 // merge the current with the previous version
2087 // in a reverse patch kind of way, so that the
2088 // result is to revert the last changes.
2089 lyxerr << "Sorry, not implemented." << endl;
2093 bool GIT::undoLastEnabled()
2099 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2101 if (info == LyXVC::Tree) {
2102 if (rev_tree_cache_.empty())
2103 if (!getTreeRevisionInfo())
2104 rev_tree_cache_ = "?";
2105 if (rev_tree_cache_ == "?")
2108 return rev_tree_cache_;
2111 // fill the rest of the attributes for a single file
2112 if (rev_file_cache_.empty())
2113 if (!getFileRevisionInfo())
2114 rev_file_cache_ = "?";
2118 if (rev_file_cache_ == "?")
2120 return rev_file_cache_;
2122 return rev_author_cache_;
2124 return rev_date_cache_;
2126 return rev_time_cache_;
2135 bool GIT::getFileRevisionInfo()
2137 TempFile tempfile("lyxvcout");
2138 FileName tmpf = tempfile.name();
2140 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2144 doVCCommand("git log -n 1 --pretty=format:%H%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2145 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2146 FileName(owner_->filePath()));
2151 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2154 getline(ifs, rev_file_cache_);
2156 getline(ifs, rev_author_cache_);
2160 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2164 return !rev_file_cache_.empty();
2168 bool GIT::getTreeRevisionInfo()
2170 TempFile tempfile("lyxvcout");
2171 FileName tmpf = tempfile.name();
2173 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2177 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2178 FileName(owner_->filePath()));
2183 // only first line in case something bad happens.
2184 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2185 getline(ifs, rev_tree_cache_);
2188 return !rev_tree_cache_.empty();
2192 void GIT::getLog(FileName const & tmpf)
2194 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2195 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2196 FileName(owner_->filePath()));
2200 //at this moment we don't accept revision SHA, but just number of revision steps back
2201 //GUI and infrastucture needs to be changed first
2202 bool GIT::prepareFileRevision(string const & revis, string & f)
2204 // anything positive means we got hash, not "0" or minus revision
2207 // hash is rarely number and should be long
2208 if (isStrInt(revis) && revis.length()<20)
2209 rev = convert<int>(revis);
2211 // revision and filename
2214 // go back for "minus" revisions
2216 pointer = "HEAD~" + convert<string>(-rev);
2223 TempFile tempfile("lyxvcrev_" + revis + '_');
2224 tempfile.setAutoRemove(false);
2225 FileName tmpf = tempfile.name();
2227 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2231 doVCCommand("git show " + pointer + "./"
2232 + quoteName(onlyFileName(owner_->absFileName()))
2233 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2234 FileName(owner_->filePath()));
2235 if (tmpf.isFileEmpty())
2238 f = tmpf.absFileName();
2243 bool GIT::prepareFileRevisionEnabled()
2249 bool GIT::toggleReadOnlyEnabled()