* de/Tutorial.lyx: minor corrections reported on lyx-docs.
[lyx.git] / src / VCBackend.cpp
blob421a748451923bb63b707f4c6f12601e9c08e414
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.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "VCBackend.h"
14 #include "Buffer.h"
16 #include "frontends/alert.h"
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"
25 #include <boost/regex.hpp>
27 #include <fstream>
29 using namespace std;
30 using namespace lyx::support;
32 using boost::regex;
33 using boost::regex_match;
34 using boost::smatch;
36 namespace lyx {
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);
46 int VCS::doVCCommand(string const & cmd, FileName const & path)
48 if (owner_)
49 owner_->setBusy(true);
51 int const ret = doVCCommandCall(cmd, path);
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;
64 /////////////////////////////////////////////////////////////////////
66 // RCS
68 /////////////////////////////////////////////////////////////////////
70 RCS::RCS(FileName const & m)
72 master_ = m;
73 scanMaster();
77 FileName const RCS::findFile(FileName const & file)
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;
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;
95 return FileName();
99 void RCS::retrieve(FileName const & file)
101 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
102 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
103 FileName());
107 void RCS::scanMaster()
109 if (master_.empty())
110 return;
112 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
114 ifstream ifs(master_.toFilesystemEncoding().c_str());
116 string token;
117 bool read_enough = false;
119 while (!read_enough && ifs >> token) {
120 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
121 << token << '\'');
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;
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;
157 } while (!contains(tmpt, ';'));
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");
170 void RCS::registrer(string const & msg)
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()));
180 string RCS::checkIn(string const & msg)
182 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
183 + quoteName(onlyFilename(owner_->absFileName())),
184 FileName(owner_->filePath()));
185 return ret ? string() : "RCS: Proceeded";
188 bool RCS::checkInEnabled()
190 return owner_ && !owner_->isReadonly();
193 string RCS::checkOut()
195 owner_->markClean();
196 int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
197 FileName(owner_->filePath()));
198 return ret ? string() : "RCS: Proceeded";
202 bool RCS::checkOutEnabled()
204 return owner_ && owner_->isReadonly();
207 string RCS::lockingToggle()
209 lyxerr << "Sorry, not implemented." << endl;
210 return string();
214 bool RCS::lockingToggleEnabled()
216 return false;
220 void RCS::revert()
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();
230 void RCS::undoLast()
232 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
233 doVCCommand("rcs -o" + version() + " "
234 + quoteName(onlyFilename(owner_->absFileName())),
235 FileName(owner_->filePath()));
239 bool RCS::undoLastEnabled()
241 return true;
245 void RCS::getLog(FileName const & tmpf)
247 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
248 + " > " + quoteName(tmpf.toFilesystemEncoding()),
249 FileName(owner_->filePath()));
253 bool RCS::toggleReadOnlyEnabled()
255 return true;
259 /////////////////////////////////////////////////////////////////////
261 // CVS
263 /////////////////////////////////////////////////////////////////////
265 CVS::CVS(FileName const & m, FileName const & f)
267 master_ = m;
268 file_ = f;
269 scanMaster();
273 FileName const CVS::findFile(FileName const & file)
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;
293 return FileName();
297 void CVS::scanMaster()
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;
312 regex_match(line, sm, reg);
314 //sm[0]; // whole matched string
315 //sm[1]; // filename
316 version_ = sm.str(2);
317 string const file_date = sm.str(3);
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;
337 break;
343 void CVS::registrer(string const & msg)
345 doVCCommand("cvs -q add -m \"" + msg + "\" "
346 + quoteName(onlyFilename(owner_->absFileName())),
347 FileName(owner_->filePath()));
351 string CVS::checkIn(string const & msg)
353 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
354 + quoteName(onlyFilename(owner_->absFileName())),
355 FileName(owner_->filePath()));
356 return ret ? string() : "CVS: Proceeded";
360 bool CVS::checkInEnabled()
362 return true;
366 string CVS::checkOut()
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();
376 bool CVS::checkOutEnabled()
378 return false;
382 string CVS::lockingToggle()
384 lyxerr << "Sorry, not implemented." << endl;
385 return string();
389 bool CVS::lockingToggleEnabled()
391 return false;
395 void CVS::revert()
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();
412 void CVS::undoLast()
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;
421 bool CVS::undoLastEnabled()
423 return false;
427 void CVS::getLog(FileName const & tmpf)
429 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
430 + " > " + quoteName(tmpf.toFilesystemEncoding()),
431 FileName(owner_->filePath()));
434 bool CVS::toggleReadOnlyEnabled()
436 return false;
439 /////////////////////////////////////////////////////////////////////
441 // SVN
443 /////////////////////////////////////////////////////////////////////
445 SVN::SVN(FileName const & m, FileName const & f)
447 owner_ = 0;
448 master_ = m;
449 file_ = f;
450 locked_mode_ = 0;
451 scanMaster();
455 FileName const SVN::findFile(FileName const & file)
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;
477 return FileName();
481 void SVN::scanMaster()
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;
497 bool SVN::checkLockMode()
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.");
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;
511 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
512 string line;
513 bool ret = false;
515 while (ifs) {
516 getline(ifs, line);
517 LYXERR(Debug::LYXVC, line);
518 if (contains(line, "svn:needs-lock"))
519 ret = true;
521 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
522 ifs.close();
523 locked_mode_ = ret;
524 return ret;
529 bool SVN::isLocked() const
531 //refresh file info
532 FileName file(file_.absFilename());
533 return !file.isReadOnly();
537 void SVN::registrer(string const & /*msg*/)
539 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
540 FileName(owner_->filePath()));
544 string SVN::checkIn(string const & msg)
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.");
552 doVCCommand("svn commit -m \"" + msg + "\" "
553 + quoteName(onlyFilename(owner_->absFileName()))
554 + " > " + quoteName(tmpf.toFilesystemEncoding()),
555 FileName(owner_->filePath()));
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);
567 tmpf.erase();
568 return "SVN: " + log;
572 bool SVN::checkInEnabled()
574 if (locked_mode_)
575 return isLocked();
576 else
577 return true;
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)
586 ifstream ifs(f.toFilesystemEncoding().c_str());
587 string line;
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;
597 if (contains(line, "svn:needs-lock")) {
598 ifs.close();
599 return line;
602 ifs.close();
603 return string();
607 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
609 if (!locked_mode_ || (isLocked() == lock))
610 return;
612 string arg = lock ? "lock " : "unlock ";
613 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
614 + " > " + quoteName(tmpf.toFilesystemEncoding()),
615 FileName(owner_->filePath()));
617 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
618 string line;
619 while (ifs) {
620 getline(ifs, line);
621 if (!line.empty()) status += line + "; ";
623 ifs.close();
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."));
638 string SVN::checkOut()
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.");
646 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
647 + " > " + quoteName(tmpf.toFilesystemEncoding()),
648 FileName(owner_->filePath()));
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)));
659 fileLock(true, tmpf, log);
661 tmpf.erase();
662 return "SVN: " + log;
666 bool SVN::checkOutEnabled()
668 if (locked_mode_)
669 return !isLocked();
670 else
671 return true;
675 string SVN::lockingToggle()
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.");
683 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
684 + " > " + quoteName(tmpf.toFilesystemEncoding()),
685 FileName(owner_->filePath()));
686 if (ret)
687 return string();
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();
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);
711 return string("SVN: ") + N_("Locking property set.");
715 bool SVN::lockingToggleEnabled()
717 return true;
720 void SVN::revert()
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()));
726 doVCCommand("svn revert -q " + fil,
727 FileName(owner_->filePath()));
728 owner_->markClean();
732 void SVN::undoLast()
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;
741 bool SVN::undoLastEnabled()
743 return false;
747 void SVN::getLog(FileName const & tmpf)
749 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
750 + " > " + quoteName(tmpf.toFilesystemEncoding()),
751 FileName(owner_->filePath()));
755 bool SVN::toggleReadOnlyEnabled()
757 return false;
761 } // namespace lyx