]> git.lyx.org Git - features.git/blob - src/LyXVC.cpp
Fix inconsistent VCS checkin return values
[features.git] / src / LyXVC.cpp
1 /**
2  * \file LyXVC.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  * \author John Levon
10  * \author André Pönitz
11  * \author Allan Rae
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "LyXVC.h"
19 #include "VCBackend.h"
20 #include "Buffer.h"
21
22 #include "frontends/alert.h"
23
24 #include "support/debug.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33
34 namespace Alert = frontend::Alert;
35
36
37 LyXVC::LyXVC()
38 {
39         owner_ = 0;
40 }
41
42
43 // for the sake of boost::scoped_ptr
44 LyXVC::~LyXVC()
45 {}
46
47
48 bool LyXVC::fileInVC(FileName const & fn)
49 {
50         if (!RCS::findFile(fn).empty())
51                 return true;
52         if (!CVS::findFile(fn).empty())
53                 return true;
54         if (!SVN::findFile(fn).empty())
55                 return true;
56         return false;
57 }
58
59
60 bool LyXVC::file_found_hook(FileName const & fn)
61 {
62         FileName found_file;
63         // Check if file is under RCS
64         if (!(found_file = RCS::findFile(fn)).empty()) {
65                 vcs.reset(new RCS(found_file, owner_));
66                 return true;
67         }
68         // Check if file is under CVS
69         if (!(found_file = CVS::findFile(fn)).empty()) {
70                 vcs.reset(new CVS(found_file, owner_));
71                 return true;
72         }
73         // Check if file is under SVN
74         if (!(found_file = SVN::findFile(fn)).empty()) {
75                 vcs.reset(new SVN(found_file, owner_));
76                 return true;
77         }
78
79         // file is not under any VCS.
80         vcs.reset(0);
81         return false;
82 }
83
84
85 bool LyXVC::file_not_found_hook(FileName const & fn)
86 {
87         // Check if file is under RCS.
88         // This happens if we are trying to load non existent
89         // file on disk, but existent in ,v version.
90         bool foundRCS = !RCS::findFile(fn).empty();
91         bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
92         bool foundSVN = (foundRCS || foundCVS) ? false : !SVN::findFile(fn).empty();
93         if (foundRCS || foundCVS || foundSVN) {
94                 docstring const file = makeDisplayPath(fn.absFileName(), 20);
95                 docstring const text =
96                         bformat(_("Do you want to retrieve the document"
97                                                    " %1$s from version control?"), file);
98                 int const ret = Alert::prompt(_("Retrieve from version control?"),
99                         text, 0, 1, _("&Retrieve"), _("&Cancel"));
100
101                 if (ret == 0) {
102                         // Since the retrieve commands are implemented using
103                         // more general update commands we need to ensure that
104                         // we do not change an existing file by accident.
105                         if (fn.exists())
106                                 return false;
107                         if (foundRCS)
108                                 return RCS::retrieve(fn);
109                         else if (foundCVS)
110                                 return CVS::retrieve(fn);
111                         else
112                                 return SVN::retrieve(fn);
113                 }
114         }
115         return false;
116 }
117
118
119 void LyXVC::setBuffer(Buffer * buf)
120 {
121         owner_ = buf;
122 }
123
124
125 bool LyXVC::registrer()
126 {
127         FileName const filename = owner_->fileName();
128
129         // there must be a file to save
130         if (!filename.isReadableFile()) {
131                 Alert::error(_("Document not saved"),
132                              _("You must save the document "
133                                             "before it can be registered."));
134                 return false;
135         }
136
137         // it is very likely here that the vcs is not created yet...
138         if (!vcs) {
139                 //check in the root directory of the document
140                 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
141                 FileName const svn_entries(onlyPath(filename.absFileName()) + "/.svn/entries");
142
143                 if (svn_entries.isReadableFile()) {
144                         LYXERR(Debug::LYXVC, "LyXVC: registering "
145                                 << to_utf8(filename.displayName()) << " with SVN");
146                         vcs.reset(new SVN(cvs_entries, owner_));
147
148                 } else if (cvs_entries.isReadableFile()) {
149                         LYXERR(Debug::LYXVC, "LyXVC: registering "
150                                 << to_utf8(filename.displayName()) << " with CVS");
151                         vcs.reset(new CVS(cvs_entries, owner_));
152
153                 } else {
154                         LYXERR(Debug::LYXVC, "LyXVC: registering "
155                                 << to_utf8(filename.displayName()) << " with RCS");
156                         vcs.reset(new RCS(FileName(), owner_));
157                 }
158         }
159
160         LYXERR(Debug::LYXVC, "LyXVC: registrer");
161         docstring response;
162         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
163                         _("(no initial description)"));
164         if (!ok) {
165                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
166                 vcs.reset(0);
167                 return false;
168         }
169         if (response.empty())
170                 response = _("(no initial description)");
171         vcs->registrer(to_utf8(response));
172         return true;
173 }
174
175
176 LyXVC::CommandResult LyXVC::checkIn(string & log)
177 {
178         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
179         if (!vcs)
180                 return ErrorBefore;
181         docstring empty(_("(no log message)"));
182         docstring response;
183         bool ok = true;
184         if (vcs->isCheckInWithConfirmation())
185                 ok = Alert::askForText(response, _("LyX VC: Log Message"));
186         if (ok) {
187                 if (response.empty())
188                         response = empty;
189                 //shell collisions
190                 response = subst(response, from_ascii("\""), from_ascii("\\\""));
191                 return vcs->checkIn(to_utf8(response), log);
192         } else {
193                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
194                 return Cancelled;
195         }
196 }
197
198
199 string LyXVC::checkOut()
200 {
201         if (!vcs)
202                 return string();
203         //RCS allows checkOut only in ReadOnly mode
204         if (vcs->toggleReadOnlyEnabled() && !owner_->isReadonly())
205                 return string();
206
207         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
208         return vcs->checkOut();
209 }
210
211
212 string LyXVC::repoUpdate()
213 {
214         LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
215         if (!vcs)
216                 return string();
217         return vcs->repoUpdate();
218 }
219
220
221 string LyXVC::lockingToggle()
222 {
223         LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
224         if (!vcs)
225                 return string();
226         return vcs->lockingToggle();
227 }
228
229
230 bool LyXVC::revert()
231 {
232         LYXERR(Debug::LYXVC, "LyXVC: revert");
233         if (!vcs)
234                 return false;
235
236         docstring const file = owner_->fileName().displayName(20);
237         docstring text = bformat(_("Reverting to the stored version of the "
238                                 "document %1$s will lose all current changes.\n\n"
239                                 "Do you want to revert to the older version?"), file);
240         int ret = 0;
241         if (vcs->isRevertWithConfirmation())
242                 ret = Alert::prompt(_("Revert to stored version of document?"),
243                         text, 0, 1, _("&Revert"), _("&Cancel"));
244
245         return ret == 0 && vcs->revert();
246 }
247
248
249 void LyXVC::undoLast()
250 {
251         if (!vcs)
252                 return;
253         vcs->undoLast();
254 }
255
256
257 string LyXVC::toggleReadOnly()
258 {
259         if (!vcs)
260                 return string();
261         if (!vcs->toggleReadOnlyEnabled())
262                 return string();
263
264         switch (vcs->status()) {
265         case VCS::UNLOCKED:
266                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
267                 return checkOut();
268         case VCS::LOCKED: {
269                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
270                 string log;
271                 if (checkIn(log) != Success)
272                         return string();
273                 return log;
274         }
275         case VCS::NOLOCKING:
276                 break;
277         }
278         return string();
279 }
280
281
282 bool LyXVC::inUse() const
283 {
284         if (vcs)
285                 return true;
286         return false;
287 }
288
289
290 string const LyXVC::versionString() const
291 {
292         if (!vcs)
293                 return string();
294         return vcs->versionString();
295 }
296
297
298 bool LyXVC::locking() const
299 {
300         if (!vcs)
301                 return false;
302         return vcs->status() != VCS::NOLOCKING;
303 }
304
305
306 string const LyXVC::getLogFile() const
307 {
308         if (!vcs)
309                 return string();
310
311         FileName const tmpf = FileName::tempName("lyxvclog");
312         if (tmpf.empty()) {
313                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
314                 return string();
315         }
316         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
317         vcs->getLog(tmpf);
318         return tmpf.absFileName();
319 }
320
321
322 string LyXVC::revisionInfo(RevisionInfo const info) const
323 {
324         if (!vcs)
325                 return string();
326
327         return vcs->revisionInfo(info);
328 }
329
330
331 bool LyXVC::checkOutEnabled() const
332 {
333         return vcs && vcs->checkOutEnabled();
334 }
335
336
337 bool LyXVC::checkInEnabled() const
338 {
339         return vcs && vcs->checkInEnabled();
340 }
341
342
343 bool LyXVC::lockingToggleEnabled() const
344 {
345         return vcs && vcs->lockingToggleEnabled();
346 }
347
348
349 bool LyXVC::undoLastEnabled() const
350 {
351         return vcs && vcs->undoLastEnabled();
352 }
353
354
355 bool LyXVC::repoUpdateEnabled() const
356 {
357         return vcs && vcs->repoUpdateEnabled();
358 }
359         
360         
361 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
362 {
363         return vcs && vcs->prepareFileRevision(rev, f);
364 }
365
366
367 bool LyXVC::prepareFileRevisionEnabled()
368 {
369         return vcs && vcs->prepareFileRevisionEnabled();
370 }
371
372 } // namespace lyx