+bool SVN::isLocked() const
+{
+ FileName file(owner_->absFileName());
+ file.refresh();
+ return !file.isReadOnly();
+}
+
+
+bool SVN::retrieve(FileName const & file)
+{
+ LYXERR(Debug::LYXVC, "LyXVC::SVN: retrieve.\n\t" << file);
+ // The caller ensures that file does not exist, so no need to check that.
+ return doVCCommandCall("svn update -q --non-interactive " + quoteName(file.onlyFileName()),
+ file.onlyPath()) == 0;
+}
+
+
+void SVN::registrer(string const & /*msg*/)
+{
+ doVCCommand("svn add -q " + quoteName(onlyFileName(owner_->absFileName())),
+ FileName(owner_->filePath()));
+}
+
+
+bool SVN::renameEnabled()
+{
+ return true;
+}
+
+
+string SVN::rename(support::FileName const & newFile, string const & msg)
+{
+ // svn move does not require a log message, since it does not commit.
+ // In LyX we commit immediately afterwards, otherwise it could be
+ // confusing to the user to have two uncommitted files.
+ FileName path(owner_->filePath());
+ string relFile(to_utf8(newFile.relPath(path.absFileName())));
+ string cmd("svn move -q " + quoteName(onlyFileName(owner_->absFileName())) +
+ ' ' + quoteName(relFile));
+ if (doVCCommand(cmd, path)) {
+ cmd = "svn revert -q " +
+ quoteName(onlyFileName(owner_->absFileName())) + ' ' +
+ quoteName(relFile);
+ doVCCommand(cmd, path);
+ if (newFile.exists())
+ newFile.removeFile();
+ return string();
+ }
+ vector<support::FileName> f;
+ f.push_back(owner_->fileName());
+ f.push_back(newFile);
+ string log;
+ if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
+ cmd = "svn revert -q " +
+ quoteName(onlyFileName(owner_->absFileName())) + ' ' +
+ quoteName(relFile);
+ doVCCommand(cmd, path);
+ if (newFile.exists())
+ newFile.removeFile();
+ return string();
+ }
+ return log;
+}
+
+
+bool SVN::copyEnabled()
+{
+ return true;
+}
+
+
+string SVN::copy(support::FileName const & newFile, string const & msg)
+{
+ // svn copy does not require a log message, since it does not commit.
+ // In LyX we commit immediately afterwards, otherwise it could be
+ // confusing to the user to have an uncommitted file.
+ FileName path(owner_->filePath());
+ string relFile(to_utf8(newFile.relPath(path.absFileName())));
+ string cmd("svn copy -q " + quoteName(onlyFileName(owner_->absFileName())) +
+ ' ' + quoteName(relFile));
+ if (doVCCommand(cmd, path))
+ return string();
+ vector<support::FileName> f(1, newFile);
+ string log;
+ if (checkIn(f, msg, log) == LyXVC::VCSuccess)
+ return log;
+ return string();
+}
+
+
+LyXVC::CommandResult SVN::checkIn(string const & msg, string & log)
+{
+ vector<support::FileName> f(1, owner_->fileName());
+ return checkIn(f, msg, log);
+}
+
+
+LyXVC::CommandResult
+SVN::checkIn(vector<support::FileName> const & f, string const & msg, string & log)
+{
+ TempFile tempfile("lyxvcout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()){
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ log = N_("Error: Could not generate logfile.");
+ return LyXVC::ErrorBefore;
+ }
+
+ ostringstream os;
+ os << "svn commit -m \"" << msg << '"';
+ for (size_t i = 0; i < f.size(); ++i)
+ os << ' ' << quoteName(f[i].onlyFileName());
+ os << " > " << quoteName(tmpf.toFilesystemEncoding());
+ LyXVC::CommandResult ret =
+ doVCCommand(os.str(), FileName(owner_->filePath())) ?
+ LyXVC::ErrorCommand : LyXVC::VCSuccess;
+
+ string res = scanLogFile(tmpf, log);
+ if (!res.empty()) {
+ frontend::Alert::error(_("Revision control error."),
+ _("Error when committing to repository.\n"
+ "You have to manually resolve the problem.\n"
+ "LyX will reopen the document after you press OK."));
+ ret = LyXVC::ErrorCommand;
+ }
+ else
+ if (!fileLock(false, tmpf, log))
+ ret = LyXVC::ErrorCommand;
+
+ if (!log.empty())
+ log.insert(0, "SVN: ");
+ if (ret == LyXVC::VCSuccess && log.empty())
+ log = "SVN: Proceeded";
+ return ret;
+}
+
+
+bool SVN::checkInEnabled()
+{
+ if (locked_mode_)
+ return isLocked();
+ else
+ return true;
+}
+
+
+bool SVN::isCheckInWithConfirmation()
+{
+ // FIXME one day common getDiff and perhaps OpMode for all backends
+
+ TempFile tempfile("lyxvcout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return true;
+ }
+
+ doVCCommandCall("svn diff " + quoteName(owner_->absFileName())
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+
+ docstring diff = tmpf.fileContents("UTF-8");
+
+ if (diff.empty())
+ return false;
+
+ return true;
+}
+
+
+// FIXME Correctly return code should be checked instead of this.
+// This would need another solution than just plain startscript.
+// Hint from Andre': QProcess::readAllStandardError()...
+string SVN::scanLogFile(FileName const & f, string & status)
+{
+ ifstream ifs(f.toFilesystemEncoding().c_str());
+ string line;
+
+ while (ifs) {
+ getline(ifs, line);
+ LYXERR(Debug::LYXVC, line << '\n');
+ if (!line.empty())
+ status += line + "; ";
+ if (prefixIs(line, "C ") || prefixIs(line, "CU ")
+ || contains(line, "Commit failed")) {
+ ifs.close();
+ return line;
+ }
+ if (contains(line, "svn:needs-lock")) {
+ ifs.close();
+ return line;
+ }
+ }
+ ifs.close();
+ return string();
+}
+
+
+bool SVN::fileLock(bool lock, FileName const & tmpf, string &status)
+{
+ if (!locked_mode_ || (isLocked() == lock))
+ return true;
+
+ string const arg = lock ? "lock " : "unlock ";
+ doVCCommand("svn "+ arg + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+
+ // Lock error messages go unfortunately on stderr and are unreachable this way.
+ ifstream ifs(tmpf.toFilesystemEncoding().c_str());
+ string line;
+ while (ifs) {
+ getline(ifs, line);
+ if (!line.empty()) status += line + "; ";
+ }
+ ifs.close();
+
+ if (isLocked() == lock)
+ return true;
+
+ if (lock)
+ frontend::Alert::error(_("Revision control error."),
+ _("Error while acquiring write lock.\n"
+ "Another user is most probably editing\n"
+ "the current document now!\n"
+ "Also check the access to the repository."));
+ else
+ frontend::Alert::error(_("Revision control error."),
+ _("Error while releasing write lock.\n"
+ "Check the access to the repository."));
+ return false;
+}
+
+
+string SVN::checkOut()
+{
+ TempFile tempfile("lyxvcout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return N_("Error: Could not generate logfile.");
+ }
+
+ doVCCommand("svn update --non-interactive " + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+
+ string log;
+ string const res = scanLogFile(tmpf, log);
+ if (!res.empty())
+ frontend::Alert::error(_("Revision control error."),
+ bformat(_("Error when updating from repository.\n"
+ "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
+ "After pressing OK, LyX will try to reopen the resolved document."),
+ from_local8bit(res)));
+
+ fileLock(true, tmpf, log);
+
+ return log.empty() ? string() : "SVN: " + log;
+}
+
+
+bool SVN::checkOutEnabled()
+{
+ if (locked_mode_)
+ return !isLocked();
+ else
+ return true;
+}
+
+
+string SVN::repoUpdate()
+{
+ TempFile tempfile("lyxvcout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return N_("Error: Could not generate logfile.");
+ }
+
+ doVCCommand("svn diff " + quoteName(owner_->filePath())
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+ docstring res = tmpf.fileContents("UTF-8");
+ if (!res.empty()) {
+ LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
+ docstring const file = from_utf8(owner_->filePath());
+ docstring text = bformat(_("There were detected changes "
+ "in the working directory:\n%1$s\n\n"
+ "In case of file conflict version of the local directory files "
+ "will be preferred."
+ "\n\nContinue?"), file);
+ int ret = frontend::Alert::prompt(_("Changes detected"),
+ text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
+ if (ret == 2) {
+ dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
+ ret = frontend::Alert::prompt(_("Changes detected"),
+ text, 0, 1, _("&Yes"), _("&No"));
+ hideDialogs("file", 0);
+ }
+ if (ret == 1)
+ return string();
+ }
+
+ // Reverting looks too harsh, see bug #6255.
+ // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
+ // + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ // FileName(owner_->filePath()));
+ // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
+ doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+ res += "Update log:\n" + tmpf.fileContents("UTF-8");
+
+ LYXERR(Debug::LYXVC, res);
+ return to_utf8(res);
+}
+
+
+bool SVN::repoUpdateEnabled()
+{
+ return true;
+}
+
+
+string SVN::lockingToggle()
+{
+ TempFile tempfile("lyxvcout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return N_("Error: Could not generate logfile.");
+ }
+
+ int ret = doVCCommand("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+ if (ret)
+ return string();
+
+ string log;
+ string res = scanLogFile(tmpf, log);
+ bool locking = contains(res, "svn:needs-lock");
+ if (!locking)
+ ret = doVCCommand("svn propset svn:needs-lock ON "
+ + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+ else
+ ret = doVCCommand("svn propdel svn:needs-lock "
+ + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+ if (ret)
+ return string();
+
+ frontend::Alert::warning(_("SVN File Locking"),
+ (locking ? _("Locking property unset.") : _("Locking property set.")) + '\n'
+ + _("Do not forget to commit the locking property into the repository."),
+ true);
+
+ return string("SVN: ") + (locking ?
+ N_("Locking property unset.") : N_("Locking property set."));
+}
+
+
+bool SVN::lockingToggleEnabled()
+{
+ return true;
+}
+
+
+bool SVN::revert()
+{
+ // Reverts to the version in SVN repository and
+ // gets the updated version from the repository.
+ string const fil = quoteName(onlyFileName(owner_->absFileName()));
+
+ if (doVCCommand("svn revert -q " + fil,
+ FileName(owner_->filePath())))
+ return false;
+ owner_->markClean();
+ return true;
+}
+
+
+bool SVN::isRevertWithConfirmation()
+{
+ //FIXME owner && diff
+ return true;
+}
+
+
+void SVN::undoLast()
+{
+ // merge the current with the previous version
+ // in a reverse patch kind of way, so that the
+ // result is to revert the last changes.
+ lyxerr << "Sorry, not implemented." << endl;
+}
+
+
+bool SVN::undoLastEnabled()
+{
+ return false;
+}
+
+
+string SVN::revisionInfo(LyXVC::RevisionInfo const info)
+{
+ if (info == LyXVC::Tree) {
+ if (rev_tree_cache_.empty())
+ if (!getTreeRevisionInfo())
+ rev_tree_cache_ = "?";
+ if (rev_tree_cache_ == "?")
+ return string();
+
+ return rev_tree_cache_;
+ }
+
+ // fill the rest of the attributes for a single file
+ if (rev_file_cache_.empty())
+ if (!getFileRevisionInfo())
+ rev_file_cache_ = "?";
+
+ switch (info) {
+ case LyXVC::File:
+ if (rev_file_cache_ == "?")
+ return string();
+ return rev_file_cache_;
+ case LyXVC::Author:
+ return rev_author_cache_;
+ case LyXVC::Date:
+ return rev_date_cache_;
+ case LyXVC::Time:
+ return rev_time_cache_;
+ default: ;
+
+ }
+
+ return string();
+}
+
+
+bool SVN::getFileRevisionInfo()
+{
+ TempFile tempfile("lyxvcout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return false;
+ }
+
+ doVCCommand("svn info --xml " + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+
+ if (tmpf.empty())
+ return false;
+
+ ifstream ifs(tmpf.toFilesystemEncoding().c_str());
+ string line;
+ // commit log part
+ bool c = false;
+ string rev;
+
+ while (ifs) {
+ getline(ifs, line);
+ LYXERR(Debug::LYXVC, line);
+ if (prefixIs(line, "<commit"))
+ c = true;
+ if (c && prefixIs(line, " revision=\"") && suffixIs(line, "\">")) {
+ string l1 = subst(line, "revision=\"", "");
+ string l2 = trim(subst(l1, "\">", ""));
+ if (isStrInt(l2))
+ rev_file_cache_ = rev = l2;
+ }
+ if (c && prefixIs(line, "<author>") && suffixIs(line, "</author>")) {
+ string l1 = subst(line, "<author>", "");
+ string l2 = subst(l1, "</author>", "");
+ rev_author_cache_ = l2;
+ }
+ if (c && prefixIs(line, "<date>") && suffixIs(line, "</date>")) {
+ string l1 = subst(line, "<date>", "");
+ string l2 = subst(l1, "</date>", "");
+ l2 = split(l2, l1, 'T');
+ rev_date_cache_ = l1;
+ l2 = split(l2, l1, '.');
+ rev_time_cache_ = l1;
+ }
+ }
+
+ ifs.close();
+ return !rev.empty();
+}
+
+
+bool SVN::getTreeRevisionInfo()
+{
+ TempFile tempfile("lyxvcout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return false;
+ }
+
+ doVCCommand("svnversion -n . > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+
+ if (tmpf.empty())
+ return false;
+
+ // only first line in case something bad happens.
+ ifstream ifs(tmpf.toFilesystemEncoding().c_str());
+ string line;
+ getline(ifs, line);
+ ifs.close();
+
+ rev_tree_cache_ = line;
+ return !line.empty();
+}
+
+
+void SVN::getLog(FileName const & tmpf)
+{
+ doVCCommand("svn log " + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+}
+
+
+bool SVN::prepareFileRevision(string const & revis, string & f)
+{
+ if (!isStrInt(revis))
+ return false;
+
+ int rev = convert<int>(revis);
+ if (rev <= 0)
+ if (!getFileRevisionInfo())
+ return false;
+ if (rev == 0)
+ rev = convert<int>(rev_file_cache_);
+ // go back for minus rev
+ else if (rev < 0) {
+ rev = rev + convert<int>(rev_file_cache_);
+ if (rev < 1)
+ return false;
+ }
+
+ string revname = convert<string>(rev);
+ TempFile tempfile("lyxvcrev_" + revname + '_');
+ tempfile.setAutoRemove(false);
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return false;
+ }
+
+ doVCCommand("svn cat -r " + revname + ' '
+ + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath()));
+ tmpf.refresh();
+ if (tmpf.isFileEmpty())
+ return false;
+
+ f = tmpf.absFileName();
+ return true;
+}
+
+
+bool SVN::prepareFileRevisionEnabled()
+{
+ return true;
+}
+
+
+
+bool SVN::toggleReadOnlyEnabled()
+{
+ return false;
+}
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// GIT
+//
+/////////////////////////////////////////////////////////////////////
+
+GIT::GIT(FileName const & m, Buffer * b) : VCS(b)
+{
+ // Here we know that the buffer file is either already in GIT or
+ // about to be registered
+ master_ = m;
+ scanMaster();
+}
+
+
+FileName const GIT::findFile(FileName const & file)
+{
+ // First we check the existence of repository meta data.
+ if (!VCS::checkparentdirs(file, ".git")) {
+ LYXERR(Debug::LYXVC, "Cannot find GIT meta data for " << file);
+ return FileName();
+ }
+
+ // Now we check the status of the file.
+ TempFile tempfile("lyxvcout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return FileName();
+ }
+
+ string const fname = onlyFileName(file.absFileName());
+ LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under git control for `"
+ << fname << '\'');
+ doVCCommandCall("git ls-files " +
+ quoteName(fname) + " > " +
+ quoteName(tmpf.toFilesystemEncoding()),
+ file.onlyPath());
+ tmpf.refresh();
+ bool found = !tmpf.isFileEmpty();
+ LYXERR(Debug::LYXVC, "GIT control: " << (found ? "enabled" : "disabled"));
+ return found ? file : FileName();
+}
+
+
+void GIT::scanMaster()
+{
+ // vcstatus code is somewhat superflous,
+ // until we want to implement read-only toggle for git.
+ vcstatus = NOLOCKING;
+}
+
+
+bool GIT::retrieve(FileName const & file)
+{
+ LYXERR(Debug::LYXVC, "LyXVC::GIT: retrieve.\n\t" << file);
+ // The caller ensures that file does not exist, so no need to check that.
+ return doVCCommandCall("git checkout -q " + quoteName(file.onlyFileName()),
+ file.onlyPath()) == 0;
+}
+
+
+void GIT::registrer(string const & /*msg*/)
+{
+ doVCCommand("git add " + quoteName(onlyFileName(owner_->absFileName())),
+ FileName(owner_->filePath()));
+}
+
+
+bool GIT::renameEnabled()
+{
+ return true;
+}
+
+
+string GIT::rename(support::FileName const & newFile, string const & msg)
+{
+ // git mv does not require a log message, since it does not commit.
+ // In LyX we commit immediately afterwards, otherwise it could be
+ // confusing to the user to have two uncommitted files.
+ FileName path(owner_->filePath());
+ string relFile(to_utf8(newFile.relPath(path.absFileName())));
+ string cmd("git mv " + quoteName(onlyFileName(owner_->absFileName())) +
+ ' ' + quoteName(relFile));
+ if (doVCCommand(cmd, path)) {
+ cmd = "git checkout -q " +
+ quoteName(onlyFileName(owner_->absFileName())) + ' ' +
+ quoteName(relFile);
+ doVCCommand(cmd, path);
+ if (newFile.exists())
+ newFile.removeFile();
+ return string();
+ }
+ vector<support::FileName> f;
+ f.push_back(owner_->fileName());
+ f.push_back(newFile);
+ string log;
+ if (checkIn(f, msg, log) != LyXVC::VCSuccess) {
+ cmd = "git checkout -q " +
+ quoteName(onlyFileName(owner_->absFileName())) + ' ' +
+ quoteName(relFile);
+ doVCCommand(cmd, path);
+ if (newFile.exists())
+ newFile.removeFile();
+ return string();
+ }
+ return log;
+}
+
+
+bool GIT::copyEnabled()
+{
+ return false;
+}
+
+
+string GIT::copy(support::FileName const & /*newFile*/, string const & /*msg*/)