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 //check in the root directory of the document
157 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
158 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
159 FileName const git_index(onlyPath(filename.absFileName()) + "/.git/index");
161 if (git_index.isReadableFile()) {
162 LYXERR(Debug::LYXVC, "LyXVC: registering "
163 << to_utf8(filename.displayName()) << " with GIT");
164 vcs.reset(new GIT(git_index, owner_));
166 } else if (svn_entries.isReadableFile()) {
167 LYXERR(Debug::LYXVC, "LyXVC: registering "
168 << to_utf8(filename.displayName()) << " with SVN");
169 vcs.reset(new SVN(svn_entries, owner_));
171 } else if (cvs_entries.isReadableFile()) {
172 LYXERR(Debug::LYXVC, "LyXVC: registering "
173 << to_utf8(filename.displayName()) << " with CVS");
174 vcs.reset(new CVS(cvs_entries, owner_));
177 LYXERR(Debug::LYXVC, "LyXVC: registering "
178 << to_utf8(filename.displayName()) << " with RCS");
179 vcs.reset(new RCS(FileName(), owner_));
183 LYXERR(Debug::LYXVC, "LyXVC: registrer");
185 bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
186 _("(no initial description)"));
188 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
192 if (response.empty())
193 response = _("(no initial description)");
194 vcs->registrer(to_utf8(response));
199 string LyXVC::rename(FileName const & fn)
201 LYXERR(Debug::LYXVC, "LyXVC: rename");
202 if (!vcs || fileInVC(fn))
205 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
206 _("(no log message)"));
208 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
211 if (response.empty())
212 response = _("(no log message)");
213 string ret = vcs->rename(fn, to_utf8(response));
218 string LyXVC::copy(FileName const & fn)
220 LYXERR(Debug::LYXVC, "LyXVC: copy");
221 if (!vcs || fileInVC(fn))
224 bool ok = Alert::askForText(response, _("LyX VC: Log message"),
225 _("(no log message)"));
227 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
230 if (response.empty())
231 response = _("(no log message)");
232 string ret = vcs->copy(fn, to_utf8(response));
237 LyXVC::CommandResult LyXVC::checkIn(string & log)
239 LYXERR(Debug::LYXVC, "LyXVC: checkIn");
242 docstring empty(_("(no log message)"));
245 if (vcs->isCheckInWithConfirmation())
246 ok = Alert::askForText(response, _("LyX VC: Log Message"));
248 if (response.empty())
251 response = subst(response, from_ascii("\""), from_ascii("\\\""));
252 return vcs->checkIn(to_utf8(response), log);
254 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
260 string LyXVC::checkOut()
264 //RCS allows checkOut only in ReadOnly mode
265 if (vcs->toggleReadOnlyEnabled() && !owner_->hasReadonlyFlag())
268 LYXERR(Debug::LYXVC, "LyXVC: checkOut");
269 return vcs->checkOut();
273 string LyXVC::repoUpdate()
275 LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
278 return vcs->repoUpdate();
282 string LyXVC::lockingToggle()
284 LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
287 return vcs->lockingToggle();
293 LYXERR(Debug::LYXVC, "LyXVC: revert");
297 docstring const file = owner_->fileName().displayName(20);
298 docstring text = bformat(_("Reverting to the stored version of the "
299 "document %1$s will lose all current changes.\n\n"
300 "Do you want to revert to the older version?"), file);
302 if (vcs->isRevertWithConfirmation())
303 ret = Alert::prompt(_("Revert to stored version of document?"),
304 text, 0, 1, _("&Revert"), _("&Cancel"));
306 return ret == 0 && vcs->revert();
310 void LyXVC::undoLast()
318 string LyXVC::toggleReadOnly()
322 if (!vcs->toggleReadOnlyEnabled())
325 switch (vcs->status()) {
327 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
330 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
332 if (checkIn(log) != VCSuccess)
337 Buffer * b = vcs->owner();
338 bool const newstate = !b->hasReadonlyFlag();
339 string result = "LyXVC: toggle to ";
340 result += (newstate ? "readonly" : "readwrite");
341 LYXERR(Debug::LYXVC, result);
342 b->setReadonly(newstate);
349 bool LyXVC::inUse() const
357 string const LyXVC::versionString() const
361 return vcs->versionString();
365 bool LyXVC::locking() const
369 return vcs->status() != VCS::NOLOCKING;
373 string const LyXVC::getLogFile() const
378 TempFile tempfile("lyxvclog");
379 tempfile.setAutoRemove(false);
380 FileName const tmpf = tempfile.name();
382 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
385 LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
387 return tmpf.absFileName();
391 string LyXVC::revisionInfo(RevisionInfo const info) const
396 return vcs->revisionInfo(info);
400 bool LyXVC::renameEnabled() const
404 return vcs->renameEnabled();
408 bool LyXVC::copyEnabled() const
412 return vcs->copyEnabled();
416 bool LyXVC::checkOutEnabled() const
418 return vcs && vcs->checkOutEnabled();
422 bool LyXVC::checkInEnabled() const
424 return vcs && vcs->checkInEnabled();
428 bool LyXVC::isCheckInWithConfirmation() const
430 return vcs && vcs->isCheckInWithConfirmation();
434 bool LyXVC::lockingToggleEnabled() const
436 return vcs && vcs->lockingToggleEnabled();
440 bool LyXVC::undoLastEnabled() const
442 return vcs && vcs->undoLastEnabled();
446 bool LyXVC::repoUpdateEnabled() const
448 return vcs && vcs->repoUpdateEnabled();
452 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
454 return vcs && vcs->prepareFileRevision(rev, f);
458 bool LyXVC::prepareFileRevisionEnabled()
460 return vcs && vcs->prepareFileRevisionEnabled();