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 RCS.
102 // This happens if we are trying to load non existent
103 // file on disk, but existent in ,v version.
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).empty();
107 bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : !GIT::findFile(fn).empty();
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(found, 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(found, 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 // FIXME This will fail with svn if the current directory has not
201 // itself been added.
202 vcs_->registrer(to_utf8(response));
207 string LyXVC::rename(FileName const & fn)
209 LYXERR(Debug::LYXVC, "LyXVC: rename");
210 if (!vcs_ || fileInVC(fn))
213 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
214 _("(no log message)"));
216 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
219 if (response.empty())
220 response = _("(no log message)");
221 string ret = vcs_->rename(fn, to_utf8(response));
226 string LyXVC::copy(FileName const & fn)
228 LYXERR(Debug::LYXVC, "LyXVC: copy");
229 if (!vcs_ || fileInVC(fn))
232 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
233 _("(no log message)"));
235 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
238 if (response.empty())
239 response = _("(no log message)");
240 string ret = vcs_->copy(fn, to_utf8(response));
245 LyXVC::CommandResult LyXVC::checkIn(string & log)
247 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
250 docstring empty(_("(no log message)"));
253 if (vcs_->isCheckInWithConfirmation())
254 ok = Alert::askForText(response, _("LyX VC: Log Message"));
256 if (response.empty())
259 response = subst(response, from_ascii("\""), from_ascii("\\\""));
260 return vcs_->checkIn(to_utf8(response), log);
262 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
268 string LyXVC::checkOut()
272 //RCS allows checkOut only in ReadOnly mode
273 if (vcs_->toggleReadOnlyEnabled() && !owner_->hasReadonlyFlag())
276 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
277 return vcs_->checkOut();
281 string LyXVC::repoUpdate()
283 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
286 return vcs_->repoUpdate();
290 string LyXVC::lockingToggle()
292 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
295 return vcs_->lockingToggle();
301 LYXERR(Debug::LYXVC, "LyXVC: revert");
305 docstring const file = owner_->fileName().displayName(20);
306 docstring text = bformat(_("Reverting to the stored version of the "
307 "document %1$s will lose all current changes.\n\n"
308 "Do you want to revert to the older version?"), file);
310 if (vcs_->isRevertWithConfirmation())
311 ret = Alert::prompt(_("Revert to stored version of document?"),
312 text, 0, 1, _("&Revert"), _("&Cancel"));
314 return ret == 0 && vcs_->revert();
318 void LyXVC::undoLast()
326 string LyXVC::toggleReadOnly()
330 if (!vcs_->toggleReadOnlyEnabled())
333 switch (vcs_->status()) {
335 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
338 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
340 if (checkIn(log) != VCSuccess)
345 Buffer * b = vcs_->owner();
346 bool const newstate = !b->hasReadonlyFlag();
347 string result = "LyXVC: toggle to ";
348 result += (newstate ? "readonly" : "readwrite");
349 LYXERR(Debug::LYXVC, result);
350 b->setReadonly(newstate);
357 bool LyXVC::inUse() const
359 return vcs_ != nullptr;
363 string const LyXVC::versionString() const
367 return vcs_->versionString();
371 bool LyXVC::locking() const
375 return vcs_->status() != VCS::NOLOCKING;
379 string const LyXVC::getLogFile() const
384 TempFile tempfile("lyxvclog");
385 tempfile.setAutoRemove(false);
386 FileName const tmpf = tempfile.name();
388 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
391 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
393 return tmpf.absFileName();
397 string LyXVC::revisionInfo(RevisionInfo const info) const
402 return vcs_->revisionInfo(info);
406 bool LyXVC::renameEnabled() const
410 return vcs_->renameEnabled();
414 bool LyXVC::copyEnabled() const
418 return vcs_->copyEnabled();
422 bool LyXVC::checkOutEnabled() const
424 return vcs_ && vcs_->checkOutEnabled();
428 bool LyXVC::checkInEnabled() const
430 return vcs_ && vcs_->checkInEnabled();
434 bool LyXVC::isCheckInWithConfirmation() const
436 return vcs_ && vcs_->isCheckInWithConfirmation();
440 bool LyXVC::lockingToggleEnabled() const
442 return vcs_ && vcs_->lockingToggleEnabled();
446 bool LyXVC::undoLastEnabled() const
448 return vcs_ && vcs_->undoLastEnabled();
452 bool LyXVC::repoUpdateEnabled() const
454 return vcs_ && vcs_->repoUpdateEnabled();
458 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
460 return vcs_ && vcs_->prepareFileRevision(rev, f);
464 bool LyXVC::prepareFileRevisionEnabled()
466 return vcs_ && vcs_->prepareFileRevisionEnabled();