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"
17 #include "support/Path.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/Systemcall.h"
22 #include <boost/regex.hpp>
29 using support::addName;
30 using support::addPath;
31 using support::contains;
32 using support::FileName;
33 using support::onlyFilename;
34 using support::onlyPath;
35 using support::quoteName;
38 using support::Systemcall;
41 using boost::regex_match;
44 #ifndef CXX_GLOBAL_CSTD
55 int VCS::doVCCommand(string const & cmd, FileName const & path)
57 LYXERR(Debug::LYXVC, "doVCCommand: " << cmd);
59 support::PathChanger p(path);
60 int const ret = one.startscript(Systemcall::Wait, cmd);
65 /////////////////////////////////////////////////////////////////////
69 /////////////////////////////////////////////////////////////////////
71 RCS::RCS(FileName const & m)
78 FileName const RCS::findFile(FileName const & file)
80 // Check if *,v exists.
81 FileName tmp(file.absFilename() + ",v");
82 LYXERR(Debug::LYXVC, "Checking if file is under rcs: " << tmp);
83 if (tmp.isReadableFile()) {
84 LYXERR(Debug::LYXVC, "Yes " << file << " is under rcs.");
88 // Check if RCS/*,v exists.
89 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
90 LYXERR(Debug::LYXVC, "Checking if file is under rcs: " << tmp);
91 if (tmp.isReadableFile()) {
92 LYXERR(Debug::LYXVC, "Yes " << file << " it is under rcs.");
100 void RCS::retrieve(FileName const & file)
102 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
103 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
108 void RCS::scanMaster()
110 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster.");
112 ifstream ifs(master_.toFilesystemEncoding().c_str());
115 bool read_enough = false;
117 while (!read_enough && ifs >> token) {
118 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
123 else if (token == "head") {
127 tmv = rtrim(tmv, ";");
129 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
130 } else if (contains(token, "access")
131 || contains(token, "symbols")
132 || contains(token, "strict")) {
134 } else if (contains(token, "locks")) {
136 if (contains(token, ';')) {
137 locker_ = "Unlocked";
146 s1 = rtrim(tmpt, ";");
147 // tmp is now in the format <user>:<version>
148 s1 = split(s1, s2, ':');
149 // s2 is user, and s1 is version
150 if (s1 == version_) {
155 } while (!contains(tmpt, ';'));
157 } else if (token == "comment") {
158 // we don't need to read any further than this.
162 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
168 void RCS::registrer(string const & msg)
170 string cmd = "ci -q -u -i -t-\"";
173 cmd += quoteName(onlyFilename(owner_->absFileName()));
174 doVCCommand(cmd, FileName(owner_->filePath()));
178 void RCS::checkIn(string const & msg)
180 doVCCommand("ci -q -u -m\"" + msg + "\" "
181 + quoteName(onlyFilename(owner_->absFileName())),
182 FileName(owner_->filePath()));
189 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
190 FileName(owner_->filePath()));
196 doVCCommand("co -f -u" + version() + " "
197 + quoteName(onlyFilename(owner_->absFileName())),
198 FileName(owner_->filePath()));
199 // We ignore changes and just reload!
206 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
207 doVCCommand("rcs -o" + version() + " "
208 + quoteName(onlyFilename(owner_->absFileName())),
209 FileName(owner_->filePath()));
213 void RCS::getLog(FileName const & tmpf)
215 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
216 + " > " + tmpf.toFilesystemEncoding(),
217 FileName(owner_->filePath()));
221 /////////////////////////////////////////////////////////////////////
225 /////////////////////////////////////////////////////////////////////
227 CVS::CVS(FileName const & m, FileName const & f)
235 FileName const CVS::findFile(FileName const & file)
237 // First we look for the CVS/Entries in the same dir
238 // where we have file.
239 FileName const dir(onlyPath(file.absFilename()) + "/CVS/Entries");
240 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
241 LYXERR(Debug::LYXVC, "LyXVC: checking in `" << dir
242 << "' for `" << tmpf << '\'');
243 if (dir.isReadableDirectory()) {
244 // Ok we are at least in a CVS dir. Parse the CVS/Entries
245 // and see if we can find this file. We do a fast and
247 ifstream ifs(dir.toFilesystemEncoding().c_str());
249 while (getline(ifs, line)) {
250 LYXERR(Debug::LYXVC, "\tEntries: " << line);
251 if (contains(line, tmpf))
259 void CVS::scanMaster()
261 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
262 // Ok now we do the real scan...
263 ifstream ifs(master_.toFilesystemEncoding().c_str());
264 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
265 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
267 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
268 while (getline(ifs, line)) {
269 LYXERR(Debug::LYXVC, "\t line: " << line);
270 if (contains(line, tmpf)) {
271 // Ok extract the fields.
274 regex_match(line, sm, reg);
276 //sm[0]; // whole matched string
278 version_ = sm.str(2);
279 string const file_date = sm.str(3);
282 //sm[5]; // tag or tagdate
283 // FIXME: must double check file is stattable/existing
284 time_t mod = file_.lastModified();
285 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
286 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
287 << "'\nModification date of file: `" << mod_date << '\'');
288 if (file_date == mod_date) {
289 locker_ = "Unlocked";
292 // Here we should also to some more checking
293 // to see if there are conflicts or not.
303 void CVS::registrer(string const & msg)
305 doVCCommand("cvs -q add -m \"" + msg + "\" "
306 + quoteName(onlyFilename(owner_->absFileName())),
307 FileName(owner_->filePath()));
311 void CVS::checkIn(string const & msg)
313 doVCCommand("cvs -q commit -m \"" + msg + "\" "
314 + quoteName(onlyFilename(owner_->absFileName())),
315 FileName(owner_->filePath()));
321 // cvs update or perhaps for cvs this should be a noop
322 lyxerr << "Sorry not implemented." << endl;
328 // Reverts to the version in CVS repository and
329 // gets the updated version from the repository.
330 string const fil = quoteName(onlyFilename(owner_->absFileName()));
332 doVCCommand("rm -f " + fil + "; cvs update " + fil,
333 FileName(owner_->filePath()));
340 // merge the current with the previous version
341 // in a reverse patch kind of way, so that the
342 // result is to revert the last changes.
343 lyxerr << "Sorry not implemented." << endl;
347 void CVS::getLog(FileName const & tmpf)
349 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
350 + " > " + tmpf.toFilesystemEncoding(),
351 FileName(owner_->filePath()));