+ return string();
+}
+
+
+LyXVC::CommandResult CVS::checkIn(string const & msg, string & log)
+{
+ CvsStatus status = getStatus();
+ switch (status) {
+ case UpToDate:
+ if (vcstatus != NOLOCKING)
+ if (unedit())
+ return LyXVC::ErrorCommand;
+ log = "CVS: Proceeded";
+ return LyXVC::VCSuccess;
+ case LocallyModified:
+ case LocallyAdded: {
+ int rc = doVCCommand("cvs -q commit -m \"" + msg + "\" "
+ + getTarget(File),
+ FileName(owner_->filePath()));
+ if (rc)
+ return LyXVC::ErrorCommand;
+ log = "CVS: Proceeded";
+ return LyXVC::VCSuccess;
+ }
+ case NeedsMerge:
+ case NeedsCheckout:
+ frontend::Alert::error(_("Revision control error."),
+ _("The repository version is newer then the current check out.\n"
+ "You have to update from repository first or revert your changes.")) ;
+ break;
+ default:
+ frontend::Alert::error(_("Revision control error."),
+ bformat(_("Bad status when checking in changes.\n"
+ "\n'%1$s'\n\n"),
+ toString(status)));
+ break;
+ }
+ return LyXVC::ErrorBefore;
+}
+
+
+bool CVS::isLocked() const
+{
+ FileName fn(owner_->absFileName());
+ fn.refresh();
+ return !fn.isReadOnly();
+}
+
+
+bool CVS::checkInEnabled()
+{
+ if (vcstatus != NOLOCKING)
+ return isLocked();
+ else
+ return true;
+}
+
+
+bool CVS::isCheckInWithConfirmation()
+{
+ CvsStatus status = getStatus();
+ return status == LocallyModified || status == LocallyAdded;
+}
+
+
+string CVS::checkOut()
+{
+ if (vcstatus != NOLOCKING && edit())
+ return string();
+ TempFile tempfile("lyxvout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return string();
+ }
+
+ int rc = update(File, tmpf);
+ 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)));
+ rc = 0;
+ }
+
+ return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
+}
+
+
+bool CVS::checkOutEnabled()
+{
+ if (vcstatus != NOLOCKING)
+ return !isLocked();
+ else
+ return true;
+}
+
+
+string CVS::repoUpdate()
+{
+ TempFile tempfile("lyxvout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return string();
+ }
+
+ getDiff(Directory, tmpf);
+ 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"
+ "Possible file conflicts must be then resolved manually "
+ "or you will need to revert back to the repository version."), file);
+ int ret = frontend::Alert::prompt(_("Changes detected"),
+ text, 0, 1, _("&Continue"), _("&Abort"), _("View &Log ..."));
+ if (ret == 2) {
+ dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFileName()));
+ ret = frontend::Alert::prompt(_("Changes detected"),
+ text, 0, 1, _("&Continue"), _("&Abort"));
+ hideDialogs("file", 0);
+ }
+ if (ret == 1)
+ return string();
+ }
+
+ int rc = update(Directory, tmpf);
+ res += "Update log:\n" + tmpf.fileContents("UTF-8");
+ LYXERR(Debug::LYXVC, res);
+
+ string log;
+ string sres = scanLogFile(tmpf, log);
+ if (!sres.empty()) {
+ docstring const file = owner_->fileName().displayName(20);
+ frontend::Alert::error(_("Revision control error."),
+ bformat(_("Error when updating document %1$s from repository.\n"
+ "You have to manually resolve the conflicts NOW!\n'%2$s'.\n\n"
+ "After pressing OK, LyX will try to reopen the resolved document."),
+ file, from_local8bit(sres)));
+ rc = 0;
+ }
+
+ return rc ? string() : log.empty() ? "CVS: Proceeded" : "CVS: " + log;
+}
+
+
+bool CVS::repoUpdateEnabled()
+{
+ return true;
+}
+
+
+string CVS::lockingToggle()
+{
+ lyxerr << "Sorry, not implemented." << endl;
+ return string();
+}
+
+
+bool CVS::lockingToggleEnabled()
+{
+ return false;
+}
+
+
+bool CVS::isRevertWithConfirmation()
+{
+ CvsStatus status = getStatus();
+ return !owner_->isClean() || status == LocallyModified || status == NeedsMerge;
+}
+
+
+bool CVS::revert()
+{
+ // Reverts to the version in CVS repository and
+ // gets the updated version from the repository.
+ CvsStatus status = getStatus();
+ switch (status) {
+ case UpToDate:
+ if (vcstatus != NOLOCKING)
+ return 0 == unedit();
+ break;
+ case NeedsMerge:
+ case NeedsCheckout:
+ case LocallyModified: {
+ FileName f(owner_->absFileName());
+ f.removeFile();
+ update(File, FileName());
+ owner_->markClean();
+ break;
+ }
+ case LocallyAdded: {
+ docstring const file = owner_->fileName().displayName(20);
+ frontend::Alert::error(_("Revision control error."),
+ bformat(_("The document %1$s is not in repository.\n"
+ "You have to check in the first revision before you can revert."),
+ file)) ;
+ return false;
+ }
+ default: {
+ docstring const file = owner_->fileName().displayName(20);
+ frontend::Alert::error(_("Revision control error."),
+ bformat(_("Cannot revert document %1$s to repository version.\n"
+ "The status '%2$s' is unexpected."),
+ file, toString(status)));
+ return false;
+ }
+ }
+ return true;
+}
+
+
+void CVS::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 CVS::undoLastEnabled()
+{
+ return false;
+}
+
+
+void CVS::getLog(FileName const & tmpf)
+{
+ doVCCommandWithOutput("cvs log " + getTarget(File),
+ FileName(owner_->filePath()),
+ tmpf);
+}
+
+
+bool CVS::toggleReadOnlyEnabled()
+{
+ return false;
+}
+
+
+string CVS::revisionInfo(LyXVC::RevisionInfo const info)
+{
+ if (!version_.empty()) {
+ getRevisionInfo();
+ switch (info) {
+ case LyXVC::File:
+ return version_;
+ 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 CVS::prepareFileRevision(string const & revis, string & f)
+{
+ string rev = revis;
+ if (!VCS::makeRCSRevision(version_, rev))
+ return false;
+
+ TempFile tempfile("lyxvcrev_" + rev + '_');
+ tempfile.setAutoRemove(false);
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()) {
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return false;
+ }
+
+ doVCCommandWithOutput("cvs update -p -r" + rev + ' '
+ + getTarget(File),
+ FileName(owner_->filePath()), tmpf);
+ tmpf.refresh();
+ if (tmpf.isFileEmpty())
+ return false;
+
+ f = tmpf.absFileName();
+ return true;
+}
+
+
+bool CVS::prepareFileRevisionEnabled()
+{
+ return true;
+}
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// SVN
+//
+/////////////////////////////////////////////////////////////////////
+
+SVN::SVN(FileName const & m, Buffer * b) : VCS(b)
+{
+ // Here we know that the buffer file is either already in SVN or
+ // about to be registered
+ master_ = m;
+ locked_mode_ = 0;
+ scanMaster();
+}
+
+
+FileName const SVN::findFile(FileName const & file)
+{
+ // First we check the existence of repository meta data.
+ if (!VCS::checkparentdirs(file, ".svn")) {
+ LYXERR(Debug::LYXVC, "Cannot find SVN 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 svn control for `" << fname << '\'');
+ bool found = 0 == doVCCommandCall("svn info " + quoteName(fname)
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ file.onlyPath());
+ LYXERR(Debug::LYXVC, "SVN control: " << (found ? "enabled" : "disabled"));
+ return found ? file : FileName();
+}
+
+
+void SVN::scanMaster()
+{
+ // vcstatus code is somewhat superflous,
+ // until we want to implement read-only toggle for svn.
+ vcstatus = NOLOCKING;
+ if (checkLockMode()) {
+ if (isLocked())
+ vcstatus = LOCKED;
+ else
+ vcstatus = UNLOCKED;
+ }
+}
+
+
+bool SVN::checkLockMode()
+{
+ TempFile tempfile("lyxvcout");
+ FileName tmpf = tempfile.name();
+ if (tmpf.empty()){
+ LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
+ return false;
+ }
+
+ LYXERR(Debug::LYXVC, "Detecting locking mode...");
+ if (doVCCommandCall("svn proplist " + quoteName(onlyFileName(owner_->absFileName()))
+ + " > " + quoteName(tmpf.toFilesystemEncoding()),
+ FileName(owner_->filePath())))
+ return false;
+
+ ifstream ifs(tmpf.toFilesystemEncoding().c_str());
+ string line;
+ bool ret = false;
+
+ while (ifs && !ret) {
+ getline(ifs, line);
+ LYXERR(Debug::LYXVC, line);
+ if (contains(line, "svn:needs-lock"))
+ ret = true;
+ }
+ LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
+ ifs.close();
+ locked_mode_ = ret;
+ return ret;
+
+}
+
+
+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";