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
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
10 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
19 #include "VCBackend.h"
22 #include "frontends/alert.h"
24 #include "support/debug.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28 #include "support/TempFile.h"
31 using namespace lyx::support;
35 namespace Alert = frontend::Alert;
44 // for the sake of boost::scoped_ptr
49 bool LyXVC::fileInVC(FileName const & fn)
51 if (!RCS::findFile(fn).empty())
53 if (!CVS::findFile(fn).empty())
55 if (!SVN::findFile(fn).empty())
57 if (!GIT::findFile(fn).empty())
63 bool LyXVC::file_found_hook(FileName const & fn)
66 // Check if file is under RCS
67 if (!(found_file = RCS::findFile(fn)).empty()) {
68 vcs.reset(new RCS(found_file, owner_));
71 // Check if file is under CVS
72 if (!(found_file = CVS::findFile(fn)).empty()) {
73 vcs.reset(new CVS(found_file, owner_));
76 // Check if file is under SVN
77 if (!(found_file = SVN::findFile(fn)).empty()) {
78 vcs.reset(new SVN(found_file, owner_));
81 // Check if file is under GIT
82 if (!(found_file = GIT::findFile(fn)).empty()) {
83 vcs.reset(new GIT(found_file, owner_));
87 // file is not under any VCS.
93 bool LyXVC::file_not_found_hook(FileName const & fn)
95 // Check if file is under RCS.
96 // This happens if we are trying to load non existent
97 // file on disk, but existent in ,v version.
98 bool foundRCS = !RCS::findFile(fn).empty();
99 bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
100 bool foundSVN = (foundRCS || foundCVS) ? false : !SVN::findFile(fn).empty();
101 bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : !GIT::findFile(fn).empty();
102 if (foundRCS || foundCVS || foundSVN || foundGIT) {
103 docstring const file = makeDisplayPath(fn.absFileName(), 20);
104 docstring const text =
105 bformat(_("Do you want to retrieve the document"
106 " %1$s from version control?"), file);
107 int const ret = Alert::prompt(_("Retrieve from version control?"),
108 text, 0, 1, _("&Retrieve"), _("&Cancel"));
111 // Since the retrieve commands are implemented using
112 // more general update commands we need to ensure that
113 // we do not change an existing file by accident.
117 return RCS::retrieve(fn);
119 return CVS::retrieve(fn);
121 return SVN::retrieve(fn);
123 return GIT::retrieve(fn);
130 void LyXVC::setBuffer(Buffer * buf)
136 bool LyXVC::registrer()
138 FileName const filename = owner_->fileName();
140 // there must be a file to save
141 if (!filename.isReadableFile()) {
142 Alert::error(_("Document not saved"),
143 _("You must save the document "
144 "before it can be registered."));
148 // it is very likely here that the vcs is not created yet...
150 //check in the root directory of the document
151 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
152 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
153 FileName const git_index(onlyPath(filename.absFileName()) + "/.git/index");
155 if (git_index.isReadableFile()) {
156 LYXERR(Debug::LYXVC, "LyXVC: registering "
157 << to_utf8(filename.displayName()) << " with GIT");
158 vcs.reset(new GIT(git_index, owner_));
160 } else if (svn_entries.isReadableFile()) {
161 LYXERR(Debug::LYXVC, "LyXVC: registering "
162 << to_utf8(filename.displayName()) << " with SVN");
163 vcs.reset(new SVN(svn_entries, owner_));
165 } else if (cvs_entries.isReadableFile()) {
166 LYXERR(Debug::LYXVC, "LyXVC: registering "
167 << to_utf8(filename.displayName()) << " with CVS");
168 vcs.reset(new CVS(cvs_entries, owner_));
171 LYXERR(Debug::LYXVC, "LyXVC: registering "
172 << to_utf8(filename.displayName()) << " with RCS");
173 vcs.reset(new RCS(FileName(), owner_));
177 LYXERR(Debug::LYXVC, "LyXVC: registrer");
179 bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
180 _("(no initial description)"));
182 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
186 if (response.empty())
187 response = _("(no initial description)");
188 vcs->registrer(to_utf8(response));
193 string LyXVC::rename(FileName const & fn)
195 LYXERR(Debug::LYXVC, "LyXVC: rename");
196 if (!vcs || fileInVC(fn))
199 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
200 _("(no log message)"));
202 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
205 if (response.empty())
206 response = _("(no log message)");
207 string ret = vcs->rename(fn, to_utf8(response));
212 string LyXVC::copy(FileName const & fn)
214 LYXERR(Debug::LYXVC, "LyXVC: copy");
215 if (!vcs || fileInVC(fn))
218 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
219 _("(no log message)"));
221 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
224 if (response.empty())
225 response = _("(no log message)");
226 string ret = vcs->copy(fn, to_utf8(response));
231 LyXVC::CommandResult LyXVC::checkIn(string & log)
233 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
236 docstring empty(_("(no log message)"));
239 if (vcs->isCheckInWithConfirmation())
240 ok = Alert::askForText(response, _("LyX VC: Log Message"));
242 if (response.empty())
245 response = subst(response, from_ascii("\""), from_ascii("\\\""));
246 return vcs->checkIn(to_utf8(response), log);
248 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
254 string LyXVC::checkOut()
258 //RCS allows checkOut only in ReadOnly mode
259 if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
262 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
263 return vcs->checkOut();
267 string LyXVC::repoUpdate()
269 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
272 return vcs->repoUpdate();
276 string LyXVC::lockingToggle()
278 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
281 return vcs->lockingToggle();
287 LYXERR(Debug::LYXVC, "LyXVC: revert");
291 docstring const file = owner_->fileName().displayName(20);
292 docstring text = bformat(_("Reverting to the stored version of the "
293 "document %1$s will lose all current changes.\n\n"
294 "Do you want to revert to the older version?"), file);
296 if (vcs->isRevertWithConfirmation())
297 ret = Alert::prompt(_("Revert to stored version of document?"),
298 text, 0, 1, _("&Revert"), _("&Cancel"));
300 return ret == 0 && vcs->revert();
304 void LyXVC::undoLast()
312 string LyXVC::toggleReadOnly()
316 if (!vcs->toggleReadOnlyEnabled())
319 switch (vcs->status()) {
321 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
324 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
326 if (checkIn(log) != VCSuccess)
331 Buffer * b = vcs->owner();
332 bool const newstate = !b->isReadonly();
333 string result = "LyXVC: toggle to ";
334 result += (newstate ? "readonly" : "readwrite");
335 LYXERR(Debug::LYXVC, result);
336 b->setReadonly(newstate);
343 bool LyXVC::inUse() const
351 string const LyXVC::versionString() const
355 return vcs->versionString();
359 bool LyXVC::locking() const
363 return vcs->status() != VCS::NOLOCKING;
367 string const LyXVC::getLogFile() const
372 TempFile tempfile("lyxvclog");
373 tempfile.setAutoRemove(false);
374 FileName const tmpf = tempfile.name();
376 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
379 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
381 return tmpf.absFileName();
385 string LyXVC::revisionInfo(RevisionInfo const info) const
390 return vcs->revisionInfo(info);
394 bool LyXVC::renameEnabled() const
398 return vcs->renameEnabled();
402 bool LyXVC::copyEnabled() const
406 return vcs->copyEnabled();
410 bool LyXVC::checkOutEnabled() const
412 return vcs && vcs->checkOutEnabled();
416 bool LyXVC::checkInEnabled() const
418 return vcs && vcs->checkInEnabled();
422 bool LyXVC::isCheckInWithConfirmation() const
424 return vcs && vcs->isCheckInWithConfirmation();
428 bool LyXVC::lockingToggleEnabled() const
430 return vcs && vcs->lockingToggleEnabled();
434 bool LyXVC::undoLastEnabled() const
436 return vcs && vcs->undoLastEnabled();
440 bool LyXVC::repoUpdateEnabled() const
442 return vcs && vcs->repoUpdateEnabled();
446 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
448 return vcs && vcs->prepareFileRevision(rev, f);
452 bool LyXVC::prepareFileRevisionEnabled()
454 return vcs && vcs->prepareFileRevisionEnabled();