3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "VCBackend.h"
17 #include "FuncRequest.h"
19 #include "frontends/alert.h"
20 #include "frontends/Application.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/PathChanger.h"
28 #include "support/Systemcall.h"
29 #include "support/regex.h"
34 using namespace lyx::support;
41 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
43 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
45 support::PathChanger p(path);
46 return one.startscript(Systemcall::Wait, cmd, string(), false);
50 int VCS::doVCCommand(string const & cmd, FileName const & path, bool reportError)
53 owner_->setBusy(true);
55 int const ret = doVCCommandCall(cmd, path);
58 owner_->setBusy(false);
59 if (ret && reportError)
60 frontend::Alert::error(_("Revision control error."),
61 bformat(_("Some problem occured while running the command:\n"
68 bool VCS::makeRCSRevision(string const &version, string &revis) const
73 int back = convert<int>(rev);
74 // if positive use as the last number in the whole revision string
77 rsplit(version, base , '.' );
78 rev = base + "." + rev;
82 // we care about the last number from revision string
83 // in case of backward indexing
86 cur = rsplit(version, base , '.' );
89 int want = convert<int>(cur) + back;
93 rev = base + "." + convert<string>(want);
102 bool VCS::checkparentdirs(FileName const & file, std::string const & pathname)
104 FileName dirname = file.onlyPath();
105 FileName tocheck = FileName(addName(dirname.absFileName(),pathname));
106 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
107 bool result = tocheck.exists();
108 while ( !result && !dirname.empty() ) {
109 //this construct because of #8295
110 dirname = FileName(dirname.absFileName()).parentPath();
111 LYXERR(Debug::LYXVC, "check directory: " << dirname.absFileName());
112 tocheck = FileName(addName(dirname.absFileName(),pathname));
113 result = tocheck.exists();
119 /////////////////////////////////////////////////////////////////////
123 /////////////////////////////////////////////////////////////////////
125 RCS::RCS(FileName const & m, Buffer * b) : VCS(b)
132 FileName const RCS::findFile(FileName const & file)
134 // Check if *,v exists.
135 FileName tmp(file.absFileName() + ",v");
136 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
137 if (tmp.isReadableFile()) {
138 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
142 // Check if RCS/*,v exists.
143 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
144 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
145 if (tmp.isReadableFile()) {
146 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
154 bool RCS::retrieve(FileName const & file)
156 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
157 // The caller ensures that file does not exist, so no need to check that.
158 return doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
163 void RCS::scanMaster()
168 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
170 ifstream ifs(master_.toFilesystemEncoding().c_str());
173 bool read_enough = false;
175 while (!read_enough && ifs >> token) {
176 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
181 else if (token == "head") {
185 tmv = rtrim(tmv, ";");
187 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
188 } else if (contains(token, "access")
189 || contains(token, "symbols")
190 || contains(token, "strict")) {
192 } else if (contains(token, "locks")) {
194 if (contains(token, ';')) {
195 locker_ = "Unlocked";
204 s1 = rtrim(tmpt, ";");
205 // tmp is now in the format <user>:<version>
206 s1 = split(s1, s2, ':');
207 // s2 is user, and s1 is version
208 if (s1 == version_) {
213 } while (!contains(tmpt, ';'));
215 } else if (token == "comment") {
216 // we don't need to read any further than this.
220 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
226 void RCS::registrer(string const & msg)
228 string cmd = "ci -q -u -i -t-\"";
231 cmd += quoteName(onlyFileName(owner_->absFileName()));
232 doVCCommand(cmd, FileName(owner_->filePath()));
236 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::Success;
283 bool RCS::checkInEnabled()
285 return owner_ && !owner_->isReadonly();
289 bool RCS::isCheckInWithConfirmation()
291 // FIXME one day common getDiff for all backends
293 // if (getDiff(file, diff) && diff.empty())
296 FileName tmpf = FileName::tempName("lyxvcout");
298 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
302 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
303 + " > " + quoteName(tmpf.toFilesystemEncoding()),
304 FileName(owner_->filePath()));
306 docstring diff = tmpf.fileContents("UTF-8");
316 string RCS::checkOut()
319 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
320 FileName(owner_->filePath()));
321 return ret ? string() : "RCS: Proceeded";
325 bool RCS::checkOutEnabled()
327 return owner_ && owner_->isReadonly();
331 string RCS::repoUpdate()
333 lyxerr << "Sorry, not implemented." << endl;
338 bool RCS::repoUpdateEnabled()
344 string RCS::lockingToggle()
346 //FIXME this might be actually possible, study rcs -U, rcs -L.
347 //State should be easy to get inside scanMaster.
348 //It would fix #4370 and make rcs/svn usage even more closer.
349 lyxerr << "Sorry, not implemented." << endl;
354 bool RCS::lockingToggleEnabled()
362 if (doVCCommand("co -f -u" + version_ + " "
363 + quoteName(onlyFileName(owner_->absFileName())),
364 FileName(owner_->filePath())))
366 // We ignore changes and just reload!
372 bool RCS::isRevertWithConfirmation()
374 //FIXME owner && diff ?
381 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
382 doVCCommand("rcs -o" + version_ + " "
383 + quoteName(onlyFileName(owner_->absFileName())),
384 FileName(owner_->filePath()));
388 bool RCS::undoLastEnabled()
394 void RCS::getLog(FileName const & tmpf)
396 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
397 + " > " + quoteName(tmpf.toFilesystemEncoding()),
398 FileName(owner_->filePath()));
402 bool RCS::toggleReadOnlyEnabled()
404 // This got broken somewhere along lfuns dispatch reorganization.
405 // reloadBuffer would be needed after this, but thats problematic
406 // since we are inside Buffer::dispatch.
407 // return return status() != UNVERSIONED;
412 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
414 if (info == LyXVC::File)
416 // fill the rest of the attributes for a single file
417 if (rev_date_cache_.empty())
418 if (!getRevisionInfo())
423 return rev_author_cache_;
425 return rev_date_cache_;
427 return rev_time_cache_;
435 bool RCS::getRevisionInfo()
437 FileName tmpf = FileName::tempName("lyxvcout");
439 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
442 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
443 + " > " + quoteName(tmpf.toFilesystemEncoding()),
444 FileName(owner_->filePath()));
449 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
452 // we reached to the entry, i.e. after initial log message
454 // line with critical info, e.g:
455 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
460 LYXERR(Debug::LYXVC, line);
461 if (entry && prefixIs(line, "date:")) {
465 if (prefixIs(line, "revision"))
471 rev_date_cache_ = token(result, ' ', 1);
472 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
473 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
475 return !rev_author_cache_.empty();
478 bool RCS::prepareFileRevision(string const &revis, string & f)
481 if (!VCS::makeRCSRevision(version_, rev))
484 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
486 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
490 doVCCommand("co -p" + rev + " "
491 + quoteName(onlyFileName(owner_->absFileName()))
492 + " > " + quoteName(tmpf.toFilesystemEncoding()),
493 FileName(owner_->filePath()));
494 if (tmpf.isFileEmpty())
497 f = tmpf.absFileName();
502 bool RCS::prepareFileRevisionEnabled()
508 /////////////////////////////////////////////////////////////////////
512 /////////////////////////////////////////////////////////////////////
514 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
517 have_rev_info_ = false;
522 FileName const CVS::findFile(FileName const & file)
524 // First we look for the CVS/Entries in the same dir
525 // where we have file.
526 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
527 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
528 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
529 << "' for `" << tmpf << '\'');
530 if (entries.isReadableFile()) {
531 // Ok we are at least in a CVS dir. Parse the CVS/Entries
532 // and see if we can find this file. We do a fast and
534 ifstream ifs(entries.toFilesystemEncoding().c_str());
536 while (getline(ifs, line)) {
537 LYXERR(Debug::LYXVC, "\tEntries: " << line);
538 if (contains(line, tmpf))
546 void CVS::scanMaster()
548 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
549 // Ok now we do the real scan...
550 ifstream ifs(master_.toFilesystemEncoding().c_str());
551 string name = onlyFileName(owner_->absFileName());
552 string tmpf = '/' + name + '/';
553 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
555 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
556 vcstatus = UNVERSIONED;
557 while (getline(ifs, line)) {
558 LYXERR(Debug::LYXVC, "\t line: " << line);
559 if (contains(line, tmpf)) {
560 // Ok extract the fields.
563 regex_match(line, sm, reg);
565 //sm[0]; // whole matched string
567 version_ = sm.str(2);
568 string const file_date = sm.str(3);
571 //sm[5]; // tag or tagdate
572 FileName file(owner_->absFileName());
573 if (file.isReadableFile()) {
574 time_t mod = file.lastModified();
575 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
576 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
577 << "'\nModification date of file: `" << mod_date << '\'');
578 if (file.isReadOnly()) {
579 // readonly checkout is unlocked
582 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
583 FileName base(addName(bdir.absFileName(),name));
584 // if base version is existent "cvs edit" was used to lock
585 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
588 vcstatus = NOLOCKING;
596 bool CVS::retrieve(FileName const & file)
598 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
599 // The caller ensures that file does not exist, so no need to check that.
600 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
601 file.onlyPath()) == 0;
605 string const CVS::getTarget(OperationMode opmode) const
609 // in client server mode CVS does not like full path operand for directory operation
610 // since LyX switches to the repo dir "." is good enough as target
613 return quoteName(onlyFileName(owner_->absFileName()));
619 docstring CVS::toString(CvsStatus status) const
623 return _("Up-to-date");
624 case LocallyModified:
625 return _("Locally Modified");
627 return _("Locally Added");
629 return _("Needs Merge");
631 return _("Needs Checkout");
633 return _("No CVS file");
635 return _("Cannot retrieve CVS status");
641 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
642 FileName const & output, bool reportError)
644 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
645 return doVCCommand(cmd + redirection, path, reportError);
649 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
650 support::FileName const & path,
651 support::FileName const & output)
653 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
654 return doVCCommandCall(cmd + redirection, path);
658 CVS::CvsStatus CVS::getStatus()
660 FileName tmpf = FileName::tempName("lyxvcout");
662 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
666 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
667 FileName(owner_->filePath()), tmpf)) {
672 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
673 CvsStatus status = NoCvsFile;
678 LYXERR(Debug::LYXVC, line << "\n");
679 if (prefixIs(line, "File:")) {
680 if (contains(line, "Up-to-date"))
682 else if (contains(line, "Locally Modified"))
683 status = LocallyModified;
684 else if (contains(line, "Locally Added"))
685 status = LocallyAdded;
686 else if (contains(line, "Needs Merge"))
688 else if (contains(line, "Needs Checkout"))
689 status = NeedsCheckout;
696 void CVS::getRevisionInfo()
700 have_rev_info_ = true;
701 FileName tmpf = FileName::tempName("lyxvcout");
703 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
707 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
708 + " " + getTarget(File),
709 FileName(owner_->filePath()), tmpf);
712 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
716 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
717 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
722 LYXERR(Debug::LYXVC, line << "\n");
723 if (prefixIs(line, "date:")) {
725 regex_match(line, sm, reg);
726 //sm[0]; // whole matched string
727 rev_date_cache_ = sm[1];
728 rev_time_cache_ = sm[2];
729 //sm[3]; // GMT offset
730 rev_author_cache_ = sm[4];
735 if (rev_author_cache_.empty())
737 "Could not retrieve revision info for " << version_ <<
738 " of " << getTarget(File));
742 void CVS::registrer(string const & msg)
744 doVCCommand("cvs -q add -m \"" + msg + "\" "
746 FileName(owner_->filePath()));
750 bool CVS::renameEnabled()
756 string CVS::rename(support::FileName const & newFile, string const & msg)
758 // CVS has no real rename command, so we create a poor mans version
759 support::FileName const oldFile(owner_->absFileName());
760 string ret = copy(newFile, msg);
763 string cmd = "cvs -q remove -m \"" + msg + "\" " +
764 quoteName(oldFile.onlyFileName());
765 FileName path(oldFile.onlyPath());
766 return doVCCommand(cmd, path) ? string() : ret;
770 bool CVS::copyEnabled()
776 string CVS::copy(support::FileName const & newFile, string const & msg)
778 // CVS has no real copy command, so we create a poor mans version
779 support::FileName const oldFile(owner_->absFileName());
780 if (!oldFile.copyTo(newFile))
782 FileName path(oldFile.onlyPath());
783 string relFile(to_utf8(newFile.relPath(path.absFileName())));
784 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
785 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
789 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
791 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
792 FileName(owner_->filePath()), tmpf, false);
799 return doVCCommand("cvs -q edit " + getTarget(File),
800 FileName(owner_->filePath()));
807 return doVCCommand("cvs -q unedit " + getTarget(File),
808 FileName(owner_->filePath()));
812 int CVS::update(OperationMode opmode, FileName const & tmpf)
814 return doVCCommandWithOutput("cvs -q update "
816 FileName(owner_->filePath()), tmpf, false);
820 string CVS::scanLogFile(FileName const & f, string & status)
822 ifstream ifs(f.toFilesystemEncoding().c_str());
827 LYXERR(Debug::LYXVC, line << "\n");
829 status += line + "; ";
830 if (prefixIs(line, "C ")) {
840 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
842 CvsStatus status = getStatus();
845 if (vcstatus != NOLOCKING)
847 return LyXVC::ErrorCommand;
848 log = "CVS: Proceeded";
849 return LyXVC::Success;
850 case LocallyModified:
852 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
854 FileName(owner_->filePath()));
856 return LyXVC::ErrorCommand;
857 log = "CVS: Proceeded";
858 return LyXVC::Success;
862 frontend::Alert::error(_("Revision control error."),
863 _("The repository version is newer then the current check out.\n"
864 "You have to update from repository first or revert your changes.")) ;
867 frontend::Alert::error(_("Revision control error."),
868 bformat(_("Bad status when checking in changes.\n"
873 return LyXVC::ErrorBefore;
877 bool CVS::isLocked() const
879 FileName fn(owner_->absFileName());
881 return !fn.isReadOnly();
885 bool CVS::checkInEnabled()
887 if (vcstatus != NOLOCKING)
894 bool CVS::isCheckInWithConfirmation()
896 CvsStatus status = getStatus();
897 return status == LocallyModified || status == LocallyAdded;
901 string CVS::checkOut()
903 if (vcstatus != NOLOCKING && edit())
905 FileName tmpf = FileName::tempName("lyxvcout");
907 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
911 int rc = update(File, tmpf);
913 string const res = scanLogFile(tmpf, log);
915 frontend::Alert::error(_("Revision control error."),
916 bformat(_("Error when updating from repository.\n"
917 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
918 "After pressing OK, LyX will try to reopen the resolved document."),
919 from_local8bit(res)));
924 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
928 bool CVS::checkOutEnabled()
930 if (vcstatus != NOLOCKING)
937 string CVS::repoUpdate()
939 FileName tmpf = FileName::tempName("lyxvcout");
941 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
945 getDiff(Directory, tmpf);
946 docstring res = tmpf.fileContents("UTF-8");
948 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
949 docstring const file = from_utf8(owner_->filePath());
950 docstring text = bformat(_("There were detected changes "
951 "in the working directory:\n%1$s\n\n"
952 "Possible file conflicts must be then resolved manually "
953 "or you will need to revert back to the repository version."), file);
954 int ret = frontend::Alert::prompt(_("Changes detected"),
955 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
957 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
958 ret = frontend::Alert::prompt(_("Changes detected"),
959 text, 0, 1, _("&Continue"), _("&Abort"));
960 hideDialogs("file", 0);
968 int rc = update(Directory, tmpf);
969 res += "Update log:\n" + tmpf.fileContents("UTF-8");
970 LYXERR(Debug::LYXVC, res);
973 string sres = scanLogFile(tmpf, log);
975 docstring const file = owner_->fileName().displayName(20);
976 frontend::Alert::error(_("Revision control error."),
977 bformat(_("Error when updating document %1$s from repository.\n"
978 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
979 "After pressing OK, LyX will try to reopen the resolved document."),
980 file, from_local8bit(sres)));
986 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
990 bool CVS::repoUpdateEnabled()
996 string CVS::lockingToggle()
998 lyxerr << "Sorry, not implemented." << endl;
1003 bool CVS::lockingToggleEnabled()
1009 bool CVS::isRevertWithConfirmation()
1011 CvsStatus status = getStatus();
1012 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1018 // Reverts to the version in CVS repository and
1019 // gets the updated version from the repository.
1020 CvsStatus status = getStatus();
1023 if (vcstatus != NOLOCKING)
1024 return 0 == unedit();
1028 case LocallyModified: {
1029 FileName f(owner_->absFileName());
1031 update(File, FileName());
1032 owner_->markClean();
1035 case LocallyAdded: {
1036 docstring const file = owner_->fileName().displayName(20);
1037 frontend::Alert::error(_("Revision control error."),
1038 bformat(_("The document %1$s is not in repository.\n"
1039 "You have to check in the first revision before you can revert."),
1044 docstring const file = owner_->fileName().displayName(20);
1045 frontend::Alert::error(_("Revision control error."),
1046 bformat(_("Cannot revert document %1$s to repository version.\n"
1047 "The status '%2$s' is unexpected."),
1048 file, toString(status)));
1056 void CVS::undoLast()
1058 // merge the current with the previous version
1059 // in a reverse patch kind of way, so that the
1060 // result is to revert the last changes.
1061 lyxerr << "Sorry, not implemented." << endl;
1065 bool CVS::undoLastEnabled()
1071 void CVS::getLog(FileName const & tmpf)
1073 doVCCommandWithOutput("cvs log " + getTarget(File),
1074 FileName(owner_->filePath()),
1079 bool CVS::toggleReadOnlyEnabled()
1085 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1087 if (!version_.empty()) {
1093 return rev_author_cache_;
1095 return rev_date_cache_;
1097 return rev_time_cache_;
1105 bool CVS::prepareFileRevision(string const & revis, string & f)
1108 if (!VCS::makeRCSRevision(version_, rev))
1111 FileName tmpf = FileName::tempName("lyxvcrev_" + rev + "_");
1113 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1117 doVCCommandWithOutput("cvs update -p -r" + rev + " "
1119 FileName(owner_->filePath()), tmpf);
1120 if (tmpf.isFileEmpty())
1123 f = tmpf.absFileName();
1128 bool CVS::prepareFileRevisionEnabled()
1134 /////////////////////////////////////////////////////////////////////
1138 /////////////////////////////////////////////////////////////////////
1140 SVN::SVN(FileName const & m, Buffer * b) : VCS(b)
1148 FileName const SVN::findFile(FileName const & file)
1150 // First we check the existence of repository meta data.
1151 if (!VCS::checkparentdirs(file, ".svn")) {
1152 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1156 // Now we check the status of the file.
1157 FileName tmpf = FileName::tempName("lyxvcout");
1159 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1163 string const fname = onlyFileName(file.absFileName());
1164 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1165 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1166 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1169 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1170 return found ? file : FileName();
1174 void SVN::scanMaster()
1176 // vcstatus code other than UNVERSIONED is somewhat superflous,
1177 // until we want to implement read-only toggle for svn.
1178 FileName f = findFile(owner_->fileName());
1180 vcstatus = UNVERSIONED;
1182 vcstatus = NOLOCKING;
1183 if (checkLockMode()) {
1187 vcstatus = UNLOCKED;
1193 bool SVN::checkLockMode()
1195 FileName tmpf = FileName::tempName("lyxvcout");
1197 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1201 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1202 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1203 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1204 FileName(owner_->filePath())))
1207 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1211 while (ifs && !ret) {
1213 LYXERR(Debug::LYXVC, line);
1214 if (contains(line, "svn:needs-lock"))
1217 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1225 bool SVN::isLocked() const
1227 FileName file(owner_->absFileName());
1229 return !file.isReadOnly();
1233 bool SVN::retrieve(FileName const & file)
1235 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1236 // The caller ensures that file does not exist, so no need to check that.
1237 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1238 file.onlyPath()) == 0;
1242 void SVN::registrer(string const & /*msg*/)
1244 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1245 FileName(owner_->filePath()));
1249 bool SVN::renameEnabled()
1255 string SVN::rename(support::FileName const & newFile, string const & msg)
1257 // svn move does not require a log message, since it does not commit.
1258 // In LyX we commit immediately afterwards, otherwise it could be
1259 // confusing to the user to have two uncommitted files.
1260 FileName path(owner_->filePath());
1261 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1262 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1263 ' ' + quoteName(relFile));
1264 if (doVCCommand(cmd, path)) {
1265 cmd = "svn revert -q " +
1266 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1268 doVCCommand(cmd, path);
1269 if (newFile.exists())
1270 newFile.removeFile();
1273 vector<support::FileName> f;
1274 f.push_back(owner_->fileName());
1275 f.push_back(newFile);
1277 if (checkIn(f, msg, log) != LyXVC::Success) {
1278 cmd = "svn revert -q " +
1279 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1281 doVCCommand(cmd, path);
1282 if (newFile.exists())
1283 newFile.removeFile();
1290 bool SVN::copyEnabled()
1296 string SVN::copy(support::FileName const & newFile, string const & msg)
1298 // svn copy does not require a log message, since it does not commit.
1299 // In LyX we commit immediately afterwards, otherwise it could be
1300 // confusing to the user to have an uncommitted file.
1301 FileName path(owner_->filePath());
1302 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1303 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1304 ' ' + quoteName(relFile));
1305 if (doVCCommand(cmd, path))
1307 vector<support::FileName> f(1, newFile);
1309 if (checkIn(f, msg, log) == LyXVC::Success)
1315 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1317 vector<support::FileName> f(1, owner_->fileName());
1318 return checkIn(f, msg, log);
1322 LyXVC::CommandResult
1323 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1325 FileName tmpf = FileName::tempName("lyxvcout");
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::Success;
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;
1355 log.insert(0, "SVN: ");
1356 if (ret == LyXVC::Success && log.empty())
1357 log = "SVN: Proceeded";
1362 bool SVN::checkInEnabled()
1371 bool SVN::isCheckInWithConfirmation()
1373 // FIXME one day common getDiff and perhaps OpMode for all backends
1375 FileName tmpf = FileName::tempName("lyxvcout");
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");
1395 // FIXME Correctly return code should be checked instead of this.
1396 // This would need another solution than just plain startscript.
1397 // Hint from Andre': QProcess::readAllStandardError()...
1398 string SVN::scanLogFile(FileName const & f, string & status)
1400 ifstream ifs(f.toFilesystemEncoding().c_str());
1405 LYXERR(Debug::LYXVC, line << "\n");
1407 status += line + "; ";
1408 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1409 || contains(line, "Commit failed")) {
1413 if (contains(line, "svn:needs-lock")) {
1423 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1425 if (!locked_mode_ || (isLocked() == lock))
1428 string const arg = lock ? "lock " : "unlock ";
1429 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1430 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1431 FileName(owner_->filePath()));
1433 // Lock error messages go unfortunately on stderr and are unreachible this way.
1434 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1438 if (!line.empty()) status += line + "; ";
1442 if (isLocked() == lock)
1446 frontend::Alert::error(_("Revision control error."),
1447 _("Error while acquiring write lock.\n"
1448 "Another user is most probably editing\n"
1449 "the current document now!\n"
1450 "Also check the access to the repository."));
1452 frontend::Alert::error(_("Revision control error."),
1453 _("Error while releasing write lock.\n"
1454 "Check the access to the repository."));
1459 string SVN::checkOut()
1461 FileName tmpf = FileName::tempName("lyxvcout");
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);
1483 return log.empty() ? string() : "SVN: " + log;
1487 bool SVN::checkOutEnabled()
1496 string SVN::repoUpdate()
1498 FileName tmpf = FileName::tempName("lyxvcout");
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);
1530 // Reverting looks too harsh, see bug #6255.
1531 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1532 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1533 // FileName(owner_->filePath()));
1534 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1535 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1536 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1537 FileName(owner_->filePath()));
1538 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1540 LYXERR(Debug::LYXVC, res);
1542 return to_utf8(res);
1546 bool SVN::repoUpdateEnabled()
1552 string SVN::lockingToggle()
1554 FileName tmpf = FileName::tempName("lyxvcout");
1556 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1557 return N_("Error: Could not generate logfile.");
1560 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1561 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1562 FileName(owner_->filePath()));
1567 string res = scanLogFile(tmpf, log);
1568 bool locking = contains(res, "svn:needs-lock");
1570 ret = doVCCommand("svn propset svn:needs-lock ON "
1571 + quoteName(onlyFileName(owner_->absFileName()))
1572 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1573 FileName(owner_->filePath()));
1575 ret = doVCCommand("svn propdel svn:needs-lock "
1576 + quoteName(onlyFileName(owner_->absFileName()))
1577 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1578 FileName(owner_->filePath()));
1583 frontend::Alert::warning(_("SVN File Locking"),
1584 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
1585 + _("Do not forget to commit the locking property into the repository."),
1588 return string("SVN: ") + (locking ?
1589 N_("Locking property unset.") : N_("Locking property set."));
1593 bool SVN::lockingToggleEnabled()
1601 // Reverts to the version in SVN repository and
1602 // gets the updated version from the repository.
1603 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1605 if (doVCCommand("svn revert -q " + fil,
1606 FileName(owner_->filePath())))
1608 owner_->markClean();
1613 bool SVN::isRevertWithConfirmation()
1615 //FIXME owner && diff
1620 void SVN::undoLast()
1622 // merge the current with the previous version
1623 // in a reverse patch kind of way, so that the
1624 // result is to revert the last changes.
1625 lyxerr << "Sorry, not implemented." << endl;
1629 bool SVN::undoLastEnabled()
1635 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1637 if (info == LyXVC::Tree) {
1638 if (rev_tree_cache_.empty())
1639 if (!getTreeRevisionInfo())
1640 rev_tree_cache_ = "?";
1641 if (rev_tree_cache_ == "?")
1644 return rev_tree_cache_;
1647 // fill the rest of the attributes for a single file
1648 if (rev_file_cache_.empty())
1649 if (!getFileRevisionInfo())
1650 rev_file_cache_ = "?";
1654 if (rev_file_cache_ == "?")
1656 return rev_file_cache_;
1658 return rev_author_cache_;
1660 return rev_date_cache_;
1662 return rev_time_cache_;
1671 bool SVN::getFileRevisionInfo()
1673 FileName tmpf = FileName::tempName("lyxvcout");
1675 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1679 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1680 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1681 FileName(owner_->filePath()));
1686 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1694 LYXERR(Debug::LYXVC, line);
1695 if (prefixIs(line, "<commit"))
1697 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1698 string l1 = subst(line, "revision=\"", "");
1699 string l2 = trim(subst(l1, "\">", ""));
1701 rev_file_cache_ = rev = l2;
1703 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1704 string l1 = subst(line, "<author>", "");
1705 string l2 = subst(l1, "</author>", "");
1706 rev_author_cache_ = l2;
1708 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1709 string l1 = subst(line, "<date>", "");
1710 string l2 = subst(l1, "</date>", "");
1711 l2 = split(l2, l1, 'T');
1712 rev_date_cache_ = l1;
1713 l2 = split(l2, l1, '.');
1714 rev_time_cache_ = l1;
1720 return !rev.empty();
1724 bool SVN::getTreeRevisionInfo()
1726 FileName tmpf = FileName::tempName("lyxvcout");
1728 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1732 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1733 FileName(owner_->filePath()));
1738 // only first line in case something bad happens.
1739 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1745 rev_tree_cache_ = line;
1746 return !line.empty();
1750 void SVN::getLog(FileName const & tmpf)
1752 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1753 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1754 FileName(owner_->filePath()));
1758 bool SVN::prepareFileRevision(string const & revis, string & f)
1760 if (!isStrInt(revis))
1763 int rev = convert<int>(revis);
1765 if (!getFileRevisionInfo())
1768 rev = convert<int>(rev_file_cache_);
1769 // go back for minus rev
1771 rev = rev + convert<int>(rev_file_cache_);
1776 string revname = convert<string>(rev);
1777 FileName tmpf = FileName::tempName("lyxvcrev_" + revname + "_");
1779 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1783 doVCCommand("svn cat -r " + revname + " "
1784 + quoteName(onlyFileName(owner_->absFileName()))
1785 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1786 FileName(owner_->filePath()));
1787 if (tmpf.isFileEmpty())
1790 f = tmpf.absFileName();
1795 bool SVN::prepareFileRevisionEnabled()
1802 bool SVN::toggleReadOnlyEnabled()
1808 /////////////////////////////////////////////////////////////////////
1812 /////////////////////////////////////////////////////////////////////
1814 GIT::GIT(FileName const & m, Buffer * b) : VCS(b)
1821 FileName const GIT::findFile(FileName const & file)
1823 // First we check the existence of repository meta data.
1824 if (!VCS::checkparentdirs(file, ".git")) {
1825 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1829 // Now we check the status of the file.
1830 FileName tmpf = FileName::tempName("lyxvcout");
1832 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1836 // --porcelain selects a format that is supposed to be stable across
1838 string const fname = onlyFileName(file.absFileName());
1839 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1841 bool found = 0 == doVCCommandCall("git status --porcelain " +
1842 quoteName(fname) + " > " +
1843 quoteName(tmpf.toFilesystemEncoding()),
1847 // The output contains a line starting with "??" for unknown
1848 // files, no line for known unmodified files and a line
1849 // starting with "M" or something else for modified/deleted
1851 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1854 found = (test != "??");
1858 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1859 return found ? file : FileName();
1863 void GIT::scanMaster()
1865 // vcstatus code other than UNVERSIONED is somewhat superflous,
1866 // until we want to implement read-only toggle for git.
1867 FileName f = findFile(owner_->fileName());
1869 vcstatus = UNVERSIONED;
1871 vcstatus = NOLOCKING;
1875 bool GIT::retrieve(FileName const & file)
1877 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1878 // The caller ensures that file does not exist, so no need to check that.
1879 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1880 file.onlyPath()) == 0;
1884 void GIT::registrer(string const & /*msg*/)
1886 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1887 FileName(owner_->filePath()));
1891 bool GIT::renameEnabled()
1897 string GIT::rename(support::FileName const & newFile, string const & msg)
1899 // git mv does not require a log message, since it does not commit.
1900 // In LyX we commit immediately afterwards, otherwise it could be
1901 // confusing to the user to have two uncommitted files.
1902 FileName path(owner_->filePath());
1903 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1904 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1905 ' ' + quoteName(relFile));
1906 if (doVCCommand(cmd, path)) {
1907 cmd = "git checkout -q " +
1908 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1910 doVCCommand(cmd, path);
1911 if (newFile.exists())
1912 newFile.removeFile();
1915 vector<support::FileName> f;
1916 f.push_back(owner_->fileName());
1917 f.push_back(newFile);
1919 if (checkIn(f, msg, log) != LyXVC::Success) {
1920 cmd = "git checkout -q " +
1921 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1923 doVCCommand(cmd, path);
1924 if (newFile.exists())
1925 newFile.removeFile();
1932 bool GIT::copyEnabled()
1938 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1940 // git does not support copy with history preservation
1945 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1947 vector<support::FileName> f(1, owner_->fileName());
1948 return checkIn(f, msg, log);
1952 LyXVC::CommandResult
1953 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1955 FileName tmpf = FileName::tempName("lyxvcout");
1957 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1958 log = N_("Error: Could not generate logfile.");
1959 return LyXVC::ErrorBefore;
1963 os << "git commit -m \"" << msg << '"';
1964 for (size_t i = 0; i < f.size(); ++i)
1965 os << ' ' << quoteName(f[i].onlyFileName());
1966 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1967 LyXVC::CommandResult ret =
1968 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1969 LyXVC::ErrorCommand : LyXVC::Success;
1971 string res = scanLogFile(tmpf, log);
1973 frontend::Alert::error(_("Revision control error."),
1974 _("Error when committing to repository.\n"
1975 "You have to manually resolve the problem.\n"
1976 "LyX will reopen the document after you press OK."));
1977 ret = LyXVC::ErrorCommand;
1982 log.insert(0, "GIT: ");
1983 if (ret == LyXVC::Success && log.empty())
1984 log = "GIT: Proceeded";
1989 bool GIT::checkInEnabled()
1995 bool GIT::isCheckInWithConfirmation()
1997 // FIXME one day common getDiff and perhaps OpMode for all backends
1999 FileName tmpf = FileName::tempName("lyxvcout");
2001 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2005 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
2006 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2007 FileName(owner_->filePath()));
2009 docstring diff = tmpf.fileContents("UTF-8");
2019 // FIXME Correctly return code should be checked instead of this.
2020 // This would need another solution than just plain startscript.
2021 // Hint from Andre': QProcess::readAllStandardError()...
2022 string GIT::scanLogFile(FileName const & f, string & status)
2024 ifstream ifs(f.toFilesystemEncoding().c_str());
2029 LYXERR(Debug::LYXVC, line << "\n");
2031 status += line + "; ";
2032 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2033 || contains(line, "Commit failed")) {
2043 string GIT::checkOut()
2049 bool GIT::checkOutEnabled()
2055 string GIT::repoUpdate()
2061 bool GIT::repoUpdateEnabled()
2067 string GIT::lockingToggle()
2073 bool GIT::lockingToggleEnabled()
2081 // Reverts to the version in GIT repository and
2082 // gets the updated version from the repository.
2083 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2085 if (doVCCommand("git checkout -q " + fil,
2086 FileName(owner_->filePath())))
2088 owner_->markClean();
2093 bool GIT::isRevertWithConfirmation()
2095 //FIXME owner && diff
2100 void GIT::undoLast()
2102 // merge the current with the previous version
2103 // in a reverse patch kind of way, so that the
2104 // result is to revert the last changes.
2105 lyxerr << "Sorry, not implemented." << endl;
2109 bool GIT::undoLastEnabled()
2115 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2117 if (info == LyXVC::Tree) {
2118 if (rev_tree_cache_.empty())
2119 if (!getTreeRevisionInfo())
2120 rev_tree_cache_ = "?";
2121 if (rev_tree_cache_ == "?")
2124 return rev_tree_cache_;
2127 // fill the rest of the attributes for a single file
2128 if (rev_file_cache_.empty())
2129 if (!getFileRevisionInfo())
2130 rev_file_cache_ = "?";
2134 if (rev_file_cache_ == "?")
2136 return rev_file_cache_;
2138 return rev_author_cache_;
2140 return rev_date_cache_;
2142 return rev_time_cache_;
2151 bool GIT::getFileRevisionInfo()
2153 FileName tmpf = FileName::tempName("lyxvcout");
2155 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2159 doVCCommand("git log -n 1 --pretty=format:%H%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2160 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2161 FileName(owner_->filePath()));
2166 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2169 getline(ifs, rev_file_cache_);
2171 getline(ifs, rev_author_cache_);
2175 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2180 return !rev_file_cache_.empty();
2184 bool GIT::getTreeRevisionInfo()
2186 FileName tmpf = FileName::tempName("lyxvcout");
2188 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2192 doVCCommand("git log -n 1 --pretty=format:%H . > " + quoteName(tmpf.toFilesystemEncoding()),
2193 FileName(owner_->filePath()));
2198 // only first line in case something bad happens.
2199 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2200 getline(ifs, rev_tree_cache_);
2204 return !rev_tree_cache_.empty();
2208 void GIT::getLog(FileName const & tmpf)
2210 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2211 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2212 FileName(owner_->filePath()));
2216 //at this moment we don't accept revision SHA, but just number of revision steps back
2217 //GUI and infrastucture needs to be changed first
2218 bool GIT::prepareFileRevision(string const & revis, string & f)
2220 // anything positive means we got hash, not "0" or minus revision
2223 // hash is rarely number and should be long
2224 if (isStrInt(revis) && revis.length()<20)
2225 rev = convert<int>(revis);
2227 // revision and filename
2230 // go back for "minus" revisions
2232 pointer = "HEAD~" + convert<string>(-rev);
2240 if (!getFileRevisionInfo())
2243 FileName tmpf = FileName::tempName("lyxvcrev_" + revis + "_");
2245 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2249 doVCCommand("git show " + pointer + "./"
2250 + quoteName(onlyFileName(owner_->absFileName()))
2251 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2252 FileName(owner_->filePath()));
2253 if (tmpf.isFileEmpty())
2256 f = tmpf.absFileName();
2261 bool GIT::prepareFileRevisionEnabled()
2267 bool GIT::toggleReadOnlyEnabled()