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()) {
1190 vcstatus_ = UNLOCKED;
1195 bool SVN::checkLockMode()
1197 TempFile tempfile("lyxvcout");
1198 FileName tmpf = tempfile.name();
1200 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1204 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1205 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1206 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1207 FileName(owner_->filePath())))
1210 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1214 while (ifs && !ret) {
1216 LYXERR(Debug::LYXVC, line);
1217 if (contains(line, "svn:needs-lock"))
1220 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1228 bool SVN::isLocked() const
1230 FileName file(owner_->absFileName());
1232 return !file.isReadOnly();
1236 bool SVN::retrieve(FileName const & file)
1238 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1239 // The caller ensures that file does not exist, so no need to check that.
1240 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1241 file.onlyPath()) == 0;
1245 void SVN::registrer(string const & /*msg*/)
1247 doVCCommand("svn add -q --parents " + quoteName(onlyFileName(owner_->absFileName())),
1248 FileName(owner_->filePath()));
1252 bool SVN::renameEnabled()
1258 string SVN::rename(support::FileName const & newFile, string const & msg)
1260 // svn move does not require a log message, since it does not commit.
1261 // In LyX we commit immediately afterwards, otherwise it could be
1262 // confusing to the user to have two uncommitted files.
1263 FileName path(owner_->filePath());
1264 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1265 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1266 ' ' + quoteName(relFile));
1267 if (doVCCommand(cmd, path)) {
1268 cmd = "svn revert -q " +
1269 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1271 doVCCommand(cmd, path);
1272 if (newFile.exists())
1273 newFile.removeFile();
1276 vector<support::FileName> f;
1277 f.push_back(owner_->fileName());
1278 f.push_back(newFile);
1280 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1281 cmd = "svn revert -q " +
1282 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1284 doVCCommand(cmd, path);
1285 if (newFile.exists())
1286 newFile.removeFile();
1293 bool SVN::copyEnabled()
1299 string SVN::copy(support::FileName const & newFile, string const & msg)
1301 // svn copy does not require a log message, since it does not commit.
1302 // In LyX we commit immediately afterwards, otherwise it could be
1303 // confusing to the user to have an uncommitted file.
1304 FileName path(owner_->filePath());
1305 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1306 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1307 ' ' + quoteName(relFile));
1308 if (doVCCommand(cmd, path))
1310 vector<support::FileName> f(1, newFile);
1312 if (checkIn(f, msg, log) == LyXVC::VCSuccess)
1318 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1320 vector<support::FileName> f(1, owner_->fileName());
1321 return checkIn(f, msg, log);
1325 LyXVC::CommandResult
1326 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1328 TempFile tempfile("lyxvcout");
1329 FileName tmpf = tempfile.name();
1331 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1332 log = N_("Error: Could not generate logfile.");
1333 return LyXVC::ErrorBefore;
1337 os << "svn commit -m \"" << msg << '"';
1338 for (size_t i = 0; i < f.size(); ++i)
1339 os << ' ' << quoteName(f[i].onlyFileName());
1340 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1341 LyXVC::CommandResult ret =
1342 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1343 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1345 string res = scanLogFile(tmpf, log);
1347 frontend::Alert::error(_("Revision control error."),
1348 _("Error when committing to repository.\n"
1349 "You have to manually resolve the problem.\n"
1350 "LyX will reopen the document after you press OK."));
1351 ret = LyXVC::ErrorCommand;
1354 if (!fileLock(false, tmpf, log))
1355 ret = LyXVC::ErrorCommand;
1358 log.insert(0, "SVN: ");
1359 if (ret == LyXVC::VCSuccess && log.empty())
1360 log = "SVN: Proceeded";
1365 bool SVN::checkInEnabled()
1374 bool SVN::isCheckInWithConfirmation()
1376 // FIXME one day common getDiff and perhaps OpMode for all backends
1378 TempFile tempfile("lyxvcout");
1379 FileName tmpf = tempfile.name();
1381 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1385 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1386 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1387 FileName(owner_->filePath()));
1389 docstring diff = tmpf.fileContents("UTF-8");
1398 // FIXME Correctly return code should be checked instead of this.
1399 // This would need another solution than just plain startscript.
1400 // Hint from Andre': QProcess::readAllStandardError()...
1401 string SVN::scanLogFile(FileName const & f, string & status)
1403 ifstream ifs(f.toFilesystemEncoding().c_str());
1408 LYXERR(Debug::LYXVC, line << '\n');
1410 status += line + "; ";
1411 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1412 || contains(line, "Commit failed")) {
1416 if (contains(line, "svn:needs-lock")) {
1426 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1428 if (!locked_mode_ || (isLocked() == lock))
1431 string const arg = lock ? "lock " : "unlock ";
1432 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1433 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1434 FileName(owner_->filePath()));
1436 // Lock error messages go unfortunately on stderr and are unreachable this way.
1437 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1441 if (!line.empty()) status += line + "; ";
1445 if (isLocked() == lock)
1449 frontend::Alert::error(_("Revision control error."),
1450 _("Error while acquiring write lock.\n"
1451 "Another user is most probably editing\n"
1452 "the current document now!\n"
1453 "Also check the access to the repository."));
1455 frontend::Alert::error(_("Revision control error."),
1456 _("Error while releasing write lock.\n"
1457 "Check the access to the repository."));
1462 string SVN::checkOut()
1464 TempFile tempfile("lyxvcout");
1465 FileName tmpf = tempfile.name();
1467 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1468 return N_("Error: Could not generate logfile.");
1471 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1472 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1473 FileName(owner_->filePath()));
1476 string const res = scanLogFile(tmpf, log);
1478 frontend::Alert::error(_("Revision control error."),
1479 bformat(_("Error when updating from repository.\n"
1480 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1481 "After pressing OK, LyX will try to reopen the resolved document."),
1482 from_local8bit(res)));
1484 fileLock(true, tmpf, log);
1486 return log.empty() ? string() : "SVN: " + log;
1490 bool SVN::checkOutEnabled()
1499 string SVN::repoUpdate()
1501 TempFile tempfile("lyxvcout");
1502 FileName tmpf = tempfile.name();
1504 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1505 return N_("Error: Could not generate logfile.");
1508 doVCCommand("svn diff " + quoteName(owner_->filePath())
1509 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1510 FileName(owner_->filePath()));
1511 docstring res = tmpf.fileContents("UTF-8");
1513 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1514 docstring const file = from_utf8(owner_->filePath());
1515 docstring text = bformat(_("There were detected changes "
1516 "in the working directory:\n%1$s\n\n"
1517 "In case of file conflict version of the local directory files "
1518 "will be preferred."
1519 "\n\nContinue?"), file);
1520 int ret = frontend::Alert::prompt(_("Changes detected"),
1521 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1523 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1524 ret = frontend::Alert::prompt(_("Changes detected"),
1525 text, 0, 1, _("&Yes"), _("&No"));
1526 hideDialogs("file", nullptr);
1532 // Reverting looks too harsh, see bug #6255.
1533 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1534 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1535 // FileName(owner_->filePath()));
1536 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1537 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1538 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1539 FileName(owner_->filePath()));
1540 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1542 LYXERR(Debug::LYXVC, res);
1543 return to_utf8(res);
1547 bool SVN::repoUpdateEnabled()
1553 string SVN::lockingToggle()
1555 TempFile tempfile("lyxvcout");
1556 FileName tmpf = tempfile.name();
1558 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1559 return N_("Error: Could not generate logfile.");
1562 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1563 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1564 FileName(owner_->filePath()));
1569 string res = scanLogFile(tmpf, log);
1570 bool locking = contains(res, "svn:needs-lock");
1572 ret = doVCCommand("svn propset svn:needs-lock ON "
1573 + quoteName(onlyFileName(owner_->absFileName()))
1574 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1575 FileName(owner_->filePath()));
1577 ret = doVCCommand("svn propdel svn:needs-lock "
1578 + quoteName(onlyFileName(owner_->absFileName()))
1579 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1580 FileName(owner_->filePath()));
1584 frontend::Alert::warning(_("SVN File Locking"),
1585 (locking ? _("Locking property unset.") : _("Locking property set.")) + '\n'
1586 + _("Do not forget to commit the locking property into the repository."),
1589 return string("SVN: ") + (locking ?
1590 N_("Locking property unset.") : N_("Locking property set."));
1594 bool SVN::lockingToggleEnabled()
1602 // Reverts to the version in SVN repository and
1603 // gets the updated version from the repository.
1604 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1606 if (doVCCommand("svn revert -q " + fil,
1607 FileName(owner_->filePath())))
1609 owner_->markClean();
1614 bool SVN::isRevertWithConfirmation()
1616 //FIXME owner && diff
1621 void SVN::undoLast()
1623 // merge the current with the previous version
1624 // in a reverse patch kind of way, so that the
1625 // result is to revert the last changes.
1626 lyxerr << "Sorry, not implemented." << endl;
1630 bool SVN::undoLastEnabled()
1636 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1638 if (info == LyXVC::Tree) {
1639 if (rev_tree_cache_.empty())
1640 if (!getTreeRevisionInfo())
1641 rev_tree_cache_ = "?";
1642 if (rev_tree_cache_ == "?")
1645 return rev_tree_cache_;
1648 // fill the rest of the attributes for a single file
1649 if (rev_file_cache_.empty())
1650 if (!getFileRevisionInfo())
1651 rev_file_cache_ = "?";
1655 if (rev_file_cache_ == "?")
1657 return rev_file_cache_;
1659 return rev_author_cache_;
1661 return rev_date_cache_;
1663 return rev_time_cache_;
1672 bool SVN::getFileRevisionInfo()
1674 TempFile tempfile("lyxvcout");
1675 FileName tmpf = tempfile.name();
1677 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1681 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1682 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1683 FileName(owner_->filePath()));
1688 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1696 LYXERR(Debug::LYXVC, line);
1697 if (prefixIs(line, "<commit"))
1699 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1700 string l1 = subst(line, "revision=\"", "");
1701 string l2 = trim(subst(l1, "\">", ""));
1703 rev_file_cache_ = rev = l2;
1705 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1706 string l1 = subst(line, "<author>", "");
1707 string l2 = subst(l1, "</author>", "");
1708 rev_author_cache_ = l2;
1710 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1711 string l1 = subst(line, "<date>", "");
1712 string l2 = subst(l1, "</date>", "");
1713 l2 = split(l2, l1, 'T');
1714 rev_date_cache_ = l1;
1715 l2 = split(l2, l1, '.');
1716 rev_time_cache_ = l1;
1721 return !rev.empty();
1725 bool SVN::getTreeRevisionInfo()
1727 TempFile tempfile("lyxvcout");
1728 FileName tmpf = tempfile.name();
1730 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1734 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1735 FileName(owner_->filePath()));
1740 // only first line in case something bad happens.
1741 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1746 rev_tree_cache_ = line;
1747 return !line.empty();
1751 void SVN::getLog(FileName const & tmpf)
1753 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1754 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1755 FileName(owner_->filePath()));
1759 bool SVN::prepareFileRevision(string const & revis, string & f)
1761 if (!isStrInt(revis))
1764 int rev = convert<int>(revis);
1766 if (!getFileRevisionInfo())
1769 rev = convert<int>(rev_file_cache_);
1770 // go back for minus rev
1772 rev = rev + convert<int>(rev_file_cache_);
1777 string revname = convert<string>(rev);
1778 TempFile tempfile("lyxvcrev_" + revname + '_');
1779 tempfile.setAutoRemove(false);
1780 FileName tmpf = tempfile.name();
1782 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1786 doVCCommand("svn cat -r " + revname + ' '
1787 + quoteName(onlyFileName(owner_->absFileName()))
1788 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1789 FileName(owner_->filePath()));
1791 if (tmpf.isFileEmpty())
1794 f = tmpf.absFileName();
1799 bool SVN::prepareFileRevisionEnabled()
1806 bool SVN::toggleReadOnlyEnabled()
1812 /////////////////////////////////////////////////////////////////////
1816 /////////////////////////////////////////////////////////////////////
1818 GIT::GIT(Buffer * b) : VCS(b)
1820 // Here we know that the buffer file is either already in GIT or
1821 // about to be registered
1826 bool GIT::findFile(FileName const & file)
1828 // First we check the existence of repository meta data.
1829 if (VCS::checkParentDirs(file, ".git").empty()) {
1830 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1834 // Now we check if the file is known to git.
1835 string const fname = onlyFileName(file.absFileName());
1836 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1838 int const ret = doVCCommandCall("git log " + quoteName(fname),
1840 bool const found = (ret == 0);
1841 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1846 void GIT::scanMaster()
1848 // vcstatus code is somewhat superflous,
1849 // until we want to implement read-only toggle for git.
1850 vcstatus_ = NOLOCKING;
1854 bool GIT::retrieve(FileName const & file)
1856 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1857 // The caller ensures that file does not exist, so no need to check that.
1858 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1859 file.onlyPath()) == 0;
1863 void GIT::registrer(string const & /*msg*/)
1865 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1866 FileName(owner_->filePath()));
1870 bool GIT::renameEnabled()
1876 string GIT::rename(support::FileName const & newFile, string const & msg)
1878 // git mv does not require a log message, since it does not commit.
1879 // In LyX we commit immediately afterwards, otherwise it could be
1880 // confusing to the user to have two uncommitted files.
1881 FileName path(owner_->filePath());
1882 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1883 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1884 ' ' + quoteName(relFile));
1885 if (doVCCommand(cmd, path)) {
1886 cmd = "git checkout -q " +
1887 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1889 doVCCommand(cmd, path);
1890 if (newFile.exists())
1891 newFile.removeFile();
1894 vector<support::FileName> f;
1895 f.push_back(owner_->fileName());
1896 f.push_back(newFile);
1898 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1899 cmd = "git checkout -q " +
1900 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1902 doVCCommand(cmd, path);
1903 if (newFile.exists())
1904 newFile.removeFile();
1911 bool GIT::copyEnabled()
1917 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1919 // git does not support copy with history preservation
1924 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1926 vector<support::FileName> f(1, owner_->fileName());
1927 return checkIn(f, msg, log);
1931 LyXVC::CommandResult
1932 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1934 TempFile tempfile("lyxvcout");
1935 FileName tmpf = tempfile.name();
1937 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1938 log = N_("Error: Could not generate logfile.");
1939 return LyXVC::ErrorBefore;
1943 os << "git commit -m \"" << msg << '"';
1944 for (size_t i = 0; i < f.size(); ++i)
1945 os << ' ' << quoteName(f[i].onlyFileName());
1946 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1947 LyXVC::CommandResult ret =
1948 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1949 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1951 string res = scanLogFile(tmpf, log);
1953 frontend::Alert::error(_("Revision control error."),
1954 _("Error when committing to repository.\n"
1955 "You have to manually resolve the problem.\n"
1956 "LyX will reopen the document after you press OK."));
1957 ret = LyXVC::ErrorCommand;
1961 log.insert(0, "GIT: ");
1962 if (ret == LyXVC::VCSuccess && log.empty())
1963 log = "GIT: Proceeded";
1968 bool GIT::checkInEnabled()
1974 bool GIT::isCheckInWithConfirmation()
1976 // FIXME one day common getDiff and perhaps OpMode for all backends
1978 TempFile tempfile("lyxvcout");
1979 FileName tmpf = tempfile.name();
1981 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1985 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
1986 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1987 FileName(owner_->filePath()));
1989 docstring diff = tmpf.fileContents("UTF-8");
1998 // FIXME Correctly return code should be checked instead of this.
1999 // This would need another solution than just plain startscript.
2000 // Hint from Andre': QProcess::readAllStandardError()...
2001 string GIT::scanLogFile(FileName const & f, string & status)
2003 ifstream ifs(f.toFilesystemEncoding().c_str());
2008 LYXERR(Debug::LYXVC, line << "\n");
2010 status += line + "; ";
2011 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2012 || contains(line, "Commit failed")) {
2022 string GIT::checkOut()
2028 bool GIT::checkOutEnabled()
2034 string GIT::repoUpdate()
2040 bool GIT::repoUpdateEnabled()
2046 string GIT::lockingToggle()
2052 bool GIT::lockingToggleEnabled()
2060 // Reverts to the version in GIT repository and
2061 // gets the updated version from the repository.
2062 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2064 if (doVCCommand("git checkout -q " + fil,
2065 FileName(owner_->filePath())))
2067 owner_->markClean();
2072 bool GIT::isRevertWithConfirmation()
2074 //FIXME owner && diff
2079 void GIT::undoLast()
2081 // merge the current with the previous version
2082 // in a reverse patch kind of way, so that the
2083 // result is to revert the last changes.
2084 lyxerr << "Sorry, not implemented." << endl;
2088 bool GIT::undoLastEnabled()
2094 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2096 if (info == LyXVC::Tree) {
2097 if (rev_tree_cache_.empty())
2098 if (!getTreeRevisionInfo())
2099 rev_tree_cache_ = "?";
2100 if (rev_tree_cache_ == "?")
2103 return rev_tree_cache_;
2106 // fill the rest of the attributes for a single file
2107 if (rev_file_cache_.empty())
2108 if (!getFileRevisionInfo()) {
2109 rev_file_cache_ = "?";
2110 rev_file_abbrev_cache_ = "?";
2115 if (rev_file_cache_ == "?")
2117 return rev_file_cache_;
2118 case LyXVC::FileAbbrev:
2119 if (rev_file_abbrev_cache_ == "?")
2121 return rev_file_abbrev_cache_;
2123 return rev_author_cache_;
2125 return rev_date_cache_;
2127 return rev_time_cache_;
2136 bool GIT::getFileRevisionInfo()
2138 TempFile tempfile("lyxvcout");
2139 FileName tmpf = tempfile.name();
2141 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2145 doVCCommand("git log -n 1 --pretty=format:%H%n%h%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2146 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2147 FileName(owner_->filePath()));
2152 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2155 getline(ifs, rev_file_cache_);
2157 getline(ifs, rev_file_abbrev_cache_);
2159 getline(ifs, rev_author_cache_);
2163 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2167 return !rev_file_cache_.empty();
2171 bool GIT::getTreeRevisionInfo()
2173 TempFile tempfile("lyxvcout");
2174 FileName tmpf = tempfile.name();
2176 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2180 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2181 FileName(owner_->filePath()),
2182 false); //git describe returns $?=128 when no tag found (but git repo still exists)
2187 // only first line in case something bad happens.
2188 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2189 getline(ifs, rev_tree_cache_);
2192 return !rev_tree_cache_.empty();
2196 void GIT::getLog(FileName const & tmpf)
2198 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2199 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2200 FileName(owner_->filePath()));
2204 //at this moment we don't accept revision SHA, but just number of revision steps back
2205 //GUI and infrastucture needs to be changed first
2206 bool GIT::prepareFileRevision(string const & revis, string & f)
2208 // anything positive means we got hash, not "0" or minus revision
2211 // hash is rarely number and should be long
2212 if (isStrInt(revis) && revis.length()<20)
2213 rev = convert<int>(revis);
2215 // revision and filename
2218 // go back for "minus" revisions
2220 pointer = "HEAD~" + convert<string>(-rev);
2227 TempFile tempfile("lyxvcrev_" + revis + '_');
2228 tempfile.setAutoRemove(false);
2229 FileName tmpf = tempfile.name();
2231 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2235 doVCCommand("git show " + pointer + "./"
2236 + quoteName(onlyFileName(owner_->absFileName()))
2237 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2238 FileName(owner_->filePath()));
2240 if (tmpf.isFileEmpty())
2243 f = tmpf.absFileName();
2248 bool GIT::prepareFileRevisionEnabled()
2254 bool GIT::toggleReadOnlyEnabled()