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))
63 if (GIT::findFile(fn))
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 (SVN::findFile(fn)) {
84 vcs_.reset(new SVN(owner_));
87 // Check if file is under GIT
88 if (GIT::findFile(fn)) {
89 vcs_.reset(new GIT(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 file that does not exist.
103 // It may yet exist in the repository and so could be checked out.
104 bool foundRCS = !RCS::findFile(fn).empty();
105 bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
106 bool foundSVN = (foundRCS || foundCVS) ? false : SVN::findFile(fn);
107 bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : GIT::findFile(fn);
108 if (foundRCS || foundCVS || foundSVN || foundGIT) {
109 docstring const file = makeDisplayPath(fn.absFileName(), 20);
110 docstring const text =
111 bformat(_("Do you want to retrieve the document"
112 " %1$s from version control?"), file);
113 int const ret = Alert::prompt(_("Retrieve from version control?"),
114 text, 0, 1, _("&Retrieve"), _("&Cancel"));
117 // Since the retrieve commands are implemented using
118 // more general update commands we need to ensure that
119 // we do not change an existing file by accident.
123 return RCS::retrieve(fn);
125 return CVS::retrieve(fn);
127 return SVN::retrieve(fn);
129 return GIT::retrieve(fn);
136 void LyXVC::setBuffer(Buffer * buf)
142 bool LyXVC::registrer()
144 FileName const filename = owner_->fileName();
146 // there must be a file to save
147 if (!filename.isReadableFile()) {
148 Alert::error(_("Document not saved"),
149 _("You must save the document "
150 "before it can be registered."));
154 // it is very likely here that the vcs is not created yet...
156 FileName found = VCS::checkParentDirs(filename, ".git/index");
158 if (!found.empty()) {
159 LYXERR(Debug::LYXVC, "LyXVC: registering "
160 << to_utf8(filename.displayName()) << " with GIT");
161 vcs_.reset(new GIT(owner_));
164 found = VCS::checkParentDirs(filename, ".svn/entries");
165 if (!found.empty()) {
166 LYXERR(Debug::LYXVC, "LyXVC: registering "
167 << to_utf8(filename.displayName()) << " with SVN");
168 vcs_.reset(new SVN(owner_));
171 // We only need to check the current directory, since CVS meta-data
172 // is in every sub-directory.
173 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
174 if (cvs_entries.isReadableFile()) {
175 LYXERR(Debug::LYXVC, "LyXVC: registering "
176 << to_utf8(filename.displayName()) << " with CVS");
177 vcs_.reset(new CVS(cvs_entries, owner_));
180 // If all else fails, use RCS
181 LYXERR(Debug::LYXVC, "LyXVC: registering "
182 << to_utf8(filename.displayName()) << " with RCS");
183 vcs_.reset(new RCS(FileName(), owner_));
189 LYXERR(Debug::LYXVC, "LyXVC: registrer");
191 bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
192 _("(no initial description)"));
194 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
198 if (response.empty())
199 response = _("(no initial description)");
200 vcs_->registrer(to_utf8(response));
205 string LyXVC::rename(FileName const & fn)
207 LYXERR(Debug::LYXVC, "LyXVC: rename");
208 if (!vcs_ || fileInVC(fn))
211 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
212 _("(no log message)"));
214 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
217 if (response.empty())
218 response = _("(no log message)");
219 string ret = vcs_->rename(fn, to_utf8(response));
224 string LyXVC::copy(FileName const & fn)
226 LYXERR(Debug::LYXVC, "LyXVC: copy");
227 if (!vcs_ || fileInVC(fn))
230 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
231 _("(no log message)"));
233 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
236 if (response.empty())
237 response = _("(no log message)");
238 string ret = vcs_->copy(fn, to_utf8(response));
243 LyXVC::CommandResult LyXVC::checkIn(string & log)
245 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
248 docstring empty(_("(no log message)"));
251 if (vcs_->isCheckInWithConfirmation())
252 ok = Alert::askForText(response, _("LyX VC: Log Message"));
254 if (response.empty())
257 response = subst(response, from_ascii("\""), from_ascii("\\\""));
258 return vcs_->checkIn(to_utf8(response), log);
260 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
266 string LyXVC::checkOut()
270 //RCS allows checkOut only in ReadOnly mode
271 if (vcs_->toggleReadOnlyEnabled() && !owner_->hasReadonlyFlag())
274 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
275 return vcs_->checkOut();
279 string LyXVC::repoUpdate()
281 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
284 return vcs_->repoUpdate();
288 string LyXVC::lockingToggle()
290 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
293 return vcs_->lockingToggle();
299 LYXERR(Debug::LYXVC, "LyXVC: revert");
303 docstring const file = owner_->fileName().displayName(20);
304 docstring text = bformat(_("Reverting to the stored version of the "
305 "document %1$s will lose all current changes.\n\n"
306 "Do you want to revert to the older version?"), file);
308 if (vcs_->isRevertWithConfirmation())
309 ret = Alert::prompt(_("Revert to stored version of document?"),
310 text, 0, 1, _("&Revert"), _("&Cancel"));
312 return ret == 0 && vcs_->revert();
316 void LyXVC::undoLast()
324 string LyXVC::toggleReadOnly()
328 if (!vcs_->toggleReadOnlyEnabled())
331 switch (vcs_->status()) {
333 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
336 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
338 if (checkIn(log) != VCSuccess)
343 Buffer * b = vcs_->owner();
344 bool const newstate = !b->hasReadonlyFlag();
345 string result = "LyXVC: toggle to ";
346 result += (newstate ? "readonly" : "readwrite");
347 LYXERR(Debug::LYXVC, result);
348 b->setReadonly(newstate);
355 bool LyXVC::inUse() const
357 return vcs_ != nullptr;
361 string const LyXVC::versionString() const
365 return vcs_->versionString();
369 bool LyXVC::locking() const
373 return vcs_->status() != VCS::NOLOCKING;
377 string const LyXVC::getLogFile() const
382 TempFile tempfile("lyxvclog");
383 tempfile.setAutoRemove(false);
384 FileName const tmpf = tempfile.name();
386 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
389 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
391 return tmpf.absFileName();
395 string LyXVC::revisionInfo(RevisionInfo const info) const
400 return vcs_->revisionInfo(info);
404 bool LyXVC::renameEnabled() const
408 return vcs_->renameEnabled();
412 bool LyXVC::copyEnabled() const
416 return vcs_->copyEnabled();
420 bool LyXVC::checkOutEnabled() const
422 return vcs_ && vcs_->checkOutEnabled();
426 bool LyXVC::checkInEnabled() const
428 return vcs_ && vcs_->checkInEnabled();
432 bool LyXVC::isCheckInWithConfirmation() const
434 return vcs_ && vcs_->isCheckInWithConfirmation();
438 bool LyXVC::lockingToggleEnabled() const
440 return vcs_ && vcs_->lockingToggleEnabled();
444 bool LyXVC::undoLastEnabled() const
446 return vcs_ && vcs_->undoLastEnabled();
450 bool LyXVC::repoUpdateEnabled() const
452 return vcs_ && vcs_->repoUpdateEnabled();
456 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
458 return vcs_ && vcs_->prepareFileRevision(rev, f);
462 bool LyXVC::prepareFileRevisionEnabled()
464 return vcs_ && vcs_->prepareFileRevisionEnabled();