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.
13 #include "VCBackend.h"
16 #include "FuncRequest.h"
18 #include "frontends/alert.h"
19 #include "frontends/Application.h"
21 #include "support/debug.h"
22 #include "support/filetools.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25 #include "support/Path.h"
26 #include "support/Systemcall.h"
28 #include <boost/regex.hpp>
33 using namespace lyx::support;
36 using boost::regex_match;
42 int VCS::doVCCommandCall(string const & cmd, FileName const & path)
44 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
46 support::PathChanger p(path);
47 return one.startscript(Systemcall::Wait, cmd, false);
51 int VCS::doVCCommand(string const & cmd, FileName const & path)
54 owner_->setBusy(true);
56 int const ret = doVCCommandCall(cmd, path);
59 owner_->setBusy(false);
61 frontend::Alert::error(_("Revision control error."),
62 bformat(_("Some problem occured while running the command:\n"
69 /////////////////////////////////////////////////////////////////////
73 /////////////////////////////////////////////////////////////////////
75 RCS::RCS(FileName const & m)
82 FileName const RCS::findFile(FileName const & file)
84 // Check if *,v exists.
85 FileName tmp(file.absFilename() + ",v");
86 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
87 if (tmp.isReadableFile()) {
88 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
92 // Check if RCS/*,v exists.
93 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
94 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
95 if (tmp.isReadableFile()) {
96 LYXERR(Debug::LYXVC, "Yes, " << file << " is under rcs.");
104 void RCS::retrieve(FileName const & file)
106 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
107 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
112 void RCS::scanMaster()
117 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster: " << master_);
119 ifstream ifs(master_.toFilesystemEncoding().c_str());
122 bool read_enough = false;
124 while (!read_enough && ifs >> token) {
125 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
130 else if (token == "head") {
134 tmv = rtrim(tmv, ";");
136 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
137 } else if (contains(token, "access")
138 || contains(token, "symbols")
139 || contains(token, "strict")) {
141 } else if (contains(token, "locks")) {
143 if (contains(token, ';')) {
144 locker_ = "Unlocked";
153 s1 = rtrim(tmpt, ";");
154 // tmp is now in the format <user>:<version>
155 s1 = split(s1, s2, ':');
156 // s2 is user, and s1 is version
157 if (s1 == version_) {
162 } while (!contains(tmpt, ';'));
164 } else if (token == "comment") {
165 // we don't need to read any further than this.
169 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
175 void RCS::registrer(string const & msg)
177 string cmd = "ci -q -u -i -t-\"";
180 cmd += quoteName(onlyFilename(owner_->absFileName()));
181 doVCCommand(cmd, FileName(owner_->filePath()));
185 string RCS::checkIn(string const & msg)
187 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
188 + quoteName(onlyFilename(owner_->absFileName())),
189 FileName(owner_->filePath()));
190 return ret ? string() : "RCS: Proceeded";
194 bool RCS::checkInEnabled()
196 return owner_ && !owner_->isReadonly();
200 string RCS::checkOut()
203 int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
204 FileName(owner_->filePath()));
205 return ret ? string() : "RCS: Proceeded";
209 bool RCS::checkOutEnabled()
211 return owner_ && owner_->isReadonly();
215 string RCS::repoUpdate()
217 lyxerr << "Sorry, not implemented." << endl;
222 bool RCS::repoUpdateEnabled()
228 string RCS::lockingToggle()
230 lyxerr << "Sorry, not implemented." << endl;
235 bool RCS::lockingToggleEnabled()
243 doVCCommand("co -f -u" + version() + " "
244 + quoteName(onlyFilename(owner_->absFileName())),
245 FileName(owner_->filePath()));
246 // We ignore changes and just reload!
253 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
254 doVCCommand("rcs -o" + version() + " "
255 + quoteName(onlyFilename(owner_->absFileName())),
256 FileName(owner_->filePath()));
260 bool RCS::undoLastEnabled()
266 void RCS::getLog(FileName const & tmpf)
268 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
269 + " > " + quoteName(tmpf.toFilesystemEncoding()),
270 FileName(owner_->filePath()));
274 bool RCS::toggleReadOnlyEnabled()
276 // This got broken somewhere along lfuns dispatch reorganization.
277 // reloadBuffer would be needed after this, but thats problematic
278 // since we are inside Buffer::dispatch.
284 /////////////////////////////////////////////////////////////////////
288 /////////////////////////////////////////////////////////////////////
290 CVS::CVS(FileName const & m, FileName const & f)
298 FileName const CVS::findFile(FileName const & file)
300 // First we look for the CVS/Entries in the same dir
301 // where we have file.
302 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
303 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
304 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
305 << "' for `" << tmpf << '\'');
306 if (entries.isReadableFile()) {
307 // Ok we are at least in a CVS dir. Parse the CVS/Entries
308 // and see if we can find this file. We do a fast and
310 ifstream ifs(entries.toFilesystemEncoding().c_str());
312 while (getline(ifs, line)) {
313 LYXERR(Debug::LYXVC, "\tEntries: " << line);
314 if (contains(line, tmpf))
322 void CVS::scanMaster()
324 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
325 // Ok now we do the real scan...
326 ifstream ifs(master_.toFilesystemEncoding().c_str());
327 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
328 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
330 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
331 while (getline(ifs, line)) {
332 LYXERR(Debug::LYXVC, "\t line: " << line);
333 if (contains(line, tmpf)) {
334 // Ok extract the fields.
337 regex_match(line, sm, reg);
339 //sm[0]; // whole matched string
341 version_ = sm.str(2);
342 string const file_date = sm.str(3);
345 //sm[5]; // tag or tagdate
346 // FIXME: must double check file is stattable/existing
347 time_t mod = file_.lastModified();
348 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
349 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
350 << "'\nModification date of file: `" << mod_date << '\'');
351 //FIXME this whole locking bussiness is not working under cvs and the machinery
352 // conforms to the ci usage, not cvs.
353 if (file_date == mod_date) {
354 locker_ = "Unlocked";
357 // Here we should also to some more checking
358 // to see if there are conflicts or not.
368 void CVS::registrer(string const & msg)
370 doVCCommand("cvs -q add -m \"" + msg + "\" "
371 + quoteName(onlyFilename(owner_->absFileName())),
372 FileName(owner_->filePath()));
376 string CVS::checkIn(string const & msg)
378 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
379 + quoteName(onlyFilename(owner_->absFileName())),
380 FileName(owner_->filePath()));
381 return ret ? string() : "CVS: Proceeded";
385 bool CVS::checkInEnabled()
391 string CVS::checkOut()
393 // cvs update or perhaps for cvs this should be a noop
394 // we need to detect conflict (eg "C" in output)
395 // before we can do this.
396 lyxerr << "Sorry, not implemented." << endl;
401 bool CVS::checkOutEnabled()
407 string CVS::repoUpdate()
409 lyxerr << "Sorry, not implemented." << endl;
414 bool CVS::repoUpdateEnabled()
420 string CVS::lockingToggle()
422 lyxerr << "Sorry, not implemented." << endl;
427 bool CVS::lockingToggleEnabled()
435 // Reverts to the version in CVS repository and
436 // gets the updated version from the repository.
437 string const fil = quoteName(onlyFilename(owner_->absFileName()));
438 // This is sensitive operation, so at lest some check about
439 // existence of cvs program and its file
440 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
442 FileName f(owner_->absFileName());
444 doVCCommand("cvs update " + fil,
445 FileName(owner_->filePath()));
452 // merge the current with the previous version
453 // in a reverse patch kind of way, so that the
454 // result is to revert the last changes.
455 lyxerr << "Sorry, not implemented." << endl;
459 bool CVS::undoLastEnabled()
465 void CVS::getLog(FileName const & tmpf)
467 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
468 + " > " + quoteName(tmpf.toFilesystemEncoding()),
469 FileName(owner_->filePath()));
473 bool CVS::toggleReadOnlyEnabled()
478 /////////////////////////////////////////////////////////////////////
482 /////////////////////////////////////////////////////////////////////
484 SVN::SVN(FileName const & m, FileName const & f)
494 FileName const SVN::findFile(FileName const & file)
496 // First we look for the .svn/entries in the same dir
497 // where we have file.
498 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
499 string const tmpf = onlyFilename(file.absFilename());
500 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
501 << "' for `" << tmpf << '\'');
502 if (entries.isReadableFile()) {
503 // Ok we are at least in a SVN dir. Parse the .svn/entries
504 // and see if we can find this file. We do a fast and
506 ifstream ifs(entries.toFilesystemEncoding().c_str());
507 string line, oldline;
508 while (getline(ifs, line)) {
509 if (line == "dir" || line == "file")
510 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
511 if (oldline == tmpf && line == "file")
520 void SVN::scanMaster()
523 // vcstatus code is somewhat superflous, until we want
524 // to implement read-only toggle for svn.
525 vcstatus = NOLOCKING;
526 if (checkLockMode()) {
531 locker_ = "Unlocked";
538 bool SVN::checkLockMode()
540 FileName tmpf = FileName::tempName("lyxvcout");
542 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
543 return N_("Error: Could not generate logfile.");
546 LYXERR(Debug::LYXVC, "Detecting locking mode...");
547 if (doVCCommandCall("svn proplist " + quoteName(file_.onlyFileName())
548 + " > " + quoteName(tmpf.toFilesystemEncoding()),
552 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
558 LYXERR(Debug::LYXVC, line);
559 if (contains(line, "svn:needs-lock"))
562 LYXERR(Debug::LYXVC, "Locking enabled: " << ret);
570 bool SVN::isLocked() const
573 return !file_.isReadOnly();
577 void SVN::registrer(string const & /*msg*/)
579 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
580 FileName(owner_->filePath()));
584 string SVN::checkIn(string const & msg)
586 FileName tmpf = FileName::tempName("lyxvcout");
588 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
589 return N_("Error: Could not generate logfile.");
592 doVCCommand("svn commit -m \"" + msg + "\" "
593 + quoteName(onlyFilename(owner_->absFileName()))
594 + " > " + quoteName(tmpf.toFilesystemEncoding()),
595 FileName(owner_->filePath()));
598 string res = scanLogFile(tmpf, log);
600 frontend::Alert::error(_("Revision control error."),
601 _("Error when committing to repository.\n"
602 "You have to manually resolve the problem.\n"
603 "After pressing OK, LyX will reopen the document."));
605 fileLock(false, tmpf, log);
608 return log.empty() ? string() : "SVN: " + log;
612 bool SVN::checkInEnabled()
621 // FIXME Correctly return code should be checked instead of this.
622 // This would need another solution than just plain startscript.
623 // Hint from Andre': QProcess::readAllStandardError()...
624 string SVN::scanLogFile(FileName const & f, string & status)
626 ifstream ifs(f.toFilesystemEncoding().c_str());
631 lyxerr << line << "\n";
632 if (!line.empty()) status += line + "; ";
633 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
637 if (contains(line, "svn:needs-lock")) {
647 void SVN::fileLock(bool lock, FileName const & tmpf, string &status)
649 if (!locked_mode_ || (isLocked() == lock))
652 string const arg = lock ? "lock " : "unlock ";
653 doVCCommand("svn "+ arg + quoteName(onlyFilename(owner_->absFileName()))
654 + " > " + quoteName(tmpf.toFilesystemEncoding()),
655 FileName(owner_->filePath()));
657 // Lock error messages go unfortunately on stderr and are unreachible this way.
658 ifstream ifs(tmpf.toFilesystemEncoding().c_str());
662 if (!line.empty()) status += line + "; ";
666 if (!isLocked() && lock)
667 frontend::Alert::error(_("Revision control error."),
668 _("Error when acquiring write lock.\n"
669 "Most probably another user is editing\n"
670 "the current document now!\n"
671 "Also check the access to the repository."));
672 if (isLocked() && !lock)
673 frontend::Alert::error(_("Revision control error."),
674 _("Error when releasing write lock.\n"
675 "Check the access to the repository."));
679 string SVN::checkOut()
681 FileName tmpf = FileName::tempName("lyxvcout");
683 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
684 return N_("Error: Could not generate logfile.");
687 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
688 + " > " + quoteName(tmpf.toFilesystemEncoding()),
689 FileName(owner_->filePath()));
692 string res = scanLogFile(tmpf, log);
694 frontend::Alert::error(_("Revision control error."),
695 bformat(_("Error when updating from repository.\n"
696 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
697 "After pressing OK, LyX will try to reopen the resolved document."),
698 from_local8bit(res)));
700 fileLock(true, tmpf, log);
703 return log.empty() ? string() : "SVN: " + log;
707 bool SVN::checkOutEnabled()
716 string SVN::repoUpdate()
718 FileName tmpf = FileName::tempName("lyxvcout");
720 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
721 return N_("Error: Could not generate logfile.");
724 doVCCommand("svn diff " + quoteName(owner_->filePath())
725 + " > " + quoteName(tmpf.toFilesystemEncoding()),
726 FileName(owner_->filePath()));
727 docstring res = tmpf.fileContents("UTF-8");
729 LYXERR(Debug::LYXVC, "Diff detected:\n" << res);
730 docstring const file = from_utf8(owner_->filePath());
731 docstring text = bformat(_("There were detected changes "
732 "in the working directory:\n%1$s\n\n"
733 "In case of file conflict version of the local directory files "
735 "\n\nContinue?"), file);
736 int ret = frontend::Alert::prompt(_("Changes detected"),
737 text, 0, 1, _("&Yes"), _("&No"), _("View &Log ..."));
739 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + tmpf.absFilename()));
740 ret = frontend::Alert::prompt(_("Changes detected"),
741 text, 0, 1, _("&Yes"), _("&No"));
742 hideDialogs("file", 0);
750 // Reverting looks too harsh, see bug #6255.
751 // doVCCommand("svn revert -R " + quoteName(owner_->filePath())
752 // + " > " + quoteName(tmpf.toFilesystemEncoding()),
753 // FileName(owner_->filePath()));
754 // res = "Revert log:\n" + tmpf.fileContents("UTF-8");
755 doVCCommand("svn update --accept mine-full " + quoteName(owner_->filePath())
756 + " > " + quoteName(tmpf.toFilesystemEncoding()),
757 FileName(owner_->filePath()));
758 res += "Update log:\n" + tmpf.fileContents("UTF-8");
760 LYXERR(Debug::LYXVC, res);
766 bool SVN::repoUpdateEnabled()
772 string SVN::lockingToggle()
774 FileName tmpf = FileName::tempName("lyxvcout");
776 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
777 return N_("Error: Could not generate logfile.");
780 int ret = doVCCommand("svn proplist " + quoteName(onlyFilename(owner_->absFileName()))
781 + " > " + quoteName(tmpf.toFilesystemEncoding()),
782 FileName(owner_->filePath()));
787 string res = scanLogFile(tmpf, log);
788 bool locking = contains(res, "svn:needs-lock");
790 ret = doVCCommand("svn propset svn:needs-lock ON "
791 + quoteName(onlyFilename(owner_->absFileName()))
792 + " > " + quoteName(tmpf.toFilesystemEncoding()),
793 FileName(owner_->filePath()));
795 ret = doVCCommand("svn propdel svn:needs-lock "
796 + quoteName(onlyFilename(owner_->absFileName()))
797 + " > " + quoteName(tmpf.toFilesystemEncoding()),
798 FileName(owner_->filePath()));
803 frontend::Alert::warning(_("VCN File Locking"),
804 (locking ? _("Locking property unset.") : _("Locking property set.")) + "\n"
805 + _("Do not forget to commit the locking property into the repository."),
808 return string("SVN: ") + N_("Locking property set.");
812 bool SVN::lockingToggleEnabled()
820 // Reverts to the version in CVS repository and
821 // gets the updated version from the repository.
822 string const fil = quoteName(onlyFilename(owner_->absFileName()));
824 doVCCommand("svn revert -q " + fil,
825 FileName(owner_->filePath()));
832 // merge the current with the previous version
833 // in a reverse patch kind of way, so that the
834 // result is to revert the last changes.
835 lyxerr << "Sorry, not implemented." << endl;
839 bool SVN::undoLastEnabled()
845 void SVN::getLog(FileName const & tmpf)
847 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
848 + " > " + quoteName(tmpf.toFilesystemEncoding()),
849 FileName(owner_->filePath()));
853 bool SVN::toggleReadOnlyEnabled()