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