]> git.lyx.org Git - lyx.git/blob - src/VCBackend.cpp
Fix bug #4510: GuiInclude->Edit marks master as changed.
[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         if (owner_)
49                 owner_->setBusy(true);
50
51         int const ret = doVCCommandCall(cmd, path);
52
53         if (owner_)
54                 owner_->setBusy(false);
55         if (ret)
56                 frontend::Alert::error(_("Revision control error."),
57                         bformat(_("Some problem occured while running the command:\n"
58                                   "'%1$s'."),
59                         from_utf8(cmd)));
60         return ret;
61 }
62
63
64 /////////////////////////////////////////////////////////////////////
65 //
66 // RCS
67 //
68 /////////////////////////////////////////////////////////////////////
69
70 RCS::RCS(FileName const & m)
71 {
72         master_ = m;
73         scanMaster();
74 }
75
76
77 FileName const RCS::findFile(FileName const & file)
78 {
79         // Check if *,v exists.
80         FileName tmp(file.absFilename() + ",v");
81         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
82         if (tmp.isReadableFile()) {
83                 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
84                 return tmp;
85         }
86
87         // Check if RCS/*,v exists.
88         tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
89         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
90         if (tmp.isReadableFile()) {
91                 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
92                 return tmp;
93         }
94
95         return FileName();
96 }
97
98
99 void RCS::retrieve(FileName const & file)
100 {
101         LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
102         doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
103                          FileName());
104 }
105
106
107 void RCS::scanMaster()
108 {
109         if (master_.empty())
110                 return;
111
112         LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
113
114         ifstream ifs(master_.toFilesystemEncoding().c_str());
115
116         string token;
117         bool read_enough = false;
118
119         while (!read_enough && ifs >> token) {
120                 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
121                         << token << '\'');
122
123                 if (token.empty())
124                         continue;
125                 else if (token == "head") {
126                         // get version here
127                         string tmv;
128                         ifs >> tmv;
129                         tmv = rtrim(tmv, ";");
130                         version_ = tmv;
131                         LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
132                 } else if (contains(token, "access")
133                            || contains(token, "symbols")
134                            || contains(token, "strict")) {
135                         // nothing
136                 } else if (contains(token, "locks")) {
137                         // get locker here
138                         if (contains(token, ';')) {
139                                 locker_ = "Unlocked";
140                                 vcstatus = UNLOCKED;
141                                 continue;
142                         }
143                         string tmpt;
144                         string s1;
145                         string s2;
146                         do {
147                                 ifs >> tmpt;
148                                 s1 = rtrim(tmpt, ";");
149                                 // tmp is now in the format <user>:<version>
150                                 s1 = split(s1, s2, ':');
151                                 // s2 is user, and s1 is version
152                                 if (s1 == version_) {
153                                         locker_ = s2;
154                                         vcstatus = LOCKED;
155                                         break;
156                                 }
157                         } while (!contains(tmpt, ';'));
158
159                 } else if (token == "comment") {
160                         // we don't need to read any further than this.
161                         read_enough = true;
162                 } else {
163                         // unexpected
164                         LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
165                 }
166         }
167 }
168
169
170 void RCS::registrer(string const & msg)
171 {
172         string cmd = "ci -q -u -i -t-\"";
173         cmd += msg;
174         cmd += "\" ";
175         cmd += quoteName(onlyFilename(owner_->absFileName()));
176         doVCCommand(cmd, FileName(owner_->filePath()));
177 }
178
179
180 string RCS::checkIn(string const & msg)
181 {
182         int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
183                     + quoteName(onlyFilename(owner_->absFileName())),
184                     FileName(owner_->filePath()));
185         return ret ? string() : "RCS: Proceeded";
186 }
187
188 bool RCS::checkInEnabled()
189 {
190         return owner_ && !owner_->isReadonly();
191 }
192
193 string RCS::checkOut()
194 {
195         owner_->markClean();
196         int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
197                     FileName(owner_->filePath()));
198         return ret ? string() : "RCS: Proceeded";
199 }
200
201
202 bool RCS::checkOutEnabled()
203 {
204         return owner_ && owner_->isReadonly();
205 }
206
207
208 void RCS::revert()
209 {
210         doVCCommand("co -f -u" + version() + " "
211                     + quoteName(onlyFilename(owner_->absFileName())),
212                     FileName(owner_->filePath()));
213         // We ignore changes and just reload!
214         owner_->markClean();
215 }
216
217
218 void RCS::undoLast()
219 {
220         LYXERR(Debug::LYXVC, "LyXVC: undoLast");
221         doVCCommand("rcs -o" + version() + " "
222                     + quoteName(onlyFilename(owner_->absFileName())),
223                     FileName(owner_->filePath()));
224 }
225
226
227 bool RCS::undoLastEnabled()
228 {
229         return true;
230 }
231
232
233 void RCS::getLog(FileName const & tmpf)
234 {
235         doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
236                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
237                     FileName(owner_->filePath()));
238 }
239
240
241 bool RCS::toggleReadOnlyEnabled()
242 {
243         return true;
244 }
245
246
247 /////////////////////////////////////////////////////////////////////
248 //
249 // CVS
250 //
251 /////////////////////////////////////////////////////////////////////
252
253 CVS::CVS(FileName const & m, FileName const & f)
254 {
255         master_ = m;
256         file_ = f;
257         scanMaster();
258 }
259
260
261 FileName const CVS::findFile(FileName const & file)
262 {
263         // First we look for the CVS/Entries in the same dir
264         // where we have file.
265         FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
266         string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
267         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
268                              << "' for `" << tmpf << '\'');
269         if (entries.isReadableFile()) {
270                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
271                 // and see if we can find this file. We do a fast and
272                 // dirty parse here.
273                 ifstream ifs(entries.toFilesystemEncoding().c_str());
274                 string line;
275                 while (getline(ifs, line)) {
276                         LYXERR(Debug::LYXVC, "\tEntries: " << line);
277                         if (contains(line, tmpf))
278                                 return entries;
279                 }
280         }
281         return FileName();
282 }
283
284
285 void CVS::scanMaster()
286 {
287         LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n     Checking: " << master_);
288         // Ok now we do the real scan...
289         ifstream ifs(master_.toFilesystemEncoding().c_str());
290         string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
291         LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
292         string line;
293         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
294         while (getline(ifs, line)) {
295                 LYXERR(Debug::LYXVC, "\t  line: " << line);
296                 if (contains(line, tmpf)) {
297                         // Ok extract the fields.
298                         smatch sm;
299
300                         regex_match(line, sm, reg);
301
302                         //sm[0]; // whole matched string
303                         //sm[1]; // filename
304                         version_ = sm.str(2);
305                         string const file_date = sm.str(3);
306
307                         //sm[4]; // options
308                         //sm[5]; // tag or tagdate
309                         // FIXME: must double check file is stattable/existing
310                         time_t mod = file_.lastModified();
311                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
312                         LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
313                                 << "'\nModification date of file: `" << mod_date << '\'');
314                         //FIXME this whole locking bussiness is not working under cvs and the machinery
315                         // conforms to the ci usage, not cvs.
316                         if (file_date == mod_date) {
317                                 locker_ = "Unlocked";
318                                 vcstatus = UNLOCKED;
319                         } else {
320                                 // Here we should also to some more checking
321                                 // to see if there are conflicts or not.
322                                 locker_ = "Locked";
323                                 vcstatus = LOCKED;
324                         }
325                         break;
326                 }
327         }
328 }
329
330
331 void CVS::registrer(string const & msg)
332 {
333         doVCCommand("cvs -q add -m \"" + msg + "\" "
334                     + quoteName(onlyFilename(owner_->absFileName())),
335                     FileName(owner_->filePath()));
336 }
337
338
339 string CVS::checkIn(string const & msg)
340 {
341         int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
342                     + quoteName(onlyFilename(owner_->absFileName())),
343                     FileName(owner_->filePath()));
344         return ret ? string() : "CVS: Proceeded";
345 }
346
347
348 bool CVS::checkInEnabled()
349 {
350         return true;
351 }
352
353
354 string CVS::checkOut()
355 {
356         // cvs update or perhaps for cvs this should be a noop
357         // we need to detect conflict (eg "C" in output)
358         // before we can do this.
359         lyxerr << "Sorry, not implemented." << endl;
360         return string();
361 }
362
363
364 bool CVS::checkOutEnabled()
365 {
366         return false;
367 }
368
369
370 void CVS::revert()
371 {
372         // Reverts to the version in CVS repository and
373         // gets the updated version from the repository.
374         string const fil = quoteName(onlyFilename(owner_->absFileName()));
375         // This is sensitive operation, so at lest some check about
376         // existence of cvs program and its file
377         if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
378                 return;
379         FileName f(owner_->absFileName());
380         f.removeFile();
381         doVCCommand("cvs update " + fil,
382                     FileName(owner_->filePath()));
383         owner_->markClean();
384 }
385
386
387 void CVS::undoLast()
388 {
389         // merge the current with the previous version
390         // in a reverse patch kind of way, so that the
391         // result is to revert the last changes.
392         lyxerr << "Sorry, not implemented." << endl;
393 }
394
395
396 bool CVS::undoLastEnabled()
397 {
398         return false;
399 }
400
401
402 void CVS::getLog(FileName const & tmpf)
403 {
404         doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
405                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
406                     FileName(owner_->filePath()));
407 }
408
409 bool CVS::toggleReadOnlyEnabled()
410 {
411         return false;
412 }
413
414 /////////////////////////////////////////////////////////////////////
415 //
416 // SVN
417 //
418 /////////////////////////////////////////////////////////////////////
419
420 SVN::SVN(FileName const & m, FileName const & f)
421 {
422         owner_ = 0;
423         master_ = m;
424         file_ = f;
425         locked_mode_ = 0;
426         scanMaster();
427 }
428
429
430 FileName const SVN::findFile(FileName const & file)
431 {
432         // First we look for the .svn/entries in the same dir
433         // where we have file.
434         FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
435         string const tmpf = onlyFilename(file.absFilename());
436         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
437                              << "' for `" << tmpf << '\'');
438         if (entries.isReadableFile()) {
439                 // Ok we are at least in a SVN dir. Parse the .svn/entries
440                 // and see if we can find this file. We do a fast and
441                 // dirty parse here.
442                 ifstream ifs(entries.toFilesystemEncoding().c_str());
443                 string line, oldline;
444                 while (getline(ifs, line)) {
445                         if (line == "dir" || line == "file")
446                                 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
447                         if (oldline == tmpf && line == "file")
448                                 return entries;
449                         oldline = line;
450                 }
451         }
452         return FileName();
453 }
454
455
456 void SVN::scanMaster()
457 {
458         locker_.clear();
459         vcstatus = NOLOCKING;
460         if (checkLockMode()) {
461                 if (isLocked()) {
462                         locker_ = "Locked";
463                         vcstatus = LOCKED;
464                 } else {
465                         locker_ = "Unlocked";
466                         vcstatus = LOCKED;
467                 }
468         }
469 }
470
471
472 bool SVN::checkLockMode()
473 {
474         FileName tmpf = FileName::tempName("lyxvcout");
475         if (tmpf.empty()){
476                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
477                 return N_("Error: Could not generate logfile.");
478         }
479
480         LYXERR(Debug::LYXVC, "Detecting locking mode...");
481         if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
482                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
483                     file_.onlyPath()))
484                 return false;
485
486         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
487         string line;
488         bool ret = false;
489
490         while (ifs) {
491                 getline(ifs, line);
492                 LYXERR(Debug::LYXVC, line);
493                 if (contains(line, "svn:needs-lock"))
494                         ret = true;
495         }
496         LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
497         ifs.close();
498         locked_mode_ = ret;
499         return ret;
500
501 }
502
503
504 bool SVN::isLocked() const
505 {
506         //refresh file info
507         FileName file(file_.absFilename());
508         return !file.isReadOnly();
509 }
510
511
512 void SVN::registrer(string const & /*msg*/)
513 {
514         doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
515                     FileName(owner_->filePath()));
516 }
517
518
519 string SVN::checkIn(string const & msg)
520 {
521         FileName tmpf = FileName::tempName("lyxvcout");
522         if (tmpf.empty()){
523                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
524                 return N_("Error: Could not generate logfile.");
525         }
526
527         doVCCommand("svn commit -m \"" + msg + "\" "
528                     + quoteName(onlyFilename(owner_->absFileName()))
529                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
530                     FileName(owner_->filePath()));
531
532         string log;
533         string res = scanLogFile(tmpf, log);
534         if (!res.empty())
535                 frontend::Alert::error(_("Revision control error."),
536                                 _("Error when committing to repository.\n"
537                                 "You have to manually resolve the problem.\n"
538                                 "After pressing OK, LyX will reopen the document."));
539         else
540                 fileLock(false, tmpf, log);
541
542         tmpf.erase();
543         return "SVN: " + log;
544 }
545
546
547 bool SVN::checkInEnabled()
548 {
549         if (locked_mode_)
550                 return isLocked();
551         else
552                 return true;
553 }
554
555
556 // FIXME Correctly return code should be checked instead of this.
557 // This would need another solution than just plain startscript.
558 // Hint from Andre': QProcess::readAllStandardError()...
559 string SVN::scanLogFile(FileName const & f, string & status)
560 {
561         ifstream ifs(f.toFilesystemEncoding().c_str());
562         string line;
563
564         while (ifs) {
565                 getline(ifs, line);
566                 lyxerr << line << "\n";
567                 if (!line.empty()) status += line + "; ";
568                 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
569                         ifs.close();
570                         return line;
571                 }
572         }
573         ifs.close();
574         return string();
575 }
576
577
578 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
579 {
580         if (!locked_mode_ || (isLocked() == lock))
581                 return;
582
583         string arg = lock ? "lock " : "unlock ";
584         doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
585                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
586                     FileName(owner_->filePath()));
587
588         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
589         string line;
590         while (ifs) {
591                 getline(ifs, line);
592                 if (!line.empty()) status += line + "; ";
593         }
594         ifs.close();
595
596         if (!isLocked() && lock)
597                 frontend::Alert::error(_("Revision control error."),
598                         _("Error when acquiring write lock.\n"
599                         "Most probably another user is editing\n"
600                         "the current document now!\n"
601                         "Also check the access to the repository."));
602         if (isLocked() && !lock)
603                 frontend::Alert::error(_("Revision control error."),
604                         _("Error when releasing write lock.\n"
605                         "Check the access to the repository."));
606 }
607
608
609 string SVN::checkOut()
610 {
611         FileName tmpf = FileName::tempName("lyxvcout");
612         if (tmpf.empty()) {
613                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
614                 return N_("Error: Could not generate logfile.");
615         }
616
617         doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
618                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
619                     FileName(owner_->filePath()));
620
621         string log;
622         string res = scanLogFile(tmpf, log);
623         if (!res.empty())
624                 frontend::Alert::error(_("Revision control error."),
625                         bformat(_("Error when updating from repository.\n"
626                                 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
627                                 "After pressing OK, LyX will try to reopen resolved document."),
628                         from_local8bit(res)));
629
630         fileLock(true, tmpf, log);
631
632         tmpf.erase();
633         return "SVN: " + log;
634 }
635
636
637 bool SVN::checkOutEnabled()
638 {
639         if (locked_mode_)
640                 return !isLocked();
641         else
642                 return true;
643 }
644
645
646 void SVN::revert()
647 {
648         // Reverts to the version in CVS repository and
649         // gets the updated version from the repository.
650         string const fil = quoteName(onlyFilename(owner_->absFileName()));
651
652         doVCCommand("svn revert -q " + fil,
653                     FileName(owner_->filePath()));
654         owner_->markClean();
655 }
656
657
658 void SVN::undoLast()
659 {
660         // merge the current with the previous version
661         // in a reverse patch kind of way, so that the
662         // result is to revert the last changes.
663         lyxerr << "Sorry, not implemented." << endl;
664 }
665
666
667 bool SVN::undoLastEnabled()
668 {
669         return false;
670 }
671
672
673 void SVN::getLog(FileName const & tmpf)
674 {
675         doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
676                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
677                     FileName(owner_->filePath()));
678 }
679
680
681 bool SVN::toggleReadOnlyEnabled()
682 {
683         return false;
684 }
685
686
687 } // namespace lyx