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/fs_extras.h"
20 #include "support/lstrings.h"
21 #include "support/Systemcall.h"
23 #include <boost/regex.hpp>
30 using support::addName;
31 using support::addPath;
32 using support::contains;
33 using support::FileName;
34 using support::onlyFilename;
35 using support::onlyPath;
36 using support::quoteName;
39 using support::Systemcall;
42 using boost::regex_match;
45 #ifndef CXX_GLOBAL_CSTD
56 int VCS::doVCCommand(string const & cmd, FileName const & path)
58 LYXERR(Debug::LYXVC) << "doVCCommand: " << cmd << endl;
60 support::Path p(path);
61 int const ret = one.startscript(Systemcall::Wait, cmd);
66 /////////////////////////////////////////////////////////////////////
70 /////////////////////////////////////////////////////////////////////
72 RCS::RCS(FileName const & m)
79 FileName const RCS::find_file(FileName const & file)
81 // Check if *,v exists.
82 FileName tmp(file.absFilename() + ",v");
83 LYXERR(Debug::LYXVC) << "Checking if file is under rcs: "
85 if (tmp.isReadable()) {
86 LYXERR(Debug::LYXVC) << "Yes " << file
87 << " is under rcs." << endl;
91 // Check if RCS/*,v exists.
92 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
93 LYXERR(Debug::LYXVC) << "Checking if file is under rcs: "
95 if (tmp.isReadable()) {
96 LYXERR(Debug::LYXVC) << "Yes " << file
97 << " it is under rcs."<< endl;
105 void RCS::retrieve(FileName const & file)
107 LYXERR(Debug::LYXVC) << "LyXVC::RCS: retrieve.\n\t" << file << endl;
108 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
113 void RCS::scanMaster()
115 LYXERR(Debug::LYXVC) << "LyXVC::RCS: scanMaster." << endl;
117 ifstream ifs(master_.toFilesystemEncoding().c_str());
120 bool read_enough = false;
122 while (!read_enough && ifs >> token) {
124 << "LyXVC::scanMaster: current lex text: `"
125 << token << '\'' << endl;
129 else if (token == "head") {
133 tmv = rtrim(tmv, ";");
135 LYXERR(Debug::LYXVC) << "LyXVC: version found to be "
137 } else if (contains(token, "access")
138 || contains(token, "symbols")
139 || contains(token, "strict")) {
141 } else if (contains(token, "locks")) {
143 if (contains(token, ';')) {
144 locker_ = "Unlocked";
153 s1 = rtrim(tmpt, ";");
154 // tmp is now in the format <user>:<version>
155 s1 = split(s1, s2, ':');
156 // s2 is user, and s1 is version
157 if (s1 == version_) {
162 } while (!contains(tmpt, ';'));
164 } else if (token == "comment") {
165 // we don't need to read any further than this.
170 << "LyXVC::scanMaster(): unexpected token"
177 void RCS::registrer(string const & msg)
179 string cmd = "ci -q -u -i -t-\"";
182 cmd += quoteName(onlyFilename(owner_->absFileName()));
183 doVCCommand(cmd, FileName(owner_->filePath()));
187 void RCS::checkIn(string const & msg)
189 doVCCommand("ci -q -u -m\"" + msg + "\" "
190 + quoteName(onlyFilename(owner_->absFileName())),
191 FileName(owner_->filePath()));
198 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
199 FileName(owner_->filePath()));
205 doVCCommand("co -f -u" + version() + " "
206 + quoteName(onlyFilename(owner_->absFileName())),
207 FileName(owner_->filePath()));
208 // We ignore changes and just reload!
215 LYXERR(Debug::LYXVC) << "LyXVC: undoLast" << endl;
216 doVCCommand("rcs -o" + version() + " "
217 + quoteName(onlyFilename(owner_->absFileName())),
218 FileName(owner_->filePath()));
222 void RCS::getLog(FileName const & tmpf)
224 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
225 + " > " + tmpf.toFilesystemEncoding(),
226 FileName(owner_->filePath()));
230 /////////////////////////////////////////////////////////////////////
234 /////////////////////////////////////////////////////////////////////
236 CVS::CVS(FileName const & m, FileName const & f)
244 FileName const CVS::find_file(FileName const & file)
246 // First we look for the CVS/Entries in the same dir
247 // where we have file.
248 FileName const dir(onlyPath(file.absFilename()) + "/CVS/Entries");
249 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
250 LYXERR(Debug::LYXVC) << "LyXVC: checking in `" << dir
251 << "' for `" << tmpf << '\'' << endl;
252 if (dir.isReadable()) {
253 // Ok we are at least in a CVS dir. Parse the CVS/Entries
254 // and see if we can find this file. We do a fast and
256 ifstream ifs(dir.toFilesystemEncoding().c_str());
258 while (getline(ifs, line)) {
259 LYXERR(Debug::LYXVC) << "\tEntries: " << line << endl;
260 if (contains(line, tmpf))
268 void CVS::scanMaster()
270 LYXERR(Debug::LYXVC) << "LyXVC::CVS: scanMaster. \n Checking: "
272 // Ok now we do the real scan...
273 ifstream ifs(master_.toFilesystemEncoding().c_str());
274 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
275 LYXERR(Debug::LYXVC) << "\tlooking for `" << tmpf << '\'' << endl;
277 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
278 while (getline(ifs, line)) {
279 LYXERR(Debug::LYXVC) << "\t line: " << line << endl;
280 if (contains(line, tmpf)) {
281 // Ok extract the fields.
284 regex_match(line, sm, reg);
286 //sm[0]; // whole matched string
288 version_ = sm.str(2);
289 string const file_date = sm.str(3);
292 //sm[5]; // tag or tagdate
293 // FIXME: must double check file is stattable/existing
294 time_t mod = file_.lastModified();
295 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
297 << "Date in Entries: `" << file_date
298 << "'\nModification date of file: `"
299 << mod_date << '\'' << endl;
300 if (file_date == mod_date) {
301 locker_ = "Unlocked";
304 // Here we should also to some more checking
305 // to see if there are conflicts or not.
315 void CVS::registrer(string const & msg)
317 doVCCommand("cvs -q add -m \"" + msg + "\" "
318 + quoteName(onlyFilename(owner_->absFileName())),
319 FileName(owner_->filePath()));
323 void CVS::checkIn(string const & msg)
325 doVCCommand("cvs -q commit -m \"" + msg + "\" "
326 + quoteName(onlyFilename(owner_->absFileName())),
327 FileName(owner_->filePath()));
333 // cvs update or perhaps for cvs this should be a noop
334 lyxerr << "Sorry not implemented." << endl;
340 // Reverts to the version in CVS repository and
341 // gets the updated version from the repository.
342 string const fil = quoteName(onlyFilename(owner_->absFileName()));
344 doVCCommand("rm -f " + fil + "; cvs update " + fil,
345 FileName(owner_->filePath()));
352 // merge the current with the previous version
353 // in a reverse patch kind of way, so that the
354 // result is to revert the last changes.
355 lyxerr << "Sorry not implemented." << endl;
359 void CVS::getLog(FileName const & tmpf)
361 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
362 + " > " + tmpf.toFilesystemEncoding(),
363 FileName(owner_->filePath()));