]> git.lyx.org Git - lyx.git/blob - src/VCBackend.cpp
8dcef4332f7d822772d4c3dd5dcd86dd371e4023
[lyx.git] / src / VCBackend.cpp
1 /**
2  * \file VCBackend.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "VCBackend.h"
14 #include "Buffer.h"
15
16 #include "frontends/alert.h"
17
18 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
22 #include "support/Path.h"
23 #include "support/Systemcall.h"
24
25 #include <boost/regex.hpp>
26
27 #include <fstream>
28
29 using namespace std;
30 using namespace lyx::support;
31
32 using boost::regex;
33 using boost::regex_match;
34 using boost::smatch;
35
36 namespace lyx {
37
38
39 int VCS::doVCCommandCall(string const & cmd, FileName const & path){
40         LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
41         Systemcall one;
42         support::PathChanger p(path);
43         return one.startscript(Systemcall::Wait, cmd);
44 }
45
46 int VCS::doVCCommand(string const & cmd, FileName const & path)
47 {
48         owner_->setBusy(true);
49         int const ret = doVCCommandCall(cmd, path);
50         owner_->setBusy(false);
51         if (ret)
52                 frontend::Alert::error(_("Revision control error."),
53                         bformat(_("Some problem occured while running the command:\n"
54                                   "'%1$s'."),
55                         from_ascii(cmd)));
56         return ret;
57 }
58
59
60 /////////////////////////////////////////////////////////////////////
61 //
62 // RCS
63 //
64 /////////////////////////////////////////////////////////////////////
65
66 RCS::RCS(FileName const & m)
67 {
68         master_ = m;
69         scanMaster();
70 }
71
72
73 FileName const RCS::findFile(FileName const & file)
74 {
75         // Check if *,v exists.
76         FileName tmp(file.absFilename() + ",v");
77         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
78         if (tmp.isReadableFile()) {
79                 LYXERR(Debug::LYXVC, "Yes " << file << " is under rcs.");
80                 return tmp;
81         }
82
83         // Check if RCS/*,v exists.
84         tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
85         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
86         if (tmp.isReadableFile()) {
87                 LYXERR(Debug::LYXVC, "Yes " << file << " it is under rcs.");
88                 return tmp;
89         }
90
91         return FileName();
92 }
93
94
95 void RCS::retrieve(FileName const & file)
96 {
97         LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
98         doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
99                          FileName());
100 }
101
102
103 void RCS::scanMaster()
104 {
105         LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster.");
106
107         ifstream ifs(master_.toFilesystemEncoding().c_str());
108
109         string token;
110         bool read_enough = false;
111
112         while (!read_enough && ifs >> token) {
113                 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
114                         << token << '\'');
115
116                 if (token.empty())
117                         continue;
118                 else if (token == "head") {
119                         // get version here
120                         string tmv;
121                         ifs >> tmv;
122                         tmv = rtrim(tmv, ";");
123                         version_ = tmv;
124                         LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
125                 } else if (contains(token, "access")
126                            || contains(token, "symbols")
127                            || contains(token, "strict")) {
128                         // nothing
129                 } else if (contains(token, "locks")) {
130                         // get locker here
131                         if (contains(token, ';')) {
132                                 locker_ = "Unlocked";
133                                 vcstatus = UNLOCKED;
134                                 continue;
135                         }
136                         string tmpt;
137                         string s1;
138                         string s2;
139                         do {
140                                 ifs >> tmpt;
141                                 s1 = rtrim(tmpt, ";");
142                                 // tmp is now in the format <user>:<version>
143                                 s1 = split(s1, s2, ':');
144                                 // s2 is user, and s1 is version
145                                 if (s1 == version_) {
146                                         locker_ = s2;
147                                         vcstatus = LOCKED;
148                                         break;
149                                 }
150                         } while (!contains(tmpt, ';'));
151
152                 } else if (token == "comment") {
153                         // we don't need to read any further than this.
154                         read_enough = true;
155                 } else {
156                         // unexpected
157                         LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
158                 }
159         }
160 }
161
162
163 void RCS::registrer(string const & msg)
164 {
165         string cmd = "ci -q -u -i -t-\"";
166         cmd += msg;
167         cmd += "\" ";
168         cmd += quoteName(onlyFilename(owner_->absFileName()));
169         doVCCommand(cmd, FileName(owner_->filePath()));
170 }
171
172
173 void RCS::checkIn(string const & msg)
174 {
175         doVCCommand("ci -q -u -m\"" + msg + "\" "
176                     + quoteName(onlyFilename(owner_->absFileName())),
177                     FileName(owner_->filePath()));
178 }
179
180 bool RCS::checkInEnabled()
181 {
182         return owner_ && !owner_->isReadonly();
183 }
184
185 void RCS::checkOut()
186 {
187         owner_->markClean();
188         doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
189                     FileName(owner_->filePath()));
190 }
191
192
193 bool RCS::checkOutEnabled()
194 {
195         return owner_ && owner_->isReadonly();
196 }
197
198
199 void RCS::revert()
200 {
201         doVCCommand("co -f -u" + version() + " "
202                     + quoteName(onlyFilename(owner_->absFileName())),
203                     FileName(owner_->filePath()));
204         // We ignore changes and just reload!
205         owner_->markClean();
206 }
207
208
209 void RCS::undoLast()
210 {
211         LYXERR(Debug::LYXVC, "LyXVC: undoLast");
212         doVCCommand("rcs -o" + version() + " "
213                     + quoteName(onlyFilename(owner_->absFileName())),
214                     FileName(owner_->filePath()));
215 }
216
217
218 bool RCS::undoLastEnabled()
219 {
220         return true;
221 }
222
223
224 void RCS::getLog(FileName const & tmpf)
225 {
226         doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
227                     + " > " + tmpf.toFilesystemEncoding(),
228                     FileName(owner_->filePath()));
229 }
230
231
232 bool RCS::toggleReadOnlyEnabled()
233 {
234         return true;
235 }
236
237
238 /////////////////////////////////////////////////////////////////////
239 //
240 // CVS
241 //
242 /////////////////////////////////////////////////////////////////////
243
244 CVS::CVS(FileName const & m, FileName const & f)
245 {
246         master_ = m;
247         file_ = f;
248         scanMaster();
249 }
250
251
252 FileName const CVS::findFile(FileName const & file)
253 {
254         // First we look for the CVS/Entries in the same dir
255         // where we have file.
256         FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
257         string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
258         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
259                              << "' for `" << tmpf << '\'');
260         if (entries.isReadableFile()) {
261                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
262                 // and see if we can find this file. We do a fast and
263                 // dirty parse here.
264                 ifstream ifs(entries.toFilesystemEncoding().c_str());
265                 string line;
266                 while (getline(ifs, line)) {
267                         LYXERR(Debug::LYXVC, "\tEntries: " << line);
268                         if (contains(line, tmpf))
269                                 return entries;
270                 }
271         }
272         return FileName();
273 }
274
275
276 void CVS::scanMaster()
277 {
278         LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n     Checking: " << master_);
279         // Ok now we do the real scan...
280         ifstream ifs(master_.toFilesystemEncoding().c_str());
281         string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
282         LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
283         string line;
284         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
285         while (getline(ifs, line)) {
286                 LYXERR(Debug::LYXVC, "\t  line: " << line);
287                 if (contains(line, tmpf)) {
288                         // Ok extract the fields.
289                         smatch sm;
290
291                         regex_match(line, sm, reg);
292
293                         //sm[0]; // whole matched string
294                         //sm[1]; // filename
295                         version_ = sm.str(2);
296                         string const file_date = sm.str(3);
297
298                         //sm[4]; // options
299                         //sm[5]; // tag or tagdate
300                         // FIXME: must double check file is stattable/existing
301                         time_t mod = file_.lastModified();
302                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
303                         LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
304                                 << "'\nModification date of file: `" << mod_date << '\'');
305                         //FIXME this whole locking bussiness is not working under cvs and the machinery
306                         // conforms to the ci usage, not cvs.
307                         if (file_date == mod_date) {
308                                 locker_ = "Unlocked";
309                                 vcstatus = UNLOCKED;
310                         } else {
311                                 // Here we should also to some more checking
312                                 // to see if there are conflicts or not.
313                                 locker_ = "Locked";
314                                 vcstatus = LOCKED;
315                         }
316                         break;
317                 }
318         }
319 }
320
321
322 void CVS::registrer(string const & msg)
323 {
324         doVCCommand("cvs -q add -m \"" + msg + "\" "
325                     + quoteName(onlyFilename(owner_->absFileName())),
326                     FileName(owner_->filePath()));
327 }
328
329
330 void CVS::checkIn(string const & msg)
331 {
332         doVCCommand("cvs -q commit -m \"" + msg + "\" "
333                     + quoteName(onlyFilename(owner_->absFileName())),
334                     FileName(owner_->filePath()));
335 }
336
337
338 bool CVS::checkInEnabled()
339 {
340         return true;
341 }
342
343
344 void CVS::checkOut()
345 {
346         // cvs update or perhaps for cvs this should be a noop
347         // we need to detect conflict (eg "C" in output)
348         // before we can do this.
349         lyxerr << "Sorry not implemented." << endl;
350 }
351
352
353 bool CVS::checkOutEnabled()
354 {
355         return false;
356 }
357
358
359 void CVS::revert()
360 {
361         // Reverts to the version in CVS repository and
362         // gets the updated version from the repository.
363         string const fil = quoteName(onlyFilename(owner_->absFileName()));
364         // This is sensitive operation, so at lest some check about
365         // existence of cvs program and its file
366         if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
367                 return;
368         FileName f(owner_->absFileName());
369         f.removeFile();
370         doVCCommand("cvs update " + fil,
371                     FileName(owner_->filePath()));
372         owner_->markClean();
373 }
374
375
376 void CVS::undoLast()
377 {
378         // merge the current with the previous version
379         // in a reverse patch kind of way, so that the
380         // result is to revert the last changes.
381         lyxerr << "Sorry not implemented." << endl;
382 }
383
384
385 bool CVS::undoLastEnabled()
386 {
387         return false;
388 }
389
390
391 void CVS::getLog(FileName const & tmpf)
392 {
393         doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
394                     + " > " + tmpf.toFilesystemEncoding(),
395                     FileName(owner_->filePath()));
396 }
397
398 bool CVS::toggleReadOnlyEnabled()
399 {
400         return false;
401 }
402
403 /////////////////////////////////////////////////////////////////////
404 //
405 // SVN
406 //
407 /////////////////////////////////////////////////////////////////////
408
409 SVN::SVN(FileName const & m, FileName const & f)
410 {
411         master_ = m;
412         file_ = f;
413         scanMaster();
414 }
415
416
417 FileName const SVN::findFile(FileName const & file)
418 {
419         // First we look for the CVS/Entries in the same dir
420         // where we have file.
421         FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
422         string const tmpf = onlyFilename(file.absFilename());
423         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
424                              << "' for `" << tmpf << '\'');
425         if (entries.isReadableFile()) {
426                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
427                 // and see if we can find this file. We do a fast and
428                 // dirty parse here.
429                 ifstream ifs(entries.toFilesystemEncoding().c_str());
430                 string line, oldline;
431                 while (getline(ifs, line)) {
432                         if (line == "dir" || line == "file")
433                                 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
434                         if (oldline == tmpf && line == "file")
435                                 return entries;
436                         oldline = line;
437                 }
438         }
439         return FileName();
440 }
441
442
443 void SVN::scanMaster()
444 {
445         // if we want some locking under svn
446         // we need different infrastructure around
447         locker_ = "Unlocked";
448         vcstatus = UNLOCKED;
449 }
450
451
452 void SVN::registrer(string const & msg)
453 {
454         doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
455                     FileName(owner_->filePath()));
456 }
457
458
459 void SVN::checkIn(string const & msg)
460 {
461         FileName tmpf = FileName::tempName("lyxvcout");
462         if (tmpf.empty()){
463                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
464                 return;
465         }
466
467         doVCCommand("svn commit -m \"" + msg + "\" "
468                     + quoteName(onlyFilename(owner_->absFileName()))
469                     + " 2> " + tmpf.toFilesystemEncoding(),
470                     FileName(owner_->filePath()));
471
472         string res = scanLogFile(tmpf);
473         if (!res.empty())
474                 frontend::Alert::error(_("Revision control error."),
475                                 _("Error when commiting to repository.\n"
476                                 "You have to manually resolve the problem.\n"
477                                 "After pressing OK, LyX will reopen the document."));
478         tmpf.erase();
479 }
480
481
482 bool SVN::checkInEnabled()
483 {
484         return true;
485 }
486
487 // FIXME Correctly return code should be checked instead of this.
488 // This would need another solution than just plain startscript.
489 // Hint from Andre': QProcess::readAllStandardError()...
490 string SVN::scanLogFile(FileName const & f)
491 {
492         ifstream ifs(f.toFilesystemEncoding().c_str());
493         string line;
494
495         while (ifs) {
496                 getline(ifs, line);
497                 lyxerr << line << "\n";
498                 if (prefixIs(line, "C "))
499                         return line;
500                 if (contains(line, "Commit failed"))
501                         return line;
502         }
503         return string();
504 }
505
506
507 void SVN::checkOut()
508 {
509         FileName tmpf = FileName::tempName("lyxvcout");
510         if (tmpf.empty()) {
511                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
512                 return;
513         }
514
515         doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
516                     + " > " + tmpf.toFilesystemEncoding(),
517                     FileName(owner_->filePath()));
518
519         string res = scanLogFile(tmpf);
520         if (!res.empty())
521                 frontend::Alert::error(_("Revision control error."),
522                         bformat(_("Error when updating from repository.\n"
523                                 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
524                                 "After pressing OK, LyX will try to reopen resolved document."),
525                         from_ascii(res)));
526         tmpf.erase();
527 }
528
529
530 bool SVN::checkOutEnabled()
531 {
532         return true;
533 }
534
535
536 void SVN::revert()
537 {
538         // Reverts to the version in CVS repository and
539         // gets the updated version from the repository.
540         string const fil = quoteName(onlyFilename(owner_->absFileName()));
541
542         doVCCommand("svn revert -q " + fil,
543                     FileName(owner_->filePath()));
544         owner_->markClean();
545 }
546
547
548 void SVN::undoLast()
549 {
550         // merge the current with the previous version
551         // in a reverse patch kind of way, so that the
552         // result is to revert the last changes.
553         lyxerr << "Sorry not implemented." << endl;
554 }
555
556
557 bool SVN::undoLastEnabled()
558 {
559         return false;
560 }
561
562
563 void SVN::getLog(FileName const & tmpf)
564 {
565         doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
566                     + " > " + tmpf.toFilesystemEncoding(),
567                     FileName(owner_->filePath()));
568 }
569
570
571 bool SVN::toggleReadOnlyEnabled()
572 {
573         return false;
574 }
575
576
577 } // namespace lyx