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 string LyXVC::vcname() const
50 bool LyXVC::fileInVC(FileName const & fn)
52 if (!RCS::findFile(fn).empty())
54 if (!CVS::findFile(fn).empty())
56 if (!SVN::findFile(fn).empty())
58 if (!GIT::findFile(fn).empty())
64 bool LyXVC::file_found_hook(FileName const & fn)
67 // Check if file is under RCS
68 if (!(found_file = RCS::findFile(fn)).empty()) {
69 vcs.reset(new RCS(found_file, owner_));
72 // Check if file is under CVS
73 if (!(found_file = CVS::findFile(fn)).empty()) {
74 vcs.reset(new CVS(found_file, owner_));
77 // Check if file is under SVN
78 if (!(found_file = SVN::findFile(fn)).empty()) {
79 vcs.reset(new SVN(found_file, owner_));
82 // Check if file is under GIT
83 if (!(found_file = GIT::findFile(fn)).empty()) {
84 vcs.reset(new GIT(found_file, owner_));
88 // file is not under any VCS.
94 bool LyXVC::file_not_found_hook(FileName const & fn)
96 // Check if file is under RCS.
97 // This happens if we are trying to load non existent
98 // file on disk, but existent in ,v version.
99 bool foundRCS = !RCS::findFile(fn).empty();
100 bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
101 bool foundSVN = (foundRCS || foundCVS) ? false : !SVN::findFile(fn).empty();
102 bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : !GIT::findFile(fn).empty();
103 if (foundRCS || foundCVS || foundSVN || foundGIT) {
104 docstring const file = makeDisplayPath(fn.absFileName(), 20);
105 docstring const text =
106 bformat(_("Do you want to retrieve the document"
107 " %1$s from version control?"), file);
108 int const ret = Alert::prompt(_("Retrieve from version control?"),
109 text, 0, 1, _("&Retrieve"), _("&Cancel"));
112 // Since the retrieve commands are implemented using
113 // more general update commands we need to ensure that
114 // we do not change an existing file by accident.
118 return RCS::retrieve(fn);
120 return CVS::retrieve(fn);
122 return SVN::retrieve(fn);
124 return GIT::retrieve(fn);
131 void LyXVC::setBuffer(Buffer * buf)
137 bool LyXVC::registrer()
139 FileName const filename = owner_->fileName();
141 // there must be a file to save
142 if (!filename.isReadableFile()) {
143 Alert::error(_("Document not saved"),
144 _("You must save the document "
145 "before it can be registered."));
149 // it is very likely here that the vcs is not created yet...
151 //check in the root directory of the document
152 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
153 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
154 FileName const git_index(onlyPath(filename.absFileName()) + "/.git/index");
156 if (git_index.isReadableFile()) {
157 LYXERR(Debug::LYXVC, "LyXVC: registering "
158 << to_utf8(filename.displayName()) << " with GIT");
159 vcs.reset(new GIT(git_index, owner_));
161 } else if (svn_entries.isReadableFile()) {
162 LYXERR(Debug::LYXVC, "LyXVC: registering "
163 << to_utf8(filename.displayName()) << " with SVN");
164 vcs.reset(new SVN(svn_entries, owner_));
166 } else if (cvs_entries.isReadableFile()) {
167 LYXERR(Debug::LYXVC, "LyXVC: registering "
168 << to_utf8(filename.displayName()) << " with CVS");
169 vcs.reset(new CVS(cvs_entries, owner_));
172 LYXERR(Debug::LYXVC, "LyXVC: registering "
173 << to_utf8(filename.displayName()) << " with RCS");
174 vcs.reset(new RCS(FileName(), owner_));
178 LYXERR(Debug::LYXVC, "LyXVC: registrer");
180 bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
181 _("(no initial description)"));
183 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
187 if (response.empty())
188 response = _("(no initial description)");
189 vcs->registrer(to_utf8(response));
194 string LyXVC::rename(FileName const & fn)
196 LYXERR(Debug::LYXVC, "LyXVC: rename");
197 if (!vcs || fileInVC(fn))
200 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
201 _("(no log message)"));
203 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
206 if (response.empty())
207 response = _("(no log message)");
208 string ret = vcs->rename(fn, to_utf8(response));
213 string LyXVC::copy(FileName const & fn)
215 LYXERR(Debug::LYXVC, "LyXVC: copy");
216 if (!vcs || fileInVC(fn))
219 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
220 _("(no log message)"));
222 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
225 if (response.empty())
226 response = _("(no log message)");
227 string ret = vcs->copy(fn, to_utf8(response));
232 LyXVC::CommandResult LyXVC::checkIn(string & log)
234 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
237 docstring empty(_("(no log message)"));
240 if (vcs->isCheckInWithConfirmation())
241 ok = Alert::askForText(response, _("LyX VC: Log Message"));
243 if (response.empty())
246 response = subst(response, from_ascii("\""), from_ascii("\\\""));
247 return vcs->checkIn(to_utf8(response), log);
249 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
255 string LyXVC::checkOut()
259 //RCS allows checkOut only in ReadOnly mode
260 if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
263 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
264 return vcs->checkOut();
268 string LyXVC::repoUpdate()
270 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
273 return vcs->repoUpdate();
277 string LyXVC::lockingToggle()
279 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
282 return vcs->lockingToggle();
288 LYXERR(Debug::LYXVC, "LyXVC: revert");
292 docstring const file = owner_->fileName().displayName(20);
293 docstring text = bformat(_("Reverting to the stored version of the "
294 "document %1$s will lose all current changes.\n\n"
295 "Do you want to revert to the older version?"), file);
297 if (vcs->isRevertWithConfirmation())
298 ret = Alert::prompt(_("Revert to stored version of document?"),
299 text, 0, 1, _("&Revert"), _("&Cancel"));
301 return ret == 0 && vcs->revert();
305 void LyXVC::undoLast()
313 string LyXVC::toggleReadOnly()
317 if (!vcs->toggleReadOnlyEnabled())
320 switch (vcs->status()) {
322 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
325 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
327 if (checkIn(log) != VCSuccess)
332 Buffer * b = vcs->owner();
333 bool const newstate = !b->isReadonly();
334 string result = "LyXVC: toggle to ";
335 result += (newstate ? "readonly" : "readwrite");
336 LYXERR(Debug::LYXVC, result);
337 b->setReadonly(newstate);
344 bool LyXVC::inUse() const
352 string const LyXVC::versionString() const
356 return vcs->versionString();
360 bool LyXVC::locking() const
364 return vcs->status() != VCS::NOLOCKING;
368 string const LyXVC::getLogFile() const
373 TempFile tempfile("lyxvclog");
374 tempfile.setAutoRemove(false);
375 FileName const tmpf = tempfile.name();
377 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
380 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
382 return tmpf.absFileName();
386 string LyXVC::revisionInfo(RevisionInfo const info) const
391 return vcs->revisionInfo(info);
395 bool LyXVC::renameEnabled() const
399 return vcs->renameEnabled();
403 bool LyXVC::copyEnabled() const
407 return vcs->copyEnabled();
411 bool LyXVC::checkOutEnabled() const
413 return vcs && vcs->checkOutEnabled();
417 bool LyXVC::checkInEnabled() const
419 return vcs && vcs->checkInEnabled();
423 bool LyXVC::isCheckInWithConfirmation() const
425 return vcs && vcs->isCheckInWithConfirmation();
429 bool LyXVC::lockingToggleEnabled() const
431 return vcs && vcs->lockingToggleEnabled();
435 bool LyXVC::undoLastEnabled() const
437 return vcs && vcs->undoLastEnabled();
441 bool LyXVC::repoUpdateEnabled() const
443 return vcs && vcs->repoUpdateEnabled();
447 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
449 return vcs && vcs->prepareFileRevision(rev, f);
453 bool LyXVC::prepareFileRevisionEnabled()
455 return vcs && vcs->prepareFileRevisionEnabled();