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) {
64 if (prefixIs(cmd, "ci "))
65 rcsmsg = "\n" + _("Perhaps the RCS package is not installed on your system?");
66 frontend::Alert::error(_("Revision control error."),
67 bformat(_("Some problem occurred while running the command:\n"
75 bool VCS::makeRCSRevision(string const &version, string &revis) const
80 int back = convert<int>(rev);
81 // if positive use as the last number in the whole revision string
84 rsplit(version, base , '.');
85 rev = base + '.' + rev;
89 // we care about the last number from revision string
90 // in case of backward indexing
93 cur = rsplit(version, base , '.');
96 int want = convert<int>(cur) + back;
100 rev = base + '.' + convert<string>(want);
109 bool VCS::checkparentdirs(FileName const & file, std::string const & vcsdir)
111 FileName dirname = file.onlyPath();
113 FileName tocheck = FileName(addName(dirname.absFileName(), vcsdir));
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";
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 FileName const entries(onlyPath(file.absFileName()) + "/CVS/Entries");
543 string const tmpf = '/' + onlyFileName(file.absFileName()) + '/';
544 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
545 << "' for `" << tmpf << '\'');
546 if (entries.isReadableFile()) {
547 // Ok we are at least in a CVS dir. Parse the CVS/Entries
548 // and see if we can find this file. We do a fast and
550 ifstream ifs(entries.toFilesystemEncoding().c_str());
552 while (getline(ifs, line)) {
553 LYXERR(Debug::LYXVC, "\tEntries: " << line);
554 if (contains(line, tmpf))
562 void CVS::scanMaster()
564 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
565 // Ok now we do the real scan...
566 ifstream ifs(master_.toFilesystemEncoding().c_str());
567 string name = onlyFileName(owner_->absFileName());
568 string tmpf = '/' + name + '/';
569 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
571 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
572 while (getline(ifs, line)) {
573 LYXERR(Debug::LYXVC, "\t line: " << line);
574 if (contains(line, tmpf)) {
575 // Ok extract the fields.
577 if (!regex_match(line, sm, reg)) {
578 LYXERR(Debug::LYXVC, "\t Cannot parse line. Skipping.");
582 //sm[0]; // whole matched string
584 version_ = sm.str(2);
585 string const file_date = sm.str(3);
588 //sm[5]; // tag or tagdate
589 FileName file(owner_->absFileName());
590 if (file.isReadableFile()) {
591 time_t mod = file.lastModified();
592 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
593 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
594 << "'\nModification date of file: `" << mod_date << '\'');
595 if (file.isReadOnly()) {
596 // readonly checkout is unlocked
599 FileName bdir(addPath(master_.onlyPath().absFileName(),"Base"));
600 FileName base(addName(bdir.absFileName(),name));
601 // if base version is existent "cvs edit" was used to lock
602 vcstatus = base.isReadableFile() ? LOCKED : NOLOCKING;
605 vcstatus = NOLOCKING;
613 bool CVS::retrieve(FileName const & file)
615 LYXERR(Debug::LYXVC, "LyXVC::CVS: retrieve.\n\t" << file);
616 // The caller ensures that file does not exist, so no need to check that.
617 return doVCCommandCall("cvs -q update " + quoteName(file.toFilesystemEncoding()),
618 file.onlyPath()) == 0;
622 string const CVS::getTarget(OperationMode opmode) const
626 // in client server mode CVS does not like full path operand for directory operation
627 // since LyX switches to the repo dir "." is good enough as target
630 return quoteName(onlyFileName(owner_->absFileName()));
636 docstring CVS::toString(CvsStatus status) const
640 return _("Up-to-date");
641 case LocallyModified:
642 return _("Locally Modified");
644 return _("Locally Added");
646 return _("Needs Merge");
648 return _("Needs Checkout");
650 return _("No CVS file");
652 return _("Cannot retrieve CVS status");
658 int CVS::doVCCommandWithOutput(string const & cmd, FileName const & path,
659 FileName const & output, bool reportError)
661 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
662 return doVCCommand(cmd + redirection, path, reportError);
666 int CVS::doVCCommandCallWithOutput(std::string const & cmd,
667 support::FileName const & path,
668 support::FileName const & output)
670 string redirection = output.empty() ? "" : " > " + quoteName(output.toFilesystemEncoding());
671 return doVCCommandCall(cmd + redirection, path);
675 CVS::CvsStatus CVS::getStatus()
677 TempFile tempfile("lyxvout");
678 FileName tmpf = tempfile.name();
680 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
684 if (doVCCommandCallWithOutput("cvs status " + getTarget(File),
685 FileName(owner_->filePath()), tmpf)) {
689 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
690 CvsStatus status = NoCvsFile;
695 LYXERR(Debug::LYXVC, line << '\n');
696 if (prefixIs(line, "File:")) {
697 if (contains(line, "Up-to-date"))
699 else if (contains(line, "Locally Modified"))
700 status = LocallyModified;
701 else if (contains(line, "Locally Added"))
702 status = LocallyAdded;
703 else if (contains(line, "Needs Merge"))
705 else if (contains(line, "Needs Checkout"))
706 status = NeedsCheckout;
712 void CVS::getRevisionInfo()
716 have_rev_info_ = true;
717 TempFile tempfile("lyxvout");
718 FileName tmpf = tempfile.name();
720 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
724 int rc = doVCCommandCallWithOutput("cvs log -r" + version_
725 + ' ' + getTarget(File),
726 FileName(owner_->filePath()), tmpf);
728 LYXERR(Debug::LYXVC, "cvs log failed with exit code " << rc);
732 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
733 static regex const reg("date: (.*) (.*) (.*); author: (.*); state: (.*);(.*)");
738 LYXERR(Debug::LYXVC, line << '\n');
739 if (prefixIs(line, "date:")) {
741 regex_match(line, sm, reg);
742 //sm[0]; // whole matched string
743 rev_date_cache_ = sm[1];
744 rev_time_cache_ = sm[2];
745 //sm[3]; // GMT offset
746 rev_author_cache_ = sm[4];
750 if (rev_author_cache_.empty())
752 "Could not retrieve revision info for " << version_ <<
753 " of " << getTarget(File));
757 void CVS::registrer(string const & msg)
759 doVCCommand("cvs -q add -m \"" + msg + "\" "
761 FileName(owner_->filePath()));
765 bool CVS::renameEnabled()
771 string CVS::rename(support::FileName const & newFile, string const & msg)
773 // CVS has no real rename command, so we create a poor mans version
774 support::FileName const oldFile(owner_->absFileName());
775 string ret = copy(newFile, msg);
778 string cmd = "cvs -q remove -m \"" + msg + "\" " +
779 quoteName(oldFile.onlyFileName());
780 FileName path(oldFile.onlyPath());
781 return doVCCommand(cmd, path) ? string() : ret;
785 bool CVS::copyEnabled()
791 string CVS::copy(support::FileName const & newFile, string const & msg)
793 // CVS has no real copy command, so we create a poor mans version
794 support::FileName const oldFile(owner_->absFileName());
795 if (!oldFile.copyTo(newFile))
797 FileName path(oldFile.onlyPath());
798 string relFile(to_utf8(newFile.relPath(path.absFileName())));
799 string cmd("cvs -q add -m \"" + msg + "\" " + quoteName(relFile));
800 return doVCCommand(cmd, path) ? string() : "CVS: Proceeded";
804 void CVS::getDiff(OperationMode opmode, FileName const & tmpf)
806 doVCCommandWithOutput("cvs diff " + getTarget(opmode),
807 FileName(owner_->filePath()), tmpf, false);
814 return doVCCommand("cvs -q edit " + getTarget(File),
815 FileName(owner_->filePath()));
822 return doVCCommand("cvs -q unedit " + getTarget(File),
823 FileName(owner_->filePath()));
827 int CVS::update(OperationMode opmode, FileName const & tmpf)
829 return doVCCommandWithOutput("cvs -q update "
831 FileName(owner_->filePath()), tmpf, false);
835 string CVS::scanLogFile(FileName const & f, string & status)
837 ifstream ifs(f.toFilesystemEncoding().c_str());
842 LYXERR(Debug::LYXVC, line << '\n');
844 status += line + "; ";
845 if (prefixIs(line, "C ")) {
855 LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
857 CvsStatus status = getStatus();
860 if (vcstatus != NOLOCKING)
862 return LyXVC::ErrorCommand;
863 log = "CVS: Proceeded";
864 return LyXVC::VCSuccess;
865 case LocallyModified:
867 int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
869 FileName(owner_->filePath()));
871 return LyXVC::ErrorCommand;
872 log = "CVS: Proceeded";
873 return LyXVC::VCSuccess;
877 frontend::Alert::error(_("Revision control error."),
878 _("The repository version is newer then the current check out.\n"
879 "You have to update from repository first or revert your changes.")) ;
882 frontend::Alert::error(_("Revision control error."),
883 bformat(_("Bad status when checking in changes.\n"
888 return LyXVC::ErrorBefore;
892 bool CVS::isLocked() const
894 FileName fn(owner_->absFileName());
896 return !fn.isReadOnly();
900 bool CVS::checkInEnabled()
902 if (vcstatus != NOLOCKING)
909 bool CVS::isCheckInWithConfirmation()
911 CvsStatus status = getStatus();
912 return status == LocallyModified || status == LocallyAdded;
916 string CVS::checkOut()
918 if (vcstatus != NOLOCKING && edit())
920 TempFile tempfile("lyxvout");
921 FileName tmpf = tempfile.name();
923 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
927 int rc = update(File, tmpf);
929 string const res = scanLogFile(tmpf, log);
931 frontend::Alert::error(_("Revision control error."),
932 bformat(_("Error when updating from repository.\n"
933 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
934 "After pressing OK, LyX will try to reopen the resolved document."),
935 from_local8bit(res)));
939 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
943 bool CVS::checkOutEnabled()
945 if (vcstatus != NOLOCKING)
952 string CVS::repoUpdate()
954 TempFile tempfile("lyxvout");
955 FileName tmpf = tempfile.name();
957 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
961 getDiff(Directory, tmpf);
962 docstring res = tmpf.fileContents("UTF-8");
964 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
965 docstring const file = from_utf8(owner_->filePath());
966 docstring text = bformat(_("There were detected changes "
967 "in the working directory:\n%1$s\n\n"
968 "Possible file conflicts must be then resolved manually "
969 "or you will need to revert back to the repository version."), file);
970 int ret = frontend::Alert::prompt(_("Changes detected"),
971 text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
973 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
974 ret = frontend::Alert::prompt(_("Changes detected"),
975 text, 0, 1, _("&Continue"), _("&Abort"));
976 hideDialogs("file", 0);
982 int rc = update(Directory, tmpf);
983 res += "Update log:\n" + tmpf.fileContents("UTF-8");
984 LYXERR(Debug::LYXVC, res);
987 string sres = scanLogFile(tmpf, log);
989 docstring const file = owner_->fileName().displayName(20);
990 frontend::Alert::error(_("Revision control error."),
991 bformat(_("Error when updating document %1$s from repository.\n"
992 "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
993 "After pressing OK, LyX will try to reopen the resolved document."),
994 file, from_local8bit(sres)));
998 return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
1002 bool CVS::repoUpdateEnabled()
1008 string CVS::lockingToggle()
1010 lyxerr << "Sorry, not implemented." << endl;
1015 bool CVS::lockingToggleEnabled()
1021 bool CVS::isRevertWithConfirmation()
1023 CvsStatus status = getStatus();
1024 return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
1030 // Reverts to the version in CVS repository and
1031 // gets the updated version from the repository.
1032 CvsStatus status = getStatus();
1035 if (vcstatus != NOLOCKING)
1036 return 0 == unedit();
1040 case LocallyModified: {
1041 FileName f(owner_->absFileName());
1043 update(File, FileName());
1044 owner_->markClean();
1047 case LocallyAdded: {
1048 docstring const file = owner_->fileName().displayName(20);
1049 frontend::Alert::error(_("Revision control error."),
1050 bformat(_("The document %1$s is not in repository.\n"
1051 "You have to check in the first revision before you can revert."),
1056 docstring const file = owner_->fileName().displayName(20);
1057 frontend::Alert::error(_("Revision control error."),
1058 bformat(_("Cannot revert document %1$s to repository version.\n"
1059 "The status '%2$s' is unexpected."),
1060 file, toString(status)));
1068 void CVS::undoLast()
1070 // merge the current with the previous version
1071 // in a reverse patch kind of way, so that the
1072 // result is to revert the last changes.
1073 lyxerr << "Sorry, not implemented." << endl;
1077 bool CVS::undoLastEnabled()
1083 void CVS::getLog(FileName const & tmpf)
1085 doVCCommandWithOutput("cvs log " + getTarget(File),
1086 FileName(owner_->filePath()),
1091 bool CVS::toggleReadOnlyEnabled()
1097 string CVS::revisionInfo(LyXVC::RevisionInfo const info)
1099 if (!version_.empty()) {
1105 return rev_author_cache_;
1107 return rev_date_cache_;
1109 return rev_time_cache_;
1118 bool CVS::prepareFileRevision(string const & revis, string & f)
1121 if (!VCS::makeRCSRevision(version_, rev))
1124 TempFile tempfile("lyxvcrev_" + rev + '_');
1125 tempfile.setAutoRemove(false);
1126 FileName tmpf = tempfile.name();
1128 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1132 doVCCommandWithOutput("cvs update -p -r" + rev + ' '
1134 FileName(owner_->filePath()), tmpf);
1136 if (tmpf.isFileEmpty())
1139 f = tmpf.absFileName();
1144 bool CVS::prepareFileRevisionEnabled()
1150 /////////////////////////////////////////////////////////////////////
1154 /////////////////////////////////////////////////////////////////////
1156 SVN::SVN(FileName const & m, Buffer * b) : VCS(b)
1158 // Here we know that the buffer file is either already in SVN or
1159 // about to be registered
1166 FileName const SVN::findFile(FileName const & file)
1168 // First we check the existence of repository meta data.
1169 if (!VCS::checkparentdirs(file, ".svn")) {
1170 LYXERR(Debug::LYXVC, "Cannot find SVN meta data for " << file);
1174 // Now we check the status of the file.
1175 TempFile tempfile("lyxvcout");
1176 FileName tmpf = tempfile.name();
1178 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1182 string const fname = onlyFileName(file.absFileName());
1183 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn control for `" << fname << '\'');
1184 bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
1185 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1187 LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
1188 return found ? file : FileName();
1192 void SVN::scanMaster()
1194 // vcstatus code is somewhat superflous,
1195 // until we want to implement read-only toggle for svn.
1196 vcstatus = NOLOCKING;
1197 if (checkLockMode()) {
1201 vcstatus = UNLOCKED;
1206 bool SVN::checkLockMode()
1208 TempFile tempfile("lyxvcout");
1209 FileName tmpf = tempfile.name();
1211 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1215 LYXERR(Debug::LYXVC, "Detecting locking mode...");
1216 if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1217 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1218 FileName(owner_->filePath())))
1221 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1225 while (ifs && !ret) {
1227 LYXERR(Debug::LYXVC, line);
1228 if (contains(line, "svn:needs-lock"))
1231 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
1239 bool SVN::isLocked() const
1241 FileName file(owner_->absFileName());
1243 return !file.isReadOnly();
1247 bool SVN::retrieve(FileName const & file)
1249 LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
1250 // The caller ensures that file does not exist, so no need to check that.
1251 return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
1252 file.onlyPath()) == 0;
1256 void SVN::registrer(string const & /*msg*/)
1258 doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
1259 FileName(owner_->filePath()));
1263 bool SVN::renameEnabled()
1269 string SVN::rename(support::FileName const & newFile, string const & msg)
1271 // svn move does not require a log message, since it does not commit.
1272 // In LyX we commit immediately afterwards, otherwise it could be
1273 // confusing to the user to have two uncommitted files.
1274 FileName path(owner_->filePath());
1275 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1276 string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
1277 ' ' + quoteName(relFile));
1278 if (doVCCommand(cmd, path)) {
1279 cmd = "svn revert -q " +
1280 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1282 doVCCommand(cmd, path);
1283 if (newFile.exists())
1284 newFile.removeFile();
1287 vector<support::FileName> f;
1288 f.push_back(owner_->fileName());
1289 f.push_back(newFile);
1291 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1292 cmd = "svn revert -q " +
1293 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1295 doVCCommand(cmd, path);
1296 if (newFile.exists())
1297 newFile.removeFile();
1304 bool SVN::copyEnabled()
1310 string SVN::copy(support::FileName const & newFile, string const & msg)
1312 // svn copy does not require a log message, since it does not commit.
1313 // In LyX we commit immediately afterwards, otherwise it could be
1314 // confusing to the user to have an uncommitted file.
1315 FileName path(owner_->filePath());
1316 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1317 string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
1318 ' ' + quoteName(relFile));
1319 if (doVCCommand(cmd, path))
1321 vector<support::FileName> f(1, newFile);
1323 if (checkIn(f, msg, log) == LyXVC::VCSuccess)
1329 LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
1331 vector<support::FileName> f(1, owner_->fileName());
1332 return checkIn(f, msg, log);
1336 LyXVC::CommandResult
1337 SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1339 TempFile tempfile("lyxvcout");
1340 FileName tmpf = tempfile.name();
1342 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1343 log = N_("Error: Could not generate logfile.");
1344 return LyXVC::ErrorBefore;
1348 os << "svn commit -m \"" << msg << '"';
1349 for (size_t i = 0; i < f.size(); ++i)
1350 os << ' ' << quoteName(f[i].onlyFileName());
1351 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1352 LyXVC::CommandResult ret =
1353 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1354 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1356 string res = scanLogFile(tmpf, log);
1358 frontend::Alert::error(_("Revision control error."),
1359 _("Error when committing to repository.\n"
1360 "You have to manually resolve the problem.\n"
1361 "LyX will reopen the document after you press OK."));
1362 ret = LyXVC::ErrorCommand;
1365 if (!fileLock(false, tmpf, log))
1366 ret = LyXVC::ErrorCommand;
1369 log.insert(0, "SVN: ");
1370 if (ret == LyXVC::VCSuccess && log.empty())
1371 log = "SVN: Proceeded";
1376 bool SVN::checkInEnabled()
1385 bool SVN::isCheckInWithConfirmation()
1387 // FIXME one day common getDiff and perhaps OpMode for all backends
1389 TempFile tempfile("lyxvcout");
1390 FileName tmpf = tempfile.name();
1392 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1396 doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
1397 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1398 FileName(owner_->filePath()));
1400 docstring diff = tmpf.fileContents("UTF-8");
1409 // FIXME Correctly return code should be checked instead of this.
1410 // This would need another solution than just plain startscript.
1411 // Hint from Andre': QProcess::readAllStandardError()...
1412 string SVN::scanLogFile(FileName const & f, string & status)
1414 ifstream ifs(f.toFilesystemEncoding().c_str());
1419 LYXERR(Debug::LYXVC, line << '\n');
1421 status += line + "; ";
1422 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
1423 || contains(line, "Commit failed")) {
1427 if (contains(line, "svn:needs-lock")) {
1437 bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
1439 if (!locked_mode_ || (isLocked() == lock))
1442 string const arg = lock ? "lock " : "unlock ";
1443 doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
1444 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1445 FileName(owner_->filePath()));
1447 // Lock error messages go unfortunately on stderr and are unreachable this way.
1448 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1452 if (!line.empty()) status += line + "; ";
1456 if (isLocked() == lock)
1460 frontend::Alert::error(_("Revision control error."),
1461 _("Error while acquiring write lock.\n"
1462 "Another user is most probably editing\n"
1463 "the current document now!\n"
1464 "Also check the access to the repository."));
1466 frontend::Alert::error(_("Revision control error."),
1467 _("Error while releasing write lock.\n"
1468 "Check the access to the repository."));
1473 string SVN::checkOut()
1475 TempFile tempfile("lyxvcout");
1476 FileName tmpf = tempfile.name();
1478 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1479 return N_("Error: Could not generate logfile.");
1482 doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
1483 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1484 FileName(owner_->filePath()));
1487 string const res = scanLogFile(tmpf, log);
1489 frontend::Alert::error(_("Revision control error."),
1490 bformat(_("Error when updating from repository.\n"
1491 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
1492 "After pressing OK, LyX will try to reopen the resolved document."),
1493 from_local8bit(res)));
1495 fileLock(true, tmpf, log);
1497 return log.empty() ? string() : "SVN: " + log;
1501 bool SVN::checkOutEnabled()
1510 string SVN::repoUpdate()
1512 TempFile tempfile("lyxvcout");
1513 FileName tmpf = tempfile.name();
1515 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1516 return N_("Error: Could not generate logfile.");
1519 doVCCommand("svn diff " + quoteName(owner_->filePath())
1520 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1521 FileName(owner_->filePath()));
1522 docstring res = tmpf.fileContents("UTF-8");
1524 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
1525 docstring const file = from_utf8(owner_->filePath());
1526 docstring text = bformat(_("There were detected changes "
1527 "in the working directory:\n%1$s\n\n"
1528 "In case of file conflict version of the local directory files "
1529 "will be preferred."
1530 "\n\nContinue?"), file);
1531 int ret = frontend::Alert::prompt(_("Changes detected"),
1532 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
1534 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
1535 ret = frontend::Alert::prompt(_("Changes detected"),
1536 text, 0, 1, _("&Yes"), _("&No"));
1537 hideDialogs("file", 0);
1543 // Reverting looks too harsh, see bug #6255.
1544 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
1545 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
1546 // FileName(owner_->filePath()));
1547 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
1548 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
1549 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1550 FileName(owner_->filePath()));
1551 res += "Update log:\n" + tmpf.fileContents("UTF-8");
1553 LYXERR(Debug::LYXVC, res);
1554 return to_utf8(res);
1558 bool SVN::repoUpdateEnabled()
1564 string SVN::lockingToggle()
1566 TempFile tempfile("lyxvcout");
1567 FileName tmpf = tempfile.name();
1569 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1570 return N_("Error: Could not generate logfile.");
1573 int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
1574 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1575 FileName(owner_->filePath()));
1580 string res = scanLogFile(tmpf, log);
1581 bool locking = contains(res, "svn:needs-lock");
1583 ret = doVCCommand("svn propset svn:needs-lock ON "
1584 + quoteName(onlyFileName(owner_->absFileName()))
1585 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1586 FileName(owner_->filePath()));
1588 ret = doVCCommand("svn propdel svn:needs-lock "
1589 + quoteName(onlyFileName(owner_->absFileName()))
1590 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1591 FileName(owner_->filePath()));
1595 frontend::Alert::warning(_("SVN File Locking"),
1596 (locking ? _("Locking property unset.") : _("Locking property set.")) + '\n'
1597 + _("Do not forget to commit the locking property into the repository."),
1600 return string("SVN: ") + (locking ?
1601 N_("Locking property unset.") : N_("Locking property set."));
1605 bool SVN::lockingToggleEnabled()
1613 // Reverts to the version in SVN repository and
1614 // gets the updated version from the repository.
1615 string const fil = quoteName(onlyFileName(owner_->absFileName()));
1617 if (doVCCommand("svn revert -q " + fil,
1618 FileName(owner_->filePath())))
1620 owner_->markClean();
1625 bool SVN::isRevertWithConfirmation()
1627 //FIXME owner && diff
1632 void SVN::undoLast()
1634 // merge the current with the previous version
1635 // in a reverse patch kind of way, so that the
1636 // result is to revert the last changes.
1637 lyxerr << "Sorry, not implemented." << endl;
1641 bool SVN::undoLastEnabled()
1647 string SVN::revisionInfo(LyXVC::RevisionInfo const info)
1649 if (info == LyXVC::Tree) {
1650 if (rev_tree_cache_.empty())
1651 if (!getTreeRevisionInfo())
1652 rev_tree_cache_ = "?";
1653 if (rev_tree_cache_ == "?")
1656 return rev_tree_cache_;
1659 // fill the rest of the attributes for a single file
1660 if (rev_file_cache_.empty())
1661 if (!getFileRevisionInfo())
1662 rev_file_cache_ = "?";
1666 if (rev_file_cache_ == "?")
1668 return rev_file_cache_;
1670 return rev_author_cache_;
1672 return rev_date_cache_;
1674 return rev_time_cache_;
1683 bool SVN::getFileRevisionInfo()
1685 TempFile tempfile("lyxvcout");
1686 FileName tmpf = tempfile.name();
1688 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1692 doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
1693 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1694 FileName(owner_->filePath()));
1699 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1707 LYXERR(Debug::LYXVC, line);
1708 if (prefixIs(line, "<commit"))
1710 if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
1711 string l1 = subst(line, "revision=\"", "");
1712 string l2 = trim(subst(l1, "\">", ""));
1714 rev_file_cache_ = rev = l2;
1716 if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
1717 string l1 = subst(line, "<author>", "");
1718 string l2 = subst(l1, "</author>", "");
1719 rev_author_cache_ = l2;
1721 if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
1722 string l1 = subst(line, "<date>", "");
1723 string l2 = subst(l1, "</date>", "");
1724 l2 = split(l2, l1, 'T');
1725 rev_date_cache_ = l1;
1726 l2 = split(l2, l1, '.');
1727 rev_time_cache_ = l1;
1732 return !rev.empty();
1736 bool SVN::getTreeRevisionInfo()
1738 TempFile tempfile("lyxvcout");
1739 FileName tmpf = tempfile.name();
1741 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1745 doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
1746 FileName(owner_->filePath()));
1751 // only first line in case something bad happens.
1752 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
1757 rev_tree_cache_ = line;
1758 return !line.empty();
1762 void SVN::getLog(FileName const & tmpf)
1764 doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
1765 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1766 FileName(owner_->filePath()));
1770 bool SVN::prepareFileRevision(string const & revis, string & f)
1772 if (!isStrInt(revis))
1775 int rev = convert<int>(revis);
1777 if (!getFileRevisionInfo())
1780 rev = convert<int>(rev_file_cache_);
1781 // go back for minus rev
1783 rev = rev + convert<int>(rev_file_cache_);
1788 string revname = convert<string>(rev);
1789 TempFile tempfile("lyxvcrev_" + revname + '_');
1790 tempfile.setAutoRemove(false);
1791 FileName tmpf = tempfile.name();
1793 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1797 doVCCommand("svn cat -r " + revname + ' '
1798 + quoteName(onlyFileName(owner_->absFileName()))
1799 + " > " + quoteName(tmpf.toFilesystemEncoding()),
1800 FileName(owner_->filePath()));
1802 if (tmpf.isFileEmpty())
1805 f = tmpf.absFileName();
1810 bool SVN::prepareFileRevisionEnabled()
1817 bool SVN::toggleReadOnlyEnabled()
1823 /////////////////////////////////////////////////////////////////////
1827 /////////////////////////////////////////////////////////////////////
1829 GIT::GIT(FileName const & m, Buffer * b) : VCS(b)
1831 // Here we know that the buffer file is either already in GIT or
1832 // about to be registered
1838 FileName const GIT::findFile(FileName const & file)
1840 // First we check the existence of repository meta data.
1841 if (!VCS::checkparentdirs(file, ".git")) {
1842 LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
1846 // Now we check the status of the file.
1847 TempFile tempfile("lyxvcout");
1848 FileName tmpf = tempfile.name();
1850 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1854 string const fname = onlyFileName(file.absFileName());
1855 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
1857 doVCCommandCall("git ls-files " +
1858 quoteName(fname) + " > " +
1859 quoteName(tmpf.toFilesystemEncoding()),
1862 bool found = !tmpf.isFileEmpty();
1863 LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
1864 return found ? file : FileName();
1868 void GIT::scanMaster()
1870 // vcstatus code is somewhat superflous,
1871 // until we want to implement read-only toggle for git.
1872 vcstatus = NOLOCKING;
1876 bool GIT::retrieve(FileName const & file)
1878 LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
1879 // The caller ensures that file does not exist, so no need to check that.
1880 return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
1881 file.onlyPath()) == 0;
1885 void GIT::registrer(string const & /*msg*/)
1887 doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
1888 FileName(owner_->filePath()));
1892 bool GIT::renameEnabled()
1898 string GIT::rename(support::FileName const & newFile, string const & msg)
1900 // git mv does not require a log message, since it does not commit.
1901 // In LyX we commit immediately afterwards, otherwise it could be
1902 // confusing to the user to have two uncommitted files.
1903 FileName path(owner_->filePath());
1904 string relFile(to_utf8(newFile.relPath(path.absFileName())));
1905 string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
1906 ' ' + quoteName(relFile));
1907 if (doVCCommand(cmd, path)) {
1908 cmd = "git checkout -q " +
1909 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1911 doVCCommand(cmd, path);
1912 if (newFile.exists())
1913 newFile.removeFile();
1916 vector<support::FileName> f;
1917 f.push_back(owner_->fileName());
1918 f.push_back(newFile);
1920 if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
1921 cmd = "git checkout -q " +
1922 quoteName(onlyFileName(owner_->absFileName())) + ' ' +
1924 doVCCommand(cmd, path);
1925 if (newFile.exists())
1926 newFile.removeFile();
1933 bool GIT::copyEnabled()
1939 string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)
1941 // git does not support copy with history preservation
1946 LyXVC::CommandResult GIT::checkIn(string const & msg, string & log)
1948 vector<support::FileName> f(1, owner_->fileName());
1949 return checkIn(f, msg, log);
1953 LyXVC::CommandResult
1954 GIT::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
1956 TempFile tempfile("lyxvcout");
1957 FileName tmpf = tempfile.name();
1959 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
1960 log = N_("Error: Could not generate logfile.");
1961 return LyXVC::ErrorBefore;
1965 os << "git commit -m \"" << msg << '"';
1966 for (size_t i = 0; i < f.size(); ++i)
1967 os << ' ' << quoteName(f[i].onlyFileName());
1968 os << " > " << quoteName(tmpf.toFilesystemEncoding());
1969 LyXVC::CommandResult ret =
1970 doVCCommand(os.str(), FileName(owner_->filePath())) ?
1971 LyXVC::ErrorCommand : LyXVC::VCSuccess;
1973 string res = scanLogFile(tmpf, log);
1975 frontend::Alert::error(_("Revision control error."),
1976 _("Error when committing to repository.\n"
1977 "You have to manually resolve the problem.\n"
1978 "LyX will reopen the document after you press OK."));
1979 ret = LyXVC::ErrorCommand;
1983 log.insert(0, "GIT: ");
1984 if (ret == LyXVC::VCSuccess && log.empty())
1985 log = "GIT: Proceeded";
1990 bool GIT::checkInEnabled()
1996 bool GIT::isCheckInWithConfirmation()
1998 // FIXME one day common getDiff and perhaps OpMode for all backends
2000 TempFile tempfile("lyxvcout");
2001 FileName tmpf = tempfile.name();
2003 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2007 doVCCommandCall("git diff " + quoteName(owner_->absFileName())
2008 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2009 FileName(owner_->filePath()));
2011 docstring diff = tmpf.fileContents("UTF-8");
2020 // FIXME Correctly return code should be checked instead of this.
2021 // This would need another solution than just plain startscript.
2022 // Hint from Andre': QProcess::readAllStandardError()...
2023 string GIT::scanLogFile(FileName const & f, string & status)
2025 ifstream ifs(f.toFilesystemEncoding().c_str());
2030 LYXERR(Debug::LYXVC, line << "\n");
2032 status += line + "; ";
2033 if (prefixIs(line, "C ") || prefixIs(line, "CU ")
2034 || contains(line, "Commit failed")) {
2044 string GIT::checkOut()
2050 bool GIT::checkOutEnabled()
2056 string GIT::repoUpdate()
2062 bool GIT::repoUpdateEnabled()
2068 string GIT::lockingToggle()
2074 bool GIT::lockingToggleEnabled()
2082 // Reverts to the version in GIT repository and
2083 // gets the updated version from the repository.
2084 string const fil = quoteName(onlyFileName(owner_->absFileName()));
2086 if (doVCCommand("git checkout -q " + fil,
2087 FileName(owner_->filePath())))
2089 owner_->markClean();
2094 bool GIT::isRevertWithConfirmation()
2096 //FIXME owner && diff
2101 void GIT::undoLast()
2103 // merge the current with the previous version
2104 // in a reverse patch kind of way, so that the
2105 // result is to revert the last changes.
2106 lyxerr << "Sorry, not implemented." << endl;
2110 bool GIT::undoLastEnabled()
2116 string GIT::revisionInfo(LyXVC::RevisionInfo const info)
2118 if (info == LyXVC::Tree) {
2119 if (rev_tree_cache_.empty())
2120 if (!getTreeRevisionInfo())
2121 rev_tree_cache_ = "?";
2122 if (rev_tree_cache_ == "?")
2125 return rev_tree_cache_;
2128 // fill the rest of the attributes for a single file
2129 if (rev_file_cache_.empty())
2130 if (!getFileRevisionInfo())
2131 rev_file_cache_ = "?";
2135 if (rev_file_cache_ == "?")
2137 return rev_file_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%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_author_cache_);
2177 rev_time_cache_ = split(line, rev_date_cache_, ' ');
2181 return !rev_file_cache_.empty();
2185 bool GIT::getTreeRevisionInfo()
2187 TempFile tempfile("lyxvcout");
2188 FileName tmpf = tempfile.name();
2190 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2194 doVCCommand("git describe --abbrev --dirty --long > " + quoteName(tmpf.toFilesystemEncoding()),
2195 FileName(owner_->filePath()),
2196 false); //git describe returns $?=128 when no tag found (but git repo still exists)
2201 // only first line in case something bad happens.
2202 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
2203 getline(ifs, rev_tree_cache_);
2206 return !rev_tree_cache_.empty();
2210 void GIT::getLog(FileName const & tmpf)
2212 doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))
2213 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2214 FileName(owner_->filePath()));
2218 //at this moment we don't accept revision SHA, but just number of revision steps back
2219 //GUI and infrastucture needs to be changed first
2220 bool GIT::prepareFileRevision(string const & revis, string & f)
2222 // anything positive means we got hash, not "0" or minus revision
2225 // hash is rarely number and should be long
2226 if (isStrInt(revis) && revis.length()<20)
2227 rev = convert<int>(revis);
2229 // revision and filename
2232 // go back for "minus" revisions
2234 pointer = "HEAD~" + convert<string>(-rev);
2241 TempFile tempfile("lyxvcrev_" + revis + '_');
2242 tempfile.setAutoRemove(false);
2243 FileName tmpf = tempfile.name();
2245 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
2249 doVCCommand("git show " + pointer + "./"
2250 + quoteName(onlyFileName(owner_->absFileName()))
2251 + " > " + quoteName(tmpf.toFilesystemEncoding()),
2252 FileName(owner_->filePath()));
2254 if (tmpf.isFileEmpty())
2257 f = tmpf.absFileName();
2262 bool GIT::prepareFileRevisionEnabled()
2268 bool GIT::toggleReadOnlyEnabled()