]> git.lyx.org Git - lyx.git/blob - src/VCBackend.cpp
421a748451923bb63b707f4c6f12601e9c08e414
[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 string RCS::lockingToggle()
208 {
209         lyxerr << "Sorry, not implemented." << endl;
210         return string();
211 }
212
213
214 bool RCS::lockingToggleEnabled()
215 {
216         return false;
217 }
218
219
220 void RCS::revert()
221 {
222         doVCCommand("co -f -u" + version() + " "
223                     + quoteName(onlyFilename(owner_->absFileName())),
224                     FileName(owner_->filePath()));
225         // We ignore changes and just reload!
226         owner_->markClean();
227 }
228
229
230 void RCS::undoLast()
231 {
232         LYXERR(Debug::LYXVC, "LyXVC: undoLast");
233         doVCCommand("rcs -o" + version() + " "
234                     + quoteName(onlyFilename(owner_->absFileName())),
235                     FileName(owner_->filePath()));
236 }
237
238
239 bool RCS::undoLastEnabled()
240 {
241         return true;
242 }
243
244
245 void RCS::getLog(FileName const & tmpf)
246 {
247         doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
248                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
249                     FileName(owner_->filePath()));
250 }
251
252
253 bool RCS::toggleReadOnlyEnabled()
254 {
255         return true;
256 }
257
258
259 /////////////////////////////////////////////////////////////////////
260 //
261 // CVS
262 //
263 /////////////////////////////////////////////////////////////////////
264
265 CVS::CVS(FileName const & m, FileName const & f)
266 {
267         master_ = m;
268         file_ = f;
269         scanMaster();
270 }
271
272
273 FileName const CVS::findFile(FileName const & file)
274 {
275         // First we look for the CVS/Entries in the same dir
276         // where we have file.
277         FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
278         string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
279         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
280                              << "' for `" << tmpf << '\'');
281         if (entries.isReadableFile()) {
282                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
283                 // and see if we can find this file. We do a fast and
284                 // dirty parse here.
285                 ifstream ifs(entries.toFilesystemEncoding().c_str());
286                 string line;
287                 while (getline(ifs, line)) {
288                         LYXERR(Debug::LYXVC, "\tEntries: " << line);
289                         if (contains(line, tmpf))
290                                 return entries;
291                 }
292         }
293         return FileName();
294 }
295
296
297 void CVS::scanMaster()
298 {
299         LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n     Checking: " << master_);
300         // Ok now we do the real scan...
301         ifstream ifs(master_.toFilesystemEncoding().c_str());
302         string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
303         LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
304         string line;
305         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
306         while (getline(ifs, line)) {
307                 LYXERR(Debug::LYXVC, "\t  line: " << line);
308                 if (contains(line, tmpf)) {
309                         // Ok extract the fields.
310                         smatch sm;
311
312                         regex_match(line, sm, reg);
313
314                         //sm[0]; // whole matched string
315                         //sm[1]; // filename
316                         version_ = sm.str(2);
317                         string const file_date = sm.str(3);
318
319                         //sm[4]; // options
320                         //sm[5]; // tag or tagdate
321                         // FIXME: must double check file is stattable/existing
322                         time_t mod = file_.lastModified();
323                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
324                         LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
325                                 << "'\nModification date of file: `" << mod_date << '\'');
326                         //FIXME this whole locking bussiness is not working under cvs and the machinery
327                         // conforms to the ci usage, not cvs.
328                         if (file_date == mod_date) {
329                                 locker_ = "Unlocked";
330                                 vcstatus = UNLOCKED;
331                         } else {
332                                 // Here we should also to some more checking
333                                 // to see if there are conflicts or not.
334                                 locker_ = "Locked";
335                                 vcstatus = LOCKED;
336                         }
337                         break;
338                 }
339         }
340 }
341
342
343 void CVS::registrer(string const & msg)
344 {
345         doVCCommand("cvs -q add -m \"" + msg + "\" "
346                     + quoteName(onlyFilename(owner_->absFileName())),
347                     FileName(owner_->filePath()));
348 }
349
350
351 string CVS::checkIn(string const & msg)
352 {
353         int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
354                     + quoteName(onlyFilename(owner_->absFileName())),
355                     FileName(owner_->filePath()));
356         return ret ? string() : "CVS: Proceeded";
357 }
358
359
360 bool CVS::checkInEnabled()
361 {
362         return true;
363 }
364
365
366 string CVS::checkOut()
367 {
368         // cvs update or perhaps for cvs this should be a noop
369         // we need to detect conflict (eg "C" in output)
370         // before we can do this.
371         lyxerr << "Sorry, not implemented." << endl;
372         return string();
373 }
374
375
376 bool CVS::checkOutEnabled()
377 {
378         return false;
379 }
380
381
382 string CVS::lockingToggle()
383 {
384         lyxerr << "Sorry, not implemented." << endl;
385         return string();
386 }
387
388
389 bool CVS::lockingToggleEnabled()
390 {
391         return false;
392 }
393
394
395 void CVS::revert()
396 {
397         // Reverts to the version in CVS repository and
398         // gets the updated version from the repository.
399         string const fil = quoteName(onlyFilename(owner_->absFileName()));
400         // This is sensitive operation, so at lest some check about
401         // existence of cvs program and its file
402         if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
403                 return;
404         FileName f(owner_->absFileName());
405         f.removeFile();
406         doVCCommand("cvs update " + fil,
407                     FileName(owner_->filePath()));
408         owner_->markClean();
409 }
410
411
412 void CVS::undoLast()
413 {
414         // merge the current with the previous version
415         // in a reverse patch kind of way, so that the
416         // result is to revert the last changes.
417         lyxerr << "Sorry, not implemented." << endl;
418 }
419
420
421 bool CVS::undoLastEnabled()
422 {
423         return false;
424 }
425
426
427 void CVS::getLog(FileName const & tmpf)
428 {
429         doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
430                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
431                     FileName(owner_->filePath()));
432 }
433
434 bool CVS::toggleReadOnlyEnabled()
435 {
436         return false;
437 }
438
439 /////////////////////////////////////////////////////////////////////
440 //
441 // SVN
442 //
443 /////////////////////////////////////////////////////////////////////
444
445 SVN::SVN(FileName const & m, FileName const & f)
446 {
447         owner_ = 0;
448         master_ = m;
449         file_ = f;
450         locked_mode_ = 0;
451         scanMaster();
452 }
453
454
455 FileName const SVN::findFile(FileName const & file)
456 {
457         // First we look for the .svn/entries in the same dir
458         // where we have file.
459         FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
460         string const tmpf = onlyFilename(file.absFilename());
461         LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
462                              << "' for `" << tmpf << '\'');
463         if (entries.isReadableFile()) {
464                 // Ok we are at least in a SVN dir. Parse the .svn/entries
465                 // and see if we can find this file. We do a fast and
466                 // dirty parse here.
467                 ifstream ifs(entries.toFilesystemEncoding().c_str());
468                 string line, oldline;
469                 while (getline(ifs, line)) {
470                         if (line == "dir" || line == "file")
471                                 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
472                         if (oldline == tmpf && line == "file")
473                                 return entries;
474                         oldline = line;
475                 }
476         }
477         return FileName();
478 }
479
480
481 void SVN::scanMaster()
482 {
483         locker_.clear();
484         vcstatus = NOLOCKING;
485         if (checkLockMode()) {
486                 if (isLocked()) {
487                         locker_ = "Locked";
488                         vcstatus = LOCKED;
489                 } else {
490                         locker_ = "Unlocked";
491                         vcstatus = LOCKED;
492                 }
493         }
494 }
495
496
497 bool SVN::checkLockMode()
498 {
499         FileName tmpf = FileName::tempName("lyxvcout");
500         if (tmpf.empty()){
501                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
502                 return N_("Error: Could not generate logfile.");
503         }
504
505         LYXERR(Debug::LYXVC, "Detecting locking mode...");
506         if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
507                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
508                     file_.onlyPath()))
509                 return false;
510
511         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
512         string line;
513         bool ret = false;
514
515         while (ifs) {
516                 getline(ifs, line);
517                 LYXERR(Debug::LYXVC, line);
518                 if (contains(line, "svn:needs-lock"))
519                         ret = true;
520         }
521         LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
522         ifs.close();
523         locked_mode_ = ret;
524         return ret;
525
526 }
527
528
529 bool SVN::isLocked() const
530 {
531         //refresh file info
532         FileName file(file_.absFilename());
533         return !file.isReadOnly();
534 }
535
536
537 void SVN::registrer(string const & /*msg*/)
538 {
539         doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
540                     FileName(owner_->filePath()));
541 }
542
543
544 string SVN::checkIn(string const & msg)
545 {
546         FileName tmpf = FileName::tempName("lyxvcout");
547         if (tmpf.empty()){
548                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
549                 return N_("Error: Could not generate logfile.");
550         }
551
552         doVCCommand("svn commit -m \"" + msg + "\" "
553                     + quoteName(onlyFilename(owner_->absFileName()))
554                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
555                     FileName(owner_->filePath()));
556
557         string log;
558         string res = scanLogFile(tmpf, log);
559         if (!res.empty())
560                 frontend::Alert::error(_("Revision control error."),
561                                 _("Error when committing to repository.\n"
562                                 "You have to manually resolve the problem.\n"
563                                 "After pressing OK, LyX will reopen the document."));
564         else
565                 fileLock(false, tmpf, log);
566
567         tmpf.erase();
568         return "SVN: " + log;
569 }
570
571
572 bool SVN::checkInEnabled()
573 {
574         if (locked_mode_)
575                 return isLocked();
576         else
577                 return true;
578 }
579
580
581 // FIXME Correctly return code should be checked instead of this.
582 // This would need another solution than just plain startscript.
583 // Hint from Andre': QProcess::readAllStandardError()...
584 string SVN::scanLogFile(FileName const & f, string & status)
585 {
586         ifstream ifs(f.toFilesystemEncoding().c_str());
587         string line;
588
589         while (ifs) {
590                 getline(ifs, line);
591                 lyxerr << line << "\n";
592                 if (!line.empty()) status += line + "; ";
593                 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
594                         ifs.close();
595                         return line;
596                 }
597                 if (contains(line, "svn:needs-lock")) {
598                         ifs.close();
599                         return line;
600                 }
601         }
602         ifs.close();
603         return string();
604 }
605
606
607 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
608 {
609         if (!locked_mode_ || (isLocked() == lock))
610                 return;
611
612         string arg = lock ? "lock " : "unlock ";
613         doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
614                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
615                     FileName(owner_->filePath()));
616
617         ifstream ifs(tmpf.toFilesystemEncoding().c_str());
618         string line;
619         while (ifs) {
620                 getline(ifs, line);
621                 if (!line.empty()) status += line + "; ";
622         }
623         ifs.close();
624
625         if (!isLocked() && lock)
626                 frontend::Alert::error(_("Revision control error."),
627                         _("Error when acquiring write lock.\n"
628                         "Most probably another user is editing\n"
629                         "the current document now!\n"
630                         "Also check the access to the repository."));
631         if (isLocked() && !lock)
632                 frontend::Alert::error(_("Revision control error."),
633                         _("Error when releasing write lock.\n"
634                         "Check the access to the repository."));
635 }
636
637
638 string SVN::checkOut()
639 {
640         FileName tmpf = FileName::tempName("lyxvcout");
641         if (tmpf.empty()) {
642                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
643                 return N_("Error: Could not generate logfile.");
644         }
645
646         doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
647                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
648                     FileName(owner_->filePath()));
649
650         string log;
651         string res = scanLogFile(tmpf, log);
652         if (!res.empty())
653                 frontend::Alert::error(_("Revision control error."),
654                         bformat(_("Error when updating from repository.\n"
655                                 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
656                                 "After pressing OK, LyX will try to reopen resolved document."),
657                         from_local8bit(res)));
658
659         fileLock(true, tmpf, log);
660
661         tmpf.erase();
662         return "SVN: " + log;
663 }
664
665
666 bool SVN::checkOutEnabled()
667 {
668         if (locked_mode_)
669                 return !isLocked();
670         else
671                 return true;
672 }
673
674
675 string SVN::lockingToggle()
676 {
677         FileName tmpf = FileName::tempName("lyxvcout");
678         if (tmpf.empty()) {
679                 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
680                 return N_("Error: Could not generate logfile.");
681         }
682
683         int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
684                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
685                     FileName(owner_->filePath()));
686         if (ret)
687                 return string();
688
689         string log;
690         string res = scanLogFile(tmpf, log);
691         bool locking = contains(res, "svn:needs-lock");
692         if (!locking)
693                 ret = doVCCommand("svn propset svn:needs-lock ON "
694                     + quoteName(onlyFilename(owner_->absFileName()))
695                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
696                     FileName(owner_->filePath()));
697         else
698                 ret = doVCCommand("svn propdel svn:needs-lock "
699                     + quoteName(onlyFilename(owner_->absFileName()))
700                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
701                     FileName(owner_->filePath()));
702         if (ret)
703                 return string();
704
705         tmpf.erase();
706         frontend::Alert::warning(_("VCN File Locking"),
707                 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
708                 + _("Do not forget to commit the locking property into the repository."),
709                 true);
710
711         return string("SVN: ") +  N_("Locking property set.");
712 }
713
714
715 bool SVN::lockingToggleEnabled()
716 {
717         return true;
718 }
719
720 void SVN::revert()
721 {
722         // Reverts to the version in CVS repository and
723         // gets the updated version from the repository.
724         string const fil = quoteName(onlyFilename(owner_->absFileName()));
725
726         doVCCommand("svn revert -q " + fil,
727                     FileName(owner_->filePath()));
728         owner_->markClean();
729 }
730
731
732 void SVN::undoLast()
733 {
734         // merge the current with the previous version
735         // in a reverse patch kind of way, so that the
736         // result is to revert the last changes.
737         lyxerr << "Sorry, not implemented." << endl;
738 }
739
740
741 bool SVN::undoLastEnabled()
742 {
743         return false;
744 }
745
746
747 void SVN::getLog(FileName const & tmpf)
748 {
749         doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
750                     + " > " + quoteName(tmpf.toFilesystemEncoding()),
751                     FileName(owner_->filePath()));
752 }
753
754
755 bool SVN::toggleReadOnlyEnabled()
756 {
757         return false;
758 }
759
760
761 } // namespace lyx