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 "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>
30 using namespace lyx::support;
33 using boost::regex_match;
39 int VCS::doVCCommand(string const & cmd, FileName const & path)
41 LYXERR(Debug::LYXVC, "doVCCommand: " << cmd);
43 support::PathChanger p(path);
44 int const ret = one.startscript(Systemcall::Wait, cmd);
46 frontend::Alert::error(_("Revision control error."),
47 bformat(_("Please check you have installed the program\n"
54 /////////////////////////////////////////////////////////////////////
58 /////////////////////////////////////////////////////////////////////
60 RCS::RCS(FileName const & m)
67 FileName const RCS::findFile(FileName const & file)
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.");
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.");
89 void RCS::retrieve(FileName const & file)
91 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
92 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
97 void RCS::scanMaster()
99 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster.");
101 ifstream ifs(master_.toFilesystemEncoding().c_str());
104 bool read_enough = false;
106 while (!read_enough && ifs >> token) {
107 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
112 else if (token == "head") {
116 tmv = rtrim(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")) {
123 } else if (contains(token, "locks")) {
125 if (contains(token, ';')) {
126 locker_ = "Unlocked";
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_) {
144 } while (!contains(tmpt, ';'));
146 } else if (token == "comment") {
147 // we don't need to read any further than this.
151 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
157 void RCS::registrer(string const & msg)
159 string cmd = "ci -q -u -i -t-\"";
162 cmd += quoteName(onlyFilename(owner_->absFileName()));
163 doVCCommand(cmd, FileName(owner_->filePath()));
167 void RCS::checkIn(string const & msg)
169 doVCCommand("ci -q -u -m\"" + msg + "\" "
170 + quoteName(onlyFilename(owner_->absFileName())),
171 FileName(owner_->filePath()));
174 bool RCS::checkInEnabled()
176 return owner_ && !owner_->isReadonly();
182 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
183 FileName(owner_->filePath()));
187 bool RCS::checkOutEnabled()
189 return owner_ && owner_->isReadonly();
195 doVCCommand("co -f -u" + version() + " "
196 + quoteName(onlyFilename(owner_->absFileName())),
197 FileName(owner_->filePath()));
198 // We ignore changes and just reload!
205 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
206 doVCCommand("rcs -o" + version() + " "
207 + quoteName(onlyFilename(owner_->absFileName())),
208 FileName(owner_->filePath()));
212 bool RCS::undoLastEnabled()
218 void RCS::getLog(FileName const & tmpf)
220 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
221 + " > " + tmpf.toFilesystemEncoding(),
222 FileName(owner_->filePath()));
226 /////////////////////////////////////////////////////////////////////
230 /////////////////////////////////////////////////////////////////////
232 CVS::CVS(FileName const & m, FileName const & f)
240 FileName const CVS::findFile(FileName const & file)
242 // First we look for the CVS/Entries in the same dir
243 // where we have file.
244 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
245 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
246 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
247 << "' for `" << tmpf << '\'');
248 if (entries.isReadableFile()) {
249 // Ok we are at least in a CVS dir. Parse the CVS/Entries
250 // and see if we can find this file. We do a fast and
252 ifstream ifs(entries.toFilesystemEncoding().c_str());
254 while (getline(ifs, line)) {
255 LYXERR(Debug::LYXVC, "\tEntries: " << line);
256 if (contains(line, tmpf))
264 void CVS::scanMaster()
266 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
267 // Ok now we do the real scan...
268 ifstream ifs(master_.toFilesystemEncoding().c_str());
269 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
270 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
272 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
273 while (getline(ifs, line)) {
274 LYXERR(Debug::LYXVC, "\t line: " << line);
275 if (contains(line, tmpf)) {
276 // Ok extract the fields.
279 regex_match(line, sm, reg);
281 //sm[0]; // whole matched string
283 version_ = sm.str(2);
284 string const file_date = sm.str(3);
287 //sm[5]; // tag or tagdate
288 // FIXME: must double check file is stattable/existing
289 time_t mod = file_.lastModified();
290 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
291 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
292 << "'\nModification date of file: `" << mod_date << '\'');
293 //FIXME this whole locking bussiness is not working under cvs and the machinery
294 // conforms to the ci usage, not cvs.
295 if (file_date == mod_date) {
296 locker_ = "Unlocked";
299 // Here we should also to some more checking
300 // to see if there are conflicts or not.
310 void CVS::registrer(string const & msg)
312 doVCCommand("cvs -q add -m \"" + msg + "\" "
313 + quoteName(onlyFilename(owner_->absFileName())),
314 FileName(owner_->filePath()));
318 void CVS::checkIn(string const & msg)
320 doVCCommand("cvs -q commit -m \"" + msg + "\" "
321 + quoteName(onlyFilename(owner_->absFileName())),
322 FileName(owner_->filePath()));
326 bool CVS::checkInEnabled()
334 // cvs update or perhaps for cvs this should be a noop
335 lyxerr << "Sorry not implemented." << endl;
339 bool CVS::checkOutEnabled()
347 // This is sensitive operation, so at lest some check before
348 if (doVCCommand("cvs --help", FileName(owner_->filePath())))
350 // Reverts to the version in CVS repository and
351 // gets the updated version from the repository.
352 string const fil = quoteName(onlyFilename(owner_->absFileName()));
354 doVCCommand("rm -f " + fil + "; cvs update " + fil,
355 FileName(owner_->filePath()));
362 // merge the current with the previous version
363 // in a reverse patch kind of way, so that the
364 // result is to revert the last changes.
365 lyxerr << "Sorry not implemented." << endl;
369 bool CVS::undoLastEnabled()
375 void CVS::getLog(FileName const & tmpf)
377 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
378 + " > " + tmpf.toFilesystemEncoding(),
379 FileName(owner_->filePath()));