]> git.lyx.org Git - lyx.git/blob - src/LyXVC.cpp
1e6cb121536f562cbb288dd6f3c2272d059acedd
[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))
62                 return true;
63         if (GIT::findFile(fn))
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 (SVN::findFile(fn)) {
84                 vcs_.reset(new SVN(owner_));
85                 return true;
86         }
87         // Check if file is under GIT
88         if (GIT::findFile(fn)) {
89                 vcs_.reset(new GIT(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 version control.
102         // This happens if we are trying to load does not exist.
103         // It may yet exist in the repository and so could be
104         // checked out.
105         bool foundRCS = !RCS::findFile(fn).empty();
106         bool foundCVS = foundRCS ? false : !CVS::findFile(fn).empty();
107         bool foundSVN = (foundRCS || foundCVS) ? false : SVN::findFile(fn);
108         bool foundGIT = (foundRCS || foundCVS || foundSVN) ? false : GIT::findFile(fn);
109         if (foundRCS || foundCVS || foundSVN || foundGIT) {
110                 docstring const file = makeDisplayPath(fn.absFileName(), 20);
111                 docstring const text =
112                         bformat(_("Do you want to retrieve the document"
113                                                    " %1$s from version control?"), file);
114                 int const ret = Alert::prompt(_("Retrieve from version control?"),
115                         text, 0, 1, _("&Retrieve"), _("&Cancel"));
116
117                 if (ret == 0) {
118                         // Since the retrieve commands are implemented using
119                         // more general update commands we need to ensure that
120                         // we do not change an existing file by accident.
121                         if (fn.exists())
122                                 return false;
123                         if (foundRCS)
124                                 return RCS::retrieve(fn);
125                         else if (foundCVS)
126                                 return CVS::retrieve(fn);
127                         else if (foundSVN)
128                                 return SVN::retrieve(fn);
129                         else
130                                 return GIT::retrieve(fn);
131                 }
132         }
133         return false;
134 }
135
136
137 void LyXVC::setBuffer(Buffer * buf)
138 {
139         owner_ = buf;
140 }
141
142
143 bool LyXVC::registrer()
144 {
145         FileName const filename = owner_->fileName();
146
147         // there must be a file to save
148         if (!filename.isReadableFile()) {
149                 Alert::error(_("Document not saved"),
150                              _("You must save the document "
151                                             "before it can be registered."));
152                 return false;
153         }
154
155         // it is very likely here that the vcs is not created yet...
156         if (!vcs_) {
157                 FileName found = VCS::checkParentDirs(filename, ".git/index");
158
159                 if (!found.empty()) {
160                         LYXERR(Debug::LYXVC, "LyXVC: registering "
161                                 << to_utf8(filename.displayName()) << " with GIT");
162                         vcs_.reset(new GIT(owner_));
163
164                 } else {
165                         found = VCS::checkParentDirs(filename, ".svn/entries");
166                         if (!found.empty()) {
167                                 LYXERR(Debug::LYXVC, "LyXVC: registering "
168                                         << to_utf8(filename.displayName()) << " with SVN");
169                                 vcs_.reset(new SVN(owner_));
170
171                         } else {
172                                 // We only need to check the current directory, since CVS meta-data
173                                 // is in every sub-directory.
174                                 FileName const cvs_entries(onlyPath(filename.absFileName()) + "/CVS/Entries");
175                                 if (cvs_entries.isReadableFile()) {
176                                         LYXERR(Debug::LYXVC, "LyXVC: registering "
177                                                 << to_utf8(filename.displayName()) << " with CVS");
178                                         vcs_.reset(new CVS(cvs_entries, owner_));
179
180                                 } else {
181                                         // If all else fails, use RCS
182                                         LYXERR(Debug::LYXVC, "LyXVC: registering "
183                                                 << to_utf8(filename.displayName()) << " with RCS");
184                                         vcs_.reset(new RCS(FileName(), owner_));
185                                 }
186                         }
187                 }
188         }
189
190         LYXERR(Debug::LYXVC, "LyXVC: registrer");
191         docstring response;
192         bool ok = Alert::askForText(response, _("LyX VC: Initial description"),
193                         _("(no initial description)"));
194         if (!ok) {
195                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
196                 vcs_.reset(nullptr);
197                 return false;
198         }
199         if (response.empty())
200                 response = _("(no initial description)");
201         vcs_->registrer(to_utf8(response));
202         return true;
203 }
204
205
206 string LyXVC::rename(FileName const & fn)
207 {
208         LYXERR(Debug::LYXVC, "LyXVC: rename");
209         if (!vcs_ || fileInVC(fn))
210                 return string();
211         docstring response;
212         bool ok = Alert::askForText(response, _("LyX VC: Log message"),
213                         _("(no log message)"));
214         if (!ok) {
215                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
216                 return string();
217         }
218         if (response.empty())
219                 response = _("(no log message)");
220         string ret = vcs_->rename(fn, to_utf8(response));
221         return ret;
222 }
223
224
225 string LyXVC::copy(FileName const & fn)
226 {
227         LYXERR(Debug::LYXVC, "LyXVC: copy");
228         if (!vcs_ || fileInVC(fn))
229                 return string();
230         docstring response;
231         bool ok = Alert::askForText(response, _("LyX VC: Log message"),
232                         _("(no log message)"));
233         if (!ok) {
234                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
235                 return string();
236         }
237         if (response.empty())
238                 response = _("(no log message)");
239         string ret = vcs_->copy(fn, to_utf8(response));
240         return ret;
241 }
242
243
244 LyXVC::CommandResult LyXVC::checkIn(string & log)
245 {
246         LYXERR(Debug::LYXVC, "LyXVC: checkIn");
247         if (!vcs_)
248                 return ErrorBefore;
249         docstring empty(_("(no log message)"));
250         docstring response;
251         bool ok = true;
252         if (vcs_->isCheckInWithConfirmation())
253                 ok = Alert::askForText(response, _("LyX VC: Log Message"));
254         if (ok) {
255                 if (response.empty())
256                         response = empty;
257                 //shell collisions
258                 response = subst(response, from_ascii("\""), from_ascii("\\\""));
259                 return vcs_->checkIn(to_utf8(response), log);
260         } else {
261                 LYXERR(Debug::LYXVC, "LyXVC: user cancelled");
262                 return Cancelled;
263         }
264 }
265
266
267 string LyXVC::checkOut()
268 {
269         if (!vcs_)
270                 return string();
271         //RCS allows checkOut only in ReadOnly mode
272         if (vcs_->toggleReadOnlyEnabled() && !owner_->hasReadonlyFlag())
273                 return string();
274
275         LYXERR(Debug::LYXVC, "LyXVC: checkOut");
276         return vcs_->checkOut();
277 }
278
279
280 string LyXVC::repoUpdate()
281 {
282         LYXERR(Debug::LYXVC, "LyXVC: repoUpdate");
283         if (!vcs_)
284                 return string();
285         return vcs_->repoUpdate();
286 }
287
288
289 string LyXVC::lockingToggle()
290 {
291         LYXERR(Debug::LYXVC, "LyXVC: toggle locking property");
292         if (!vcs_)
293                 return string();
294         return vcs_->lockingToggle();
295 }
296
297
298 bool LyXVC::revert()
299 {
300         LYXERR(Debug::LYXVC, "LyXVC: revert");
301         if (!vcs_)
302                 return false;
303
304         docstring const file = owner_->fileName().displayName(20);
305         docstring text = bformat(_("Reverting to the stored version of the "
306                                 "document %1$s will lose all current changes.\n\n"
307                                 "Do you want to revert to the older version?"), file);
308         int ret = 0;
309         if (vcs_->isRevertWithConfirmation())
310                 ret = Alert::prompt(_("Revert to stored version of document?"),
311                         text, 0, 1, _("&Revert"), _("&Cancel"));
312
313         return ret == 0 && vcs_->revert();
314 }
315
316
317 void LyXVC::undoLast()
318 {
319         if (!vcs_)
320                 return;
321         vcs_->undoLast();
322 }
323
324
325 string LyXVC::toggleReadOnly()
326 {
327         if (!vcs_)
328                 return string();
329         if (!vcs_->toggleReadOnlyEnabled())
330                 return string();
331
332         switch (vcs_->status()) {
333         case VCS::UNLOCKED:
334                 LYXERR(Debug::LYXVC, "LyXVC: toggle to locked");
335                 return checkOut();
336         case VCS::LOCKED: {
337                 LYXERR(Debug::LYXVC, "LyXVC: toggle to unlocked");
338                 string log;
339                 if (checkIn(log) != VCSuccess)
340                         return string();
341                 return log;
342         }
343         case VCS::NOLOCKING:
344                 Buffer * b = vcs_->owner();
345                 bool const newstate = !b->hasReadonlyFlag();
346                 string result = "LyXVC: toggle to ";
347                 result += (newstate ? "readonly" : "readwrite");
348                 LYXERR(Debug::LYXVC, result);
349                 b->setReadonly(newstate);
350                 return result;
351         }
352         return string();
353 }
354
355
356 bool LyXVC::inUse() const
357 {
358         return vcs_ != nullptr;
359 }
360
361
362 string const LyXVC::versionString() const
363 {
364         if (!vcs_)
365                 return string();
366         return vcs_->versionString();
367 }
368
369
370 bool LyXVC::locking() const
371 {
372         if (!vcs_)
373                 return false;
374         return vcs_->status() != VCS::NOLOCKING;
375 }
376
377
378 string const LyXVC::getLogFile() const
379 {
380         if (!vcs_)
381                 return string();
382
383         TempFile tempfile("lyxvclog");
384         tempfile.setAutoRemove(false);
385         FileName const tmpf = tempfile.name();
386         if (tmpf.empty()) {
387                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
388                 return string();
389         }
390         LYXERR(Debug::LYXVC, "Generating logfile " << tmpf);
391         vcs_->getLog(tmpf);
392         return tmpf.absFileName();
393 }
394
395
396 string LyXVC::revisionInfo(RevisionInfo const info) const
397 {
398         if (!vcs_)
399                 return string();
400
401         return vcs_->revisionInfo(info);
402 }
403
404
405 bool LyXVC::renameEnabled() const
406 {
407         if (!inUse())
408                 return false;
409         return vcs_->renameEnabled();
410 }
411
412
413 bool LyXVC::copyEnabled() const
414 {
415         if (!inUse())
416                 return false;
417         return vcs_->copyEnabled();
418 }
419
420
421 bool LyXVC::checkOutEnabled() const
422 {
423         return vcs_ && vcs_->checkOutEnabled();
424 }
425
426
427 bool LyXVC::checkInEnabled() const
428 {
429         return vcs_ && vcs_->checkInEnabled();
430 }
431
432
433 bool LyXVC::isCheckInWithConfirmation() const
434 {
435         return vcs_ && vcs_->isCheckInWithConfirmation();
436 }
437
438
439 bool LyXVC::lockingToggleEnabled() const
440 {
441         return vcs_ && vcs_->lockingToggleEnabled();
442 }
443
444
445 bool LyXVC::undoLastEnabled() const
446 {
447         return vcs_ && vcs_->undoLastEnabled();
448 }
449
450
451 bool LyXVC::repoUpdateEnabled() const
452 {
453         return vcs_ && vcs_->repoUpdateEnabled();
454 }
455
456
457 bool LyXVC::prepareFileRevision(string const & rev, std::string & f)
458 {
459         return vcs_ && vcs_->prepareFileRevision(rev, f);
460 }
461
462
463 bool LyXVC::prepareFileRevisionEnabled()
464 {
465         return vcs_ && vcs_->prepareFileRevisionEnabled();
466 }
467
468 } // namespace lyx