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 _("Please check you have installed revision control program.") + "\n"
53 /////////////////////////////////////////////////////////////////////
57 /////////////////////////////////////////////////////////////////////
59 RCS::RCS(FileName const & m)
66 FileName const RCS::findFile(FileName const & file)
68 // Check if *,v exists.
69 FileName tmp(file.absFilename() + ",v");
70 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
71 if (tmp.isReadableFile()) {
72 LYXERR(Debug::LYXVC, "Yes " << file << " is under rcs.");
76 // Check if RCS/*,v exists.
77 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
78 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
79 if (tmp.isReadableFile()) {
80 LYXERR(Debug::LYXVC, "Yes " << file << " it is under rcs.");
88 void RCS::retrieve(FileName const & file)
90 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
91 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
96 void RCS::scanMaster()
98 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster.");
100 ifstream ifs(master_.toFilesystemEncoding().c_str());
103 bool read_enough = false;
105 while (!read_enough && ifs >> token) {
106 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
111 else if (token == "head") {
115 tmv = rtrim(tmv, ";");
117 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
118 } else if (contains(token, "access")
119 || contains(token, "symbols")
120 || contains(token, "strict")) {
122 } else if (contains(token, "locks")) {
124 if (contains(token, ';')) {
125 locker_ = "Unlocked";
134 s1 = rtrim(tmpt, ";");
135 // tmp is now in the format <user>:<version>
136 s1 = split(s1, s2, ':');
137 // s2 is user, and s1 is version
138 if (s1 == version_) {
143 } while (!contains(tmpt, ';'));
145 } else if (token == "comment") {
146 // we don't need to read any further than this.
150 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
156 void RCS::registrer(string const & msg)
158 string cmd = "ci -q -u -i -t-\"";
161 cmd += quoteName(onlyFilename(owner_->absFileName()));
162 doVCCommand(cmd, FileName(owner_->filePath()));
166 void RCS::checkIn(string const & msg)
168 doVCCommand("ci -q -u -m\"" + msg + "\" "
169 + quoteName(onlyFilename(owner_->absFileName())),
170 FileName(owner_->filePath()));
173 bool RCS::checkInEnabled()
175 return owner_ && !owner_->isReadonly();
181 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
182 FileName(owner_->filePath()));
186 bool RCS::checkOutEnabled()
188 return owner_ && owner_->isReadonly();
194 doVCCommand("co -f -u" + version() + " "
195 + quoteName(onlyFilename(owner_->absFileName())),
196 FileName(owner_->filePath()));
197 // We ignore changes and just reload!
204 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
205 doVCCommand("rcs -o" + version() + " "
206 + quoteName(onlyFilename(owner_->absFileName())),
207 FileName(owner_->filePath()));
211 bool RCS::undoLastEnabled()
217 void RCS::getLog(FileName const & tmpf)
219 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
220 + " > " + tmpf.toFilesystemEncoding(),
221 FileName(owner_->filePath()));
225 /////////////////////////////////////////////////////////////////////
229 /////////////////////////////////////////////////////////////////////
231 CVS::CVS(FileName const & m, FileName const & f)
239 FileName const CVS::findFile(FileName const & file)
241 // First we look for the CVS/Entries in the same dir
242 // where we have file.
243 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
244 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
245 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
246 << "' for `" << tmpf << '\'');
247 if (entries.isReadableFile()) {
248 // Ok we are at least in a CVS dir. Parse the CVS/Entries
249 // and see if we can find this file. We do a fast and
251 ifstream ifs(entries.toFilesystemEncoding().c_str());
253 while (getline(ifs, line)) {
254 LYXERR(Debug::LYXVC, "\tEntries: " << line);
255 if (contains(line, tmpf))
263 void CVS::scanMaster()
265 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
266 // Ok now we do the real scan...
267 ifstream ifs(master_.toFilesystemEncoding().c_str());
268 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
269 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
271 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
272 while (getline(ifs, line)) {
273 LYXERR(Debug::LYXVC, "\t line: " << line);
274 if (contains(line, tmpf)) {
275 // Ok extract the fields.
278 regex_match(line, sm, reg);
280 //sm[0]; // whole matched string
282 version_ = sm.str(2);
283 string const file_date = sm.str(3);
286 //sm[5]; // tag or tagdate
287 // FIXME: must double check file is stattable/existing
288 time_t mod = file_.lastModified();
289 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
290 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
291 << "'\nModification date of file: `" << mod_date << '\'');
292 //FIXME this whole locking bussiness is not working under cvs and the machinery
293 // conforms to the ci usage, not cvs.
294 if (file_date == mod_date) {
295 locker_ = "Unlocked";
298 // Here we should also to some more checking
299 // to see if there are conflicts or not.
309 void CVS::registrer(string const & msg)
311 doVCCommand("cvs -q add -m \"" + msg + "\" "
312 + quoteName(onlyFilename(owner_->absFileName())),
313 FileName(owner_->filePath()));
317 void CVS::checkIn(string const & msg)
319 doVCCommand("cvs -q commit -m \"" + msg + "\" "
320 + quoteName(onlyFilename(owner_->absFileName())),
321 FileName(owner_->filePath()));
325 bool CVS::checkInEnabled()
333 // cvs update or perhaps for cvs this should be a noop
334 lyxerr << "Sorry not implemented." << endl;
338 bool CVS::checkOutEnabled()
346 // This is sensitive operation, so at lest some check before
347 if (doVCCommand("cvs --help", FileName(owner_->filePath())))
349 // Reverts to the version in CVS repository and
350 // gets the updated version from the repository.
351 string const fil = quoteName(onlyFilename(owner_->absFileName()));
353 doVCCommand("rm -f " + fil + "; cvs update " + fil,
354 FileName(owner_->filePath()));
361 // merge the current with the previous version
362 // in a reverse patch kind of way, so that the
363 // result is to revert the last changes.
364 lyxerr << "Sorry not implemented." << endl;
368 bool CVS::undoLastEnabled()
374 void CVS::getLog(FileName const & tmpf)
376 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
377 + " > " + tmpf.toFilesystemEncoding(),
378 FileName(owner_->filePath()));