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 docstring LyXVC::vcstatus() const
54 return bformat(_("%1$s lock"), from_ascii(vcs->vcname()));
56 return from_ascii(vcs->vcname());
60 bool LyXVC::fileInVC(FileName const & fn)
62 if (!RCS::findFile(fn).empty())
64 if (!CVS::findFile(fn).empty())
66 if (!SVN::findFile(fn).empty())
68 if (!GIT::findFile(fn).empty())
74 bool LyXVC::file_found_hook(FileName const & fn)
77 // Check if file is under RCS
78 if (!(found_file = RCS::findFile(fn)).empty()) {
79 vcs.reset(new RCS(found_file, owner_));
82 // Check if file is under CVS
83 if (!(found_file = CVS::findFile(fn)).empty()) {
84 vcs.reset(new CVS(found_file, owner_));
87 // Check if file is under SVN
88 if (!(found_file = SVN::findFile(fn)).empty()) {
89 vcs.reset(new SVN(found_file, owner_));
92 // Check if file is under GIT
93 if (!(found_file = GIT::findFile(fn)).empty()) {
94 vcs.reset(new GIT(found_file, owner_));
98 // file is not under any VCS.
104 bool LyXVC::file_not_found_hook(FileName const & fn)
106 // Check if file is under RCS.
107 // This happens if we are trying to load non existent
108 // file on disk, but existent in ,v version.
109 bool foundRCS = !RCS::findFile(fn).empty();
110 bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
111 bool foundSVN = (foundRCS || foundCVS) ? false : !SVN::findFile(fn).empty();
112 bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : !GIT::findFile(fn).empty();
113 if (foundRCS || foundCVS || foundSVN || foundGIT) {
114 docstring const file = makeDisplayPath(fn.absFileName(), 20);
115 docstring const text =
116 bformat(_("Do you want to retrieve the document"
117 " %1$s from version control?"), file);
118 int const ret = Alert::prompt(_("Retrieve from version control?"),
119 text, 0, 1, _("&Retrieve"), _("&Cancel"));
122 // Since the retrieve commands are implemented using
123 // more general update commands we need to ensure that
124 // we do not change an existing file by accident.
128 return RCS::retrieve(fn);
130 return CVS::retrieve(fn);
132 return SVN::retrieve(fn);
134 return GIT::retrieve(fn);
141 void LyXVC::setBuffer(Buffer * buf)
147 bool LyXVC::registrer()
149 FileName const filename = owner_->fileName();
151 // there must be a file to save
152 if (!filename.isReadableFile()) {
153 Alert::error(_("Document not saved"),
154 _("You must save the document "
155 "before it can be registered."));
159 // it is very likely here that the vcs is not created yet...
161 //check in the root directory of the document
162 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
163 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
164 FileName const git_index(onlyPath(filename.absFileName()) + "/.git/index");
166 if (git_index.isReadableFile()) {
167 LYXERR(Debug::LYXVC, "LyXVC: registering "
168 << to_utf8(filename.displayName()) << " with GIT");
169 vcs.reset(new GIT(git_index, owner_));
171 } else if (svn_entries.isReadableFile()) {
172 LYXERR(Debug::LYXVC, "LyXVC: registering "
173 << to_utf8(filename.displayName()) << " with SVN");
174 vcs.reset(new SVN(svn_entries, owner_));
176 } else if (cvs_entries.isReadableFile()) {
177 LYXERR(Debug::LYXVC, "LyXVC: registering "
178 << to_utf8(filename.displayName()) << " with CVS");
179 vcs.reset(new CVS(cvs_entries, owner_));
182 LYXERR(Debug::LYXVC, "LyXVC: registering "
183 << to_utf8(filename.displayName()) << " with RCS");
184 vcs.reset(new RCS(FileName(), owner_));
188 LYXERR(Debug::LYXVC, "LyXVC: registrer");
190 bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
191 _("(no initial description)"));
193 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
197 if (response.empty())
198 response = _("(no initial description)");
199 vcs->registrer(to_utf8(response));
204 string LyXVC::rename(FileName const & fn)
206 LYXERR(Debug::LYXVC, "LyXVC: rename");
207 if (!vcs || fileInVC(fn))
210 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
211 _("(no log message)"));
213 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
216 if (response.empty())
217 response = _("(no log message)");
218 string ret = vcs->rename(fn, to_utf8(response));
223 string LyXVC::copy(FileName const & fn)
225 LYXERR(Debug::LYXVC, "LyXVC: copy");
226 if (!vcs || fileInVC(fn))
229 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
230 _("(no log message)"));
232 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
235 if (response.empty())
236 response = _("(no log message)");
237 string ret = vcs->copy(fn, to_utf8(response));
242 LyXVC::CommandResult LyXVC::checkIn(string & log)
244 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
247 docstring empty(_("(no log message)"));
250 if (vcs->isCheckInWithConfirmation())
251 ok = Alert::askForText(response, _("LyX VC: Log Message"));
253 if (response.empty())
256 response = subst(response, from_ascii("\""), from_ascii("\\\""));
257 return vcs->checkIn(to_utf8(response), log);
259 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
265 string LyXVC::checkOut()
269 //RCS allows checkOut only in ReadOnly mode
270 if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
273 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
274 return vcs->checkOut();
278 string LyXVC::repoUpdate()
280 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
283 return vcs->repoUpdate();
287 string LyXVC::lockingToggle()
289 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
292 return vcs->lockingToggle();
298 LYXERR(Debug::LYXVC, "LyXVC: revert");
302 docstring const file = owner_->fileName().displayName(20);
303 docstring text = bformat(_("Reverting to the stored version of the "
304 "document %1$s will lose all current changes.\n\n"
305 "Do you want to revert to the older version?"), file);
307 if (vcs->isRevertWithConfirmation())
308 ret = Alert::prompt(_("Revert to stored version of document?"),
309 text, 0, 1, _("&Revert"), _("&Cancel"));
311 return ret == 0 && vcs->revert();
315 void LyXVC::undoLast()
323 string LyXVC::toggleReadOnly()
327 if (!vcs->toggleReadOnlyEnabled())
330 switch (vcs->status()) {
332 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
335 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
337 if (checkIn(log) != VCSuccess)
342 Buffer * b = vcs->owner();
343 bool const newstate = !b->isReadonly();
344 string result = "LyXVC: toggle to ";
345 result += (newstate ? "readonly" : "readwrite");
346 LYXERR(Debug::LYXVC, result);
347 b->setReadonly(newstate);
354 bool LyXVC::inUse() const
362 string const LyXVC::versionString() const
366 return vcs->versionString();
370 bool LyXVC::locking() const
374 return vcs->status() != VCS::NOLOCKING;
378 string const LyXVC::getLogFile() const
383 TempFile tempfile("lyxvclog");
384 tempfile.setAutoRemove(false);
385 FileName const tmpf = tempfile.name();
387 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
390 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
392 return tmpf.absFileName();
396 string LyXVC::revisionInfo(RevisionInfo const info) const
401 return vcs->revisionInfo(info);
405 bool LyXVC::renameEnabled() const
409 return vcs->renameEnabled();
413 bool LyXVC::copyEnabled() const
417 return vcs->copyEnabled();
421 bool LyXVC::checkOutEnabled() const
423 return vcs && vcs->checkOutEnabled();
427 bool LyXVC::checkInEnabled() const
429 return vcs && vcs->checkInEnabled();
433 bool LyXVC::isCheckInWithConfirmation() const
435 return vcs && vcs->isCheckInWithConfirmation();
439 bool LyXVC::lockingToggleEnabled() const
441 return vcs && vcs->lockingToggleEnabled();
445 bool LyXVC::undoLastEnabled() const
447 return vcs && vcs->undoLastEnabled();
451 bool LyXVC::repoUpdateEnabled() const
453 return vcs && vcs->repoUpdateEnabled();
457 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
459 return vcs && vcs->prepareFileRevision(rev, f);
463 bool LyXVC::prepareFileRevisionEnabled()
465 return vcs && vcs->prepareFileRevisionEnabled();