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 "support/debug.h"
17 #include "support/filetools.h"
18 #include "support/lstrings.h"
19 #include "support/Path.h"
20 #include "support/Systemcall.h"
22 #include <boost/regex.hpp>
27 using namespace lyx::support;
30 using boost::regex_match;
36 int VCS::doVCCommand(string const & cmd, FileName const & path)
38 LYXERR(Debug::LYXVC, "doVCCommand: " << cmd);
40 support::PathChanger p(path);
41 int const ret = one.startscript(Systemcall::Wait, cmd);
46 /////////////////////////////////////////////////////////////////////
50 /////////////////////////////////////////////////////////////////////
52 RCS::RCS(FileName const & m)
59 FileName const RCS::findFile(FileName const & file)
61 // Check if *,v exists.
62 FileName tmp(file.absFilename() + ",v");
63 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
64 if (tmp.isReadableFile()) {
65 LYXERR(Debug::LYXVC, "Yes " << file << " is under rcs.");
69 // Check if RCS/*,v exists.
70 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), 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 << " it is under rcs.");
81 void RCS::retrieve(FileName const & file)
83 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
84 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
89 void RCS::scanMaster()
91 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster.");
93 ifstream ifs(master_.toFilesystemEncoding().c_str());
96 bool read_enough = false;
98 while (!read_enough && ifs >> token) {
99 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
104 else if (token == "head") {
108 tmv = rtrim(tmv, ";");
110 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
111 } else if (contains(token, "access")
112 || contains(token, "symbols")
113 || contains(token, "strict")) {
115 } else if (contains(token, "locks")) {
117 if (contains(token, ';')) {
118 locker_ = "Unlocked";
127 s1 = rtrim(tmpt, ";");
128 // tmp is now in the format <user>:<version>
129 s1 = split(s1, s2, ':');
130 // s2 is user, and s1 is version
131 if (s1 == version_) {
136 } while (!contains(tmpt, ';'));
138 } else if (token == "comment") {
139 // we don't need to read any further than this.
143 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
149 void RCS::registrer(string const & msg)
151 string cmd = "ci -q -u -i -t-\"";
154 cmd += quoteName(onlyFilename(owner_->absFileName()));
155 doVCCommand(cmd, FileName(owner_->filePath()));
159 void RCS::checkIn(string const & msg)
161 doVCCommand("ci -q -u -m\"" + msg + "\" "
162 + quoteName(onlyFilename(owner_->absFileName())),
163 FileName(owner_->filePath()));
170 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
171 FileName(owner_->filePath()));
177 doVCCommand("co -f -u" + version() + " "
178 + quoteName(onlyFilename(owner_->absFileName())),
179 FileName(owner_->filePath()));
180 // We ignore changes and just reload!
187 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
188 doVCCommand("rcs -o" + version() + " "
189 + quoteName(onlyFilename(owner_->absFileName())),
190 FileName(owner_->filePath()));
194 void RCS::getLog(FileName const & tmpf)
196 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
197 + " > " + tmpf.toFilesystemEncoding(),
198 FileName(owner_->filePath()));
202 /////////////////////////////////////////////////////////////////////
206 /////////////////////////////////////////////////////////////////////
208 CVS::CVS(FileName const & m, FileName const & f)
216 FileName const CVS::findFile(FileName const & file)
218 // First we look for the CVS/Entries in the same dir
219 // where we have file.
220 FileName const dir(onlyPath(file.absFilename()) + "/CVS/Entries");
221 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
222 LYXERR(Debug::LYXVC, "LyXVC: Checking in `" << dir
223 << "' for `" << tmpf << '\'');
224 if (dir.isReadableDirectory()) {
225 // Ok we are at least in a CVS dir. Parse the CVS/Entries
226 // and see if we can find this file. We do a fast and
228 ifstream ifs(dir.toFilesystemEncoding().c_str());
230 while (getline(ifs, line)) {
231 LYXERR(Debug::LYXVC, "\tEntries: " << line);
232 if (contains(line, tmpf))
240 void CVS::scanMaster()
242 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
243 // Ok now we do the real scan...
244 ifstream ifs(master_.toFilesystemEncoding().c_str());
245 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
246 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
248 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
249 while (getline(ifs, line)) {
250 LYXERR(Debug::LYXVC, "\t line: " << line);
251 if (contains(line, tmpf)) {
252 // Ok extract the fields.
255 regex_match(line, sm, reg);
257 //sm[0]; // whole matched string
259 version_ = sm.str(2);
260 string const file_date = sm.str(3);
263 //sm[5]; // tag or tagdate
264 // FIXME: must double check file is stattable/existing
265 time_t mod = file_.lastModified();
266 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
267 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
268 << "'\nModification date of file: `" << mod_date << '\'');
269 if (file_date == mod_date) {
270 locker_ = "Unlocked";
273 // Here we should also to some more checking
274 // to see if there are conflicts or not.
284 void CVS::registrer(string const & msg)
286 doVCCommand("cvs -q add -m \"" + msg + "\" "
287 + quoteName(onlyFilename(owner_->absFileName())),
288 FileName(owner_->filePath()));
292 void CVS::checkIn(string const & msg)
294 doVCCommand("cvs -q commit -m \"" + msg + "\" "
295 + quoteName(onlyFilename(owner_->absFileName())),
296 FileName(owner_->filePath()));
302 // cvs update or perhaps for cvs this should be a noop
303 lyxerr << "Sorry not implemented." << endl;
309 // Reverts to the version in CVS repository and
310 // gets the updated version from the repository.
311 string const fil = quoteName(onlyFilename(owner_->absFileName()));
313 doVCCommand("rm -f " + fil + "; cvs update " + fil,
314 FileName(owner_->filePath()));
321 // merge the current with the previous version
322 // in a reverse patch kind of way, so that the
323 // result is to revert the last changes.
324 lyxerr << "Sorry not implemented." << endl;
328 void CVS::getLog(FileName const & tmpf)
330 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
331 + " > " + tmpf.toFilesystemEncoding(),
332 FileName(owner_->filePath()));