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 bool VCS::checkparentdirs(FileName const & file, std::string const & vcsdir)
110 FileName dirname = file.onlyPath();
112 FileName tocheck = FileName(addName(dirname.absFileName(), vcsdir));
113 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
114 if (tocheck.exists())
116 //this construct because of #8295
117 dirname = FileName(dirname.absFileName()).parentPath();
118 } while (!dirname.empty());
123 /////////////////////////////////////////////////////////////////////
127 /////////////////////////////////////////////////////////////////////
129 RCS::RCS(FileName const & m, Buffer * b) : VCS(b)
131 // Here we know that the buffer file is either already in RCS or
132 // about to be registered
138 FileName const RCS::findFile(FileName const & file)
140 // Check if *,v exists.
141 FileName tmp(file.absFileName() + ",v");
142 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
143 if (tmp.isReadableFile()) {
144 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
148 // Check if RCS/*,v exists.
149 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
150 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
151 if (tmp.isReadableFile()) {
152 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
160 bool RCS::retrieve(FileName const & file)
162 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
163 // The caller ensures that file does not exist, so no need to check that.
164 return doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
169 void RCS::scanMaster()
174 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
176 ifstream ifs(master_.toFilesystemEncoding().c_str());
177 // limit the size of strings we read to avoid memory problems
181 bool read_enough = false;
183 while (!read_enough && ifs >> token) {
184 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
189 else if (token == "head") {
193 tmv = rtrim(tmv, ";");
195 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
196 } else if (contains(token, "access")
197 || contains(token, "symbols")
198 || contains(token, "strict")) {
200 } else if (contains(token, "locks")) {
202 if (contains(token, ';')) {
203 locker_ = "Unlocked";
212 s1 = rtrim(tmpt, ";");
213 // tmp is now in the format <user>:<version>
214 s1 = split(s1, s2, ':');
215 // s2 is user, and s1 is version
216 if (s1 == version_) {
221 } while (!contains(tmpt, ';'));
223 } else if (token == "comment") {
224 // we don't need to read any further than this.
228 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
234 void RCS::registrer(string const & msg)
236 string cmd = "ci -q -u -i -t-\"";
239 cmd += quoteName(onlyFileName(owner_->absFileName()));
240 doVCCommand(cmd, FileName(owner_->filePath()));
244 bool RCS::renameEnabled()
250 string RCS::rename(support::FileName const & /*newFile*/, string const & /*msg*/)
252 // not implemented, since a left-over file.lyx,v would be confusing.
257 bool RCS::copyEnabled()
263 string RCS::copy(support::FileName const & newFile, string const & msg)
265 // RCS has no real copy command, so we create a poor mans version
266 support::FileName const oldFile(owner_->absFileName());
267 if (!oldFile.copyTo(newFile))
269 FileName path(oldFile.onlyPath());
270 string relFile(to_utf8(newFile.relPath(path.absFileName())));
271 string cmd = "ci -q -u -i -t-\"";
274 cmd += quoteName(relFile);
275 return doVCCommand(cmd, path) ? string() : "RCS: Proceeded";
279 LyXVC::CommandResult RCS::checkIn(string const & msg, string & log)
281 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
282 + quoteName(onlyFileName(owner_->absFileName())),
283 FileName(owner_->filePath()));
285 return LyXVC::ErrorCommand;
286 log = "RCS: Proceeded";
287 return LyXVC::VCSuccess;
291 bool RCS::checkInEnabled()
293 return owner_ && !owner_->hasReadonlyFlag();
297 bool RCS::isCheckInWithConfirmation()
299 // FIXME one day common getDiff for all backends
301 // if (getDiff(file, diff) && diff.empty())
304 TempFile tempfile("lyxvcout");
305 FileName tmpf = tempfile.name();
307 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
311 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
312 + " > " + quoteName(tmpf.toFilesystemEncoding()),
313 FileName(owner_->filePath()));
315 docstring diff = tmpf.fileContents("UTF-8");
324 string RCS::checkOut()
327 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
328 FileName(owner_->filePath()));
329 return ret ? string() : "RCS: Proceeded";
333 bool RCS::checkOutEnabled()
335 return owner_ && owner_->hasReadonlyFlag();
339 string RCS::repoUpdate()
341 lyxerr << "Sorry, not implemented." << endl;
346 bool RCS::repoUpdateEnabled()
352 string RCS::lockingToggle()
354 //FIXME this might be actually possible, study rcs -U, rcs -L.
355 //State should be easy to get inside scanMaster.
356 //It would fix #4370 and make rcs/svn usage even more closer.
357 lyxerr << "Sorry, not implemented." << endl;
362 bool RCS::lockingToggleEnabled()
370 if (doVCCommand("co -f -u" + version_ + ' '
371 + quoteName(onlyFileName(owner_->absFileName())),
372 FileName(owner_->filePath())))
374 // We ignore changes and just reload!
380 bool RCS::isRevertWithConfirmation()
382 //FIXME owner && diff ?
389 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
390 doVCCommand("rcs -o" + version_ + ' '
391 + quoteName(onlyFileName(owner_->absFileName())),
392 FileName(owner_->filePath()));
396 bool RCS::undoLastEnabled()
398 return owner_->hasReadonlyFlag();
402 void RCS::getLog(FileName const & tmpf)
404 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
405 + " > " + quoteName(tmpf.toFilesystemEncoding()),
406 FileName(owner_->filePath()));
410 bool RCS::toggleReadOnlyEnabled()
412 // This got broken somewhere along lfuns dispatch reorganization.
413 // reloadBuffer would be needed after this, but thats problematic
414 // since we are inside Buffer::dispatch.
420 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
422 if (info == LyXVC::File)
424 // fill the rest of the attributes for a single file
425 if (rev_date_cache_.empty())
426 if (!getRevisionInfo())
431 return rev_author_cache_;
433 return rev_date_cache_;
435 return rev_time_cache_;
444 bool RCS::getRevisionInfo()
446 TempFile tempfile("lyxvcout");
447 FileName tmpf = tempfile.name();
449 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
452 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
453 + " > " + quoteName(tmpf.toFilesystemEncoding()),
454 FileName(owner_->filePath()));
459 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
462 // we reached to the entry, i.e. after initial log message
464 // line with critical info, e.g:
465 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
470 LYXERR(Debug::LYXVC, line);
471 if (entry && prefixIs(line, "date:")) {
475 if (prefixIs(line, "revision"))
481 rev_date_cache_ = token(result, ' ', 1);
482 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
483 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
485 return !rev_author_cache_.empty();
488 bool RCS::prepareFileRevision(string const &revis, string & f)
491 if (!VCS::makeRCSRevision(version_, rev))
494 TempFile tempfile("lyxvcrev_" + rev + '_');
495 tempfile.setAutoRemove(false);
496 FileName tmpf = tempfile.name();
498 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
502 doVCCommand("co -p" + rev + ' '
503 + quoteName(onlyFileName(owner_->absFileName()))
504 + " > " + quoteName(tmpf.toFilesystemEncoding()),
505 FileName(owner_->filePath()));
507 if (tmpf.isFileEmpty())
510 f = tmpf.absFileName();
515 bool RCS::prepareFileRevisionEnabled()
521 /////////////////////////////////////////////////////////////////////
525 /////////////////////////////////////////////////////////////////////
527 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
529 // Here we know that the buffer file is either already in CVS or
530 // about to be registered
532 have_rev_info_ = false;
537 FileName const CVS::findFile(FileName const & file)
539 // First we look for the CVS/Entries in the same dir
540 // where we have file.
541 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
542 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
543 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
544 << "' for `" << tmpf << '\'');
545 if (entries.isReadableFile()) {
546 // Ok we are at least in a CVS dir. Parse the CVS/Entries
547 // and see if we can find this file. We do a fast and
549 ifstream ifs(entries.toFilesystemEncoding().c_str());
551 while (getline(ifs, line)) {
552 LYXERR(Debug::LYXVC, "\tEntries: " << line);
553 if (contains(line, tmpf))
561 void CVS::scanMaster()
563 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
564 // Ok now we do the real scan...
565 ifstream ifs(master_.toFilesystemEncoding().c_str());
566 string name = onlyFileName(owner_->absFileName());
567 string tmpf = '/' + name + '/';
568 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
570 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
571 while (getline(ifs, line)) {
572 LYXERR(Debug::LYXVC, "\t line: " << line);
573 if (contains(line, tmpf)) {
574 // Ok extract the fields.
576 if (!regex_match(line, sm, reg)) {
577 LYXERR(Debug::LYXVC, "\t Cannot parse line. Skipping.");
581 //sm[0]; // whole matched string
583 version_ = sm.str(2);
584 string const file_date = sm.str(3);
587 //sm[5]; // tag or tagdate
588 FileName file(owner_->absFileName());
589 if (file.isReadableFile()) {
590 time_t mod = file.lastModified();
591 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
592 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
593 << "'\nModification date of file: `" << mod_date << '\'');
594 if (file.isReadOnly()) {
595 // readonly checkout is unlocked
598 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
599 FileName base(addName(bdir.absFileName(),name));
600 // if base version is existent "cvs edit" was used to lock
601 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
604 vcstatus = NOLOCKING;
612 bool CVS::retrieve(FileName const & file)
614 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
615 // The caller ensures that file does not exist, so no need to check that.
616 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
617 file.onlyPath()) == 0;
621 string const CVS::getTarget(OperationMode opmode) const
625 // in client server mode CVS does not like full path operand for directory operation
626 // since LyX switches to the repo dir "." is good enough as target
629 return quoteName(onlyFileName(owner_->absFileName()));
635 docstring CVS::toString(CvsStatus status) const
639 return _("Up-to-date");
640 case LocallyModified:
641 return _("Locally Modified");
643 return _("Locally Added");
645 return _("Needs Merge");
647 return _("Needs Checkout");
649 return _("No CVS file");
651 return _("Cannot retrieve CVS status");
657 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
658 FileName const & output, bool reportError)
660 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
661 return doVCCommand(cmd + redirection, path, reportError);
665 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
666 support::FileName const & path,
667 support::FileName const & output)
669 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
670 return doVCCommandCall(cmd + redirection, path);
674 CVS::CvsStatus CVS::getStatus()
676 TempFile tempfile("lyxvout");
677 FileName tmpf = tempfile.name();
679 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
683 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
684 FileName(owner_->filePath()), tmpf)) {
688 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
689 CvsStatus status = NoCvsFile;
694 LYXERR(Debug::LYXVC, line << '\n');
695 if (prefixIs(line, "File:")) {
696 if (contains(line, "Up-to-date"))
698 else if (contains(line, "Locally Modified"))
699 status = LocallyModified;
700 else if (contains(line, "Locally Added"))
701 status = LocallyAdded;
702 else if (contains(line, "Needs Merge"))
704 else if (contains(line, "Needs Checkout"))
705 status = NeedsCheckout;
711 void CVS::getRevisionInfo()
715 have_rev_info_ = true;
716 TempFile tempfile("lyxvout");
717 FileName tmpf = tempfile.name();
719 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
723 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
724 + ' ' + getTarget(File),
725 FileName(owner_->filePath()), tmpf);
727 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
731 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
732 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
737 LYXERR(Debug::LYXVC, line << '\n');
738 if (prefixIs(line, "date:")) {
740 if (regex_match(line, sm, reg)) {
741 //sm[0]; // whole matched string
742 rev_date_cache_ = sm[1];
743 rev_time_cache_ = sm[2];
744 //sm[3]; // GMT offset
745 rev_author_cache_ = sm[4];
747 LYXERR(Debug::LYXVC, "\tCannot parse line. Skipping.");
751 if (rev_author_cache_.empty())
753 "Could not retrieve revision info for " << version_ <<
754 " of " << getTarget(File));
758 void CVS::registrer(string const & msg)
760 doVCCommand("cvs -q add -m \"" + msg + "\" "
762 FileName(owner_->filePath()));
766 bool CVS::renameEnabled()
772 string CVS::rename(support::FileName const & newFile, string const & msg)
774 // CVS has no real rename command, so we create a poor mans version
775 support::FileName const oldFile(owner_->absFileName());
776 string ret = copy(newFile, msg);
779 string cmd = "cvs -q remove -m \"" + msg + "\" " +
780 quoteName(oldFile.onlyFileName());
781 FileName path(oldFile.onlyPath());
782 return doVCCommand(cmd, path) ? string() : ret;
786 bool CVS::copyEnabled()
792 string CVS::copy(support::FileName const & newFile, string const & msg)
794 // CVS has no real copy command, so we create a poor mans version
795 support::FileName const oldFile(owner_->absFileName());
796 if (!oldFile.copyTo(newFile))
798 FileName path(oldFile.onlyPath());
799 string relFile(to_utf8(newFile.relPath(path.absFileName())));
800 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
801 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
805 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
807 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
808 FileName(owner_->filePath()), tmpf, false);
815 return doVCCommand("cvs -q edit " + getTarget(File),
816 FileName(owner_->filePath()));
823 return doVCCommand("cvs -q unedit " + getTarget(File),
824 FileName(owner_->filePath()));
828 int CVS::update(OperationMode opmode, FileName const & tmpf)
830 return doVCCommandWithOutput("cvs -q update "
832 FileName(owner_->filePath()), tmpf, false);
836 string CVS::scanLogFile(FileName const & f, string & status)
838 ifstream ifs(f.toFilesystemEncoding().c_str());
843 LYXERR(Debug::LYXVC, line << '\n');
845 status += line + "; ";
846 if (prefixIs(line, "C ")) {
856 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
858 CvsStatus status = getStatus();
861 if (vcstatus != NOLOCKING)
863 return LyXVC::ErrorCommand;
864 log = "CVS: Proceeded";
865 return LyXVC::VCSuccess;
866 case LocallyModified:
868 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
870 FileName(owner_->filePath()));
872 return LyXVC::ErrorCommand;
873 log = "CVS: Proceeded";
874 return LyXVC::VCSuccess;
878 frontend::Alert::error(_("Revision control error."),
879 _("The repository version is newer then the current check out.\n"
880 "You have to update from repository first or revert your changes.")) ;
883 frontend::Alert::error(_("Revision control error."),
884 bformat(_("Bad status when checking in changes.\n"
889 return LyXVC::ErrorBefore;
893 bool CVS::isLocked() const
895 FileName fn(owner_->absFileName());
897 return !fn.isReadOnly();
901 bool CVS::checkInEnabled()
903 if (vcstatus != NOLOCKING)
910 bool CVS::isCheckInWithConfirmation()
912 CvsStatus status = getStatus();
913 return status == LocallyModified || status == LocallyAdded;
917 string CVS::checkOut()
919 if (vcstatus != NOLOCKING && edit())
921 TempFile tempfile("lyxvout");
922 FileName tmpf = tempfile.name();
924 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
928 int rc = update(File, tmpf);
930 string const res = scanLogFile(tmpf, log);
932 frontend::Alert::error(_("Revision control error."),
933 bformat(_("Error when updating from repository.\n"
934 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
935 "After pressing OK, LyX will try to reopen the resolved document."),
936 from_local8bit(res)));
940 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
944 bool CVS::checkOutEnabled()
946 if (vcstatus != NOLOCKING)
953 string CVS::repoUpdate()
955 TempFile tempfile("lyxvout");
956 FileName tmpf = tempfile.name();
958 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
962 getDiff(Directory, tmpf);
963 docstring res = tmpf.fileContents("UTF-8");
965 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
966 docstring const file = from_utf8(owner_->filePath());
967 docstring text = bformat(_("There were detected changes "
968 "in the working directory:\n%1$s\n\n"
969 "Possible file conflicts must be then resolved manually "
970 "or you will need to revert back to the repository version."), file);
971 int ret = frontend::Alert::prompt(_("Changes detected"),
972 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
974 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
975 ret = frontend::Alert::prompt(_("Changes detected"),
976 text, 0, 1, _("&Continue"), _("&Abort"));
977 hideDialogs("file", nullptr);
983 int rc = update(Directory, tmpf);
984 res += "Update log:\n" + tmpf.fileContents("UTF-8");
985 LYXERR(Debug::LYXVC, res);
988 string sres = scanLogFile(tmpf, log);
990 docstring const file = owner_->fileName().displayName(20);
991 frontend::Alert::error(_("Revision control error."),
992 bformat(_("Error when updating document %1$s from repository.\n"
993 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
994 "After pressing OK, LyX will try to reopen the resolved document."),
995 file, from_local8bit(sres)));
999 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
1003 bool CVS::repoUpdateEnabled()
1009 string CVS::lockingToggle()
1011 lyxerr << "Sorry, not implemented." << endl;
1016 bool CVS::lockingToggleEnabled()
1022 bool CVS::isRevertWithConfirmation()
1024 CvsStatus status = getStatus();
1025 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1031 // Reverts to the version in CVS repository and
1032 // gets the updated version from the repository.
1033 CvsStatus status = getStatus();
1036 if (vcstatus != NOLOCKING)
1037 return 0 == unedit();
1041 case LocallyModified: {
1042 FileName f(owner_->absFileName());
1044 update(File, FileName());
1045 owner_->markClean();
1048 case LocallyAdded: {
1049 docstring const file = owner_->fileName().displayName(20);
1050 frontend::Alert::error(_("Revision control error."),
1051 bformat(_("The document %1$s is not in repository.\n"
1052 "You have to check in the first revision before you can revert."),
1057 docstring const file = owner_->fileName().displayName(20);
1058 frontend::Alert::error(_("Revision control error."),
1059 bformat(_("Cannot revert document %1$s to repository version.\n"
1060 "The status '%2$s' is unexpected."),
1061 file, toString(status)));
1069 void CVS::undoLast()
1071 // merge the current with the previous version
1072 // in a reverse patch kind of way, so that the
1073 // result is to revert the last changes.
1074 lyxerr << "Sorry, not implemented." << endl;
1078 bool CVS::undoLastEnabled()
1084 void CVS::getLog(FileName const & tmpf)
1086 doVCCommandWithOutput("cvs log " + getTarget(File),
1087 FileName(owner_->filePath()),
1092 bool CVS::toggleReadOnlyEnabled()
1098 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1100 if (!version_.empty()) {
1106 return rev_author_cache_;
1108 return rev_date_cache_;
1110 return rev_time_cache_;
1119 bool CVS::prepareFileRevision(string const & revis, string & f)
1122 if (!VCS::makeRCSRevision(version_, rev))
1125 TempFile tempfile("lyxvcrev_" + rev + '_');
1126 tempfile.setAutoRemove(false);
1127 FileName tmpf = tempfile.name();
1129 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1133 doVCCommandWithOutput("cvs update -p -r" + rev + ' '
1135 FileName(owner_->filePath()), tmpf);
1137 if (tmpf.isFileEmpty())
1140 f = tmpf.absFileName();
1145 bool CVS::prepareFileRevisionEnabled()
1151 /////////////////////////////////////////////////////////////////////
1155 /////////////////////////////////////////////////////////////////////
1157 SVN::SVN(FileName const & m, Buffer * b) : VCS(b)
1159 // Here we know that the buffer file is either already in SVN or
1160 // about to be registered
1162 locked_mode_ = false;
1167 FileName const SVN::findFile(FileName const & file)
1169 // First we check the existence of repository meta data.
1170 if (!VCS::checkparentdirs(file, ".svn")) {
1171 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1175 // Now we check the status of the file.
1176 TempFile tempfile("lyxvcout");
1177 FileName tmpf = tempfile.name();
1179 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1183 string const fname = onlyFileName(file.absFileName());
1184 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1185 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1186 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1188 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1189 return found ? file : FileName();
1193 void SVN::scanMaster()
1195 // vcstatus code is somewhat superflous,
1196 // until we want to implement read-only toggle for svn.
1197 vcstatus = NOLOCKING;
1198 if (checkLockMode()) {
1202 vcstatus = UNLOCKED;
1207 bool SVN::checkLockMode()
1209 TempFile tempfile("lyxvcout");
1210 FileName tmpf = tempfile.name();
1212 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1216 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1217 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1218 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1219 FileName(owner_->filePath())))
1222 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1226 while (ifs && !ret) {
1228 LYXERR(Debug::LYXVC, line);
1229 if (contains(line, "svn:needs-lock"))
1232 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1240 bool SVN::isLocked() const
1242 FileName file(owner_->absFileName());
1244 return !file.isReadOnly();
1248 bool SVN::retrieve(FileName const & file)
1250 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1251 // The caller ensures that file does not exist, so no need to check that.
1252 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1253 file.onlyPath()) == 0;
1257 void SVN::registrer(string const & /*msg*/)
1259 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1260 FileName(owner_->filePath()));
1264 bool SVN::renameEnabled()
1270 string SVN::rename(support::FileName const & newFile, string const & msg)
1272 // svn move does not require a log message, since it does not commit.
1273 // In LyX we commit immediately afterwards, otherwise it could be
1274 // confusing to the user to have two uncommitted files.
1275 FileName path(owner_->filePath());
1276 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1277 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1278 ' ' + quoteName(relFile));
1279 if (doVCCommand(cmd, path)) {
1280 cmd = "svn revert -q " +
1281 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1283 doVCCommand(cmd, path);
1284 if (newFile.exists())
1285 newFile.removeFile();
1288 vector<support::FileName> f;
1289 f.push_back(owner_->fileName());
1290 f.push_back(newFile);
1292 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1293 cmd = "svn revert -q " +
1294 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1296 doVCCommand(cmd, path);
1297 if (newFile.exists())
1298 newFile.removeFile();
1305 bool SVN::copyEnabled()
1311 string SVN::copy(support::FileName const & newFile, string const & msg)
1313 // svn copy does not require a log message, since it does not commit.
1314 // In LyX we commit immediately afterwards, otherwise it could be
1315 // confusing to the user to have an uncommitted file.
1316 FileName path(owner_->filePath());
1317 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1318 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1319 ' ' + quoteName(relFile));
1320 if (doVCCommand(cmd, path))
1322 vector<support::FileName> f(1, newFile);
1324 if (checkIn(f, msg, log) == LyXVC::VCSuccess)
1330 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1332 vector<support::FileName> f(1, owner_->fileName());
1333 return checkIn(f, msg, log);
1337 LyXVC::CommandResult
1338 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1340 TempFile tempfile("lyxvcout");
1341 FileName tmpf = tempfile.name();
1343 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1344 log = N_("Error: Could not generate logfile.");
1345 return LyXVC::ErrorBefore;
1349 os << "svn commit -m \"" << msg << '"';
1350 for (size_t i = 0; i < f.size(); ++i)
1351 os << ' ' << quoteName(f[i].onlyFileName());
1352 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1353 LyXVC::CommandResult ret =
1354 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1355 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1357 string res = scanLogFile(tmpf, log);
1359 frontend::Alert::error(_("Revision control error."),
1360 _("Error when committing to repository.\n"
1361 "You have to manually resolve the problem.\n"
1362 "LyX will reopen the document after you press OK."));
1363 ret = LyXVC::ErrorCommand;
1366 if (!fileLock(false, tmpf, log))
1367 ret = LyXVC::ErrorCommand;
1370 log.insert(0, "SVN: ");
1371 if (ret == LyXVC::VCSuccess && log.empty())
1372 log = "SVN: Proceeded";
1377 bool SVN::checkInEnabled()
1386 bool SVN::isCheckInWithConfirmation()
1388 // FIXME one day common getDiff and perhaps OpMode for all backends
1390 TempFile tempfile("lyxvcout");
1391 FileName tmpf = tempfile.name();
1393 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1397 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1398 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1399 FileName(owner_->filePath()));
1401 docstring diff = tmpf.fileContents("UTF-8");
1410 // FIXME Correctly return code should be checked instead of this.
1411 // This would need another solution than just plain startscript.
1412 // Hint from Andre': QProcess::readAllStandardError()...
1413 string SVN::scanLogFile(FileName const & f, string & status)
1415 ifstream ifs(f.toFilesystemEncoding().c_str());
1420 LYXERR(Debug::LYXVC, line << '\n');
1422 status += line + "; ";
1423 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1424 || contains(line, "Commit failed")) {
1428 if (contains(line, "svn:needs-lock")) {
1438 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1440 if (!locked_mode_ || (isLocked() == lock))
1443 string const arg = lock ? "lock " : "unlock ";
1444 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1445 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1446 FileName(owner_->filePath()));
1448 // Lock error messages go unfortunately on stderr and are unreachable this way.
1449 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1453 if (!line.empty()) status += line + "; ";
1457 if (isLocked() == lock)
1461 frontend::Alert::error(_("Revision control error."),
1462 _("Error while acquiring write lock.\n"
1463 "Another user is most probably editing\n"
1464 "the current document now!\n"
1465 "Also check the access to the repository."));
1467 frontend::Alert::error(_("Revision control error."),
1468 _("Error while releasing write lock.\n"
1469 "Check the access to the repository."));
1474 string SVN::checkOut()
1476 TempFile tempfile("lyxvcout");
1477 FileName tmpf = tempfile.name();
1479 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1480 return N_("Error: Could not generate logfile.");
1483 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1484 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1485 FileName(owner_->filePath()));
1488 string const res = scanLogFile(tmpf, log);
1490 frontend::Alert::error(_("Revision control error."),
1491 bformat(_("Error when updating from repository.\n"
1492 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1493 "After pressing OK, LyX will try to reopen the resolved document."),
1494 from_local8bit(res)));
1496 fileLock(true, tmpf, log);
1498 return log.empty() ? string() : "SVN: " + log;
1502 bool SVN::checkOutEnabled()
1511 string SVN::repoUpdate()
1513 TempFile tempfile("lyxvcout");
1514 FileName tmpf = tempfile.name();
1516 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1517 return N_("Error: Could not generate logfile.");
1520 doVCCommand("svn diff " + quoteName(owner_->filePath())
1521 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1522 FileName(owner_->filePath()));
1523 docstring res = tmpf.fileContents("UTF-8");
1525 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1526 docstring const file = from_utf8(owner_->filePath());
1527 docstring text = bformat(_("There were detected changes "
1528 "in the working directory:\n%1$s\n\n"
1529 "In case of file conflict version of the local directory files "
1530 "will be preferred."
1531 "\n\nContinue?"), file);
1532 int ret = frontend::Alert::prompt(_("Changes detected"),
1533 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1535 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1536 ret = frontend::Alert::prompt(_("Changes detected"),
1537 text, 0, 1, _("&Yes"), _("&No"));
1538 hideDialogs("file", nullptr);
1544 // Reverting looks too harsh, see bug #6255.
1545 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1546 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1547 // FileName(owner_->filePath()));
1548 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1549 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1550 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1551 FileName(owner_->filePath()));
1552 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1554 LYXERR(Debug::LYXVC, res);
1555 return to_utf8(res);
1559 bool SVN::repoUpdateEnabled()
1565 string SVN::lockingToggle()
1567 TempFile tempfile("lyxvcout");
1568 FileName tmpf = tempfile.name();
1570 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1571 return N_("Error: Could not generate logfile.");
1574 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1575 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1576 FileName(owner_->filePath()));
1581 string res = scanLogFile(tmpf, log);
1582 bool locking = contains(res, "svn:needs-lock");
1584 ret = doVCCommand("svn propset svn:needs-lock ON "
1585 + quoteName(onlyFileName(owner_->absFileName()))
1586 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1587 FileName(owner_->filePath()));
1589 ret = doVCCommand("svn propdel svn:needs-lock "
1590 + quoteName(onlyFileName(owner_->absFileName()))
1591 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1592 FileName(owner_->filePath()));
1596 frontend::Alert::warning(_("SVN File Locking"),
1597 (locking ? _("Locking property unset.") : _("Locking property set.")) + '\n'
1598 + _("Do not forget to commit the locking property into the repository."),
1601 return string("SVN: ") + (locking ?
1602 N_("Locking property unset.") : N_("Locking property set."));
1606 bool SVN::lockingToggleEnabled()
1614 // Reverts to the version in SVN repository and
1615 // gets the updated version from the repository.
1616 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1618 if (doVCCommand("svn revert -q " + fil,
1619 FileName(owner_->filePath())))
1621 owner_->markClean();
1626 bool SVN::isRevertWithConfirmation()
1628 //FIXME owner && diff
1633 void SVN::undoLast()
1635 // merge the current with the previous version
1636 // in a reverse patch kind of way, so that the
1637 // result is to revert the last changes.
1638 lyxerr << "Sorry, not implemented." << endl;
1642 bool SVN::undoLastEnabled()
1648 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1650 if (info == LyXVC::Tree) {
1651 if (rev_tree_cache_.empty())
1652 if (!getTreeRevisionInfo())
1653 rev_tree_cache_ = "?";
1654 if (rev_tree_cache_ == "?")
1657 return rev_tree_cache_;
1660 // fill the rest of the attributes for a single file
1661 if (rev_file_cache_.empty())
1662 if (!getFileRevisionInfo())
1663 rev_file_cache_ = "?";
1667 if (rev_file_cache_ == "?")
1669 return rev_file_cache_;
1671 return rev_author_cache_;
1673 return rev_date_cache_;
1675 return rev_time_cache_;
1684 bool SVN::getFileRevisionInfo()
1686 TempFile tempfile("lyxvcout");
1687 FileName tmpf = tempfile.name();
1689 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1693 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1694 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1695 FileName(owner_->filePath()));
1700 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1708 LYXERR(Debug::LYXVC, line);
1709 if (prefixIs(line, "<commit"))
1711 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1712 string l1 = subst(line, "revision=\"", "");
1713 string l2 = trim(subst(l1, "\">", ""));
1715 rev_file_cache_ = rev = l2;
1717 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1718 string l1 = subst(line, "<author>", "");
1719 string l2 = subst(l1, "</author>", "");
1720 rev_author_cache_ = l2;
1722 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1723 string l1 = subst(line, "<date>", "");
1724 string l2 = subst(l1, "</date>", "");
1725 l2 = split(l2, l1, 'T');
1726 rev_date_cache_ = l1;
1727 l2 = split(l2, l1, '.');
1728 rev_time_cache_ = l1;
1733 return !rev.empty();
1737 bool SVN::getTreeRevisionInfo()
1739 TempFile tempfile("lyxvcout");
1740 FileName tmpf = tempfile.name();
1742 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1746 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1747 FileName(owner_->filePath()));
1752 // only first line in case something bad happens.
1753 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1758 rev_tree_cache_ = line;
1759 return !line.empty();
1763 void SVN::getLog(FileName const & tmpf)
1765 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1766 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1767 FileName(owner_->filePath()));
1771 bool SVN::prepareFileRevision(string const & revis, string & f)
1773 if (!isStrInt(revis))
1776 int rev = convert<int>(revis);
1778 if (!getFileRevisionInfo())
1781 rev = convert<int>(rev_file_cache_);
1782 // go back for minus rev
1784 rev = rev + convert<int>(rev_file_cache_);
1789 string revname = convert<string>(rev);
1790 TempFile tempfile("lyxvcrev_" + revname + '_');
1791 tempfile.setAutoRemove(false);
1792 FileName tmpf = tempfile.name();
1794 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1798 doVCCommand("svn cat -r " + revname + ' '
1799 + quoteName(onlyFileName(owner_->absFileName()))
1800 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1801 FileName(owner_->filePath()));
1803 if (tmpf.isFileEmpty())
1806 f = tmpf.absFileName();
1811 bool SVN::prepareFileRevisionEnabled()
1818 bool SVN::toggleReadOnlyEnabled()
1824 /////////////////////////////////////////////////////////////////////
1828 /////////////////////////////////////////////////////////////////////
1830 GIT::GIT(FileName const & m, Buffer * b) : VCS(b)
1832 // Here we know that the buffer file is either already in GIT or
1833 // about to be registered
1839 FileName const GIT::findFile(FileName const & file)
1841 // First we check the existence of repository meta data.
1842 if (!VCS::checkparentdirs(file, ".git")) {
1843 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1847 // Now we check the status of the file.
1848 TempFile tempfile("lyxvcout");
1849 FileName tmpf = tempfile.name();
1851 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1855 string const fname = onlyFileName(file.absFileName());
1856 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1858 doVCCommandCall("git ls-files " +
1859 quoteName(fname) + " > " +
1860 quoteName(tmpf.toFilesystemEncoding()),
1863 bool found = !tmpf.isFileEmpty();
1864 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1865 return found ? file : FileName();
1869 void GIT::scanMaster()
1871 // vcstatus code is somewhat superflous,
1872 // until we want to implement read-only toggle for git.
1873 vcstatus = NOLOCKING;
1877 bool GIT::retrieve(FileName const & file)
1879 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1880 // The caller ensures that file does not exist, so no need to check that.
1881 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1882 file.onlyPath()) == 0;
1886 void GIT::registrer(string const & /*msg*/)
1888 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1889 FileName(owner_->filePath()));
1893 bool GIT::renameEnabled()
1899 string GIT::rename(support::FileName const & newFile, string const & msg)
1901 // git mv does not require a log message, since it does not commit.
1902 // In LyX we commit immediately afterwards, otherwise it could be
1903 // confusing to the user to have two uncommitted files.
1904 FileName path(owner_->filePath());
1905 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1906 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1907 ' ' + quoteName(relFile));
1908 if (doVCCommand(cmd, path)) {
1909 cmd = "git checkout -q " +
1910 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1912 doVCCommand(cmd, path);
1913 if (newFile.exists())
1914 newFile.removeFile();
1917 vector<support::FileName> f;
1918 f.push_back(owner_->fileName());
1919 f.push_back(newFile);
1921 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1922 cmd = "git checkout -q " +
1923 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1925 doVCCommand(cmd, path);
1926 if (newFile.exists())
1927 newFile.removeFile();
1934 bool GIT::copyEnabled()
1940 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1942 // git does not support copy with history preservation
1947 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1949 vector<support::FileName> f(1, owner_->fileName());
1950 return checkIn(f, msg, log);
1954 LyXVC::CommandResult
1955 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1957 TempFile tempfile("lyxvcout");
1958 FileName tmpf = tempfile.name();
1960 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1961 log = N_("Error: Could not generate logfile.");
1962 return LyXVC::ErrorBefore;
1966 os << "git commit -m \"" << msg << '"';
1967 for (size_t i = 0; i < f.size(); ++i)
1968 os << ' ' << quoteName(f[i].onlyFileName());
1969 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1970 LyXVC::CommandResult ret =
1971 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1972 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1974 string res = scanLogFile(tmpf, log);
1976 frontend::Alert::error(_("Revision control error."),
1977 _("Error when committing to repository.\n"
1978 "You have to manually resolve the problem.\n"
1979 "LyX will reopen the document after you press OK."));
1980 ret = LyXVC::ErrorCommand;
1984 log.insert(0, "GIT: ");
1985 if (ret == LyXVC::VCSuccess && log.empty())
1986 log = "GIT: Proceeded";
1991 bool GIT::checkInEnabled()
1997 bool GIT::isCheckInWithConfirmation()
1999 // FIXME one day common getDiff and perhaps OpMode for all backends
2001 TempFile tempfile("lyxvcout");
2002 FileName tmpf = tempfile.name();
2004 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2008 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
2009 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2010 FileName(owner_->filePath()));
2012 docstring diff = tmpf.fileContents("UTF-8");
2021 // FIXME Correctly return code should be checked instead of this.
2022 // This would need another solution than just plain startscript.
2023 // Hint from Andre': QProcess::readAllStandardError()...
2024 string GIT::scanLogFile(FileName const & f, string & status)
2026 ifstream ifs(f.toFilesystemEncoding().c_str());
2031 LYXERR(Debug::LYXVC, line << "\n");
2033 status += line + "; ";
2034 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2035 || contains(line, "Commit failed")) {
2045 string GIT::checkOut()
2051 bool GIT::checkOutEnabled()
2057 string GIT::repoUpdate()
2063 bool GIT::repoUpdateEnabled()
2069 string GIT::lockingToggle()
2075 bool GIT::lockingToggleEnabled()
2083 // Reverts to the version in GIT repository and
2084 // gets the updated version from the repository.
2085 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2087 if (doVCCommand("git checkout -q " + fil,
2088 FileName(owner_->filePath())))
2090 owner_->markClean();
2095 bool GIT::isRevertWithConfirmation()
2097 //FIXME owner && diff
2102 void GIT::undoLast()
2104 // merge the current with the previous version
2105 // in a reverse patch kind of way, so that the
2106 // result is to revert the last changes.
2107 lyxerr << "Sorry, not implemented." << endl;
2111 bool GIT::undoLastEnabled()
2117 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2119 if (info == LyXVC::Tree) {
2120 if (rev_tree_cache_.empty())
2121 if (!getTreeRevisionInfo())
2122 rev_tree_cache_ = "?";
2123 if (rev_tree_cache_ == "?")
2126 return rev_tree_cache_;
2129 // fill the rest of the attributes for a single file
2130 if (rev_file_cache_.empty())
2131 if (!getFileRevisionInfo()) {
2132 rev_file_cache_ = "?";
2133 rev_file_abbrev_cache_ = "?";
2138 if (rev_file_cache_ == "?")
2140 return rev_file_cache_;
2141 case LyXVC::FileAbbrev:
2142 if (rev_file_abbrev_cache_ == "?")
2144 return rev_file_abbrev_cache_;
2146 return rev_author_cache_;
2148 return rev_date_cache_;
2150 return rev_time_cache_;
2159 bool GIT::getFileRevisionInfo()
2161 TempFile tempfile("lyxvcout");
2162 FileName tmpf = tempfile.name();
2164 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2168 doVCCommand("git log -n 1 --pretty=format:%H%n%h%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2169 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2170 FileName(owner_->filePath()));
2175 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2178 getline(ifs, rev_file_cache_);
2180 getline(ifs, rev_file_abbrev_cache_);
2182 getline(ifs, rev_author_cache_);
2186 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2190 return !rev_file_cache_.empty();
2194 bool GIT::getTreeRevisionInfo()
2196 TempFile tempfile("lyxvcout");
2197 FileName tmpf = tempfile.name();
2199 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2203 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2204 FileName(owner_->filePath()),
2205 false); //git describe returns $?=128 when no tag found (but git repo still exists)
2210 // only first line in case something bad happens.
2211 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2212 getline(ifs, rev_tree_cache_);
2215 return !rev_tree_cache_.empty();
2219 void GIT::getLog(FileName const & tmpf)
2221 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2222 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2223 FileName(owner_->filePath()));
2227 //at this moment we don't accept revision SHA, but just number of revision steps back
2228 //GUI and infrastucture needs to be changed first
2229 bool GIT::prepareFileRevision(string const & revis, string & f)
2231 // anything positive means we got hash, not "0" or minus revision
2234 // hash is rarely number and should be long
2235 if (isStrInt(revis) && revis.length()<20)
2236 rev = convert<int>(revis);
2238 // revision and filename
2241 // go back for "minus" revisions
2243 pointer = "HEAD~" + convert<string>(-rev);
2250 TempFile tempfile("lyxvcrev_" + revis + '_');
2251 tempfile.setAutoRemove(false);
2252 FileName tmpf = tempfile.name();
2254 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2258 doVCCommand("git show " + pointer + "./"
2259 + quoteName(onlyFileName(owner_->absFileName()))
2260 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2261 FileName(owner_->filePath()));
2263 if (tmpf.isFileEmpty())
2266 f = tmpf.absFileName();
2271 bool GIT::prepareFileRevisionEnabled()
2277 bool GIT::toggleReadOnlyEnabled()