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 bool LyXVC::fileInVC(FileName const & fn)
46 if (!RCS::findFile(fn).empty())
48 if (!CVS::findFile(fn).empty())
50 if (!SVN::findFile(fn).empty())
52 if (!GIT::findFile(fn).empty())
58 bool LyXVC::file_found_hook(FileName const & fn)
61 // Check if file is under RCS
62 if (!(found_file = RCS::findFile(fn)).empty()) {
63 vcs.reset(new RCS(found_file, owner_));
66 // Check if file is under CVS
67 if (!(found_file = CVS::findFile(fn)).empty()) {
68 vcs.reset(new CVS(found_file, owner_));
71 // Check if file is under SVN
72 if (!(found_file = SVN::findFile(fn)).empty()) {
73 vcs.reset(new SVN(found_file, owner_));
76 // Check if file is under GIT
77 if (!(found_file = GIT::findFile(fn)).empty()) {
78 vcs.reset(new GIT(found_file, owner_));
82 // file is not under any VCS.
88 bool LyXVC::file_not_found_hook(FileName const & fn)
90 // Check if file is under RCS.
91 // This happens if we are trying to load non existent
92 // file on disk, but existent in ,v version.
93 bool foundRCS = !RCS::findFile(fn).empty();
94 bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
95 bool foundSVN = (foundRCS || foundCVS) ? false : !SVN::findFile(fn).empty();
96 bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : !GIT::findFile(fn).empty();
97 if (foundRCS || foundCVS || foundSVN || foundGIT) {
98 docstring const file = makeDisplayPath(fn.absFileName(), 20);
99 docstring const text =
100 bformat(_("Do you want to retrieve the document"
101 " %1$s from version control?"), file);
102 int const ret = Alert::prompt(_("Retrieve from version control?"),
103 text, 0, 1, _("&Retrieve"), _("&Cancel"));
106 // Since the retrieve commands are implemented using
107 // more general update commands we need to ensure that
108 // we do not change an existing file by accident.
112 return RCS::retrieve(fn);
114 return CVS::retrieve(fn);
116 return SVN::retrieve(fn);
118 return GIT::retrieve(fn);
125 void LyXVC::setBuffer(Buffer * buf)
131 bool LyXVC::registrer()
133 FileName const filename = owner_->fileName();
135 // there must be a file to save
136 if (!filename.isReadableFile()) {
137 Alert::error(_("Document not saved"),
138 _("You must save the document "
139 "before it can be registered."));
143 // it is very likely here that the vcs is not created yet...
145 //check in the root directory of the document
146 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
147 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
148 FileName const git_index(onlyPath(filename.absFileName()) + "/.git/index");
150 if (git_index.isReadableFile()) {
151 LYXERR(Debug::LYXVC, "LyXVC: registering "
152 << to_utf8(filename.displayName()) << " with GIT");
153 vcs.reset(new GIT(git_index, owner_));
155 } else if (svn_entries.isReadableFile()) {
156 LYXERR(Debug::LYXVC, "LyXVC: registering "
157 << to_utf8(filename.displayName()) << " with SVN");
158 vcs.reset(new SVN(svn_entries, owner_));
160 } else if (cvs_entries.isReadableFile()) {
161 LYXERR(Debug::LYXVC, "LyXVC: registering "
162 << to_utf8(filename.displayName()) << " with CVS");
163 vcs.reset(new CVS(cvs_entries, owner_));
166 LYXERR(Debug::LYXVC, "LyXVC: registering "
167 << to_utf8(filename.displayName()) << " with RCS");
168 vcs.reset(new RCS(FileName(), owner_));
172 LYXERR(Debug::LYXVC, "LyXVC: registrer");
174 bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
175 _("(no initial description)"));
177 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
181 if (response.empty())
182 response = _("(no initial description)");
183 vcs->registrer(to_utf8(response));
188 string LyXVC::rename(FileName const & fn)
190 LYXERR(Debug::LYXVC, "LyXVC: rename");
191 if (!vcs || fileInVC(fn))
194 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
195 _("(no log message)"));
197 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
200 if (response.empty())
201 response = _("(no log message)");
202 string ret = vcs->rename(fn, to_utf8(response));
207 string LyXVC::copy(FileName const & fn)
209 LYXERR(Debug::LYXVC, "LyXVC: copy");
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->copy(fn, to_utf8(response));
226 LyXVC::CommandResult LyXVC::checkIn(string & log)
228 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
231 docstring empty(_("(no log message)"));
234 if (vcs->isCheckInWithConfirmation())
235 ok = Alert::askForText(response, _("LyX VC: Log Message"));
237 if (response.empty())
240 response = subst(response, from_ascii("\""), from_ascii("\\\""));
241 return vcs->checkIn(to_utf8(response), log);
243 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
249 string LyXVC::checkOut()
253 //RCS allows checkOut only in ReadOnly mode
254 if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
257 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
258 return vcs->checkOut();
262 string LyXVC::repoUpdate()
264 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
267 return vcs->repoUpdate();
271 string LyXVC::lockingToggle()
273 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
276 return vcs->lockingToggle();
282 LYXERR(Debug::LYXVC, "LyXVC: revert");
286 docstring const file = owner_->fileName().displayName(20);
287 docstring text = bformat(_("Reverting to the stored version of the "
288 "document %1$s will lose all current changes.\n\n"
289 "Do you want to revert to the older version?"), file);
291 if (vcs->isRevertWithConfirmation())
292 ret = Alert::prompt(_("Revert to stored version of document?"),
293 text, 0, 1, _("&Revert"), _("&Cancel"));
295 return ret == 0 && vcs->revert();
299 void LyXVC::undoLast()
307 string LyXVC::toggleReadOnly()
311 if (!vcs->toggleReadOnlyEnabled())
314 switch (vcs->status()) {
316 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
319 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
321 if (checkIn(log) != VCSuccess)
326 Buffer * b = vcs->owner();
327 bool const newstate = !b->isReadonly();
328 string result = "LyXVC: toggle to ";
329 result += (newstate ? "readonly" : "readwrite");
330 LYXERR(Debug::LYXVC, result);
331 b->setReadonly(newstate);
338 bool LyXVC::inUse() const
346 string const LyXVC::versionString() const
350 return vcs->versionString();
354 bool LyXVC::locking() const
358 return vcs->status() != VCS::NOLOCKING;
362 string const LyXVC::getLogFile() const
367 TempFile tempfile("lyxvclog");
368 tempfile.setAutoRemove(false);
369 FileName const tmpf = tempfile.name();
371 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
374 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
376 return tmpf.absFileName();
380 string LyXVC::revisionInfo(RevisionInfo const info) const
385 return vcs->revisionInfo(info);
389 bool LyXVC::renameEnabled() const
393 return vcs->renameEnabled();
397 bool LyXVC::copyEnabled() const
401 return vcs->copyEnabled();
405 bool LyXVC::checkOutEnabled() const
407 return vcs && vcs->checkOutEnabled();
411 bool LyXVC::checkInEnabled() const
413 return vcs && vcs->checkInEnabled();
417 bool LyXVC::isCheckInWithConfirmation() const
419 return vcs && vcs->isCheckInWithConfirmation();
423 bool LyXVC::lockingToggleEnabled() const
425 return vcs && vcs->lockingToggleEnabled();
429 bool LyXVC::undoLastEnabled() const
431 return vcs && vcs->undoLastEnabled();
435 bool LyXVC::repoUpdateEnabled() const
437 return vcs && vcs->repoUpdateEnabled();
441 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
443 return vcs && vcs->prepareFileRevision(rev, f);
447 bool LyXVC::prepareFileRevisionEnabled()
449 return vcs && vcs->prepareFileRevisionEnabled();