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 docstring LyXVC::vcstatus() const
49 return bformat(_("%1$s lock"), from_ascii(vcs_->vcname()));
51 return from_ascii(vcs_->vcname());
55 bool LyXVC::fileInVC(FileName const & fn)
57 if (!RCS::findFile(fn).empty())
59 if (!CVS::findFile(fn).empty())
61 if (!SVN::findFile(fn).empty())
63 if (!GIT::findFile(fn).empty())
69 bool LyXVC::file_found_hook(FileName const & fn)
72 // Check if file is under RCS
73 if (!(found_file = RCS::findFile(fn)).empty()) {
74 vcs_.reset(new RCS(found_file, owner_));
77 // Check if file is under CVS
78 if (!(found_file = CVS::findFile(fn)).empty()) {
79 vcs_.reset(new CVS(found_file, owner_));
82 // Check if file is under SVN
83 if (!(found_file = SVN::findFile(fn)).empty()) {
84 vcs_.reset(new SVN(found_file, owner_));
87 // Check if file is under GIT
88 if (!(found_file = GIT::findFile(fn)).empty()) {
89 vcs_.reset(new GIT(found_file, owner_));
93 // file is not under any VCS.
99 bool LyXVC::file_not_found_hook(FileName const & fn)
101 // Check if file is under version control.
102 // This happens if we are trying to load does not exist.
103 // It may yet exist in the repository and so could be
105 bool foundRCS = !RCS::findFile(fn).empty();
106 bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
107 bool foundSVN = (foundRCS || foundCVS) ? false : !SVN::findFile(fn).empty();
108 bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : !GIT::findFile(fn).empty();
109 if (foundRCS || foundCVS || foundSVN || foundGIT) {
110 docstring const file = makeDisplayPath(fn.absFileName(), 20);
111 docstring const text =
112 bformat(_("Do you want to retrieve the document"
113 " %1$s from version control?"), file);
114 int const ret = Alert::prompt(_("Retrieve from version control?"),
115 text, 0, 1, _("&Retrieve"), _("&Cancel"));
118 // Since the retrieve commands are implemented using
119 // more general update commands we need to ensure that
120 // we do not change an existing file by accident.
124 return RCS::retrieve(fn);
126 return CVS::retrieve(fn);
128 return SVN::retrieve(fn);
130 return GIT::retrieve(fn);
137 void LyXVC::setBuffer(Buffer * buf)
143 bool LyXVC::registrer()
145 FileName const filename = owner_->fileName();
147 // there must be a file to save
148 if (!filename.isReadableFile()) {
149 Alert::error(_("Document not saved"),
150 _("You must save the document "
151 "before it can be registered."));
155 // it is very likely here that the vcs is not created yet...
157 FileName found = VCS::checkParentDirs(filename, ".git/index");
159 if (!found.empty()) {
160 LYXERR(Debug::LYXVC, "LyXVC: registering "
161 << to_utf8(filename.displayName()) << " with GIT");
162 vcs_.reset(new GIT(found, owner_));
165 found = VCS::checkParentDirs(filename, ".svn/entries");
166 if (!found.empty()) {
167 LYXERR(Debug::LYXVC, "LyXVC: registering "
168 << to_utf8(filename.displayName()) << " with SVN");
169 vcs_.reset(new SVN(found, owner_));
172 // We only need to check the current directory, since CVS meta-data
173 // is in every sub-directory.
174 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
175 if (cvs_entries.isReadableFile()) {
176 LYXERR(Debug::LYXVC, "LyXVC: registering "
177 << to_utf8(filename.displayName()) << " with CVS");
178 vcs_.reset(new CVS(cvs_entries, owner_));
181 // If all else fails, use RCS
182 LYXERR(Debug::LYXVC, "LyXVC: registering "
183 << to_utf8(filename.displayName()) << " with RCS");
184 vcs_.reset(new RCS(FileName(), owner_));
190 LYXERR(Debug::LYXVC, "LyXVC: registrer");
192 bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
193 _("(no initial description)"));
195 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
199 if (response.empty())
200 response = _("(no initial description)");
201 // FIXME This will fail with svn if the current directory has not
202 // itself been added.
203 vcs_->registrer(to_utf8(response));
208 string LyXVC::rename(FileName const & fn)
210 LYXERR(Debug::LYXVC, "LyXVC: rename");
211 if (!vcs_ || fileInVC(fn))
214 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
215 _("(no log message)"));
217 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
220 if (response.empty())
221 response = _("(no log message)");
222 string ret = vcs_->rename(fn, to_utf8(response));
227 string LyXVC::copy(FileName const & fn)
229 LYXERR(Debug::LYXVC, "LyXVC: copy");
230 if (!vcs_ || fileInVC(fn))
233 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
234 _("(no log message)"));
236 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
239 if (response.empty())
240 response = _("(no log message)");
241 string ret = vcs_->copy(fn, to_utf8(response));
246 LyXVC::CommandResult LyXVC::checkIn(string & log)
248 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
251 docstring empty(_("(no log message)"));
254 if (vcs_->isCheckInWithConfirmation())
255 ok = Alert::askForText(response, _("LyX VC: Log Message"));
257 if (response.empty())
260 response = subst(response, from_ascii("\""), from_ascii("\\\""));
261 return vcs_->checkIn(to_utf8(response), log);
263 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
269 string LyXVC::checkOut()
273 //RCS allows checkOut only in ReadOnly mode
274 if (vcs_->toggleReadOnlyEnabled() && !owner_->hasReadonlyFlag())
277 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
278 return vcs_->checkOut();
282 string LyXVC::repoUpdate()
284 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
287 return vcs_->repoUpdate();
291 string LyXVC::lockingToggle()
293 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
296 return vcs_->lockingToggle();
302 LYXERR(Debug::LYXVC, "LyXVC: revert");
306 docstring const file = owner_->fileName().displayName(20);
307 docstring text = bformat(_("Reverting to the stored version of the "
308 "document %1$s will lose all current changes.\n\n"
309 "Do you want to revert to the older version?"), file);
311 if (vcs_->isRevertWithConfirmation())
312 ret = Alert::prompt(_("Revert to stored version of document?"),
313 text, 0, 1, _("&Revert"), _("&Cancel"));
315 return ret == 0 && vcs_->revert();
319 void LyXVC::undoLast()
327 string LyXVC::toggleReadOnly()
331 if (!vcs_->toggleReadOnlyEnabled())
334 switch (vcs_->status()) {
336 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
339 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
341 if (checkIn(log) != VCSuccess)
346 Buffer * b = vcs_->owner();
347 bool const newstate = !b->hasReadonlyFlag();
348 string result = "LyXVC: toggle to ";
349 result += (newstate ? "readonly" : "readwrite");
350 LYXERR(Debug::LYXVC, result);
351 b->setReadonly(newstate);
358 bool LyXVC::inUse() const
360 return vcs_ != nullptr;
364 string const LyXVC::versionString() const
368 return vcs_->versionString();
372 bool LyXVC::locking() const
376 return vcs_->status() != VCS::NOLOCKING;
380 string const LyXVC::getLogFile() const
385 TempFile tempfile("lyxvclog");
386 tempfile.setAutoRemove(false);
387 FileName const tmpf = tempfile.name();
389 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
392 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
394 return tmpf.absFileName();
398 string LyXVC::revisionInfo(RevisionInfo const info) const
403 return vcs_->revisionInfo(info);
407 bool LyXVC::renameEnabled() const
411 return vcs_->renameEnabled();
415 bool LyXVC::copyEnabled() const
419 return vcs_->copyEnabled();
423 bool LyXVC::checkOutEnabled() const
425 return vcs_ && vcs_->checkOutEnabled();
429 bool LyXVC::checkInEnabled() const
431 return vcs_ && vcs_->checkInEnabled();
435 bool LyXVC::isCheckInWithConfirmation() const
437 return vcs_ && vcs_->isCheckInWithConfirmation();
441 bool LyXVC::lockingToggleEnabled() const
443 return vcs_ && vcs_->lockingToggleEnabled();
447 bool LyXVC::undoLastEnabled() const
449 return vcs_ && vcs_->undoLastEnabled();
453 bool LyXVC::repoUpdateEnabled() const
455 return vcs_ && vcs_->repoUpdateEnabled();
459 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
461 return vcs_ && vcs_->prepareFileRevision(rev, f);
465 bool LyXVC::prepareFileRevisionEnabled()
467 return vcs_ && vcs_->prepareFileRevisionEnabled();