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 << endl;
59 support::Path p(path);
60 int const ret = one.startscript(Systemcall::Wait, cmd);
65 /////////////////////////////////////////////////////////////////////
69 /////////////////////////////////////////////////////////////////////
71 RCS::RCS(FileName const & m)
78 FileName const RCS::find_file(FileName const & file)
80 // Check if *,v exists.
81 FileName tmp(file.absFilename() + ",v");
82 LYXERR(Debug::LYXVC) << "Checking if file is under rcs: "
84 if (tmp.isReadable()) {
85 LYXERR(Debug::LYXVC) << "Yes " << file
86 << " is under rcs." << endl;
90 // Check if RCS/*,v exists.
91 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
92 LYXERR(Debug::LYXVC) << "Checking if file is under rcs: "
94 if (tmp.isReadable()) {
95 LYXERR(Debug::LYXVC) << "Yes " << file
96 << " it is under rcs."<< endl;
104 void RCS::retrieve(FileName const & file)
106 LYXERR(Debug::LYXVC) << "LyXVC::RCS: retrieve.\n\t" << file << endl;
107 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
112 void RCS::scanMaster()
114 LYXERR(Debug::LYXVC) << "LyXVC::RCS: scanMaster." << endl;
116 ifstream ifs(master_.toFilesystemEncoding().c_str());
119 bool read_enough = false;
121 while (!read_enough && ifs >> token) {
123 << "LyXVC::scanMaster: current lex text: `"
124 << token << '\'' << endl;
128 else if (token == "head") {
132 tmv = rtrim(tmv, ";");
134 LYXERR(Debug::LYXVC) << "LyXVC: version found to be "
136 } else if (contains(token, "access")
137 || contains(token, "symbols")
138 || contains(token, "strict")) {
140 } else if (contains(token, "locks")) {
142 if (contains(token, ';')) {
143 locker_ = "Unlocked";
152 s1 = rtrim(tmpt, ";");
153 // tmp is now in the format <user>:<version>
154 s1 = split(s1, s2, ':');
155 // s2 is user, and s1 is version
156 if (s1 == version_) {
161 } while (!contains(tmpt, ';'));
163 } else if (token == "comment") {
164 // we don't need to read any further than this.
169 << "LyXVC::scanMaster(): unexpected token"
176 void RCS::registrer(string const & msg)
178 string cmd = "ci -q -u -i -t-\"";
181 cmd += quoteName(onlyFilename(owner_->absFileName()));
182 doVCCommand(cmd, FileName(owner_->filePath()));
186 void RCS::checkIn(string const & msg)
188 doVCCommand("ci -q -u -m\"" + msg + "\" "
189 + quoteName(onlyFilename(owner_->absFileName())),
190 FileName(owner_->filePath()));
197 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
198 FileName(owner_->filePath()));
204 doVCCommand("co -f -u" + version() + " "
205 + quoteName(onlyFilename(owner_->absFileName())),
206 FileName(owner_->filePath()));
207 // We ignore changes and just reload!
214 LYXERR(Debug::LYXVC) << "LyXVC: undoLast" << endl;
215 doVCCommand("rcs -o" + version() + " "
216 + quoteName(onlyFilename(owner_->absFileName())),
217 FileName(owner_->filePath()));
221 void RCS::getLog(FileName const & tmpf)
223 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
224 + " > " + tmpf.toFilesystemEncoding(),
225 FileName(owner_->filePath()));
229 /////////////////////////////////////////////////////////////////////
233 /////////////////////////////////////////////////////////////////////
235 CVS::CVS(FileName const & m, FileName const & f)
243 FileName const CVS::find_file(FileName const & file)
245 // First we look for the CVS/Entries in the same dir
246 // where we have file.
247 FileName const dir(onlyPath(file.absFilename()) + "/CVS/Entries");
248 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
249 LYXERR(Debug::LYXVC) << "LyXVC: checking in `" << dir
250 << "' for `" << tmpf << '\'' << endl;
251 if (dir.isReadable()) {
252 // Ok we are at least in a CVS dir. Parse the CVS/Entries
253 // and see if we can find this file. We do a fast and
255 ifstream ifs(dir.toFilesystemEncoding().c_str());
257 while (getline(ifs, line)) {
258 LYXERR(Debug::LYXVC) << "\tEntries: " << line << endl;
259 if (contains(line, tmpf))
267 void CVS::scanMaster()
269 LYXERR(Debug::LYXVC) << "LyXVC::CVS: scanMaster. \n Checking: "
271 // Ok now we do the real scan...
272 ifstream ifs(master_.toFilesystemEncoding().c_str());
273 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
274 LYXERR(Debug::LYXVC) << "\tlooking for `" << tmpf << '\'' << endl;
276 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
277 while (getline(ifs, line)) {
278 LYXERR(Debug::LYXVC) << "\t line: " << line << endl;
279 if (contains(line, tmpf)) {
280 // Ok extract the fields.
283 regex_match(line, sm, reg);
285 //sm[0]; // whole matched string
287 version_ = sm.str(2);
288 string const file_date = sm.str(3);
291 //sm[5]; // tag or tagdate
292 // FIXME: must double check file is stattable/existing
293 time_t mod = file_.lastModified();
294 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
296 << "Date in Entries: `" << file_date
297 << "'\nModification date of file: `"
298 << mod_date << '\'' << endl;
299 if (file_date == mod_date) {
300 locker_ = "Unlocked";
303 // Here we should also to some more checking
304 // to see if there are conflicts or not.
314 void CVS::registrer(string const & msg)
316 doVCCommand("cvs -q add -m \"" + msg + "\" "
317 + quoteName(onlyFilename(owner_->absFileName())),
318 FileName(owner_->filePath()));
322 void CVS::checkIn(string const & msg)
324 doVCCommand("cvs -q commit -m \"" + msg + "\" "
325 + quoteName(onlyFilename(owner_->absFileName())),
326 FileName(owner_->filePath()));
332 // cvs update or perhaps for cvs this should be a noop
333 lyxerr << "Sorry not implemented." << endl;
339 // Reverts to the version in CVS repository and
340 // gets the updated version from the repository.
341 string const fil = quoteName(onlyFilename(owner_->absFileName()));
343 doVCCommand("rm -f " + fil + "; cvs update " + fil,
344 FileName(owner_->filePath()));
351 // merge the current with the previous version
352 // in a reverse patch kind of way, so that the
353 // result is to revert the last changes.
354 lyxerr << "Sorry not implemented." << endl;
358 void CVS::getLog(FileName const & tmpf)
360 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
361 + " > " + tmpf.toFilesystemEncoding(),
362 FileName(owner_->filePath()));