3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "VCBackend.h"
17 #include "FuncRequest.h"
19 #include "frontends/alert.h"
20 #include "frontends/Application.h"
22 #include "support/convert.h"
23 #include "support/debug.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/PathChanger.h"
28 #include "support/Systemcall.h"
29 #include "support/TempFile.h"
37 using namespace lyx::support;
43 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
45 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
47 support::PathChanger p(path);
48 return one.startscript(Systemcall::Wait, cmd, string(), string(), false);
52 int VCS::doVCCommand(string const & cmd, FileName const & path, bool reportError)
55 owner_->setBusy(true);
57 int const ret = doVCCommandCall(cmd, path);
60 owner_->setBusy(false);
61 if (ret && reportError) {
63 if (prefixIs(cmd, "ci "))
64 rcsmsg = "\n" + _("Perhaps the RCS package is not installed on your system?");
65 frontend::Alert::error(_("Revision control error."),
66 bformat(_("Some problem occurred while running the command:\n"
74 bool VCS::makeRCSRevision(string const &version, string &revis) const
79 int back = convert<int>(rev);
80 // if positive use as the last number in the whole revision string
83 rsplit(version, base , '.');
84 rev = base + '.' + rev;
88 // we care about the last number from revision string
89 // in case of backward indexing
92 cur = rsplit(version, base , '.');
95 int want = convert<int>(cur) + back;
99 rev = base + '.' + convert<string>(want);
108 FileName VCS::checkParentDirs(FileName const & start, std::string const & file)
110 static FileName empty;
111 FileName dirname = start.onlyPath();
113 FileName tocheck = FileName(addPathName(dirname.absFileName(), file));
114 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
115 if (tocheck.exists())
117 // this construct because of #8295
118 dirname = FileName(dirname.absFileName()).parentPath();
119 } while (!dirname.empty());
124 /////////////////////////////////////////////////////////////////////
128 /////////////////////////////////////////////////////////////////////
130 RCS::RCS(FileName const & m, Buffer * b) : VCS(b)
132 // Here we know that the buffer file is either already in RCS or
133 // about to be registered
139 FileName const RCS::findFile(FileName const & file)
141 // Check if *,v exists.
142 FileName tmp(file.absFileName() + ",v");
143 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
144 if (tmp.isReadableFile()) {
145 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
149 // Check if RCS/*,v exists.
150 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
151 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
152 if (tmp.isReadableFile()) {
153 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
161 bool RCS::retrieve(FileName const & file)
163 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
164 // The caller ensures that file does not exist, so no need to check that.
165 return doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
170 void RCS::scanMaster()
175 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
177 ifstream ifs(master_.toFilesystemEncoding().c_str());
178 // limit the size of strings we read to avoid memory problems
182 bool read_enough = false;
184 while (!read_enough && ifs >> token) {
185 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
190 else if (token == "head") {
194 tmv = rtrim(tmv, ";");
196 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
197 } else if (contains(token, "access")
198 || contains(token, "symbols")
199 || contains(token, "strict")) {
201 } else if (contains(token, "locks")) {
203 if (contains(token, ';')) {
204 locker_ = "Unlocked";
205 vcstatus_ = UNLOCKED;
213 s1 = rtrim(tmpt, ";");
214 // tmp is now in the format <user>:<version>
215 s1 = split(s1, s2, ':');
216 // s2 is user, and s1 is version
217 if (s1 == version_) {
222 } while (!contains(tmpt, ';'));
224 } else if (token == "comment") {
225 // we don't need to read any further than this.
229 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
235 void RCS::registrer(string const & msg)
237 string cmd = "ci -q -u -i -t-\"";
240 cmd += quoteName(onlyFileName(owner_->absFileName()));
241 doVCCommand(cmd, FileName(owner_->filePath()));
245 bool RCS::renameEnabled()
251 string RCS::rename(support::FileName const & /*newFile*/, string const & /*msg*/)
253 // not implemented, since a left-over file.lyx,v would be confusing.
258 bool RCS::copyEnabled()
264 string RCS::copy(support::FileName const & newFile, string const & msg)
266 // RCS has no real copy command, so we create a poor mans version
267 support::FileName const oldFile(owner_->absFileName());
268 if (!oldFile.copyTo(newFile))
270 FileName path(oldFile.onlyPath());
271 string relFile(to_utf8(newFile.relPath(path.absFileName())));
272 string cmd = "ci -q -u -i -t-\"";
275 cmd += quoteName(relFile);
276 return doVCCommand(cmd, path) ? string() : "RCS: Proceeded";
280 LyXVC::CommandResult RCS::checkIn(string const & msg, string & log)
282 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
283 + quoteName(onlyFileName(owner_->absFileName())),
284 FileName(owner_->filePath()));
286 return LyXVC::ErrorCommand;
287 log = "RCS: Proceeded";
288 return LyXVC::VCSuccess;
292 bool RCS::checkInEnabled()
294 return owner_ && !owner_->hasReadonlyFlag();
298 bool RCS::isCheckInWithConfirmation()
300 // FIXME one day common getDiff for all backends
302 // if (getDiff(file, diff) && diff.empty())
305 TempFile tempfile("lyxvcout");
306 FileName tmpf = tempfile.name();
308 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
312 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
313 + " > " + quoteName(tmpf.toFilesystemEncoding()),
314 FileName(owner_->filePath()));
316 docstring diff = tmpf.fileContents("UTF-8");
325 string RCS::checkOut()
328 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
329 FileName(owner_->filePath()));
330 return ret ? string() : "RCS: Proceeded";
334 bool RCS::checkOutEnabled()
336 return owner_ && owner_->hasReadonlyFlag();
340 string RCS::repoUpdate()
342 lyxerr << "Sorry, not implemented." << endl;
347 bool RCS::repoUpdateEnabled()
353 string RCS::lockingToggle()
355 //FIXME this might be actually possible, study rcs -U, rcs -L.
356 //State should be easy to get inside scanMaster.
357 //It would fix #4370 and make rcs/svn usage even more closer.
358 lyxerr << "Sorry, not implemented." << endl;
363 bool RCS::lockingToggleEnabled()
371 if (doVCCommand("co -f -u" + version_ + ' '
372 + quoteName(onlyFileName(owner_->absFileName())),
373 FileName(owner_->filePath())))
375 // We ignore changes and just reload!
381 bool RCS::isRevertWithConfirmation()
383 //FIXME owner && diff ?
390 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
391 doVCCommand("rcs -o" + version_ + ' '
392 + quoteName(onlyFileName(owner_->absFileName())),
393 FileName(owner_->filePath()));
397 bool RCS::undoLastEnabled()
399 return owner_->hasReadonlyFlag();
403 void RCS::getLog(FileName const & tmpf)
405 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
406 + " > " + quoteName(tmpf.toFilesystemEncoding()),
407 FileName(owner_->filePath()));
411 bool RCS::toggleReadOnlyEnabled()
413 // This got broken somewhere along lfuns dispatch reorganization.
414 // reloadBuffer would be needed after this, but thats problematic
415 // since we are inside Buffer::dispatch.
421 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
423 if (info == LyXVC::File)
425 // fill the rest of the attributes for a single file
426 if (rev_date_cache_.empty())
427 if (!getRevisionInfo())
432 return rev_author_cache_;
434 return rev_date_cache_;
436 return rev_time_cache_;
445 bool RCS::getRevisionInfo()
447 TempFile tempfile("lyxvcout");
448 FileName tmpf = tempfile.name();
450 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
453 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
454 + " > " + quoteName(tmpf.toFilesystemEncoding()),
455 FileName(owner_->filePath()));
460 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
463 // we reached to the entry, i.e. after initial log message
465 // line with critical info, e.g:
466 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
471 LYXERR(Debug::LYXVC, line);
472 if (entry && prefixIs(line, "date:")) {
476 if (prefixIs(line, "revision"))
482 rev_date_cache_ = token(result, ' ', 1);
483 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
484 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
486 return !rev_author_cache_.empty();
489 bool RCS::prepareFileRevision(string const &revis, string & f)
492 if (!VCS::makeRCSRevision(version_, rev))
495 TempFile tempfile("lyxvcrev_" + rev + '_');
496 tempfile.setAutoRemove(false);
497 FileName tmpf = tempfile.name();
499 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
503 doVCCommand("co -p" + rev + ' '
504 + quoteName(onlyFileName(owner_->absFileName()))
505 + " > " + quoteName(tmpf.toFilesystemEncoding()),
506 FileName(owner_->filePath()));
508 if (tmpf.isFileEmpty())
511 f = tmpf.absFileName();
516 bool RCS::prepareFileRevisionEnabled()
522 /////////////////////////////////////////////////////////////////////
526 /////////////////////////////////////////////////////////////////////
528 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
530 // Here we know that the buffer file is either already in CVS or
531 // about to be registered
533 have_rev_info_ = false;
538 FileName const CVS::findFile(FileName const & file)
540 // First we look for the CVS/Entries in the same dir
541 // where we have file.
542 // Note that it is not necessary to search parent directories, since
543 // there will be a CVS/Entries file in every subdirectory.
544 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
545 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
546 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
547 << "' for `" << tmpf << '\'');
548 if (entries.isReadableFile()) {
549 // Ok we are at least in a CVS dir. Parse the CVS/Entries
550 // and see if we can find this file. We do a fast and
552 ifstream ifs(entries.toFilesystemEncoding().c_str());
554 while (getline(ifs, line)) {
555 LYXERR(Debug::LYXVC, "\tEntries: " << line);
556 if (contains(line, tmpf))
564 void CVS::scanMaster()
566 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
567 // Ok now we do the real scan...
568 ifstream ifs(master_.toFilesystemEncoding().c_str());
569 string const name = onlyFileName(owner_->absFileName());
570 string const tmpf = '/' + name + '/';
571 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
573 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
574 while (getline(ifs, line)) {
575 LYXERR(Debug::LYXVC, "\t line: " << line);
576 if (contains(line, tmpf)) {
577 // Ok extract the fields.
579 if (!regex_match(line, sm, reg)) {
580 LYXERR(Debug::LYXVC, "\t Cannot parse line. Skipping.");
584 //sm[0]; // whole matched string
586 version_ = sm.str(2);
587 string const file_date = sm.str(3);
590 //sm[5]; // tag or tagdate
591 FileName file(owner_->absFileName());
592 if (file.isReadableFile()) {
593 time_t const mod = file.lastModified();
594 string const mod_date = rtrim(asctime(gmtime(&mod)), "\n");
595 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
596 << "'\nModification date of file: `" << mod_date << '\'');
597 if (file.isReadOnly()) {
598 // readonly checkout is unlocked
599 vcstatus_ = UNLOCKED;
601 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
602 FileName base(addName(bdir.absFileName(),name));
603 // if base version is existent "cvs edit" was used to lock
604 vcstatus_ = base.isReadableFile() ? LOCKED : NOLOCKING;
607 vcstatus_ = NOLOCKING;
615 bool CVS::retrieve(FileName const & file)
617 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
618 // The caller ensures that file does not exist, so no need to check that.
619 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
620 file.onlyPath()) == 0;
624 string const CVS::getTarget(OperationMode opmode) const
628 // in client server mode CVS does not like full path operand for directory operation
629 // since LyX switches to the repo dir "." is good enough as target
632 return quoteName(onlyFileName(owner_->absFileName()));
638 docstring CVS::toString(CvsStatus status) const
642 return _("Up-to-date");
643 case LocallyModified:
644 return _("Locally Modified");
646 return _("Locally Added");
648 return _("Needs Merge");
650 return _("Needs Checkout");
652 return _("No CVS file");
654 return _("Cannot retrieve CVS status");
660 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
661 FileName const & output, bool reportError)
663 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
664 return doVCCommand(cmd + redirection, path, reportError);
668 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
669 support::FileName const & path,
670 support::FileName const & output)
672 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
673 return doVCCommandCall(cmd + redirection, path);
677 CVS::CvsStatus CVS::getStatus()
679 TempFile tempfile("lyxvout");
680 FileName tmpf = tempfile.name();
682 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
686 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
687 FileName(owner_->filePath()), tmpf)) {
691 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
692 CvsStatus status = NoCvsFile;
697 LYXERR(Debug::LYXVC, line << '\n');
698 if (prefixIs(line, "File:")) {
699 if (contains(line, "Up-to-date"))
701 else if (contains(line, "Locally Modified"))
702 status = LocallyModified;
703 else if (contains(line, "Locally Added"))
704 status = LocallyAdded;
705 else if (contains(line, "Needs Merge"))
707 else if (contains(line, "Needs Checkout"))
708 status = NeedsCheckout;
714 void CVS::getRevisionInfo()
718 have_rev_info_ = true;
719 TempFile tempfile("lyxvout");
720 FileName tmpf = tempfile.name();
722 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
726 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
727 + ' ' + getTarget(File),
728 FileName(owner_->filePath()), tmpf);
730 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
734 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
735 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
740 LYXERR(Debug::LYXVC, line << '\n');
741 if (prefixIs(line, "date:")) {
743 if (regex_match(line, sm, reg)) {
744 //sm[0]; // whole matched string
745 rev_date_cache_ = sm[1];
746 rev_time_cache_ = sm[2];
747 //sm[3]; // GMT offset
748 rev_author_cache_ = sm[4];
750 LYXERR(Debug::LYXVC, "\tCannot parse line. Skipping.");
754 if (rev_author_cache_.empty())
756 "Could not retrieve revision info for " << version_ <<
757 " of " << getTarget(File));
761 void CVS::registrer(string const & msg)
763 doVCCommand("cvs -q add -m \"" + msg + "\" "
765 FileName(owner_->filePath()));
769 bool CVS::renameEnabled()
775 string CVS::rename(support::FileName const & newFile, string const & msg)
777 // CVS has no real rename command, so we create a poor mans version
778 support::FileName const oldFile(owner_->absFileName());
779 string ret = copy(newFile, msg);
782 string cmd = "cvs -q remove -m \"" + msg + "\" " +
783 quoteName(oldFile.onlyFileName());
784 FileName path(oldFile.onlyPath());
785 return doVCCommand(cmd, path) ? string() : ret;
789 bool CVS::copyEnabled()
795 string CVS::copy(support::FileName const & newFile, string const & msg)
797 // CVS has no real copy command, so we create a poor mans version
798 support::FileName const oldFile(owner_->absFileName());
799 if (!oldFile.copyTo(newFile))
801 FileName path(oldFile.onlyPath());
802 string relFile(to_utf8(newFile.relPath(path.absFileName())));
803 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
804 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
808 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
810 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
811 FileName(owner_->filePath()), tmpf, false);
818 return doVCCommand("cvs -q edit " + getTarget(File),
819 FileName(owner_->filePath()));
825 vcstatus_ = UNLOCKED;
826 return doVCCommand("cvs -q unedit " + getTarget(File),
827 FileName(owner_->filePath()));
831 int CVS::update(OperationMode opmode, FileName const & tmpf)
833 return doVCCommandWithOutput("cvs -q update "
835 FileName(owner_->filePath()), tmpf, false);
839 string CVS::scanLogFile(FileName const & f, string & status)
841 ifstream ifs(f.toFilesystemEncoding().c_str());
846 LYXERR(Debug::LYXVC, line << '\n');
848 status += line + "; ";
849 if (prefixIs(line, "C ")) {
859 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
861 CvsStatus status = getStatus();
864 if (vcstatus_ != NOLOCKING)
866 return LyXVC::ErrorCommand;
867 log = "CVS: Proceeded";
868 return LyXVC::VCSuccess;
869 case LocallyModified:
871 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
873 FileName(owner_->filePath()));
875 return LyXVC::ErrorCommand;
876 log = "CVS: Proceeded";
877 return LyXVC::VCSuccess;
881 frontend::Alert::error(_("Revision control error."),
882 _("The repository version is newer then the current check out.\n"
883 "You have to update from repository first or revert your changes.")) ;
886 frontend::Alert::error(_("Revision control error."),
887 bformat(_("Bad status when checking in changes.\n"
892 return LyXVC::ErrorBefore;
896 bool CVS::isLocked() const
898 FileName fn(owner_->absFileName());
900 return !fn.isReadOnly();
904 bool CVS::checkInEnabled()
906 if (vcstatus_ != NOLOCKING)
913 bool CVS::isCheckInWithConfirmation()
915 CvsStatus status = getStatus();
916 return status == LocallyModified || status == LocallyAdded;
920 string CVS::checkOut()
922 if (vcstatus_ != NOLOCKING && edit())
924 TempFile tempfile("lyxvout");
925 FileName tmpf = tempfile.name();
927 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
931 int rc = update(File, tmpf);
933 string const res = scanLogFile(tmpf, log);
935 frontend::Alert::error(_("Revision control error."),
936 bformat(_("Error when updating from repository.\n"
937 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
938 "After pressing OK, LyX will try to reopen the resolved document."),
939 from_local8bit(res)));
943 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
947 bool CVS::checkOutEnabled()
949 if (vcstatus_ != NOLOCKING)
956 string CVS::repoUpdate()
958 TempFile tempfile("lyxvout");
959 FileName tmpf = tempfile.name();
961 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
965 getDiff(Directory, tmpf);
966 docstring res = tmpf.fileContents("UTF-8");
968 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
969 docstring const file = from_utf8(owner_->filePath());
970 docstring text = bformat(_("There were detected changes "
971 "in the working directory:\n%1$s\n\n"
972 "Possible file conflicts must be then resolved manually "
973 "or you will need to revert back to the repository version."), file);
974 int ret = frontend::Alert::prompt(_("Changes detected"),
975 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
977 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
978 ret = frontend::Alert::prompt(_("Changes detected"),
979 text, 0, 1, _("&Continue"), _("&Abort"));
980 hideDialogs("file", nullptr);
986 int rc = update(Directory, tmpf);
987 res += "Update log:\n" + tmpf.fileContents("UTF-8");
988 LYXERR(Debug::LYXVC, res);
991 string sres = scanLogFile(tmpf, log);
993 docstring const file = owner_->fileName().displayName(20);
994 frontend::Alert::error(_("Revision control error."),
995 bformat(_("Error when updating document %1$s from repository.\n"
996 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
997 "After pressing OK, LyX will try to reopen the resolved document."),
998 file, from_local8bit(sres)));
1002 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
1006 bool CVS::repoUpdateEnabled()
1012 string CVS::lockingToggle()
1014 lyxerr << "Sorry, not implemented." << endl;
1019 bool CVS::lockingToggleEnabled()
1025 bool CVS::isRevertWithConfirmation()
1027 CvsStatus status = getStatus();
1028 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1034 // Reverts to the version in CVS repository and
1035 // gets the updated version from the repository.
1036 CvsStatus status = getStatus();
1039 if (vcstatus_ != NOLOCKING)
1040 return 0 == unedit();
1044 case LocallyModified: {
1045 FileName f(owner_->absFileName());
1047 update(File, FileName());
1048 owner_->markClean();
1051 case LocallyAdded: {
1052 docstring const file = owner_->fileName().displayName(20);
1053 frontend::Alert::error(_("Revision control error."),
1054 bformat(_("The document %1$s is not in repository.\n"
1055 "You have to check in the first revision before you can revert."),
1060 docstring const file = owner_->fileName().displayName(20);
1061 frontend::Alert::error(_("Revision control error."),
1062 bformat(_("Cannot revert document %1$s to repository version.\n"
1063 "The status '%2$s' is unexpected."),
1064 file, toString(status)));
1072 void CVS::undoLast()
1074 // merge the current with the previous version
1075 // in a reverse patch kind of way, so that the
1076 // result is to revert the last changes.
1077 lyxerr << "Sorry, not implemented." << endl;
1081 bool CVS::undoLastEnabled()
1087 void CVS::getLog(FileName const & tmpf)
1089 doVCCommandWithOutput("cvs log " + getTarget(File),
1090 FileName(owner_->filePath()),
1095 bool CVS::toggleReadOnlyEnabled()
1101 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1103 if (!version_.empty()) {
1109 return rev_author_cache_;
1111 return rev_date_cache_;
1113 return rev_time_cache_;
1122 bool CVS::prepareFileRevision(string const & revis, string & f)
1125 if (!VCS::makeRCSRevision(version_, rev))
1128 TempFile tempfile("lyxvcrev_" + rev + '_');
1129 tempfile.setAutoRemove(false);
1130 FileName tmpf = tempfile.name();
1132 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1136 doVCCommandWithOutput("cvs update -p -r" + rev + ' '
1138 FileName(owner_->filePath()), tmpf);
1140 if (tmpf.isFileEmpty())
1143 f = tmpf.absFileName();
1148 bool CVS::prepareFileRevisionEnabled()
1154 /////////////////////////////////////////////////////////////////////
1158 /////////////////////////////////////////////////////////////////////
1160 SVN::SVN(Buffer * b) : VCS(b)
1162 // Here we know that the buffer file is either already in SVN or
1163 // about to be registered
1164 locked_mode_ = false;
1169 bool SVN::findFile(FileName const & file)
1171 // First we check the existence of repository meta data.
1172 if (VCS::checkParentDirs(file, ".svn").empty()) {
1173 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1177 // Now we check the status of the file.
1178 string const fname = onlyFileName(file.absFileName());
1179 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1180 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname),
1182 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1187 void SVN::scanMaster()
1189 // vcstatus code is somewhat superflous,
1190 // until we want to implement read-only toggle for svn.
1191 vcstatus_ = NOLOCKING;
1192 if (checkLockMode()) {
1196 vcstatus_ = UNLOCKED;
1201 bool SVN::checkLockMode()
1203 TempFile tempfile("lyxvcout");
1204 FileName tmpf = tempfile.name();
1206 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1210 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1211 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1212 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1213 FileName(owner_->filePath())))
1216 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1220 while (ifs && !ret) {
1222 LYXERR(Debug::LYXVC, line);
1223 if (contains(line, "svn:needs-lock"))
1226 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1234 bool SVN::isLocked() const
1236 FileName file(owner_->absFileName());
1238 return !file.isReadOnly();
1242 bool SVN::retrieve(FileName const & file)
1244 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1245 // The caller ensures that file does not exist, so no need to check that.
1246 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1247 file.onlyPath()) == 0;
1251 void SVN::registrer(string const & /*msg*/)
1253 doVCCommand("svn add -q --parents " + quoteName(onlyFileName(owner_->absFileName())),
1254 FileName(owner_->filePath()));
1258 bool SVN::renameEnabled()
1264 string SVN::rename(support::FileName const & newFile, string const & msg)
1266 // svn move does not require a log message, since it does not commit.
1267 // In LyX we commit immediately afterwards, otherwise it could be
1268 // confusing to the user to have two uncommitted files.
1269 FileName path(owner_->filePath());
1270 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1271 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1272 ' ' + quoteName(relFile));
1273 if (doVCCommand(cmd, path)) {
1274 cmd = "svn revert -q " +
1275 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1277 doVCCommand(cmd, path);
1278 if (newFile.exists())
1279 newFile.removeFile();
1282 vector<support::FileName> f;
1283 f.push_back(owner_->fileName());
1284 f.push_back(newFile);
1286 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1287 cmd = "svn revert -q " +
1288 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1290 doVCCommand(cmd, path);
1291 if (newFile.exists())
1292 newFile.removeFile();
1299 bool SVN::copyEnabled()
1305 string SVN::copy(support::FileName const & newFile, string const & msg)
1307 // svn copy does not require a log message, since it does not commit.
1308 // In LyX we commit immediately afterwards, otherwise it could be
1309 // confusing to the user to have an uncommitted file.
1310 FileName path(owner_->filePath());
1311 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1312 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1313 ' ' + quoteName(relFile));
1314 if (doVCCommand(cmd, path))
1316 vector<support::FileName> f(1, newFile);
1318 if (checkIn(f, msg, log) == LyXVC::VCSuccess)
1324 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1326 vector<support::FileName> f(1, owner_->fileName());
1327 return checkIn(f, msg, log);
1331 LyXVC::CommandResult
1332 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1334 TempFile tempfile("lyxvcout");
1335 FileName tmpf = tempfile.name();
1337 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1338 log = N_("Error: Could not generate logfile.");
1339 return LyXVC::ErrorBefore;
1343 os << "svn commit -m \"" << msg << '"';
1344 for (size_t i = 0; i < f.size(); ++i)
1345 os << ' ' << quoteName(f[i].onlyFileName());
1346 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1347 LyXVC::CommandResult ret =
1348 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1349 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1351 string res = scanLogFile(tmpf, log);
1353 frontend::Alert::error(_("Revision control error."),
1354 _("Error when committing to repository.\n"
1355 "You have to manually resolve the problem.\n"
1356 "LyX will reopen the document after you press OK."));
1357 ret = LyXVC::ErrorCommand;
1360 if (!fileLock(false, tmpf, log))
1361 ret = LyXVC::ErrorCommand;
1364 log.insert(0, "SVN: ");
1365 if (ret == LyXVC::VCSuccess && log.empty())
1366 log = "SVN: Proceeded";
1371 bool SVN::checkInEnabled()
1380 bool SVN::isCheckInWithConfirmation()
1382 // FIXME one day common getDiff and perhaps OpMode for all backends
1384 TempFile tempfile("lyxvcout");
1385 FileName tmpf = tempfile.name();
1387 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1391 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1392 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1393 FileName(owner_->filePath()));
1395 docstring diff = tmpf.fileContents("UTF-8");
1404 // FIXME Correctly return code should be checked instead of this.
1405 // This would need another solution than just plain startscript.
1406 // Hint from Andre': QProcess::readAllStandardError()...
1407 string SVN::scanLogFile(FileName const & f, string & status)
1409 ifstream ifs(f.toFilesystemEncoding().c_str());
1414 LYXERR(Debug::LYXVC, line << '\n');
1416 status += line + "; ";
1417 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1418 || contains(line, "Commit failed")) {
1422 if (contains(line, "svn:needs-lock")) {
1432 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1434 if (!locked_mode_ || (isLocked() == lock))
1437 string const arg = lock ? "lock " : "unlock ";
1438 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1439 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1440 FileName(owner_->filePath()));
1442 // Lock error messages go unfortunately on stderr and are unreachable this way.
1443 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1447 if (!line.empty()) status += line + "; ";
1451 if (isLocked() == lock)
1455 frontend::Alert::error(_("Revision control error."),
1456 _("Error while acquiring write lock.\n"
1457 "Another user is most probably editing\n"
1458 "the current document now!\n"
1459 "Also check the access to the repository."));
1461 frontend::Alert::error(_("Revision control error."),
1462 _("Error while releasing write lock.\n"
1463 "Check the access to the repository."));
1468 string SVN::checkOut()
1470 TempFile tempfile("lyxvcout");
1471 FileName tmpf = tempfile.name();
1473 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1474 return N_("Error: Could not generate logfile.");
1477 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1478 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1479 FileName(owner_->filePath()));
1482 string const res = scanLogFile(tmpf, log);
1484 frontend::Alert::error(_("Revision control error."),
1485 bformat(_("Error when updating from repository.\n"
1486 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1487 "After pressing OK, LyX will try to reopen the resolved document."),
1488 from_local8bit(res)));
1490 fileLock(true, tmpf, log);
1492 return log.empty() ? string() : "SVN: " + log;
1496 bool SVN::checkOutEnabled()
1505 string SVN::repoUpdate()
1507 TempFile tempfile("lyxvcout");
1508 FileName tmpf = tempfile.name();
1510 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1511 return N_("Error: Could not generate logfile.");
1514 doVCCommand("svn diff " + quoteName(owner_->filePath())
1515 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1516 FileName(owner_->filePath()));
1517 docstring res = tmpf.fileContents("UTF-8");
1519 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1520 docstring const file = from_utf8(owner_->filePath());
1521 docstring text = bformat(_("There were detected changes "
1522 "in the working directory:\n%1$s\n\n"
1523 "In case of file conflict version of the local directory files "
1524 "will be preferred."
1525 "\n\nContinue?"), file);
1526 int ret = frontend::Alert::prompt(_("Changes detected"),
1527 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1529 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1530 ret = frontend::Alert::prompt(_("Changes detected"),
1531 text, 0, 1, _("&Yes"), _("&No"));
1532 hideDialogs("file", nullptr);
1538 // Reverting looks too harsh, see bug #6255.
1539 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1540 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1541 // FileName(owner_->filePath()));
1542 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1543 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1544 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1545 FileName(owner_->filePath()));
1546 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1548 LYXERR(Debug::LYXVC, res);
1549 return to_utf8(res);
1553 bool SVN::repoUpdateEnabled()
1559 string SVN::lockingToggle()
1561 TempFile tempfile("lyxvcout");
1562 FileName tmpf = tempfile.name();
1564 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1565 return N_("Error: Could not generate logfile.");
1568 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1569 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1570 FileName(owner_->filePath()));
1575 string res = scanLogFile(tmpf, log);
1576 bool locking = contains(res, "svn:needs-lock");
1578 ret = doVCCommand("svn propset svn:needs-lock ON "
1579 + quoteName(onlyFileName(owner_->absFileName()))
1580 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1581 FileName(owner_->filePath()));
1583 ret = doVCCommand("svn propdel svn:needs-lock "
1584 + quoteName(onlyFileName(owner_->absFileName()))
1585 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1586 FileName(owner_->filePath()));
1590 frontend::Alert::warning(_("SVN File Locking"),
1591 (locking ? _("Locking property unset.") : _("Locking property set.")) + '\n'
1592 + _("Do not forget to commit the locking property into the repository."),
1595 return string("SVN: ") + (locking ?
1596 N_("Locking property unset.") : N_("Locking property set."));
1600 bool SVN::lockingToggleEnabled()
1608 // Reverts to the version in SVN repository and
1609 // gets the updated version from the repository.
1610 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1612 if (doVCCommand("svn revert -q " + fil,
1613 FileName(owner_->filePath())))
1615 owner_->markClean();
1620 bool SVN::isRevertWithConfirmation()
1622 //FIXME owner && diff
1627 void SVN::undoLast()
1629 // merge the current with the previous version
1630 // in a reverse patch kind of way, so that the
1631 // result is to revert the last changes.
1632 lyxerr << "Sorry, not implemented." << endl;
1636 bool SVN::undoLastEnabled()
1642 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1644 if (info == LyXVC::Tree) {
1645 if (rev_tree_cache_.empty())
1646 if (!getTreeRevisionInfo())
1647 rev_tree_cache_ = "?";
1648 if (rev_tree_cache_ == "?")
1651 return rev_tree_cache_;
1654 // fill the rest of the attributes for a single file
1655 if (rev_file_cache_.empty())
1656 if (!getFileRevisionInfo())
1657 rev_file_cache_ = "?";
1661 if (rev_file_cache_ == "?")
1663 return rev_file_cache_;
1665 return rev_author_cache_;
1667 return rev_date_cache_;
1669 return rev_time_cache_;
1678 bool SVN::getFileRevisionInfo()
1680 TempFile tempfile("lyxvcout");
1681 FileName tmpf = tempfile.name();
1683 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1687 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1688 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1689 FileName(owner_->filePath()));
1694 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1702 LYXERR(Debug::LYXVC, line);
1703 if (prefixIs(line, "<commit"))
1705 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1706 string l1 = subst(line, "revision=\"", "");
1707 string l2 = trim(subst(l1, "\">", ""));
1709 rev_file_cache_ = rev = l2;
1711 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1712 string l1 = subst(line, "<author>", "");
1713 string l2 = subst(l1, "</author>", "");
1714 rev_author_cache_ = l2;
1716 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1717 string l1 = subst(line, "<date>", "");
1718 string l2 = subst(l1, "</date>", "");
1719 l2 = split(l2, l1, 'T');
1720 rev_date_cache_ = l1;
1721 l2 = split(l2, l1, '.');
1722 rev_time_cache_ = l1;
1727 return !rev.empty();
1731 bool SVN::getTreeRevisionInfo()
1733 TempFile tempfile("lyxvcout");
1734 FileName tmpf = tempfile.name();
1736 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1740 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1741 FileName(owner_->filePath()));
1746 // only first line in case something bad happens.
1747 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1752 rev_tree_cache_ = line;
1753 return !line.empty();
1757 void SVN::getLog(FileName const & tmpf)
1759 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1760 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1761 FileName(owner_->filePath()));
1765 bool SVN::prepareFileRevision(string const & revis, string & f)
1767 if (!isStrInt(revis))
1770 int rev = convert<int>(revis);
1772 if (!getFileRevisionInfo())
1775 rev = convert<int>(rev_file_cache_);
1776 // go back for minus rev
1778 rev = rev + convert<int>(rev_file_cache_);
1783 string revname = convert<string>(rev);
1784 TempFile tempfile("lyxvcrev_" + revname + '_');
1785 tempfile.setAutoRemove(false);
1786 FileName tmpf = tempfile.name();
1788 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1792 doVCCommand("svn cat -r " + revname + ' '
1793 + quoteName(onlyFileName(owner_->absFileName()))
1794 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1795 FileName(owner_->filePath()));
1797 if (tmpf.isFileEmpty())
1800 f = tmpf.absFileName();
1805 bool SVN::prepareFileRevisionEnabled()
1812 bool SVN::toggleReadOnlyEnabled()
1818 /////////////////////////////////////////////////////////////////////
1822 /////////////////////////////////////////////////////////////////////
1824 GIT::GIT(Buffer * b) : VCS(b)
1826 // Here we know that the buffer file is either already in GIT or
1827 // about to be registered
1832 bool GIT::findFile(FileName const & file)
1834 // First we check the existence of repository meta data.
1835 if (VCS::checkParentDirs(file, ".git").empty()) {
1836 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1840 // Now we check the status of the file.
1841 TempFile tempfile("lyxvcout");
1842 FileName tmpf = tempfile.name();
1844 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1848 string const fname = onlyFileName(file.absFileName());
1849 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1851 doVCCommandCall("git ls-files " +
1852 quoteName(fname) + " > " +
1853 quoteName(tmpf.toFilesystemEncoding()),
1856 bool found = !tmpf.isFileEmpty();
1857 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1862 void GIT::scanMaster()
1864 // vcstatus code is somewhat superflous,
1865 // until we want to implement read-only toggle for git.
1866 vcstatus_ = NOLOCKING;
1870 bool GIT::retrieve(FileName const & file)
1872 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1873 // The caller ensures that file does not exist, so no need to check that.
1874 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1875 file.onlyPath()) == 0;
1879 void GIT::registrer(string const & /*msg*/)
1881 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1882 FileName(owner_->filePath()));
1886 bool GIT::renameEnabled()
1892 string GIT::rename(support::FileName const & newFile, string const & msg)
1894 // git mv does not require a log message, since it does not commit.
1895 // In LyX we commit immediately afterwards, otherwise it could be
1896 // confusing to the user to have two uncommitted files.
1897 FileName path(owner_->filePath());
1898 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1899 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1900 ' ' + quoteName(relFile));
1901 if (doVCCommand(cmd, path)) {
1902 cmd = "git checkout -q " +
1903 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1905 doVCCommand(cmd, path);
1906 if (newFile.exists())
1907 newFile.removeFile();
1910 vector<support::FileName> f;
1911 f.push_back(owner_->fileName());
1912 f.push_back(newFile);
1914 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1915 cmd = "git checkout -q " +
1916 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1918 doVCCommand(cmd, path);
1919 if (newFile.exists())
1920 newFile.removeFile();
1927 bool GIT::copyEnabled()
1933 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1935 // git does not support copy with history preservation
1940 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1942 vector<support::FileName> f(1, owner_->fileName());
1943 return checkIn(f, msg, log);
1947 LyXVC::CommandResult
1948 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1950 TempFile tempfile("lyxvcout");
1951 FileName tmpf = tempfile.name();
1953 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1954 log = N_("Error: Could not generate logfile.");
1955 return LyXVC::ErrorBefore;
1959 os << "git commit -m \"" << msg << '"';
1960 for (size_t i = 0; i < f.size(); ++i)
1961 os << ' ' << quoteName(f[i].onlyFileName());
1962 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1963 LyXVC::CommandResult ret =
1964 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1965 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1967 string res = scanLogFile(tmpf, log);
1969 frontend::Alert::error(_("Revision control error."),
1970 _("Error when committing to repository.\n"
1971 "You have to manually resolve the problem.\n"
1972 "LyX will reopen the document after you press OK."));
1973 ret = LyXVC::ErrorCommand;
1977 log.insert(0, "GIT: ");
1978 if (ret == LyXVC::VCSuccess && log.empty())
1979 log = "GIT: Proceeded";
1984 bool GIT::checkInEnabled()
1990 bool GIT::isCheckInWithConfirmation()
1992 // FIXME one day common getDiff and perhaps OpMode for all backends
1994 TempFile tempfile("lyxvcout");
1995 FileName tmpf = tempfile.name();
1997 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2001 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
2002 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2003 FileName(owner_->filePath()));
2005 docstring diff = tmpf.fileContents("UTF-8");
2014 // FIXME Correctly return code should be checked instead of this.
2015 // This would need another solution than just plain startscript.
2016 // Hint from Andre': QProcess::readAllStandardError()...
2017 string GIT::scanLogFile(FileName const & f, string & status)
2019 ifstream ifs(f.toFilesystemEncoding().c_str());
2024 LYXERR(Debug::LYXVC, line << "\n");
2026 status += line + "; ";
2027 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2028 || contains(line, "Commit failed")) {
2038 string GIT::checkOut()
2044 bool GIT::checkOutEnabled()
2050 string GIT::repoUpdate()
2056 bool GIT::repoUpdateEnabled()
2062 string GIT::lockingToggle()
2068 bool GIT::lockingToggleEnabled()
2076 // Reverts to the version in GIT repository and
2077 // gets the updated version from the repository.
2078 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2080 if (doVCCommand("git checkout -q " + fil,
2081 FileName(owner_->filePath())))
2083 owner_->markClean();
2088 bool GIT::isRevertWithConfirmation()
2090 //FIXME owner && diff
2095 void GIT::undoLast()
2097 // merge the current with the previous version
2098 // in a reverse patch kind of way, so that the
2099 // result is to revert the last changes.
2100 lyxerr << "Sorry, not implemented." << endl;
2104 bool GIT::undoLastEnabled()
2110 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2112 if (info == LyXVC::Tree) {
2113 if (rev_tree_cache_.empty())
2114 if (!getTreeRevisionInfo())
2115 rev_tree_cache_ = "?";
2116 if (rev_tree_cache_ == "?")
2119 return rev_tree_cache_;
2122 // fill the rest of the attributes for a single file
2123 if (rev_file_cache_.empty())
2124 if (!getFileRevisionInfo()) {
2125 rev_file_cache_ = "?";
2126 rev_file_abbrev_cache_ = "?";
2131 if (rev_file_cache_ == "?")
2133 return rev_file_cache_;
2134 case LyXVC::FileAbbrev:
2135 if (rev_file_abbrev_cache_ == "?")
2137 return rev_file_abbrev_cache_;
2139 return rev_author_cache_;
2141 return rev_date_cache_;
2143 return rev_time_cache_;
2152 bool GIT::getFileRevisionInfo()
2154 TempFile tempfile("lyxvcout");
2155 FileName tmpf = tempfile.name();
2157 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2161 doVCCommand("git log -n 1 --pretty=format:%H%n%h%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2162 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2163 FileName(owner_->filePath()));
2168 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2171 getline(ifs, rev_file_cache_);
2173 getline(ifs, rev_file_abbrev_cache_);
2175 getline(ifs, rev_author_cache_);
2179 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2183 return !rev_file_cache_.empty();
2187 bool GIT::getTreeRevisionInfo()
2189 TempFile tempfile("lyxvcout");
2190 FileName tmpf = tempfile.name();
2192 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2196 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2197 FileName(owner_->filePath()),
2198 false); //git describe returns $?=128 when no tag found (but git repo still exists)
2203 // only first line in case something bad happens.
2204 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2205 getline(ifs, rev_tree_cache_);
2208 return !rev_tree_cache_.empty();
2212 void GIT::getLog(FileName const & tmpf)
2214 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2215 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2216 FileName(owner_->filePath()));
2220 //at this moment we don't accept revision SHA, but just number of revision steps back
2221 //GUI and infrastucture needs to be changed first
2222 bool GIT::prepareFileRevision(string const & revis, string & f)
2224 // anything positive means we got hash, not "0" or minus revision
2227 // hash is rarely number and should be long
2228 if (isStrInt(revis) && revis.length()<20)
2229 rev = convert<int>(revis);
2231 // revision and filename
2234 // go back for "minus" revisions
2236 pointer = "HEAD~" + convert<string>(-rev);
2243 TempFile tempfile("lyxvcrev_" + revis + '_');
2244 tempfile.setAutoRemove(false);
2245 FileName tmpf = tempfile.name();
2247 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2251 doVCCommand("git show " + pointer + "./"
2252 + quoteName(onlyFileName(owner_->absFileName()))
2253 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2254 FileName(owner_->filePath()));
2256 if (tmpf.isFileEmpty())
2259 f = tmpf.absFileName();
2264 bool GIT::prepareFileRevisionEnabled()
2270 bool GIT::toggleReadOnlyEnabled()