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" + _("Check whether the GNU RCS package is 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 FileName dirname = start.onlyPath();
112 FileName tocheck = FileName(addPathName(dirname.absFileName(), file));
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 int const ret = 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";
204 vcstatus_ = 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 // Note that it is not necessary to search parent directories, since
542 // there will be a CVS/Entries file in every subdirectory.
543 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
544 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
545 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
546 << "' for `" << tmpf << '\'');
547 if (entries.isReadableFile()) {
548 // Ok we are at least in a CVS dir. Parse the CVS/Entries
549 // and see if we can find this file. We do a fast and
551 ifstream ifs(entries.toFilesystemEncoding().c_str());
553 while (getline(ifs, line)) {
554 LYXERR(Debug::LYXVC, "\tEntries: " << line);
555 if (contains(line, tmpf))
563 void CVS::scanMaster()
565 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
566 // Ok now we do the real scan...
567 ifstream ifs(master_.toFilesystemEncoding().c_str());
568 string const name = onlyFileName(owner_->absFileName());
569 string const tmpf = '/' + name + '/';
570 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
572 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
573 while (getline(ifs, line)) {
574 LYXERR(Debug::LYXVC, "\t line: " << line);
575 if (contains(line, tmpf)) {
576 // Ok extract the fields.
578 if (!regex_match(line, sm, reg)) {
579 LYXERR(Debug::LYXVC, "\t Cannot parse line. Skipping.");
583 //sm[0]; // whole matched string
585 version_ = sm.str(2);
586 string const file_date = sm.str(3);
589 //sm[5]; // tag or tagdate
590 FileName file(owner_->absFileName());
591 if (file.isReadableFile()) {
592 time_t const mod = file.lastModified();
593 string const mod_date = rtrim(asctime(gmtime(&mod)), "\n");
594 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
595 << "'\nModification date of file: `" << mod_date << '\'');
596 if (file.isReadOnly()) {
597 // readonly checkout is unlocked
598 vcstatus_ = UNLOCKED;
600 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
601 FileName base(addName(bdir.absFileName(),name));
602 // if base version is existent "cvs edit" was used to lock
603 vcstatus_ = base.isReadableFile() ? LOCKED : NOLOCKING;
606 vcstatus_ = NOLOCKING;
614 bool CVS::retrieve(FileName const & file)
616 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
617 // The caller ensures that file does not exist, so no need to check that.
618 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
619 file.onlyPath()) == 0;
623 string const CVS::getTarget(OperationMode opmode) const
627 // in client server mode CVS does not like full path operand for directory operation
628 // since LyX switches to the repo dir "." is good enough as target
631 return quoteName(onlyFileName(owner_->absFileName()));
637 docstring CVS::toString(CvsStatus status) const
641 return _("Up-to-date");
642 case LocallyModified:
643 return _("Locally Modified");
645 return _("Locally Added");
647 return _("Needs Merge");
649 return _("Needs Checkout");
651 return _("No CVS file");
653 return _("Cannot retrieve CVS status");
659 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
660 FileName const & output, bool reportError)
662 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
663 return doVCCommand(cmd + redirection, path, reportError);
667 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
668 support::FileName const & path,
669 support::FileName const & output)
671 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
672 return doVCCommandCall(cmd + redirection, path);
676 CVS::CvsStatus CVS::getStatus()
678 TempFile tempfile("lyxvout");
679 FileName tmpf = tempfile.name();
681 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
685 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
686 FileName(owner_->filePath()), tmpf)) {
690 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
691 CvsStatus status = NoCvsFile;
696 LYXERR(Debug::LYXVC, line << '\n');
697 if (prefixIs(line, "File:")) {
698 if (contains(line, "Up-to-date"))
700 else if (contains(line, "Locally Modified"))
701 status = LocallyModified;
702 else if (contains(line, "Locally Added"))
703 status = LocallyAdded;
704 else if (contains(line, "Needs Merge"))
706 else if (contains(line, "Needs Checkout"))
707 status = NeedsCheckout;
713 void CVS::getRevisionInfo()
717 have_rev_info_ = true;
718 TempFile tempfile("lyxvout");
719 FileName tmpf = tempfile.name();
721 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
725 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
726 + ' ' + getTarget(File),
727 FileName(owner_->filePath()), tmpf);
729 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
733 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
734 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
739 LYXERR(Debug::LYXVC, line << '\n');
740 if (prefixIs(line, "date:")) {
742 if (regex_match(line, sm, reg)) {
743 //sm[0]; // whole matched string
744 rev_date_cache_ = sm[1];
745 rev_time_cache_ = sm[2];
746 //sm[3]; // GMT offset
747 rev_author_cache_ = sm[4];
749 LYXERR(Debug::LYXVC, "\tCannot parse line. Skipping.");
753 if (rev_author_cache_.empty())
755 "Could not retrieve revision info for " << version_ <<
756 " of " << getTarget(File));
760 void CVS::registrer(string const & msg)
762 doVCCommand("cvs -q add -m \"" + msg + "\" "
764 FileName(owner_->filePath()));
768 bool CVS::renameEnabled()
774 string CVS::rename(support::FileName const & newFile, string const & msg)
776 // CVS has no real rename command, so we create a poor mans version
777 support::FileName const oldFile(owner_->absFileName());
778 string ret = copy(newFile, msg);
781 string cmd = "cvs -q remove -m \"" + msg + "\" " +
782 quoteName(oldFile.onlyFileName());
783 FileName path(oldFile.onlyPath());
784 return doVCCommand(cmd, path) ? string() : ret;
788 bool CVS::copyEnabled()
794 string CVS::copy(support::FileName const & newFile, string const & msg)
796 // CVS has no real copy command, so we create a poor mans version
797 support::FileName const oldFile(owner_->absFileName());
798 if (!oldFile.copyTo(newFile))
800 FileName path(oldFile.onlyPath());
801 string relFile(to_utf8(newFile.relPath(path.absFileName())));
802 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
803 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
807 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
809 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
810 FileName(owner_->filePath()), tmpf, false);
817 return doVCCommand("cvs -q edit " + getTarget(File),
818 FileName(owner_->filePath()));
824 vcstatus_ = UNLOCKED;
825 return doVCCommand("cvs -q unedit " + getTarget(File),
826 FileName(owner_->filePath()));
830 int CVS::update(OperationMode opmode, FileName const & tmpf)
832 return doVCCommandWithOutput("cvs -q update "
834 FileName(owner_->filePath()), tmpf, false);
838 string CVS::scanLogFile(FileName const & f, string & status)
840 ifstream ifs(f.toFilesystemEncoding().c_str());
845 LYXERR(Debug::LYXVC, line << '\n');
847 status += line + "; ";
848 if (prefixIs(line, "C ")) {
858 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
860 CvsStatus status = getStatus();
863 if (vcstatus_ != NOLOCKING)
865 return LyXVC::ErrorCommand;
866 log = "CVS: Proceeded";
867 return LyXVC::VCSuccess;
868 case LocallyModified:
870 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
872 FileName(owner_->filePath()));
874 return LyXVC::ErrorCommand;
875 log = "CVS: Proceeded";
876 return LyXVC::VCSuccess;
880 frontend::Alert::error(_("Revision control error."),
881 _("The repository version is newer then the current check out.\n"
882 "You have to update from repository first or revert your changes.")) ;
885 frontend::Alert::error(_("Revision control error."),
886 bformat(_("Bad status when checking in changes.\n"
891 return LyXVC::ErrorBefore;
895 bool CVS::isLocked() const
897 FileName fn(owner_->absFileName());
899 return !fn.isReadOnly();
903 bool CVS::checkInEnabled()
905 if (vcstatus_ != NOLOCKING)
912 bool CVS::isCheckInWithConfirmation()
914 CvsStatus status = getStatus();
915 return status == LocallyModified || status == LocallyAdded;
919 string CVS::checkOut()
921 if (vcstatus_ != NOLOCKING && edit())
923 TempFile tempfile("lyxvout");
924 FileName tmpf = tempfile.name();
926 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
930 int rc = update(File, tmpf);
932 string const res = scanLogFile(tmpf, log);
934 frontend::Alert::error(_("Revision control error."),
935 bformat(_("Error when updating from repository.\n"
936 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
937 "After pressing OK, LyX will try to reopen the resolved document."),
938 from_local8bit(res)));
942 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
946 bool CVS::checkOutEnabled()
948 if (vcstatus_ != NOLOCKING)
955 string CVS::repoUpdate()
957 TempFile tempfile("lyxvout");
958 FileName tmpf = tempfile.name();
960 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
964 getDiff(Directory, tmpf);
965 docstring res = tmpf.fileContents("UTF-8");
967 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
968 docstring const file = from_utf8(owner_->filePath());
969 docstring text = bformat(_("There were detected changes "
970 "in the working directory:\n%1$s\n\n"
971 "Possible file conflicts must be then resolved manually "
972 "or you will need to revert back to the repository version."), file);
973 int ret = frontend::Alert::prompt(_("Changes detected"),
974 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
976 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
977 ret = frontend::Alert::prompt(_("Changes detected"),
978 text, 0, 1, _("&Continue"), _("&Abort"));
979 hideDialogs("file", nullptr);
985 int rc = update(Directory, tmpf);
986 res += "Update log:\n" + tmpf.fileContents("UTF-8");
987 LYXERR(Debug::LYXVC, res);
990 string sres = scanLogFile(tmpf, log);
992 docstring const file = owner_->fileName().displayName(20);
993 frontend::Alert::error(_("Revision control error."),
994 bformat(_("Error when updating document %1$s from repository.\n"
995 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
996 "After pressing OK, LyX will try to reopen the resolved document."),
997 file, from_local8bit(sres)));
1001 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
1005 bool CVS::repoUpdateEnabled()
1011 string CVS::lockingToggle()
1013 lyxerr << "Sorry, not implemented." << endl;
1018 bool CVS::lockingToggleEnabled()
1024 bool CVS::isRevertWithConfirmation()
1026 CvsStatus status = getStatus();
1027 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1033 // Reverts to the version in CVS repository and
1034 // gets the updated version from the repository.
1035 CvsStatus status = getStatus();
1038 if (vcstatus_ != NOLOCKING)
1039 return 0 == unedit();
1043 case LocallyModified: {
1044 FileName f(owner_->absFileName());
1046 update(File, FileName());
1047 owner_->markClean();
1050 case LocallyAdded: {
1051 docstring const file = owner_->fileName().displayName(20);
1052 frontend::Alert::error(_("Revision control error."),
1053 bformat(_("The document %1$s is not in repository.\n"
1054 "You have to check in the first revision before you can revert."),
1059 docstring const file = owner_->fileName().displayName(20);
1060 frontend::Alert::error(_("Revision control error."),
1061 bformat(_("Cannot revert document %1$s to repository version.\n"
1062 "The status '%2$s' is unexpected."),
1063 file, toString(status)));
1071 void CVS::undoLast()
1073 // merge the current with the previous version
1074 // in a reverse patch kind of way, so that the
1075 // result is to revert the last changes.
1076 lyxerr << "Sorry, not implemented." << endl;
1080 bool CVS::undoLastEnabled()
1086 void CVS::getLog(FileName const & tmpf)
1088 doVCCommandWithOutput("cvs log " + getTarget(File),
1089 FileName(owner_->filePath()),
1094 bool CVS::toggleReadOnlyEnabled()
1100 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1102 if (!version_.empty()) {
1108 return rev_author_cache_;
1110 return rev_date_cache_;
1112 return rev_time_cache_;
1121 bool CVS::prepareFileRevision(string const & revis, string & f)
1124 if (!VCS::makeRCSRevision(version_, rev))
1127 TempFile tempfile("lyxvcrev_" + rev + '_');
1128 tempfile.setAutoRemove(false);
1129 FileName tmpf = tempfile.name();
1131 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1135 doVCCommandWithOutput("cvs update -p -r" + rev + ' '
1137 FileName(owner_->filePath()), tmpf);
1139 if (tmpf.isFileEmpty())
1142 f = tmpf.absFileName();
1147 bool CVS::prepareFileRevisionEnabled()
1153 /////////////////////////////////////////////////////////////////////
1157 /////////////////////////////////////////////////////////////////////
1159 SVN::SVN(Buffer * b) : VCS(b)
1161 // Here we know that the buffer file is either already in SVN or
1162 // about to be registered
1163 locked_mode_ = false;
1168 bool SVN::findFile(FileName const & file)
1170 // First we check the existence of repository meta data.
1171 if (VCS::checkParentDirs(file, ".svn").empty()) {
1172 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1176 // Now we check the status of the file.
1177 string const fname = onlyFileName(file.absFileName());
1178 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1179 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname),
1181 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1186 void SVN::scanMaster()
1188 // vcstatus code is somewhat superflous,
1189 // until we want to implement read-only toggle for svn.
1190 vcstatus_ = NOLOCKING;
1191 if (checkLockMode())
1192 vcstatus_ = isLocked() ? LOCKED : UNLOCKED;
1196 bool SVN::checkLockMode()
1198 TempFile tempfile("lyxvcout");
1199 FileName tmpf = tempfile.name();
1201 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1205 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1206 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1207 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1208 FileName(owner_->filePath())))
1211 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1215 while (ifs && !ret) {
1217 LYXERR(Debug::LYXVC, line);
1218 if (contains(line, "svn:needs-lock"))
1221 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1229 bool SVN::isLocked() const
1231 FileName file(owner_->absFileName());
1233 return !file.isReadOnly();
1237 bool SVN::retrieve(FileName const & file)
1239 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1240 // The caller ensures that file does not exist, so no need to check that.
1241 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1242 file.onlyPath()) == 0;
1246 void SVN::registrer(string const & /*msg*/)
1248 doVCCommand("svn add -q --parents " + quoteName(onlyFileName(owner_->absFileName())),
1249 FileName(owner_->filePath()));
1253 bool SVN::renameEnabled()
1259 string SVN::rename(support::FileName const & newFile, string const & msg)
1261 // svn move does not require a log message, since it does not commit.
1262 // In LyX we commit immediately afterwards, otherwise it could be
1263 // confusing to the user to have two uncommitted files.
1264 FileName path(owner_->filePath());
1265 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1266 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1267 ' ' + quoteName(relFile));
1268 if (doVCCommand(cmd, path)) {
1269 cmd = "svn revert -q " +
1270 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1272 doVCCommand(cmd, path);
1273 if (newFile.exists())
1274 newFile.removeFile();
1277 vector<support::FileName> f;
1278 f.push_back(owner_->fileName());
1279 f.push_back(newFile);
1281 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1282 cmd = "svn revert -q " +
1283 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1285 doVCCommand(cmd, path);
1286 if (newFile.exists())
1287 newFile.removeFile();
1294 bool SVN::copyEnabled()
1300 string SVN::copy(support::FileName const & newFile, string const & msg)
1302 // svn copy does not require a log message, since it does not commit.
1303 // In LyX we commit immediately afterwards, otherwise it could be
1304 // confusing to the user to have an uncommitted file.
1305 FileName path(owner_->filePath());
1306 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1307 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1308 ' ' + quoteName(relFile));
1309 if (doVCCommand(cmd, path))
1311 vector<support::FileName> f(1, newFile);
1313 if (checkIn(f, msg, log) == LyXVC::VCSuccess)
1319 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1321 vector<support::FileName> f(1, owner_->fileName());
1322 return checkIn(f, msg, log);
1326 LyXVC::CommandResult
1327 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1329 TempFile tempfile("lyxvcout");
1330 FileName tmpf = tempfile.name();
1332 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1333 log = N_("Error: Could not generate logfile.");
1334 return LyXVC::ErrorBefore;
1338 os << "svn commit -m \"" << msg << '"';
1339 for (size_t i = 0; i < f.size(); ++i)
1340 os << ' ' << quoteName(f[i].onlyFileName());
1341 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1342 LyXVC::CommandResult ret =
1343 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1344 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1346 string res = scanLogFile(tmpf, log);
1348 frontend::Alert::error(_("Revision control error."),
1349 _("Error when committing to repository.\n"
1350 "You have to manually resolve the problem.\n"
1351 "LyX will reopen the document after you press OK."));
1352 ret = LyXVC::ErrorCommand;
1355 if (!fileLock(false, tmpf, log))
1356 ret = LyXVC::ErrorCommand;
1359 log.insert(0, "SVN: ");
1360 if (ret == LyXVC::VCSuccess && log.empty())
1361 log = "SVN: Proceeded";
1366 bool SVN::checkInEnabled()
1375 bool SVN::isCheckInWithConfirmation()
1377 // FIXME one day common getDiff and perhaps OpMode for all backends
1379 TempFile tempfile("lyxvcout");
1380 FileName tmpf = tempfile.name();
1382 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1386 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1387 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1388 FileName(owner_->filePath()));
1390 docstring diff = tmpf.fileContents("UTF-8");
1399 // FIXME Correctly return code should be checked instead of this.
1400 // This would need another solution than just plain startscript.
1401 // Hint from Andre': QProcess::readAllStandardError()...
1402 string SVN::scanLogFile(FileName const & f, string & status)
1404 ifstream ifs(f.toFilesystemEncoding().c_str());
1409 LYXERR(Debug::LYXVC, line << '\n');
1411 status += line + "; ";
1412 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1413 || contains(line, "Commit failed")) {
1417 if (contains(line, "svn:needs-lock")) {
1427 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1429 if (!locked_mode_ || (isLocked() == lock))
1432 string const arg = lock ? "lock " : "unlock ";
1433 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1434 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1435 FileName(owner_->filePath()));
1437 // Lock error messages go unfortunately on stderr and are unreachable this way.
1438 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1442 if (!line.empty()) status += line + "; ";
1446 if (isLocked() == lock)
1450 frontend::Alert::error(_("Revision control error."),
1451 _("Error while acquiring write lock.\n"
1452 "Another user is most probably editing\n"
1453 "the current document now!\n"
1454 "Also check the access to the repository."));
1456 frontend::Alert::error(_("Revision control error."),
1457 _("Error while releasing write lock.\n"
1458 "Check the access to the repository."));
1463 string SVN::checkOut()
1465 TempFile tempfile("lyxvcout");
1466 FileName tmpf = tempfile.name();
1468 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1469 return N_("Error: Could not generate logfile.");
1472 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1473 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1474 FileName(owner_->filePath()));
1477 string const res = scanLogFile(tmpf, log);
1479 frontend::Alert::error(_("Revision control error."),
1480 bformat(_("Error when updating from repository.\n"
1481 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1482 "After pressing OK, LyX will try to reopen the resolved document."),
1483 from_local8bit(res)));
1485 fileLock(true, tmpf, log);
1487 return log.empty() ? string() : "SVN: " + log;
1491 bool SVN::checkOutEnabled()
1500 string SVN::repoUpdate()
1502 TempFile tempfile("lyxvcout");
1503 FileName tmpf = tempfile.name();
1505 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1506 return N_("Error: Could not generate logfile.");
1509 doVCCommand("svn diff " + quoteName(owner_->filePath())
1510 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1511 FileName(owner_->filePath()));
1512 docstring res = tmpf.fileContents("UTF-8");
1514 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1515 docstring const file = from_utf8(owner_->filePath());
1516 docstring text = bformat(_("There were detected changes "
1517 "in the working directory:\n%1$s\n\n"
1518 "In case of file conflict version of the local directory files "
1519 "will be preferred."
1520 "\n\nContinue?"), file);
1521 int ret = frontend::Alert::prompt(_("Changes detected"),
1522 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1524 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1525 ret = frontend::Alert::prompt(_("Changes detected"),
1526 text, 0, 1, _("&Yes"), _("&No"));
1527 hideDialogs("file", nullptr);
1533 // Reverting looks too harsh, see bug #6255.
1534 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1535 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1536 // FileName(owner_->filePath()));
1537 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1538 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1539 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1540 FileName(owner_->filePath()));
1541 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1543 LYXERR(Debug::LYXVC, res);
1544 return to_utf8(res);
1548 bool SVN::repoUpdateEnabled()
1554 string SVN::lockingToggle()
1556 TempFile tempfile("lyxvcout");
1557 FileName tmpf = tempfile.name();
1559 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1560 return N_("Error: Could not generate logfile.");
1563 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1564 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1565 FileName(owner_->filePath()));
1570 string res = scanLogFile(tmpf, log);
1571 bool locking = contains(res, "svn:needs-lock");
1573 ret = doVCCommand("svn propset svn:needs-lock ON "
1574 + quoteName(onlyFileName(owner_->absFileName()))
1575 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1576 FileName(owner_->filePath()));
1578 ret = doVCCommand("svn propdel svn:needs-lock "
1579 + quoteName(onlyFileName(owner_->absFileName()))
1580 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1581 FileName(owner_->filePath()));
1585 frontend::Alert::warning(_("SVN File Locking"),
1586 (locking ? _("Locking property unset.") : _("Locking property set.")) + '\n'
1587 + _("Do not forget to commit the locking property into the repository."),
1590 return string("SVN: ") + (locking ?
1591 N_("Locking property unset.") : N_("Locking property set."));
1595 bool SVN::lockingToggleEnabled()
1603 // Reverts to the version in SVN repository and
1604 // gets the updated version from the repository.
1605 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1607 if (doVCCommand("svn revert -q " + fil,
1608 FileName(owner_->filePath())))
1610 owner_->markClean();
1615 bool SVN::isRevertWithConfirmation()
1617 //FIXME owner && diff
1622 void SVN::undoLast()
1624 // merge the current with the previous version
1625 // in a reverse patch kind of way, so that the
1626 // result is to revert the last changes.
1627 lyxerr << "Sorry, not implemented." << endl;
1631 bool SVN::undoLastEnabled()
1637 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1639 if (info == LyXVC::Tree) {
1640 if (rev_tree_cache_.empty())
1641 if (!getTreeRevisionInfo())
1642 rev_tree_cache_ = "?";
1643 if (rev_tree_cache_ == "?")
1646 return rev_tree_cache_;
1649 // fill the rest of the attributes for a single file
1650 if (rev_file_cache_.empty())
1651 if (!getFileRevisionInfo())
1652 rev_file_cache_ = "?";
1656 if (rev_file_cache_ == "?")
1658 return rev_file_cache_;
1660 return rev_author_cache_;
1662 return rev_date_cache_;
1664 return rev_time_cache_;
1673 bool SVN::getFileRevisionInfo()
1675 TempFile tempfile("lyxvcout");
1676 FileName tmpf = tempfile.name();
1678 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1682 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1683 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1684 FileName(owner_->filePath()));
1689 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1697 LYXERR(Debug::LYXVC, line);
1698 if (prefixIs(line, "<commit"))
1700 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1701 string l1 = subst(line, "revision=\"", "");
1702 string l2 = trim(subst(l1, "\">", ""));
1704 rev_file_cache_ = rev = l2;
1706 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1707 string l1 = subst(line, "<author>", "");
1708 string l2 = subst(l1, "</author>", "");
1709 rev_author_cache_ = l2;
1711 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1712 string l1 = subst(line, "<date>", "");
1713 string l2 = subst(l1, "</date>", "");
1714 l2 = split(l2, l1, 'T');
1715 rev_date_cache_ = l1;
1716 l2 = split(l2, l1, '.');
1717 rev_time_cache_ = l1;
1722 return !rev.empty();
1726 bool SVN::getTreeRevisionInfo()
1728 TempFile tempfile("lyxvcout");
1729 FileName tmpf = tempfile.name();
1731 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1735 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1736 FileName(owner_->filePath()));
1741 // only first line in case something bad happens.
1742 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1747 rev_tree_cache_ = line;
1748 return !line.empty();
1752 void SVN::getLog(FileName const & tmpf)
1754 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1755 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1756 FileName(owner_->filePath()));
1760 bool SVN::prepareFileRevision(string const & revis, string & f)
1762 if (!isStrInt(revis))
1765 int rev = convert<int>(revis);
1767 if (!getFileRevisionInfo())
1770 rev = convert<int>(rev_file_cache_);
1771 // go back for minus rev
1773 rev = rev + convert<int>(rev_file_cache_);
1778 string revname = convert<string>(rev);
1779 TempFile tempfile("lyxvcrev_" + revname + '_');
1780 tempfile.setAutoRemove(false);
1781 FileName tmpf = tempfile.name();
1783 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1787 doVCCommand("svn cat -r " + revname + ' '
1788 + quoteName(onlyFileName(owner_->absFileName()))
1789 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1790 FileName(owner_->filePath()));
1792 if (tmpf.isFileEmpty())
1795 f = tmpf.absFileName();
1800 bool SVN::prepareFileRevisionEnabled()
1807 bool SVN::toggleReadOnlyEnabled()
1813 /////////////////////////////////////////////////////////////////////
1817 /////////////////////////////////////////////////////////////////////
1819 GIT::GIT(Buffer * b) : VCS(b)
1821 // Here we know that the buffer file is either already in GIT or
1822 // about to be registered
1827 bool GIT::findFile(FileName const & file)
1829 // First we check the existence of repository meta data.
1830 if (VCS::checkParentDirs(file, ".git").empty()) {
1831 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1835 // Now we check the status of the file.
1836 TempFile tempfile("lyxvcout");
1837 FileName tmpf = tempfile.name();
1839 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1843 string const fname = onlyFileName(file.absFileName());
1844 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1846 doVCCommandCall("git ls-files " +
1847 quoteName(fname) + " > " +
1848 quoteName(tmpf.toFilesystemEncoding()),
1851 bool found = !tmpf.isFileEmpty();
1852 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1857 void GIT::scanMaster()
1859 // vcstatus code is somewhat superflous,
1860 // until we want to implement read-only toggle for git.
1861 vcstatus_ = NOLOCKING;
1865 bool GIT::retrieve(FileName const & file)
1867 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1868 // The caller ensures that file does not exist, so no need to check that.
1869 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1870 file.onlyPath()) == 0;
1874 void GIT::registrer(string const & /*msg*/)
1876 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1877 FileName(owner_->filePath()));
1881 bool GIT::renameEnabled()
1887 string GIT::rename(support::FileName const & newFile, string const & msg)
1889 // git mv does not require a log message, since it does not commit.
1890 // In LyX we commit immediately afterwards, otherwise it could be
1891 // confusing to the user to have two uncommitted files.
1892 FileName path(owner_->filePath());
1893 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1894 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1895 ' ' + quoteName(relFile));
1896 if (doVCCommand(cmd, path)) {
1897 cmd = "git checkout -q " +
1898 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1900 doVCCommand(cmd, path);
1901 if (newFile.exists())
1902 newFile.removeFile();
1905 vector<support::FileName> f;
1906 f.push_back(owner_->fileName());
1907 f.push_back(newFile);
1909 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1910 cmd = "git checkout -q " +
1911 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1913 doVCCommand(cmd, path);
1914 if (newFile.exists())
1915 newFile.removeFile();
1922 bool GIT::copyEnabled()
1928 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1930 // git does not support copy with history preservation
1935 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1937 vector<support::FileName> f(1, owner_->fileName());
1938 return checkIn(f, msg, log);
1942 LyXVC::CommandResult
1943 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1945 TempFile tempfile("lyxvcout");
1946 FileName tmpf = tempfile.name();
1948 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1949 log = N_("Error: Could not generate logfile.");
1950 return LyXVC::ErrorBefore;
1954 os << "git commit -m \"" << msg << '"';
1955 for (size_t i = 0; i < f.size(); ++i)
1956 os << ' ' << quoteName(f[i].onlyFileName());
1957 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1958 LyXVC::CommandResult ret =
1959 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1960 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1962 string res = scanLogFile(tmpf, log);
1964 frontend::Alert::error(_("Revision control error."),
1965 _("Error when committing to repository.\n"
1966 "You have to manually resolve the problem.\n"
1967 "LyX will reopen the document after you press OK."));
1968 ret = LyXVC::ErrorCommand;
1972 log.insert(0, "GIT: ");
1973 if (ret == LyXVC::VCSuccess && log.empty())
1974 log = "GIT: Proceeded";
1979 bool GIT::checkInEnabled()
1985 bool GIT::isCheckInWithConfirmation()
1987 // FIXME one day common getDiff and perhaps OpMode for all backends
1989 TempFile tempfile("lyxvcout");
1990 FileName tmpf = tempfile.name();
1992 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1996 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
1997 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1998 FileName(owner_->filePath()));
2000 docstring diff = tmpf.fileContents("UTF-8");
2009 // FIXME Correctly return code should be checked instead of this.
2010 // This would need another solution than just plain startscript.
2011 // Hint from Andre': QProcess::readAllStandardError()...
2012 string GIT::scanLogFile(FileName const & f, string & status)
2014 ifstream ifs(f.toFilesystemEncoding().c_str());
2019 LYXERR(Debug::LYXVC, line << "\n");
2021 status += line + "; ";
2022 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2023 || contains(line, "Commit failed")) {
2033 string GIT::checkOut()
2039 bool GIT::checkOutEnabled()
2045 string GIT::repoUpdate()
2051 bool GIT::repoUpdateEnabled()
2057 string GIT::lockingToggle()
2063 bool GIT::lockingToggleEnabled()
2071 // Reverts to the version in GIT repository and
2072 // gets the updated version from the repository.
2073 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2075 if (doVCCommand("git checkout -q " + fil,
2076 FileName(owner_->filePath())))
2078 owner_->markClean();
2083 bool GIT::isRevertWithConfirmation()
2085 //FIXME owner && diff
2090 void GIT::undoLast()
2092 // merge the current with the previous version
2093 // in a reverse patch kind of way, so that the
2094 // result is to revert the last changes.
2095 lyxerr << "Sorry, not implemented." << endl;
2099 bool GIT::undoLastEnabled()
2105 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2107 if (info == LyXVC::Tree) {
2108 if (rev_tree_cache_.empty())
2109 if (!getTreeRevisionInfo())
2110 rev_tree_cache_ = "?";
2111 if (rev_tree_cache_ == "?")
2114 return rev_tree_cache_;
2117 // fill the rest of the attributes for a single file
2118 if (rev_file_cache_.empty())
2119 if (!getFileRevisionInfo()) {
2120 rev_file_cache_ = "?";
2121 rev_file_abbrev_cache_ = "?";
2126 if (rev_file_cache_ == "?")
2128 return rev_file_cache_;
2129 case LyXVC::FileAbbrev:
2130 if (rev_file_abbrev_cache_ == "?")
2132 return rev_file_abbrev_cache_;
2134 return rev_author_cache_;
2136 return rev_date_cache_;
2138 return rev_time_cache_;
2147 bool GIT::getFileRevisionInfo()
2149 TempFile tempfile("lyxvcout");
2150 FileName tmpf = tempfile.name();
2152 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2156 doVCCommand("git log -n 1 --pretty=format:%H%n%h%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2157 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2158 FileName(owner_->filePath()));
2163 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2166 getline(ifs, rev_file_cache_);
2168 getline(ifs, rev_file_abbrev_cache_);
2170 getline(ifs, rev_author_cache_);
2174 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2178 return !rev_file_cache_.empty();
2182 bool GIT::getTreeRevisionInfo()
2184 TempFile tempfile("lyxvcout");
2185 FileName tmpf = tempfile.name();
2187 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2191 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2192 FileName(owner_->filePath()),
2193 false); //git describe returns $?=128 when no tag found (but git repo still exists)
2198 // only first line in case something bad happens.
2199 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2200 getline(ifs, rev_tree_cache_);
2203 return !rev_tree_cache_.empty();
2207 void GIT::getLog(FileName const & tmpf)
2209 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2210 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2211 FileName(owner_->filePath()));
2215 //at this moment we don't accept revision SHA, but just number of revision steps back
2216 //GUI and infrastucture needs to be changed first
2217 bool GIT::prepareFileRevision(string const & revis, string & f)
2219 // anything positive means we got hash, not "0" or minus revision
2222 // hash is rarely number and should be long
2223 if (isStrInt(revis) && revis.length()<20)
2224 rev = convert<int>(revis);
2226 // revision and filename
2229 // go back for "minus" revisions
2231 pointer = "HEAD~" + convert<string>(-rev);
2238 TempFile tempfile("lyxvcrev_" + revis + '_');
2239 tempfile.setAutoRemove(false);
2240 FileName tmpf = tempfile.name();
2242 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2246 doVCCommand("git show " + pointer + "./"
2247 + quoteName(onlyFileName(owner_->absFileName()))
2248 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2249 FileName(owner_->filePath()));
2251 if (tmpf.isFileEmpty())
2254 f = tmpf.absFileName();
2259 bool GIT::prepareFileRevisionEnabled()
2265 bool GIT::toggleReadOnlyEnabled()