3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "VCBackend.h"
17 #include "FuncRequest.h"
19 #include "frontends/alert.h"
20 #include "frontends/Application.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/PathChanger.h"
28 #include "support/Systemcall.h"
29 #include "support/TempFile.h"
37 using namespace lyx::support;
43 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
45 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
47 support::PathChanger p(path);
48 return one.startscript(Systemcall::Wait, cmd, string(), string(), false);
52 int VCS::doVCCommand(string const & cmd, FileName const & path, bool reportError)
55 owner_->setBusy(true);
57 int const ret = doVCCommandCall(cmd, path);
60 owner_->setBusy(false);
61 if (ret && reportError) {
63 if (prefixIs(cmd, "ci "))
64 rcsmsg = "\n" + _("Perhaps the RCS package is not installed on your system?");
65 frontend::Alert::error(_("Revision control error."),
66 bformat(_("Some problem occurred while running the command:\n"
74 bool VCS::makeRCSRevision(string const &version, string &revis) const
79 int back = convert<int>(rev);
80 // if positive use as the last number in the whole revision string
83 rsplit(version, base , '.');
84 rev = base + '.' + rev;
88 // we care about the last number from revision string
89 // in case of backward indexing
92 cur = rsplit(version, base , '.');
95 int want = convert<int>(cur) + back;
99 rev = base + '.' + convert<string>(want);
108 FileName VCS::checkParentDirs(FileName const & start, std::string const & file)
110 static FileName empty;
111 FileName dirname = start.onlyPath();
113 FileName tocheck = FileName(addPathName(dirname.absFileName(), file));
114 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
115 if (tocheck.exists())
117 // this construct because of #8295
118 dirname = FileName(dirname.absFileName()).parentPath();
119 } while (!dirname.empty());
124 /////////////////////////////////////////////////////////////////////
128 /////////////////////////////////////////////////////////////////////
130 RCS::RCS(FileName const & m, Buffer * b) : VCS(b)
132 // Here we know that the buffer file is either already in RCS or
133 // about to be registered
139 FileName const RCS::findFile(FileName const & file)
141 // Check if *,v exists.
142 FileName tmp(file.absFileName() + ",v");
143 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
144 if (tmp.isReadableFile()) {
145 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
149 // Check if RCS/*,v exists.
150 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
151 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
152 if (tmp.isReadableFile()) {
153 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
161 bool RCS::retrieve(FileName const & file)
163 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
164 // The caller ensures that file does not exist, so no need to check that.
165 return doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
170 void RCS::scanMaster()
175 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
177 ifstream ifs(master_.toFilesystemEncoding().c_str());
178 // limit the size of strings we read to avoid memory problems
182 bool read_enough = false;
184 while (!read_enough && ifs >> token) {
185 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
190 else if (token == "head") {
194 tmv = rtrim(tmv, ";");
196 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
197 } else if (contains(token, "access")
198 || contains(token, "symbols")
199 || contains(token, "strict")) {
201 } else if (contains(token, "locks")) {
203 if (contains(token, ';')) {
204 locker_ = "Unlocked";
205 vcstatus_ = UNLOCKED;
213 s1 = rtrim(tmpt, ";");
214 // tmp is now in the format <user>:<version>
215 s1 = split(s1, s2, ':');
216 // s2 is user, and s1 is version
217 if (s1 == version_) {
222 } while (!contains(tmpt, ';'));
224 } else if (token == "comment") {
225 // we don't need to read any further than this.
229 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
235 void RCS::registrer(string const & msg)
237 string cmd = "ci -q -u -i -t-\"";
240 cmd += quoteName(onlyFileName(owner_->absFileName()));
241 doVCCommand(cmd, FileName(owner_->filePath()));
245 bool RCS::renameEnabled()
251 string RCS::rename(support::FileName const & /*newFile*/, string const & /*msg*/)
253 // not implemented, since a left-over file.lyx,v would be confusing.
258 bool RCS::copyEnabled()
264 string RCS::copy(support::FileName const & newFile, string const & msg)
266 // RCS has no real copy command, so we create a poor mans version
267 support::FileName const oldFile(owner_->absFileName());
268 if (!oldFile.copyTo(newFile))
270 FileName path(oldFile.onlyPath());
271 string relFile(to_utf8(newFile.relPath(path.absFileName())));
272 string cmd = "ci -q -u -i -t-\"";
275 cmd += quoteName(relFile);
276 return doVCCommand(cmd, path) ? string() : "RCS: Proceeded";
280 LyXVC::CommandResult RCS::checkIn(string const & msg, string & log)
282 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
283 + quoteName(onlyFileName(owner_->absFileName())),
284 FileName(owner_->filePath()));
286 return LyXVC::ErrorCommand;
287 log = "RCS: Proceeded";
288 return LyXVC::VCSuccess;
292 bool RCS::checkInEnabled()
294 return owner_ && !owner_->hasReadonlyFlag();
298 bool RCS::isCheckInWithConfirmation()
300 // FIXME one day common getDiff for all backends
302 // if (getDiff(file, diff) && diff.empty())
305 TempFile tempfile("lyxvcout");
306 FileName tmpf = tempfile.name();
308 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
312 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
313 + " > " + quoteName(tmpf.toFilesystemEncoding()),
314 FileName(owner_->filePath()));
316 docstring diff = tmpf.fileContents("UTF-8");
325 string RCS::checkOut()
328 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
329 FileName(owner_->filePath()));
330 return ret ? string() : "RCS: Proceeded";
334 bool RCS::checkOutEnabled()
336 return owner_ && owner_->hasReadonlyFlag();
340 string RCS::repoUpdate()
342 lyxerr << "Sorry, not implemented." << endl;
347 bool RCS::repoUpdateEnabled()
353 string RCS::lockingToggle()
355 //FIXME this might be actually possible, study rcs -U, rcs -L.
356 //State should be easy to get inside scanMaster.
357 //It would fix #4370 and make rcs/svn usage even more closer.
358 lyxerr << "Sorry, not implemented." << endl;
363 bool RCS::lockingToggleEnabled()
371 if (doVCCommand("co -f -u" + version_ + ' '
372 + quoteName(onlyFileName(owner_->absFileName())),
373 FileName(owner_->filePath())))
375 // We ignore changes and just reload!
381 bool RCS::isRevertWithConfirmation()
383 //FIXME owner && diff ?
390 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
391 doVCCommand("rcs -o" + version_ + ' '
392 + quoteName(onlyFileName(owner_->absFileName())),
393 FileName(owner_->filePath()));
397 bool RCS::undoLastEnabled()
399 return owner_->hasReadonlyFlag();
403 void RCS::getLog(FileName const & tmpf)
405 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
406 + " > " + quoteName(tmpf.toFilesystemEncoding()),
407 FileName(owner_->filePath()));
411 bool RCS::toggleReadOnlyEnabled()
413 // This got broken somewhere along lfuns dispatch reorganization.
414 // reloadBuffer would be needed after this, but thats problematic
415 // since we are inside Buffer::dispatch.
421 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
423 if (info == LyXVC::File)
425 // fill the rest of the attributes for a single file
426 if (rev_date_cache_.empty())
427 if (!getRevisionInfo())
432 return rev_author_cache_;
434 return rev_date_cache_;
436 return rev_time_cache_;
445 bool RCS::getRevisionInfo()
447 TempFile tempfile("lyxvcout");
448 FileName tmpf = tempfile.name();
450 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
453 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
454 + " > " + quoteName(tmpf.toFilesystemEncoding()),
455 FileName(owner_->filePath()));
460 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
463 // we reached to the entry, i.e. after initial log message
465 // line with critical info, e.g:
466 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
471 LYXERR(Debug::LYXVC, line);
472 if (entry && prefixIs(line, "date:")) {
476 if (prefixIs(line, "revision"))
482 rev_date_cache_ = token(result, ' ', 1);
483 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
484 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
486 return !rev_author_cache_.empty();
489 bool RCS::prepareFileRevision(string const &revis, string & f)
492 if (!VCS::makeRCSRevision(version_, rev))
495 TempFile tempfile("lyxvcrev_" + rev + '_');
496 tempfile.setAutoRemove(false);
497 FileName tmpf = tempfile.name();
499 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
503 doVCCommand("co -p" + rev + ' '
504 + quoteName(onlyFileName(owner_->absFileName()))
505 + " > " + quoteName(tmpf.toFilesystemEncoding()),
506 FileName(owner_->filePath()));
508 if (tmpf.isFileEmpty())
511 f = tmpf.absFileName();
516 bool RCS::prepareFileRevisionEnabled()
522 /////////////////////////////////////////////////////////////////////
526 /////////////////////////////////////////////////////////////////////
528 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
530 // Here we know that the buffer file is either already in CVS or
531 // about to be registered
533 have_rev_info_ = false;
538 FileName const CVS::findFile(FileName const & file)
540 LYXERR(Debug::LYXVC, "LyXVC: Checking if "
541 << onlyFileName(file.absFileName()) << "is under cvs");
542 // First we look for the CVS/Entries in the same dir where we have file.
543 // Note that it is not necessary to search parent directories, since
544 // there will be a CVS/Entries file in every subdirectory.
545 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
546 if (entries.isReadableFile()) {
547 // We are in a CVS-managed directory
548 // See if the file is known to CVS
549 string const cmd = "cvs log " + quoteName(file.toFilesystemEncoding());
550 int const ret = doVCCommandCall(cmd, file.onlyPath());
558 void CVS::scanMaster()
560 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
561 // Ok now we do the real scan...
562 ifstream ifs(master_.toFilesystemEncoding().c_str());
563 string const name = onlyFileName(owner_->absFileName());
564 string const tmpf = '/' + name + '/';
565 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
567 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
568 while (getline(ifs, line)) {
569 LYXERR(Debug::LYXVC, "\t line: " << line);
570 if (contains(line, tmpf)) {
571 // Ok extract the fields.
573 if (!regex_match(line, sm, reg)) {
574 LYXERR(Debug::LYXVC, "\t Cannot parse line. Skipping.");
578 //sm[0]; // whole matched string
580 version_ = sm.str(2);
581 string const file_date = sm.str(3);
584 //sm[5]; // tag or tagdate
585 FileName file(owner_->absFileName());
586 if (file.isReadableFile()) {
587 time_t const mod = file.lastModified();
588 string const mod_date = rtrim(asctime(gmtime(&mod)), "\n");
589 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
590 << "'\nModification date of file: `" << mod_date << '\'');
591 if (file.isReadOnly()) {
592 // readonly checkout is unlocked
593 vcstatus_ = UNLOCKED;
595 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
596 FileName base(addName(bdir.absFileName(),name));
597 // if base version is existent "cvs edit" was used to lock
598 vcstatus_ = base.isReadableFile() ? LOCKED : NOLOCKING;
601 vcstatus_ = NOLOCKING;
609 bool CVS::retrieve(FileName const & file)
611 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
612 // The caller ensures that file does not exist, so no need to check that.
613 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
614 file.onlyPath()) == 0;
618 string const CVS::getTarget(OperationMode opmode) const
622 // in client server mode CVS does not like full path operand for directory operation
623 // since LyX switches to the repo dir "." is good enough as target
626 return quoteName(onlyFileName(owner_->absFileName()));
632 docstring CVS::toString(CvsStatus status) const
636 return _("Up-to-date");
637 case LocallyModified:
638 return _("Locally Modified");
640 return _("Locally Added");
642 return _("Needs Merge");
644 return _("Needs Checkout");
646 return _("No CVS file");
648 return _("Cannot retrieve CVS status");
654 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
655 FileName const & output, bool reportError)
657 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
658 return doVCCommand(cmd + redirection, path, reportError);
662 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
663 support::FileName const & path,
664 support::FileName const & output)
666 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
667 return doVCCommandCall(cmd + redirection, path);
671 CVS::CvsStatus CVS::getStatus()
673 TempFile tempfile("lyxvout");
674 FileName tmpf = tempfile.name();
676 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
680 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
681 FileName(owner_->filePath()), tmpf)) {
685 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
686 CvsStatus status = NoCvsFile;
691 LYXERR(Debug::LYXVC, line << '\n');
692 if (prefixIs(line, "File:")) {
693 if (contains(line, "Up-to-date"))
695 else if (contains(line, "Locally Modified"))
696 status = LocallyModified;
697 else if (contains(line, "Locally Added"))
698 status = LocallyAdded;
699 else if (contains(line, "Needs Merge"))
701 else if (contains(line, "Needs Checkout"))
702 status = NeedsCheckout;
708 void CVS::getRevisionInfo()
712 have_rev_info_ = true;
713 TempFile tempfile("lyxvout");
714 FileName tmpf = tempfile.name();
716 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
720 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
721 + ' ' + getTarget(File),
722 FileName(owner_->filePath()), tmpf);
724 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
728 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
729 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
734 LYXERR(Debug::LYXVC, line << '\n');
735 if (prefixIs(line, "date:")) {
737 if (regex_match(line, sm, reg)) {
738 //sm[0]; // whole matched string
739 rev_date_cache_ = sm[1];
740 rev_time_cache_ = sm[2];
741 //sm[3]; // GMT offset
742 rev_author_cache_ = sm[4];
744 LYXERR(Debug::LYXVC, "\tCannot parse line. Skipping.");
748 if (rev_author_cache_.empty())
750 "Could not retrieve revision info for " << version_ <<
751 " of " << getTarget(File));
755 void CVS::registrer(string const & msg)
757 doVCCommand("cvs -q add -m \"" + msg + "\" "
759 FileName(owner_->filePath()));
763 bool CVS::renameEnabled()
769 string CVS::rename(support::FileName const & newFile, string const & msg)
771 // CVS has no real rename command, so we create a poor mans version
772 support::FileName const oldFile(owner_->absFileName());
773 string ret = copy(newFile, msg);
776 string cmd = "cvs -q remove -m \"" + msg + "\" " +
777 quoteName(oldFile.onlyFileName());
778 FileName path(oldFile.onlyPath());
779 return doVCCommand(cmd, path) ? string() : ret;
783 bool CVS::copyEnabled()
789 string CVS::copy(support::FileName const & newFile, string const & msg)
791 // CVS has no real copy command, so we create a poor mans version
792 support::FileName const oldFile(owner_->absFileName());
793 if (!oldFile.copyTo(newFile))
795 FileName path(oldFile.onlyPath());
796 string relFile(to_utf8(newFile.relPath(path.absFileName())));
797 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
798 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
802 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
804 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
805 FileName(owner_->filePath()), tmpf, false);
812 return doVCCommand("cvs -q edit " + getTarget(File),
813 FileName(owner_->filePath()));
819 vcstatus_ = UNLOCKED;
820 return doVCCommand("cvs -q unedit " + getTarget(File),
821 FileName(owner_->filePath()));
825 int CVS::update(OperationMode opmode, FileName const & tmpf)
827 return doVCCommandWithOutput("cvs -q update "
829 FileName(owner_->filePath()), tmpf, false);
833 string CVS::scanLogFile(FileName const & f, string & status)
835 ifstream ifs(f.toFilesystemEncoding().c_str());
840 LYXERR(Debug::LYXVC, line << '\n');
842 status += line + "; ";
843 if (prefixIs(line, "C ")) {
853 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
855 CvsStatus status = getStatus();
858 if (vcstatus_ != NOLOCKING)
860 return LyXVC::ErrorCommand;
861 log = "CVS: Proceeded";
862 return LyXVC::VCSuccess;
863 case LocallyModified:
865 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
867 FileName(owner_->filePath()));
869 return LyXVC::ErrorCommand;
870 log = "CVS: Proceeded";
871 return LyXVC::VCSuccess;
875 frontend::Alert::error(_("Revision control error."),
876 _("The repository version is newer then the current check out.\n"
877 "You have to update from repository first or revert your changes.")) ;
880 frontend::Alert::error(_("Revision control error."),
881 bformat(_("Bad status when checking in changes.\n"
886 return LyXVC::ErrorBefore;
890 bool CVS::isLocked() const
892 FileName fn(owner_->absFileName());
894 return !fn.isReadOnly();
898 bool CVS::checkInEnabled()
900 if (vcstatus_ != NOLOCKING)
907 bool CVS::isCheckInWithConfirmation()
909 CvsStatus status = getStatus();
910 return status == LocallyModified || status == LocallyAdded;
914 string CVS::checkOut()
916 if (vcstatus_ != NOLOCKING && edit())
918 TempFile tempfile("lyxvout");
919 FileName tmpf = tempfile.name();
921 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
925 int rc = update(File, tmpf);
927 string const res = scanLogFile(tmpf, log);
929 frontend::Alert::error(_("Revision control error."),
930 bformat(_("Error when updating from repository.\n"
931 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
932 "After pressing OK, LyX will try to reopen the resolved document."),
933 from_local8bit(res)));
937 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
941 bool CVS::checkOutEnabled()
943 if (vcstatus_ != NOLOCKING)
950 string CVS::repoUpdate()
952 TempFile tempfile("lyxvout");
953 FileName tmpf = tempfile.name();
955 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
959 getDiff(Directory, tmpf);
960 docstring res = tmpf.fileContents("UTF-8");
962 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
963 docstring const file = from_utf8(owner_->filePath());
964 docstring text = bformat(_("There were detected changes "
965 "in the working directory:\n%1$s\n\n"
966 "Possible file conflicts must be then resolved manually "
967 "or you will need to revert back to the repository version."), file);
968 int ret = frontend::Alert::prompt(_("Changes detected"),
969 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
971 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
972 ret = frontend::Alert::prompt(_("Changes detected"),
973 text, 0, 1, _("&Continue"), _("&Abort"));
974 hideDialogs("file", nullptr);
980 int rc = update(Directory, tmpf);
981 res += "Update log:\n" + tmpf.fileContents("UTF-8");
982 LYXERR(Debug::LYXVC, res);
985 string sres = scanLogFile(tmpf, log);
987 docstring const file = owner_->fileName().displayName(20);
988 frontend::Alert::error(_("Revision control error."),
989 bformat(_("Error when updating document %1$s from repository.\n"
990 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
991 "After pressing OK, LyX will try to reopen the resolved document."),
992 file, from_local8bit(sres)));
996 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
1000 bool CVS::repoUpdateEnabled()
1006 string CVS::lockingToggle()
1008 lyxerr << "Sorry, not implemented." << endl;
1013 bool CVS::lockingToggleEnabled()
1019 bool CVS::isRevertWithConfirmation()
1021 CvsStatus status = getStatus();
1022 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1028 // Reverts to the version in CVS repository and
1029 // gets the updated version from the repository.
1030 CvsStatus status = getStatus();
1033 if (vcstatus_ != NOLOCKING)
1034 return 0 == unedit();
1038 case LocallyModified: {
1039 FileName f(owner_->absFileName());
1041 update(File, FileName());
1042 owner_->markClean();
1045 case LocallyAdded: {
1046 docstring const file = owner_->fileName().displayName(20);
1047 frontend::Alert::error(_("Revision control error."),
1048 bformat(_("The document %1$s is not in repository.\n"
1049 "You have to check in the first revision before you can revert."),
1054 docstring const file = owner_->fileName().displayName(20);
1055 frontend::Alert::error(_("Revision control error."),
1056 bformat(_("Cannot revert document %1$s to repository version.\n"
1057 "The status '%2$s' is unexpected."),
1058 file, toString(status)));
1066 void CVS::undoLast()
1068 // merge the current with the previous version
1069 // in a reverse patch kind of way, so that the
1070 // result is to revert the last changes.
1071 lyxerr << "Sorry, not implemented." << endl;
1075 bool CVS::undoLastEnabled()
1081 void CVS::getLog(FileName const & tmpf)
1083 doVCCommandWithOutput("cvs log " + getTarget(File),
1084 FileName(owner_->filePath()),
1089 bool CVS::toggleReadOnlyEnabled()
1095 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1097 if (!version_.empty()) {
1103 return rev_author_cache_;
1105 return rev_date_cache_;
1107 return rev_time_cache_;
1116 bool CVS::prepareFileRevision(string const & revis, string & f)
1119 if (!VCS::makeRCSRevision(version_, rev))
1122 TempFile tempfile("lyxvcrev_" + rev + '_');
1123 tempfile.setAutoRemove(false);
1124 FileName tmpf = tempfile.name();
1126 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1130 doVCCommandWithOutput("cvs update -p -r" + rev + ' '
1132 FileName(owner_->filePath()), tmpf);
1134 if (tmpf.isFileEmpty())
1137 f = tmpf.absFileName();
1142 bool CVS::prepareFileRevisionEnabled()
1148 /////////////////////////////////////////////////////////////////////
1152 /////////////////////////////////////////////////////////////////////
1154 SVN::SVN(Buffer * b) : VCS(b)
1156 // Here we know that the buffer file is either already in SVN or
1157 // about to be registered
1158 locked_mode_ = false;
1163 bool SVN::findFile(FileName const & file)
1165 // First we check the existence of repository meta data.
1166 if (VCS::checkParentDirs(file, ".svn").empty()) {
1167 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1171 // Now we check the status of the file.
1172 string const fname = onlyFileName(file.absFileName());
1173 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1174 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname),
1176 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1181 void SVN::scanMaster()
1183 // vcstatus code is somewhat superflous,
1184 // until we want to implement read-only toggle for svn.
1185 vcstatus_ = NOLOCKING;
1186 if (checkLockMode())
1187 vcstatus_ = isLocked() ? LOCKED : 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 --parents " + 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 unreachable 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", nullptr);
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()));
1787 if (tmpf.isFileEmpty())
1790 f = tmpf.absFileName();
1795 bool SVN::prepareFileRevisionEnabled()
1802 bool SVN::toggleReadOnlyEnabled()
1808 /////////////////////////////////////////////////////////////////////
1812 /////////////////////////////////////////////////////////////////////
1814 GIT::GIT(Buffer * b) : VCS(b)
1816 // Here we know that the buffer file is either already in GIT or
1817 // about to be registered
1822 bool GIT::findFile(FileName const & file)
1824 // First we check the existence of repository meta data.
1825 if (VCS::checkParentDirs(file, ".git").empty()) {
1826 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1830 // Now we check if the file is known to git.
1831 string const fname = onlyFileName(file.absFileName());
1832 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1834 int const ret = doVCCommandCall("git log " + quoteName(fname),
1836 bool const found = (ret == 0);
1837 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1842 void GIT::scanMaster()
1844 // vcstatus code is somewhat superflous,
1845 // until we want to implement read-only toggle for git.
1846 vcstatus_ = NOLOCKING;
1850 bool GIT::retrieve(FileName const & file)
1852 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1853 // The caller ensures that file does not exist, so no need to check that.
1854 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1855 file.onlyPath()) == 0;
1859 void GIT::registrer(string const & /*msg*/)
1861 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1862 FileName(owner_->filePath()));
1866 bool GIT::renameEnabled()
1872 string GIT::rename(support::FileName const & newFile, string const & msg)
1874 // git mv does not require a log message, since it does not commit.
1875 // In LyX we commit immediately afterwards, otherwise it could be
1876 // confusing to the user to have two uncommitted files.
1877 FileName path(owner_->filePath());
1878 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1879 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1880 ' ' + quoteName(relFile));
1881 if (doVCCommand(cmd, path)) {
1882 cmd = "git checkout -q " +
1883 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1885 doVCCommand(cmd, path);
1886 if (newFile.exists())
1887 newFile.removeFile();
1890 vector<support::FileName> f;
1891 f.push_back(owner_->fileName());
1892 f.push_back(newFile);
1894 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1895 cmd = "git checkout -q " +
1896 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1898 doVCCommand(cmd, path);
1899 if (newFile.exists())
1900 newFile.removeFile();
1907 bool GIT::copyEnabled()
1913 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1915 // git does not support copy with history preservation
1920 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1922 vector<support::FileName> f(1, owner_->fileName());
1923 return checkIn(f, msg, log);
1927 LyXVC::CommandResult
1928 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1930 TempFile tempfile("lyxvcout");
1931 FileName tmpf = tempfile.name();
1933 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1934 log = N_("Error: Could not generate logfile.");
1935 return LyXVC::ErrorBefore;
1939 os << "git commit -m \"" << msg << '"';
1940 for (size_t i = 0; i < f.size(); ++i)
1941 os << ' ' << quoteName(f[i].onlyFileName());
1942 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1943 LyXVC::CommandResult ret =
1944 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1945 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1947 string res = scanLogFile(tmpf, log);
1949 frontend::Alert::error(_("Revision control error."),
1950 _("Error when committing to repository.\n"
1951 "You have to manually resolve the problem.\n"
1952 "LyX will reopen the document after you press OK."));
1953 ret = LyXVC::ErrorCommand;
1957 log.insert(0, "GIT: ");
1958 if (ret == LyXVC::VCSuccess && log.empty())
1959 log = "GIT: Proceeded";
1964 bool GIT::checkInEnabled()
1970 bool GIT::isCheckInWithConfirmation()
1972 // FIXME one day common getDiff and perhaps OpMode for all backends
1974 TempFile tempfile("lyxvcout");
1975 FileName tmpf = tempfile.name();
1977 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1981 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
1982 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1983 FileName(owner_->filePath()));
1985 docstring diff = tmpf.fileContents("UTF-8");
1994 // FIXME Correctly return code should be checked instead of this.
1995 // This would need another solution than just plain startscript.
1996 // Hint from Andre': QProcess::readAllStandardError()...
1997 string GIT::scanLogFile(FileName const & f, string & status)
1999 ifstream ifs(f.toFilesystemEncoding().c_str());
2004 LYXERR(Debug::LYXVC, line << "\n");
2006 status += line + "; ";
2007 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2008 || contains(line, "Commit failed")) {
2018 string GIT::checkOut()
2024 bool GIT::checkOutEnabled()
2030 string GIT::repoUpdate()
2036 bool GIT::repoUpdateEnabled()
2042 string GIT::lockingToggle()
2048 bool GIT::lockingToggleEnabled()
2056 // Reverts to the version in GIT repository and
2057 // gets the updated version from the repository.
2058 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2060 if (doVCCommand("git checkout -q " + fil,
2061 FileName(owner_->filePath())))
2063 owner_->markClean();
2068 bool GIT::isRevertWithConfirmation()
2070 //FIXME owner && diff
2075 void GIT::undoLast()
2077 // merge the current with the previous version
2078 // in a reverse patch kind of way, so that the
2079 // result is to revert the last changes.
2080 lyxerr << "Sorry, not implemented." << endl;
2084 bool GIT::undoLastEnabled()
2090 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2092 if (info == LyXVC::Tree) {
2093 if (rev_tree_cache_.empty())
2094 if (!getTreeRevisionInfo())
2095 rev_tree_cache_ = "?";
2096 if (rev_tree_cache_ == "?")
2099 return rev_tree_cache_;
2102 // fill the rest of the attributes for a single file
2103 if (rev_file_cache_.empty())
2104 if (!getFileRevisionInfo()) {
2105 rev_file_cache_ = "?";
2106 rev_file_abbrev_cache_ = "?";
2111 if (rev_file_cache_ == "?")
2113 return rev_file_cache_;
2114 case LyXVC::FileAbbrev:
2115 if (rev_file_abbrev_cache_ == "?")
2117 return rev_file_abbrev_cache_;
2119 return rev_author_cache_;
2121 return rev_date_cache_;
2123 return rev_time_cache_;
2132 bool GIT::getFileRevisionInfo()
2134 TempFile tempfile("lyxvcout");
2135 FileName tmpf = tempfile.name();
2137 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2141 doVCCommand("git log -n 1 --pretty=format:%H%n%h%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2142 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2143 FileName(owner_->filePath()));
2148 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2151 getline(ifs, rev_file_cache_);
2153 getline(ifs, rev_file_abbrev_cache_);
2155 getline(ifs, rev_author_cache_);
2159 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2163 return !rev_file_cache_.empty();
2167 bool GIT::getTreeRevisionInfo()
2169 TempFile tempfile("lyxvcout");
2170 FileName tmpf = tempfile.name();
2172 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2176 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2177 FileName(owner_->filePath()),
2178 false); //git describe returns $?=128 when no tag found (but git repo still exists)
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()));
2236 if (tmpf.isFileEmpty())
2239 f = tmpf.absFileName();
2244 bool GIT::prepareFileRevisionEnabled()
2250 bool GIT::toggleReadOnlyEnabled()