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 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);
108 bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : GIT::findFile(fn);
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(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(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 vcs_->registrer(to_utf8(response));
206 string LyXVC::rename(FileName const & fn)
208 LYXERR(Debug::LYXVC, "LyXVC: rename");
209 if (!vcs_ || fileInVC(fn))
212 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
213 _("(no log message)"));
215 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
218 if (response.empty())
219 response = _("(no log message)");
220 string ret = vcs_->rename(fn, to_utf8(response));
225 string LyXVC::copy(FileName const & fn)
227 LYXERR(Debug::LYXVC, "LyXVC: copy");
228 if (!vcs_ || fileInVC(fn))
231 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
232 _("(no log message)"));
234 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
237 if (response.empty())
238 response = _("(no log message)");
239 string ret = vcs_->copy(fn, to_utf8(response));
244 LyXVC::CommandResult LyXVC::checkIn(string & log)
246 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
249 docstring empty(_("(no log message)"));
252 if (vcs_->isCheckInWithConfirmation())
253 ok = Alert::askForText(response, _("LyX VC: Log Message"));
255 if (response.empty())
258 response = subst(response, from_ascii("\""), from_ascii("\\\""));
259 return vcs_->checkIn(to_utf8(response), log);
261 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
267 string LyXVC::checkOut()
271 //RCS allows checkOut only in ReadOnly mode
272 if (vcs_->toggleReadOnlyEnabled() && !owner_->hasReadonlyFlag())
275 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
276 return vcs_->checkOut();
280 string LyXVC::repoUpdate()
282 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
285 return vcs_->repoUpdate();
289 string LyXVC::lockingToggle()
291 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
294 return vcs_->lockingToggle();
300 LYXERR(Debug::LYXVC, "LyXVC: revert");
304 docstring const file = owner_->fileName().displayName(20);
305 docstring text = bformat(_("Reverting to the stored version of the "
306 "document %1$s will lose all current changes.\n\n"
307 "Do you want to revert to the older version?"), file);
309 if (vcs_->isRevertWithConfirmation())
310 ret = Alert::prompt(_("Revert to stored version of document?"),
311 text, 0, 1, _("&Revert"), _("&Cancel"));
313 return ret == 0 && vcs_->revert();
317 void LyXVC::undoLast()
325 string LyXVC::toggleReadOnly()
329 if (!vcs_->toggleReadOnlyEnabled())
332 switch (vcs_->status()) {
334 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
337 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
339 if (checkIn(log) != VCSuccess)
344 Buffer * b = vcs_->owner();
345 bool const newstate = !b->hasReadonlyFlag();
346 string result = "LyXVC: toggle to ";
347 result += (newstate ? "readonly" : "readwrite");
348 LYXERR(Debug::LYXVC, result);
349 b->setReadonly(newstate);
356 bool LyXVC::inUse() const
358 return vcs_ != nullptr;
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();