]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
ed0a7991434845c15068d74ed9f4d933bcac28f1
[lyx.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 #include "support/TempFile.h"
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 namespace Alert = frontend::Alert;
36
37
38 LyXVC::LyXVC()
39 {
40         owner_ = nullptr;
41 }
42
43
44 docstring LyXVC::vcstatus() const
45 {
46         if (!vcs_)
47                 return docstring();
48         if (locking())
49                 return bformat(_("%1$s lock"), from_ascii(vcs_->vcname()));
50         else
51                 return from_ascii(vcs_->vcname());
52 }
53
54
55 bool LyXVC::fileInVC(FileName const & fn)
56 {
57         if (!RCS::findFile(fn).empty())
58                 return true;
59         if (!CVS::findFile(fn).empty())
60                 return true;
61         if (!SVN::findFile(fn).empty())
62                 return true;
63         if (!GIT::findFile(fn).empty())
64                 return true;
65         return false;
66 }
67
68
69 bool LyXVC::file_found_hook(FileName const & fn)
70 {
71         FileName found_file;
72         // Check if file is under RCS
73         if (!(found_file = RCS::findFile(fn)).empty()) {
74                 vcs_.reset(new RCS(found_file, owner_));
75                 return true;
76         }
77         // Check if file is under CVS
78         if (!(found_file = CVS::findFile(fn)).empty()) {
79                 vcs_.reset(new CVS(found_file, owner_));
80                 return true;
81         }
82         // Check if file is under SVN
83         if (!(found_file = SVN::findFile(fn)).empty()) {
84                 vcs_.reset(new SVN(found_file, owner_));
85                 return true;
86         }
87         // Check if file is under GIT
88         if (!(found_file = GIT::findFile(fn)).empty()) {
89                 vcs_.reset(new GIT(found_file, owner_));
90                 return true;
91         }
92
93         // file is not under any VCS.
94         vcs_.reset(nullptr);
95         return false;
96 }
97
98
99 bool LyXVC::file_not_found_hook(FileName const & fn)
100 {
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"));
115
116                 if (ret == 0) {
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.
120                         if (fn.exists())
121                                 return false;
122                         if (foundRCS)
123                                 return RCS::retrieve(fn);
124                         else if (foundCVS)
125                                 return CVS::retrieve(fn);
126                         else if (foundSVN)
127                                 return SVN::retrieve(fn);
128                         else
129                                 return GIT::retrieve(fn);
130                 }
131         }
132         return false;
133 }
134
135
136 void LyXVC::setBuffer(Buffer * buf)
137 {
138         owner_ = buf;
139 }
140
141
142 bool LyXVC::registrer()
143 {
144         FileName const filename = owner_->fileName();
145
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."));
151                 return false;
152         }
153
154         // it is very likely here that the vcs is not created yet...
155         if (!vcs_) {
156                 FileName found = VCS::checkParentDirs(filename, ".git/index");
157
158                 if (!found.empty()) {
159                         LYXERR(Debug::LYXVC, "LyXVC: registering "
160                                 << to_utf8(filename.displayName()) << " with GIT");
161                         vcs_.reset(new GIT(found, owner_));
162
163                 } else {
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_));
169
170                         } else {
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_));
178
179                                 } else {
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_));
184                                 }
185                         }
186                 }
187         }
188
189         LYXERR(Debug::LYXVC, "LyXVC: registrer");
190         docstring response;
191         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
192                         _("(no initial description)"));
193         if (!ok) {
194                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
195                 vcs_.reset(nullptr);
196                 return false;
197         }
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));
203         return true;
204 }
205
206
207 string LyXVC::rename(FileName const & fn)
208 {
209         LYXERR(Debug::LYXVC, "LyXVC: rename");
210         if (!vcs_ || fileInVC(fn))
211                 return string();
212         docstring response;
213         bool ok = Alert::askForText(response, _("LyX VC: Log message"),
214                         _("(no log message)"));
215         if (!ok) {
216                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
217                 return string();
218         }
219         if (response.empty())
220                 response = _("(no log message)");
221         string ret = vcs_->rename(fn, to_utf8(response));
222         return ret;
223 }
224
225
226 string LyXVC::copy(FileName const & fn)
227 {
228         LYXERR(Debug::LYXVC, "LyXVC: copy");
229         if (!vcs_ || fileInVC(fn))
230                 return string();
231         docstring response;
232         bool ok = Alert::askForText(response, _("LyX VC: Log message"),
233                         _("(no log message)"));
234         if (!ok) {
235                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
236                 return string();
237         }
238         if (response.empty())
239                 response = _("(no log message)");
240         string ret = vcs_->copy(fn, to_utf8(response));
241         return ret;
242 }
243
244
245 LyXVC::CommandResult LyXVC::checkIn(string & log)
246 {
247         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
248         if (!vcs_)
249                 return ErrorBefore;
250         docstring empty(_("(no log message)"));
251         docstring response;
252         bool ok = true;
253         if (vcs_->isCheckInWithConfirmation())
254                 ok = Alert::askForText(response, _("LyX VC: Log Message"));
255         if (ok) {
256                 if (response.empty())
257                         response = empty;
258                 //shell collisions
259                 response = subst(response, from_ascii("\""), from_ascii("\\\""));
260                 return vcs_->checkIn(to_utf8(response), log);
261         } else {
262                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
263                 return Cancelled;
264         }
265 }
266
267
268 string LyXVC::checkOut()
269 {
270         if (!vcs_)
271                 return string();
272         //RCS allows checkOut only in ReadOnly mode
273         if (vcs_->toggleReadOnlyEnabled() && !owner_->hasReadonlyFlag())
274                 return string();
275
276         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
277         return vcs_->checkOut();
278 }
279
280
281 string LyXVC::repoUpdate()
282 {
283         LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
284         if (!vcs_)
285                 return string();
286         return vcs_->repoUpdate();
287 }
288
289
290 string LyXVC::lockingToggle()
291 {
292         LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
293         if (!vcs_)
294                 return string();
295         return vcs_->lockingToggle();
296 }
297
298
299 bool LyXVC::revert()
300 {
301         LYXERR(Debug::LYXVC, "LyXVC: revert");
302         if (!vcs_)
303                 return false;
304
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);
309         int ret = 0;
310         if (vcs_->isRevertWithConfirmation())
311                 ret = Alert::prompt(_("Revert to stored version of document?"),
312                         text, 0, 1, _("&Revert"), _("&Cancel"));
313
314         return ret == 0 && vcs_->revert();
315 }
316
317
318 void LyXVC::undoLast()
319 {
320         if (!vcs_)
321                 return;
322         vcs_->undoLast();
323 }
324
325
326 string LyXVC::toggleReadOnly()
327 {
328         if (!vcs_)
329                 return string();
330         if (!vcs_->toggleReadOnlyEnabled())
331                 return string();
332
333         switch (vcs_->status()) {
334         case VCS::UNLOCKED:
335                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
336                 return checkOut();
337         case VCS::LOCKED: {
338                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
339                 string log;
340                 if (checkIn(log) != VCSuccess)
341                         return string();
342                 return log;
343         }
344         case VCS::NOLOCKING:
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);
351                 return result;
352         }
353         return string();
354 }
355
356
357 bool LyXVC::inUse() const
358 {
359         return vcs_ != nullptr;
360 }
361
362
363 string const LyXVC::versionString() const
364 {
365         if (!vcs_)
366                 return string();
367         return vcs_->versionString();
368 }
369
370
371 bool LyXVC::locking() const
372 {
373         if (!vcs_)
374                 return false;
375         return vcs_->status() != VCS::NOLOCKING;
376 }
377
378
379 string const LyXVC::getLogFile() const
380 {
381         if (!vcs_)
382                 return string();
383
384         TempFile tempfile("lyxvclog");
385         tempfile.setAutoRemove(false);
386         FileName const tmpf = tempfile.name();
387         if (tmpf.empty()) {
388                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
389                 return string();
390         }
391         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
392         vcs_->getLog(tmpf);
393         return tmpf.absFileName();
394 }
395
396
397 string LyXVC::revisionInfo(RevisionInfo const info) const
398 {
399         if (!vcs_)
400                 return string();
401
402         return vcs_->revisionInfo(info);
403 }
404
405
406 bool LyXVC::renameEnabled() const
407 {
408         if (!inUse())
409                 return false;
410         return vcs_->renameEnabled();
411 }
412
413
414 bool LyXVC::copyEnabled() const
415 {
416         if (!inUse())
417                 return false;
418         return vcs_->copyEnabled();
419 }
420
421
422 bool LyXVC::checkOutEnabled() const
423 {
424         return vcs_ && vcs_->checkOutEnabled();
425 }
426
427
428 bool LyXVC::checkInEnabled() const
429 {
430         return vcs_ && vcs_->checkInEnabled();
431 }
432
433
434 bool LyXVC::isCheckInWithConfirmation() const
435 {
436         return vcs_ && vcs_->isCheckInWithConfirmation();
437 }
438
439
440 bool LyXVC::lockingToggleEnabled() const
441 {
442         return vcs_ && vcs_->lockingToggleEnabled();
443 }
444
445
446 bool LyXVC::undoLastEnabled() const
447 {
448         return vcs_ && vcs_->undoLastEnabled();
449 }
450
451
452 bool LyXVC::repoUpdateEnabled() const
453 {
454         return vcs_ && vcs_->repoUpdateEnabled();
455 }
456
457
458 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
459 {
460         return vcs_ && vcs_->prepareFileRevision(rev, f);
461 }
462
463
464 bool LyXVC::prepareFileRevisionEnabled()
465 {
466         return vcs_ && vcs_->prepareFileRevisionEnabled();
467 }
468
469 } // namespace lyx