]> git.lyx.org Git - lyx.git/blob - src/VCBackend.cpp
fix a bunch of harmless warnings
[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 string RCS::checkIn(string const & msg)
174 {
175         int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
176                     + quoteName(onlyFilename(owner_->absFileName())),
177                     FileName(owner_->filePath()));
178         return ret ? string() : "RCS: Proceeded";
179 }
180
181 bool RCS::checkInEnabled()
182 {
183         return owner_ && !owner_->isReadonly();
184 }
185
186 string RCS::checkOut()
187 {
188         owner_->markClean();
189         int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
190                     FileName(owner_->filePath()));
191         return ret ? string() : "RCS: Proceeded";
192 }
193
194
195 bool RCS::checkOutEnabled()
196 {
197         return owner_ && owner_->isReadonly();
198 }
199
200
201 void RCS::revert()
202 {
203         doVCCommand("co -f -u" + version() + " "
204                     + quoteName(onlyFilename(owner_->absFileName())),
205                     FileName(owner_->filePath()));
206         // We ignore changes and just reload!
207         owner_->markClean();
208 }
209
210
211 void RCS::undoLast()
212 {
213         LYXERR(Debug::LYXVC, "LyXVC: undoLast");
214         doVCCommand("rcs -o" + version() + " "
215                     + quoteName(onlyFilename(owner_->absFileName())),
216                     FileName(owner_->filePath()));
217 }
218
219
220 bool RCS::undoLastEnabled()
221 {
222         return true;
223 }
224
225
226 void RCS::getLog(FileName const & tmpf)
227 {
228         doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
229                     + " > " + tmpf.toFilesystemEncoding(),
230                     FileName(owner_->filePath()));
231 }
232
233
234 bool RCS::toggleReadOnlyEnabled()
235 {
236         return true;
237 }
238
239
240 /////////////////////////////////////////////////////////////////////
241 //
242 // CVS
243 //
244 /////////////////////////////////////////////////////////////////////
245
246 CVS::CVS(FileName const & m, FileName const & f)
247 {
248         master_ = m;
249         file_ = f;
250         scanMaster();
251 }
252
253
254 FileName const CVS::findFile(FileName const & file)
255 {
256         // First we look for the CVS/Entries in the same dir
257         // where we have file.
258         FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
259         string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
260         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
261                              << "' for `" << tmpf << '\'');
262         if (entries.isReadableFile()) {
263                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
264                 // and see if we can find this file. We do a fast and
265                 // dirty parse here.
266                 ifstream ifs(entries.toFilesystemEncoding().c_str());
267                 string line;
268                 while (getline(ifs, line)) {
269                         LYXERR(Debug::LYXVC, "\tEntries: " << line);
270                         if (contains(line, tmpf))
271                                 return entries;
272                 }
273         }
274         return FileName();
275 }
276
277
278 void CVS::scanMaster()
279 {
280         LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n     Checking: " << master_);
281         // Ok now we do the real scan...
282         ifstream ifs(master_.toFilesystemEncoding().c_str());
283         string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
284         LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
285         string line;
286         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
287         while (getline(ifs, line)) {
288                 LYXERR(Debug::LYXVC, "\t  line: " << line);
289                 if (contains(line, tmpf)) {
290                         // Ok extract the fields.
291                         smatch sm;
292
293                         regex_match(line, sm, reg);
294
295                         //sm[0]; // whole matched string
296                         //sm[1]; // filename
297                         version_ = sm.str(2);
298                         string const file_date = sm.str(3);
299
300                         //sm[4]; // options
301                         //sm[5]; // tag or tagdate
302                         // FIXME: must double check file is stattable/existing
303                         time_t mod = file_.lastModified();
304                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
305                         LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
306                                 << "'\nModification date of file: `" << mod_date << '\'');
307                         //FIXME this whole locking bussiness is not working under cvs and the machinery
308                         // conforms to the ci usage, not cvs.
309                         if (file_date == mod_date) {
310                                 locker_ = "Unlocked";
311                                 vcstatus = UNLOCKED;
312                         } else {
313                                 // Here we should also to some more checking
314                                 // to see if there are conflicts or not.
315                                 locker_ = "Locked";
316                                 vcstatus = LOCKED;
317                         }
318                         break;
319                 }
320         }
321 }
322
323
324 void CVS::registrer(string const & msg)
325 {
326         doVCCommand("cvs -q add -m \"" + msg + "\" "
327                     + quoteName(onlyFilename(owner_->absFileName())),
328                     FileName(owner_->filePath()));
329 }
330
331
332 string CVS::checkIn(string const & msg)
333 {
334         int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
335                     + quoteName(onlyFilename(owner_->absFileName())),
336                     FileName(owner_->filePath()));
337         return ret ? string() : "CVS: Proceeded";
338 }
339
340
341 bool CVS::checkInEnabled()
342 {
343         return true;
344 }
345
346
347 string CVS::checkOut()
348 {
349         // cvs update or perhaps for cvs this should be a noop
350         // we need to detect conflict (eg "C" in output)
351         // before we can do this.
352         lyxerr << "Sorry not implemented." << endl;
353         return string();
354 }
355
356
357 bool CVS::checkOutEnabled()
358 {
359         return false;
360 }
361
362
363 void CVS::revert()
364 {
365         // Reverts to the version in CVS repository and
366         // gets the updated version from the repository.
367         string const fil = quoteName(onlyFilename(owner_->absFileName()));
368         // This is sensitive operation, so at lest some check about
369         // existence of cvs program and its file
370         if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
371                 return;
372         FileName f(owner_->absFileName());
373         f.removeFile();
374         doVCCommand("cvs update " + fil,
375                     FileName(owner_->filePath()));
376         owner_->markClean();
377 }
378
379
380 void CVS::undoLast()
381 {
382         // merge the current with the previous version
383         // in a reverse patch kind of way, so that the
384         // result is to revert the last changes.
385         lyxerr << "Sorry not implemented." << endl;
386 }
387
388
389 bool CVS::undoLastEnabled()
390 {
391         return false;
392 }
393
394
395 void CVS::getLog(FileName const & tmpf)
396 {
397         doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
398                     + " > " + tmpf.toFilesystemEncoding(),
399                     FileName(owner_->filePath()));
400 }
401
402 bool CVS::toggleReadOnlyEnabled()
403 {
404         return false;
405 }
406
407 /////////////////////////////////////////////////////////////////////
408 //
409 // SVN
410 //
411 /////////////////////////////////////////////////////////////////////
412
413 SVN::SVN(FileName const & m, FileName const & f)
414 {
415         master_ = m;
416         file_ = f;
417         scanMaster();
418 }
419
420
421 FileName const SVN::findFile(FileName const & file)
422 {
423         // First we look for the CVS/Entries in the same dir
424         // where we have file.
425         FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
426         string const tmpf = onlyFilename(file.absFilename());
427         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
428                              << "' for `" << tmpf << '\'');
429         if (entries.isReadableFile()) {
430                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
431                 // and see if we can find this file. We do a fast and
432                 // dirty parse here.
433                 ifstream ifs(entries.toFilesystemEncoding().c_str());
434                 string line, oldline;
435                 while (getline(ifs, line)) {
436                         if (line == "dir" || line == "file")
437                                 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
438                         if (oldline == tmpf && line == "file")
439                                 return entries;
440                         oldline = line;
441                 }
442         }
443         return FileName();
444 }
445
446
447 void SVN::scanMaster()
448 {
449         // if we want some locking under svn
450         // we need different infrastructure around
451         locker_ = "Unlocked";
452         vcstatus = UNLOCKED;
453 }
454
455
456 void SVN::registrer(string const & /*msg*/)
457 {
458         doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
459                     FileName(owner_->filePath()));
460 }
461
462
463 string SVN::checkIn(string const & msg)
464 {
465         FileName tmpf = FileName::tempName("lyxvcout");
466         if (tmpf.empty()){
467                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
468                 return N_("Error: Could not generate logfile.");
469         }
470
471         doVCCommand("svn commit -m \"" + msg + "\" "
472                     + quoteName(onlyFilename(owner_->absFileName()))
473                     + " > " + tmpf.toFilesystemEncoding(),
474                     FileName(owner_->filePath()));
475
476         string log;
477         string res = scanLogFile(tmpf, log);
478         if (!res.empty())
479                 frontend::Alert::error(_("Revision control error."),
480                                 _("Error when commiting to repository.\n"
481                                 "You have to manually resolve the problem.\n"
482                                 "After pressing OK, LyX will reopen the document."));
483         tmpf.erase();
484         return "SVN: " + log;
485 }
486
487
488 bool SVN::checkInEnabled()
489 {
490         return true;
491 }
492
493 // FIXME Correctly return code should be checked instead of this.
494 // This would need another solution than just plain startscript.
495 // Hint from Andre': QProcess::readAllStandardError()...
496 string SVN::scanLogFile(FileName const & f, string & status)
497 {
498         ifstream ifs(f.toFilesystemEncoding().c_str());
499         string line;
500
501         while (ifs) {
502                 getline(ifs, line);
503                 lyxerr << line << "\n";
504                 if (!line.empty()) status += line + "; ";
505                 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
506                         ifs.close();
507                         return line;
508                 }
509         }
510         ifs.close();
511         return string();
512 }
513
514
515 string SVN::checkOut()
516 {
517         FileName tmpf = FileName::tempName("lyxvcout");
518         if (tmpf.empty()) {
519                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
520                 return N_("Error: Could not generate logfile.");
521         }
522
523         doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
524                     + " > " + tmpf.toFilesystemEncoding(),
525                     FileName(owner_->filePath()));
526
527         string log;
528         string res = scanLogFile(tmpf, log);
529         if (!res.empty())
530                 frontend::Alert::error(_("Revision control error."),
531                         bformat(_("Error when updating from repository.\n"
532                                 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
533                                 "After pressing OK, LyX will try to reopen resolved document."),
534                         from_ascii(res)));
535         tmpf.erase();
536         return "SVN: " + log;
537 }
538
539
540 bool SVN::checkOutEnabled()
541 {
542         return true;
543 }
544
545
546 void SVN::revert()
547 {
548         // Reverts to the version in CVS repository and
549         // gets the updated version from the repository.
550         string const fil = quoteName(onlyFilename(owner_->absFileName()));
551
552         doVCCommand("svn revert -q " + fil,
553                     FileName(owner_->filePath()));
554         owner_->markClean();
555 }
556
557
558 void SVN::undoLast()
559 {
560         // merge the current with the previous version
561         // in a reverse patch kind of way, so that the
562         // result is to revert the last changes.
563         lyxerr << "Sorry not implemented." << endl;
564 }
565
566
567 bool SVN::undoLastEnabled()
568 {
569         return false;
570 }
571
572
573 void SVN::getLog(FileName const & tmpf)
574 {
575         doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
576                     + " > " + tmpf.toFilesystemEncoding(),
577                     FileName(owner_->filePath()));
578 }
579
580
581 bool SVN::toggleReadOnlyEnabled()
582 {
583         return false;
584 }
585
586
587 } // namespace lyx