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"
16 #include "DispatchResult.h"
18 #include "FuncRequest.h"
20 #include "frontends/alert.h"
21 #include "frontends/Application.h"
23 #include "support/convert.h"
24 #include "support/debug.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28 #include "support/PathChanger.h"
29 #include "support/Systemcall.h"
30 #include "support/regex.h"
31 #include "support/TempFile.h"
38 using namespace lyx::support;
44 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
46 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
48 support::PathChanger p(path);
49 return one.startscript(Systemcall::Wait, cmd, string(), string(), false);
53 int VCS::doVCCommand(string const & cmd, FileName const & path, bool reportError)
56 owner_->setBusy(true);
58 int const ret = doVCCommandCall(cmd, path);
61 owner_->setBusy(false);
62 if (ret && reportError)
63 frontend::Alert::error(_("Revision control error."),
64 bformat(_("Some problem occurred while running the command:\n"
71 bool VCS::makeRCSRevision(string const &version, string &revis) const
76 int back = convert<int>(rev);
77 // if positive use as the last number in the whole revision string
80 rsplit(version, base , '.');
81 rev = base + '.' + rev;
85 // we care about the last number from revision string
86 // in case of backward indexing
89 cur = rsplit(version, base , '.');
92 int want = convert<int>(cur) + back;
96 rev = base + '.' + convert<string>(want);
105 bool VCS::checkparentdirs(FileName const & file, std::string const & vcsdir)
107 FileName dirname = file.onlyPath();
109 FileName tocheck = FileName(addName(dirname.absFileName(), vcsdir));
110 LYXERR(Debug::LYXVC, "check file: " << tocheck.absFileName());
111 if (tocheck.exists())
113 //this construct because of #8295
114 dirname = FileName(dirname.absFileName()).parentPath();
115 } while (!dirname.empty());
120 /////////////////////////////////////////////////////////////////////
124 /////////////////////////////////////////////////////////////////////
126 RCS::RCS(FileName const & m, Buffer * b) : VCS(b)
128 // Here we know that the buffer file is either already in RCS or
129 // about to be registered
135 FileName const RCS::findFile(FileName const & file)
137 // Check if *,v exists.
138 FileName tmp(file.absFileName() + ",v");
139 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
140 if (tmp.isReadableFile()) {
141 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
145 // Check if RCS/*,v exists.
146 tmp = FileName(addName(addPath(onlyPath(file.absFileName()), "RCS"), file.absFileName()) + ",v");
147 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
148 if (tmp.isReadableFile()) {
149 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
157 bool RCS::retrieve(FileName const & file)
159 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
160 // The caller ensures that file does not exist, so no need to check that.
161 return doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
166 void RCS::scanMaster()
171 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
173 ifstream ifs(master_.toFilesystemEncoding().c_str());
174 // limit the size of strings we read to avoid memory problems
178 bool read_enough = false;
180 while (!read_enough && ifs >> token) {
181 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
186 else if (token == "head") {
190 tmv = rtrim(tmv, ";");
192 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
193 } else if (contains(token, "access")
194 || contains(token, "symbols")
195 || contains(token, "strict")) {
197 } else if (contains(token, "locks")) {
199 if (contains(token, ';')) {
200 locker_ = "Unlocked";
209 s1 = rtrim(tmpt, ";");
210 // tmp is now in the format <user>:<version>
211 s1 = split(s1, s2, ':');
212 // s2 is user, and s1 is version
213 if (s1 == version_) {
218 } while (!contains(tmpt, ';'));
220 } else if (token == "comment") {
221 // we don't need to read any further than this.
225 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
231 void RCS::registrer(string const & msg)
233 string cmd = "ci -q -u -i -t-\"";
236 cmd += quoteName(onlyFileName(owner_->absFileName()));
237 doVCCommand(cmd, FileName(owner_->filePath()));
241 bool RCS::renameEnabled()
247 string RCS::rename(support::FileName const & /*newFile*/, string const & /*msg*/)
249 // not implemented, since a left-over file.lyx,v would be confusing.
254 bool RCS::copyEnabled()
260 string RCS::copy(support::FileName const & newFile, string const & msg)
262 // RCS has no real copy command, so we create a poor mans version
263 support::FileName const oldFile(owner_->absFileName());
264 if (!oldFile.copyTo(newFile))
266 FileName path(oldFile.onlyPath());
267 string relFile(to_utf8(newFile.relPath(path.absFileName())));
268 string cmd = "ci -q -u -i -t-\"";
271 cmd += quoteName(relFile);
272 return doVCCommand(cmd, path) ? string() : "RCS: Proceeded";
276 LyXVC::CommandResult RCS::checkIn(string const & msg, string & log)
278 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
279 + quoteName(onlyFileName(owner_->absFileName())),
280 FileName(owner_->filePath()));
282 return LyXVC::ErrorCommand;
283 log = "RCS: Proceeded";
284 return LyXVC::VCSuccess;
288 bool RCS::checkInEnabled()
290 return owner_ && !owner_->hasReadonlyFlag();
294 bool RCS::isCheckInWithConfirmation()
296 // FIXME one day common getDiff for all backends
298 // if (getDiff(file, diff) && diff.empty())
301 TempFile tempfile("lyxvcout");
302 FileName tmpf = tempfile.name();
304 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
308 doVCCommandCall("rcsdiff " + quoteName(owner_->absFileName())
309 + " > " + quoteName(tmpf.toFilesystemEncoding()),
310 FileName(owner_->filePath()));
312 docstring diff = tmpf.fileContents("UTF-8");
321 string RCS::checkOut()
324 int ret = doVCCommand("co -q -l " + quoteName(onlyFileName(owner_->absFileName())),
325 FileName(owner_->filePath()));
326 return ret ? string() : "RCS: Proceeded";
330 bool RCS::checkOutEnabled()
332 return owner_ && owner_->hasReadonlyFlag();
336 string RCS::repoUpdate()
338 lyxerr << "Sorry, not implemented." << endl;
343 bool RCS::repoUpdateEnabled()
349 string RCS::lockingToggle()
351 //FIXME this might be actually possible, study rcs -U, rcs -L.
352 //State should be easy to get inside scanMaster.
353 //It would fix #4370 and make rcs/svn usage even more closer.
354 lyxerr << "Sorry, not implemented." << endl;
359 bool RCS::lockingToggleEnabled()
367 if (doVCCommand("co -f -u" + version_ + ' '
368 + quoteName(onlyFileName(owner_->absFileName())),
369 FileName(owner_->filePath())))
371 // We ignore changes and just reload!
377 bool RCS::isRevertWithConfirmation()
379 //FIXME owner && diff ?
386 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
387 doVCCommand("rcs -o" + version_ + ' '
388 + quoteName(onlyFileName(owner_->absFileName())),
389 FileName(owner_->filePath()));
393 bool RCS::undoLastEnabled()
395 return owner_->hasReadonlyFlag();
399 void RCS::getLog(FileName const & tmpf)
401 doVCCommand("rlog " + quoteName(onlyFileName(owner_->absFileName()))
402 + " > " + quoteName(tmpf.toFilesystemEncoding()),
403 FileName(owner_->filePath()));
407 bool RCS::toggleReadOnlyEnabled()
409 // This got broken somewhere along lfuns dispatch reorganization.
410 // reloadBuffer would be needed after this, but thats problematic
411 // since we are inside Buffer::dispatch.
417 string RCS::revisionInfo(LyXVC::RevisionInfo const info)
419 if (info == LyXVC::File)
421 // fill the rest of the attributes for a single file
422 if (rev_date_cache_.empty())
423 if (!getRevisionInfo())
428 return rev_author_cache_;
430 return rev_date_cache_;
432 return rev_time_cache_;
441 bool RCS::getRevisionInfo()
443 TempFile tempfile("lyxvcout");
444 FileName tmpf = tempfile.name();
446 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
449 doVCCommand("rlog -r " + quoteName(onlyFileName(owner_->absFileName()))
450 + " > " + quoteName(tmpf.toFilesystemEncoding()),
451 FileName(owner_->filePath()));
456 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
459 // we reached to the entry, i.e. after initial log message
461 // line with critical info, e.g:
462 //"date: 2011/07/02 11:02:54; author: sanda; state: Exp; lines: +17 -2"
467 LYXERR(Debug::LYXVC, line);
468 if (entry && prefixIs(line, "date:")) {
472 if (prefixIs(line, "revision"))
478 rev_date_cache_ = token(result, ' ', 1);
479 rev_time_cache_ = rtrim(token(result, ' ', 2), ";");
480 rev_author_cache_ = trim(token(token(result, ';', 1), ':', 1));
482 return !rev_author_cache_.empty();
485 bool RCS::prepareFileRevision(string const &revis, string & f)
488 if (!VCS::makeRCSRevision(version_, rev))
491 TempFile tempfile("lyxvcrev_" + rev + '_');
492 tempfile.setAutoRemove(false);
493 FileName tmpf = tempfile.name();
495 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
499 doVCCommand("co -p" + rev + ' '
500 + quoteName(onlyFileName(owner_->absFileName()))
501 + " > " + quoteName(tmpf.toFilesystemEncoding()),
502 FileName(owner_->filePath()));
504 if (tmpf.isFileEmpty())
507 f = tmpf.absFileName();
512 bool RCS::prepareFileRevisionEnabled()
518 /////////////////////////////////////////////////////////////////////
522 /////////////////////////////////////////////////////////////////////
524 CVS::CVS(FileName const & m, Buffer * b) : VCS(b)
526 // Here we know that the buffer file is either already in CVS or
527 // about to be registered
529 have_rev_info_ = false;
534 FileName const CVS::findFile(FileName const & file)
536 // First we look for the CVS/Entries in the same dir
537 // where we have file.
538 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
539 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
540 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
541 << "' for `" << tmpf << '\'');
542 if (entries.isReadableFile()) {
543 // Ok we are at least in a CVS dir. Parse the CVS/Entries
544 // and see if we can find this file. We do a fast and
546 ifstream ifs(entries.toFilesystemEncoding().c_str());
548 while (getline(ifs, line)) {
549 LYXERR(Debug::LYXVC, "\tEntries: " << line);
550 if (contains(line, tmpf))
558 void CVS::scanMaster()
560 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
561 // Ok now we do the real scan...
562 ifstream ifs(master_.toFilesystemEncoding().c_str());
563 string name = onlyFileName(owner_->absFileName());
564 string tmpf = '/' + name + '/';
565 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
567 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
568 while (getline(ifs, line)) {
569 LYXERR(Debug::LYXVC, "\t line: " << line);
570 if (contains(line, tmpf)) {
571 // Ok extract the fields.
573 if (!regex_match(line, sm, reg)) {
574 LYXERR(Debug::LYXVC, "\t Cannot parse line. Skipping.");
578 //sm[0]; // whole matched string
580 version_ = sm.str(2);
581 string const file_date = sm.str(3);
584 //sm[5]; // tag or tagdate
585 FileName file(owner_->absFileName());
586 if (file.isReadableFile()) {
587 time_t mod = file.lastModified();
588 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
589 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
590 << "'\nModification date of file: `" << mod_date << '\'');
591 if (file.isReadOnly()) {
592 // readonly checkout is unlocked
595 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
596 FileName base(addName(bdir.absFileName(),name));
597 // if base version is existent "cvs edit" was used to lock
598 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
601 vcstatus = NOLOCKING;
609 bool CVS::retrieve(FileName const & file)
611 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
612 // The caller ensures that file does not exist, so no need to check that.
613 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
614 file.onlyPath()) == 0;
618 string const CVS::getTarget(OperationMode opmode) const
622 // in client server mode CVS does not like full path operand for directory operation
623 // since LyX switches to the repo dir "." is good enough as target
626 return quoteName(onlyFileName(owner_->absFileName()));
632 docstring CVS::toString(CvsStatus status) const
636 return _("Up-to-date");
637 case LocallyModified:
638 return _("Locally Modified");
640 return _("Locally Added");
642 return _("Needs Merge");
644 return _("Needs Checkout");
646 return _("No CVS file");
648 return _("Cannot retrieve CVS status");
654 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
655 FileName const & output, bool reportError)
657 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
658 return doVCCommand(cmd + redirection, path, reportError);
662 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
663 support::FileName const & path,
664 support::FileName const & output)
666 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
667 return doVCCommandCall(cmd + redirection, path);
671 CVS::CvsStatus CVS::getStatus()
673 TempFile tempfile("lyxvout");
674 FileName tmpf = tempfile.name();
676 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
680 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
681 FileName(owner_->filePath()), tmpf)) {
685 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
686 CvsStatus status = NoCvsFile;
691 LYXERR(Debug::LYXVC, line << '\n');
692 if (prefixIs(line, "File:")) {
693 if (contains(line, "Up-to-date"))
695 else if (contains(line, "Locally Modified"))
696 status = LocallyModified;
697 else if (contains(line, "Locally Added"))
698 status = LocallyAdded;
699 else if (contains(line, "Needs Merge"))
701 else if (contains(line, "Needs Checkout"))
702 status = NeedsCheckout;
708 void CVS::getRevisionInfo()
712 have_rev_info_ = true;
713 TempFile tempfile("lyxvout");
714 FileName tmpf = tempfile.name();
716 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
720 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
721 + ' ' + getTarget(File),
722 FileName(owner_->filePath()), tmpf);
724 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
728 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
729 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
734 LYXERR(Debug::LYXVC, line << '\n');
735 if (prefixIs(line, "date:")) {
737 if (regex_match(line, sm, reg)) {
738 //sm[0]; // whole matched string
739 rev_date_cache_ = sm[1];
740 rev_time_cache_ = sm[2];
741 //sm[3]; // GMT offset
742 rev_author_cache_ = sm[4];
744 LYXERR(Debug::LYXVC, "\tCannot parse line. Skipping.");
748 if (rev_author_cache_.empty())
750 "Could not retrieve revision info for " << version_ <<
751 " of " << getTarget(File));
755 void CVS::registrer(string const & msg)
757 doVCCommand("cvs -q add -m \"" + msg + "\" "
759 FileName(owner_->filePath()));
763 bool CVS::renameEnabled()
769 string CVS::rename(support::FileName const & newFile, string const & msg)
771 // CVS has no real rename command, so we create a poor mans version
772 support::FileName const oldFile(owner_->absFileName());
773 string ret = copy(newFile, msg);
776 string cmd = "cvs -q remove -m \"" + msg + "\" " +
777 quoteName(oldFile.onlyFileName());
778 FileName path(oldFile.onlyPath());
779 return doVCCommand(cmd, path) ? string() : ret;
783 bool CVS::copyEnabled()
789 string CVS::copy(support::FileName const & newFile, string const & msg)
791 // CVS has no real copy command, so we create a poor mans version
792 support::FileName const oldFile(owner_->absFileName());
793 if (!oldFile.copyTo(newFile))
795 FileName path(oldFile.onlyPath());
796 string relFile(to_utf8(newFile.relPath(path.absFileName())));
797 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
798 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
802 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
804 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
805 FileName(owner_->filePath()), tmpf, false);
812 return doVCCommand("cvs -q edit " + getTarget(File),
813 FileName(owner_->filePath()));
820 return doVCCommand("cvs -q unedit " + getTarget(File),
821 FileName(owner_->filePath()));
825 int CVS::update(OperationMode opmode, FileName const & tmpf)
827 return doVCCommandWithOutput("cvs -q update "
829 FileName(owner_->filePath()), tmpf, false);
833 string CVS::scanLogFile(FileName const & f, string & status)
835 ifstream ifs(f.toFilesystemEncoding().c_str());
840 LYXERR(Debug::LYXVC, line << '\n');
842 status += line + "; ";
843 if (prefixIs(line, "C ")) {
853 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
855 CvsStatus status = getStatus();
858 if (vcstatus != NOLOCKING)
860 return LyXVC::ErrorCommand;
861 log = "CVS: Proceeded";
862 return LyXVC::VCSuccess;
863 case LocallyModified:
865 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
867 FileName(owner_->filePath()));
869 return LyXVC::ErrorCommand;
870 log = "CVS: Proceeded";
871 return LyXVC::VCSuccess;
875 frontend::Alert::error(_("Revision control error."),
876 _("The repository version is newer then the current check out.\n"
877 "You have to update from repository first or revert your changes.")) ;
880 frontend::Alert::error(_("Revision control error."),
881 bformat(_("Bad status when checking in changes.\n"
886 return LyXVC::ErrorBefore;
890 bool CVS::isLocked() const
892 FileName fn(owner_->absFileName());
894 return !fn.isReadOnly();
898 bool CVS::checkInEnabled()
900 if (vcstatus != NOLOCKING)
907 bool CVS::isCheckInWithConfirmation()
909 CvsStatus status = getStatus();
910 return status == LocallyModified || status == LocallyAdded;
914 string CVS::checkOut()
916 if (vcstatus != NOLOCKING && edit())
918 TempFile tempfile("lyxvout");
919 FileName tmpf = tempfile.name();
921 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
925 int rc = update(File, tmpf);
927 string const res = scanLogFile(tmpf, log);
929 frontend::Alert::error(_("Revision control error."),
930 bformat(_("Error when updating from repository.\n"
931 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
932 "After pressing OK, LyX will try to reopen the resolved document."),
933 from_local8bit(res)));
937 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
941 bool CVS::checkOutEnabled()
943 if (vcstatus != NOLOCKING)
950 string CVS::repoUpdate()
952 TempFile tempfile("lyxvout");
953 FileName tmpf = tempfile.name();
955 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
959 getDiff(Directory, tmpf);
960 docstring res = tmpf.fileContents("UTF-8");
962 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
963 docstring const file = from_utf8(owner_->filePath());
964 docstring text = bformat(_("There were detected changes "
965 "in the working directory:\n%1$s\n\n"
966 "Possible file conflicts must be then resolved manually "
967 "or you will need to revert back to the repository version."), file);
968 int ret = frontend::Alert::prompt(_("Changes detected"),
969 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
971 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
972 ret = frontend::Alert::prompt(_("Changes detected"),
973 text, 0, 1, _("&Continue"), _("&Abort"));
974 hideDialogs("file", 0);
980 int rc = update(Directory, tmpf);
981 res += "Update log:\n" + tmpf.fileContents("UTF-8");
982 LYXERR(Debug::LYXVC, res);
985 string sres = scanLogFile(tmpf, log);
987 docstring const file = owner_->fileName().displayName(20);
988 frontend::Alert::error(_("Revision control error."),
989 bformat(_("Error when updating document %1$s from repository.\n"
990 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
991 "After pressing OK, LyX will try to reopen the resolved document."),
992 file, from_local8bit(sres)));
996 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
1000 bool CVS::repoUpdateEnabled()
1006 string CVS::lockingToggle()
1008 lyxerr << "Sorry, not implemented." << endl;
1013 bool CVS::lockingToggleEnabled()
1019 bool CVS::isRevertWithConfirmation()
1021 CvsStatus status = getStatus();
1022 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1028 // Reverts to the version in CVS repository and
1029 // gets the updated version from the repository.
1030 CvsStatus status = getStatus();
1033 if (vcstatus != NOLOCKING)
1034 return 0 == unedit();
1038 case LocallyModified: {
1039 FileName f(owner_->absFileName());
1041 update(File, FileName());
1042 owner_->markClean();
1045 case LocallyAdded: {
1046 docstring const file = owner_->fileName().displayName(20);
1047 frontend::Alert::error(_("Revision control error."),
1048 bformat(_("The document %1$s is not in repository.\n"
1049 "You have to check in the first revision before you can revert."),
1054 docstring const file = owner_->fileName().displayName(20);
1055 frontend::Alert::error(_("Revision control error."),
1056 bformat(_("Cannot revert document %1$s to repository version.\n"
1057 "The status '%2$s' is unexpected."),
1058 file, toString(status)));
1066 void CVS::undoLast()
1068 // merge the current with the previous version
1069 // in a reverse patch kind of way, so that the
1070 // result is to revert the last changes.
1071 lyxerr << "Sorry, not implemented." << endl;
1075 bool CVS::undoLastEnabled()
1081 void CVS::getLog(FileName const & tmpf)
1083 doVCCommandWithOutput("cvs log " + getTarget(File),
1084 FileName(owner_->filePath()),
1089 bool CVS::toggleReadOnlyEnabled()
1095 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1097 if (!version_.empty()) {
1103 return rev_author_cache_;
1105 return rev_date_cache_;
1107 return rev_time_cache_;
1116 bool CVS::prepareFileRevision(string const & revis, string & f)
1119 if (!VCS::makeRCSRevision(version_, rev))
1122 TempFile tempfile("lyxvcrev_" + rev + '_');
1123 tempfile.setAutoRemove(false);
1124 FileName tmpf = tempfile.name();
1126 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1130 doVCCommandWithOutput("cvs update -p -r" + rev + ' '
1132 FileName(owner_->filePath()), tmpf);
1134 if (tmpf.isFileEmpty())
1137 f = tmpf.absFileName();
1142 bool CVS::prepareFileRevisionEnabled()
1148 /////////////////////////////////////////////////////////////////////
1152 /////////////////////////////////////////////////////////////////////
1154 SVN::SVN(FileName const & m, Buffer * b) : VCS(b)
1156 // Here we know that the buffer file is either already in SVN or
1157 // about to be registered
1164 FileName const SVN::findFile(FileName const & file)
1166 // First we check the existence of repository meta data.
1167 if (!VCS::checkparentdirs(file, ".svn")) {
1168 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1172 // Now we check the status of the file.
1173 TempFile tempfile("lyxvcout");
1174 FileName tmpf = tempfile.name();
1176 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1180 string const fname = onlyFileName(file.absFileName());
1181 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1182 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1183 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1185 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1186 return found ? file : FileName();
1190 void SVN::scanMaster()
1192 // vcstatus code is somewhat superflous,
1193 // until we want to implement read-only toggle for svn.
1194 vcstatus = NOLOCKING;
1195 if (checkLockMode()) {
1199 vcstatus = UNLOCKED;
1204 bool SVN::checkLockMode()
1206 TempFile tempfile("lyxvcout");
1207 FileName tmpf = tempfile.name();
1209 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1213 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1214 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1215 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1216 FileName(owner_->filePath())))
1219 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1223 while (ifs && !ret) {
1225 LYXERR(Debug::LYXVC, line);
1226 if (contains(line, "svn:needs-lock"))
1229 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1237 bool SVN::isLocked() const
1239 FileName file(owner_->absFileName());
1241 return !file.isReadOnly();
1245 bool SVN::retrieve(FileName const & file)
1247 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1248 // The caller ensures that file does not exist, so no need to check that.
1249 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1250 file.onlyPath()) == 0;
1254 void SVN::registrer(string const & /*msg*/)
1256 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1257 FileName(owner_->filePath()));
1261 bool SVN::renameEnabled()
1267 string SVN::rename(support::FileName const & newFile, string const & msg)
1269 // svn move does not require a log message, since it does not commit.
1270 // In LyX we commit immediately afterwards, otherwise it could be
1271 // confusing to the user to have two uncommitted files.
1272 FileName path(owner_->filePath());
1273 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1274 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1275 ' ' + quoteName(relFile));
1276 if (doVCCommand(cmd, path)) {
1277 cmd = "svn revert -q " +
1278 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1280 doVCCommand(cmd, path);
1281 if (newFile.exists())
1282 newFile.removeFile();
1285 vector<support::FileName> f;
1286 f.push_back(owner_->fileName());
1287 f.push_back(newFile);
1289 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1290 cmd = "svn revert -q " +
1291 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1293 doVCCommand(cmd, path);
1294 if (newFile.exists())
1295 newFile.removeFile();
1302 bool SVN::copyEnabled()
1308 string SVN::copy(support::FileName const & newFile, string const & msg)
1310 // svn copy does not require a log message, since it does not commit.
1311 // In LyX we commit immediately afterwards, otherwise it could be
1312 // confusing to the user to have an uncommitted file.
1313 FileName path(owner_->filePath());
1314 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1315 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1316 ' ' + quoteName(relFile));
1317 if (doVCCommand(cmd, path))
1319 vector<support::FileName> f(1, newFile);
1321 if (checkIn(f, msg, log) == LyXVC::VCSuccess)
1327 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1329 vector<support::FileName> f(1, owner_->fileName());
1330 return checkIn(f, msg, log);
1334 LyXVC::CommandResult
1335 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1337 TempFile tempfile("lyxvcout");
1338 FileName tmpf = tempfile.name();
1340 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1341 log = N_("Error: Could not generate logfile.");
1342 return LyXVC::ErrorBefore;
1346 os << "svn commit -m \"" << msg << '"';
1347 for (size_t i = 0; i < f.size(); ++i)
1348 os << ' ' << quoteName(f[i].onlyFileName());
1349 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1350 LyXVC::CommandResult ret =
1351 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1352 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1354 string res = scanLogFile(tmpf, log);
1356 frontend::Alert::error(_("Revision control error."),
1357 _("Error when committing to repository.\n"
1358 "You have to manually resolve the problem.\n"
1359 "LyX will reopen the document after you press OK."));
1360 ret = LyXVC::ErrorCommand;
1363 if (!fileLock(false, tmpf, log))
1364 ret = LyXVC::ErrorCommand;
1367 log.insert(0, "SVN: ");
1368 if (ret == LyXVC::VCSuccess && log.empty())
1369 log = "SVN: Proceeded";
1374 bool SVN::checkInEnabled()
1383 bool SVN::isCheckInWithConfirmation()
1385 // FIXME one day common getDiff and perhaps OpMode for all backends
1387 TempFile tempfile("lyxvcout");
1388 FileName tmpf = tempfile.name();
1390 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1394 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1395 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1396 FileName(owner_->filePath()));
1398 docstring diff = tmpf.fileContents("UTF-8");
1407 // FIXME Correctly return code should be checked instead of this.
1408 // This would need another solution than just plain startscript.
1409 // Hint from Andre': QProcess::readAllStandardError()...
1410 string SVN::scanLogFile(FileName const & f, string & status)
1412 ifstream ifs(f.toFilesystemEncoding().c_str());
1417 LYXERR(Debug::LYXVC, line << '\n');
1419 status += line + "; ";
1420 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1421 || contains(line, "Commit failed")) {
1425 if (contains(line, "svn:needs-lock")) {
1435 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1437 if (!locked_mode_ || (isLocked() == lock))
1440 string const arg = lock ? "lock " : "unlock ";
1441 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1442 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1443 FileName(owner_->filePath()));
1445 // Lock error messages go unfortunately on stderr and are unreachable this way.
1446 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1450 if (!line.empty()) status += line + "; ";
1454 if (isLocked() == lock)
1458 frontend::Alert::error(_("Revision control error."),
1459 _("Error while acquiring write lock.\n"
1460 "Another user is most probably editing\n"
1461 "the current document now!\n"
1462 "Also check the access to the repository."));
1464 frontend::Alert::error(_("Revision control error."),
1465 _("Error while releasing write lock.\n"
1466 "Check the access to the repository."));
1471 string SVN::checkOut()
1473 TempFile tempfile("lyxvcout");
1474 FileName tmpf = tempfile.name();
1476 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1477 return N_("Error: Could not generate logfile.");
1480 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1481 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1482 FileName(owner_->filePath()));
1485 string const res = scanLogFile(tmpf, log);
1487 frontend::Alert::error(_("Revision control error."),
1488 bformat(_("Error when updating from repository.\n"
1489 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1490 "After pressing OK, LyX will try to reopen the resolved document."),
1491 from_local8bit(res)));
1493 fileLock(true, tmpf, log);
1495 return log.empty() ? string() : "SVN: " + log;
1499 bool SVN::checkOutEnabled()
1508 string SVN::repoUpdate()
1510 TempFile tempfile("lyxvcout");
1511 FileName tmpf = tempfile.name();
1513 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1514 return N_("Error: Could not generate logfile.");
1517 doVCCommand("svn diff " + quoteName(owner_->filePath())
1518 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1519 FileName(owner_->filePath()));
1520 docstring res = tmpf.fileContents("UTF-8");
1522 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1523 docstring const file = from_utf8(owner_->filePath());
1524 docstring text = bformat(_("There were detected changes "
1525 "in the working directory:\n%1$s\n\n"
1526 "In case of file conflict version of the local directory files "
1527 "will be preferred."
1528 "\n\nContinue?"), file);
1529 int ret = frontend::Alert::prompt(_("Changes detected"),
1530 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1532 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1533 ret = frontend::Alert::prompt(_("Changes detected"),
1534 text, 0, 1, _("&Yes"), _("&No"));
1535 hideDialogs("file", 0);
1541 // Reverting looks too harsh, see bug #6255.
1542 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1543 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1544 // FileName(owner_->filePath()));
1545 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1546 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1547 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1548 FileName(owner_->filePath()));
1549 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1551 LYXERR(Debug::LYXVC, res);
1552 return to_utf8(res);
1556 bool SVN::repoUpdateEnabled()
1562 string SVN::lockingToggle()
1564 TempFile tempfile("lyxvcout");
1565 FileName tmpf = tempfile.name();
1567 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1568 return N_("Error: Could not generate logfile.");
1571 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1572 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1573 FileName(owner_->filePath()));
1578 string res = scanLogFile(tmpf, log);
1579 bool locking = contains(res, "svn:needs-lock");
1581 ret = doVCCommand("svn propset svn:needs-lock ON "
1582 + quoteName(onlyFileName(owner_->absFileName()))
1583 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1584 FileName(owner_->filePath()));
1586 ret = doVCCommand("svn propdel svn:needs-lock "
1587 + quoteName(onlyFileName(owner_->absFileName()))
1588 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1589 FileName(owner_->filePath()));
1593 frontend::Alert::warning(_("SVN File Locking"),
1594 (locking ? _("Locking property unset.") : _("Locking property set.")) + '\n'
1595 + _("Do not forget to commit the locking property into the repository."),
1598 return string("SVN: ") + (locking ?
1599 N_("Locking property unset.") : N_("Locking property set."));
1603 bool SVN::lockingToggleEnabled()
1611 // Reverts to the version in SVN repository and
1612 // gets the updated version from the repository.
1613 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1615 if (doVCCommand("svn revert -q " + fil,
1616 FileName(owner_->filePath())))
1618 owner_->markClean();
1623 bool SVN::isRevertWithConfirmation()
1625 //FIXME owner && diff
1630 void SVN::undoLast()
1632 // merge the current with the previous version
1633 // in a reverse patch kind of way, so that the
1634 // result is to revert the last changes.
1635 lyxerr << "Sorry, not implemented." << endl;
1639 bool SVN::undoLastEnabled()
1645 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1647 if (info == LyXVC::Tree) {
1648 if (rev_tree_cache_.empty())
1649 if (!getTreeRevisionInfo())
1650 rev_tree_cache_ = "?";
1651 if (rev_tree_cache_ == "?")
1654 return rev_tree_cache_;
1657 // fill the rest of the attributes for a single file
1658 if (rev_file_cache_.empty())
1659 if (!getFileRevisionInfo())
1660 rev_file_cache_ = "?";
1664 if (rev_file_cache_ == "?")
1666 return rev_file_cache_;
1668 return rev_author_cache_;
1670 return rev_date_cache_;
1672 return rev_time_cache_;
1681 bool SVN::getFileRevisionInfo()
1683 TempFile tempfile("lyxvcout");
1684 FileName tmpf = tempfile.name();
1686 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1690 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1691 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1692 FileName(owner_->filePath()));
1697 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1705 LYXERR(Debug::LYXVC, line);
1706 if (prefixIs(line, "<commit"))
1708 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1709 string l1 = subst(line, "revision=\"", "");
1710 string l2 = trim(subst(l1, "\">", ""));
1712 rev_file_cache_ = rev = l2;
1714 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1715 string l1 = subst(line, "<author>", "");
1716 string l2 = subst(l1, "</author>", "");
1717 rev_author_cache_ = l2;
1719 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1720 string l1 = subst(line, "<date>", "");
1721 string l2 = subst(l1, "</date>", "");
1722 l2 = split(l2, l1, 'T');
1723 rev_date_cache_ = l1;
1724 l2 = split(l2, l1, '.');
1725 rev_time_cache_ = l1;
1730 return !rev.empty();
1734 bool SVN::getTreeRevisionInfo()
1736 TempFile tempfile("lyxvcout");
1737 FileName tmpf = tempfile.name();
1739 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1743 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1744 FileName(owner_->filePath()));
1749 // only first line in case something bad happens.
1750 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1755 rev_tree_cache_ = line;
1756 return !line.empty();
1760 void SVN::getLog(FileName const & tmpf)
1762 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1763 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1764 FileName(owner_->filePath()));
1768 bool SVN::prepareFileRevision(string const & revis, string & f)
1770 if (!isStrInt(revis))
1773 int rev = convert<int>(revis);
1775 if (!getFileRevisionInfo())
1778 rev = convert<int>(rev_file_cache_);
1779 // go back for minus rev
1781 rev = rev + convert<int>(rev_file_cache_);
1786 string revname = convert<string>(rev);
1787 TempFile tempfile("lyxvcrev_" + revname + '_');
1788 tempfile.setAutoRemove(false);
1789 FileName tmpf = tempfile.name();
1791 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1795 doVCCommand("svn cat -r " + revname + ' '
1796 + quoteName(onlyFileName(owner_->absFileName()))
1797 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1798 FileName(owner_->filePath()));
1800 if (tmpf.isFileEmpty())
1803 f = tmpf.absFileName();
1808 bool SVN::prepareFileRevisionEnabled()
1815 bool SVN::toggleReadOnlyEnabled()
1821 /////////////////////////////////////////////////////////////////////
1825 /////////////////////////////////////////////////////////////////////
1827 GIT::GIT(FileName const & m, Buffer * b) : VCS(b)
1829 // Here we know that the buffer file is either already in GIT or
1830 // about to be registered
1836 FileName const GIT::findFile(FileName const & file)
1838 // First we check the existence of repository meta data.
1839 if (!VCS::checkparentdirs(file, ".git")) {
1840 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1844 // Now we check the status of the file.
1845 TempFile tempfile("lyxvcout");
1846 FileName tmpf = tempfile.name();
1848 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1852 string const fname = onlyFileName(file.absFileName());
1853 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1855 doVCCommandCall("git ls-files " +
1856 quoteName(fname) + " > " +
1857 quoteName(tmpf.toFilesystemEncoding()),
1860 bool found = !tmpf.isFileEmpty();
1861 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1862 return found ? file : FileName();
1866 void GIT::scanMaster()
1868 // vcstatus code is somewhat superflous,
1869 // until we want to implement read-only toggle for git.
1870 vcstatus = NOLOCKING;
1874 bool GIT::retrieve(FileName const & file)
1876 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1877 // The caller ensures that file does not exist, so no need to check that.
1878 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1879 file.onlyPath()) == 0;
1883 void GIT::registrer(string const & /*msg*/)
1885 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1886 FileName(owner_->filePath()));
1890 bool GIT::renameEnabled()
1896 string GIT::rename(support::FileName const & newFile, string const & msg)
1898 // git mv does not require a log message, since it does not commit.
1899 // In LyX we commit immediately afterwards, otherwise it could be
1900 // confusing to the user to have two uncommitted files.
1901 FileName path(owner_->filePath());
1902 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1903 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1904 ' ' + quoteName(relFile));
1905 if (doVCCommand(cmd, path)) {
1906 cmd = "git checkout -q " +
1907 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1909 doVCCommand(cmd, path);
1910 if (newFile.exists())
1911 newFile.removeFile();
1914 vector<support::FileName> f;
1915 f.push_back(owner_->fileName());
1916 f.push_back(newFile);
1918 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1919 cmd = "git checkout -q " +
1920 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1922 doVCCommand(cmd, path);
1923 if (newFile.exists())
1924 newFile.removeFile();
1931 bool GIT::copyEnabled()
1937 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1939 // git does not support copy with history preservation
1944 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1946 vector<support::FileName> f(1, owner_->fileName());
1947 return checkIn(f, msg, log);
1951 LyXVC::CommandResult
1952 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1954 TempFile tempfile("lyxvcout");
1955 FileName tmpf = tempfile.name();
1957 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1958 log = N_("Error: Could not generate logfile.");
1959 return LyXVC::ErrorBefore;
1963 os << "git commit -m \"" << msg << '"';
1964 for (size_t i = 0; i < f.size(); ++i)
1965 os << ' ' << quoteName(f[i].onlyFileName());
1966 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1967 LyXVC::CommandResult ret =
1968 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1969 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1971 string res = scanLogFile(tmpf, log);
1973 frontend::Alert::error(_("Revision control error."),
1974 _("Error when committing to repository.\n"
1975 "You have to manually resolve the problem.\n"
1976 "LyX will reopen the document after you press OK."));
1977 ret = LyXVC::ErrorCommand;
1981 log.insert(0, "GIT: ");
1982 if (ret == LyXVC::VCSuccess && log.empty())
1983 log = "GIT: Proceeded";
1988 bool GIT::checkInEnabled()
1994 bool GIT::isCheckInWithConfirmation()
1996 // FIXME one day common getDiff and perhaps OpMode for all backends
1998 TempFile tempfile("lyxvcout");
1999 FileName tmpf = tempfile.name();
2001 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2005 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
2006 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2007 FileName(owner_->filePath()));
2009 docstring diff = tmpf.fileContents("UTF-8");
2018 // FIXME Correctly return code should be checked instead of this.
2019 // This would need another solution than just plain startscript.
2020 // Hint from Andre': QProcess::readAllStandardError()...
2021 string GIT::scanLogFile(FileName const & f, string & status)
2023 ifstream ifs(f.toFilesystemEncoding().c_str());
2028 LYXERR(Debug::LYXVC, line << "\n");
2030 status += line + "; ";
2031 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2032 || contains(line, "Commit failed")) {
2042 string GIT::checkOut()
2048 bool GIT::checkOutEnabled()
2054 string GIT::repoUpdate()
2060 bool GIT::repoUpdateEnabled()
2066 string GIT::lockingToggle()
2072 bool GIT::lockingToggleEnabled()
2080 // Reverts to the version in GIT repository and
2081 // gets the updated version from the repository.
2082 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2084 if (doVCCommand("git checkout -q " + fil,
2085 FileName(owner_->filePath())))
2087 owner_->markClean();
2092 bool GIT::isRevertWithConfirmation()
2094 //FIXME owner && diff
2099 void GIT::undoLast()
2101 // merge the current with the previous version
2102 // in a reverse patch kind of way, so that the
2103 // result is to revert the last changes.
2104 lyxerr << "Sorry, not implemented." << endl;
2108 bool GIT::undoLastEnabled()
2114 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2116 if (info == LyXVC::Tree) {
2117 if (rev_tree_cache_.empty())
2118 if (!getTreeRevisionInfo())
2119 rev_tree_cache_ = "?";
2120 if (rev_tree_cache_ == "?")
2123 return rev_tree_cache_;
2126 // fill the rest of the attributes for a single file
2127 if (rev_file_cache_.empty())
2128 if (!getFileRevisionInfo()) {
2129 rev_file_cache_ = "?";
2130 rev_file_abbrev_cache_ = "?";
2135 if (rev_file_cache_ == "?")
2137 return rev_file_cache_;
2138 case LyXVC::FileAbbrev:
2139 if (rev_file_abbrev_cache_ == "?")
2141 return rev_file_abbrev_cache_;
2143 return rev_author_cache_;
2145 return rev_date_cache_;
2147 return rev_time_cache_;
2156 bool GIT::getFileRevisionInfo()
2158 TempFile tempfile("lyxvcout");
2159 FileName tmpf = tempfile.name();
2161 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2165 doVCCommand("git log -n 1 --pretty=format:%H%n%h%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName()))
2166 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2167 FileName(owner_->filePath()));
2172 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2175 getline(ifs, rev_file_cache_);
2177 getline(ifs, rev_file_abbrev_cache_);
2179 getline(ifs, rev_author_cache_);
2183 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2187 return !rev_file_cache_.empty();
2191 bool GIT::getTreeRevisionInfo()
2193 TempFile tempfile("lyxvcout");
2194 FileName tmpf = tempfile.name();
2196 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2200 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2201 FileName(owner_->filePath()),
2202 false); //git describe returns $?=128 when no tag found (but git repo still exists)
2207 // only first line in case something bad happens.
2208 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2209 getline(ifs, rev_tree_cache_);
2212 return !rev_tree_cache_.empty();
2216 void GIT::getLog(FileName const & tmpf)
2218 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2219 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2220 FileName(owner_->filePath()));
2224 //at this moment we don't accept revision SHA, but just number of revision steps back
2225 //GUI and infrastucture needs to be changed first
2226 bool GIT::prepareFileRevision(string const & revis, string & f)
2228 // anything positive means we got hash, not "0" or minus revision
2231 // hash is rarely number and should be long
2232 if (isStrInt(revis) && revis.length()<20)
2233 rev = convert<int>(revis);
2235 // revision and filename
2238 // go back for "minus" revisions
2240 pointer = "HEAD~" + convert<string>(-rev);
2247 TempFile tempfile("lyxvcrev_" + revis + '_');
2248 tempfile.setAutoRemove(false);
2249 FileName tmpf = tempfile.name();
2251 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2255 doVCCommand("git show " + pointer + "./"
2256 + quoteName(onlyFileName(owner_->absFileName()))
2257 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2258 FileName(owner_->filePath()));
2260 if (tmpf.isFileEmpty())
2263 f = tmpf.absFileName();
2268 bool GIT::prepareFileRevisionEnabled()
2274 bool GIT::toggleReadOnlyEnabled()