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/filesystem/operations.hpp>
24 #include <boost/regex.hpp>
31 using support::addName;
32 using support::addPath;
33 using support::contains;
34 using support::FileName;
35 using support::onlyFilename;
36 using support::onlyPath;
37 using support::quoteName;
40 using support::Systemcall;
43 using boost::regex_match;
46 #ifndef CXX_GLOBAL_CSTD
56 namespace fs = boost::filesystem;
59 int VCS::doVCCommand(string const & cmd, FileName const & path)
61 LYXERR(Debug::LYXVC) << "doVCCommand: " << cmd << endl;
63 support::Path p(path);
64 int const ret = one.startscript(Systemcall::Wait, cmd);
69 /////////////////////////////////////////////////////////////////////
73 /////////////////////////////////////////////////////////////////////
75 RCS::RCS(FileName const & m)
82 FileName const RCS::find_file(FileName const & file)
84 // Check if *,v exists.
85 FileName tmp(file.absFilename() + ",v");
86 LYXERR(Debug::LYXVC) << "Checking if file is under rcs: "
88 if (fs::is_readable(tmp.toFilesystemEncoding())) {
89 LYXERR(Debug::LYXVC) << "Yes " << file
90 << " is under rcs." << endl;
93 // Check if RCS/*,v exists.
94 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
95 LYXERR(Debug::LYXVC) << "Checking if file is under rcs: "
97 if (fs::is_readable(tmp.toFilesystemEncoding())) {
98 LYXERR(Debug::LYXVC) << "Yes " << file
99 << " it is under rcs."<< endl;
107 void RCS::retrieve(FileName const & file)
109 LYXERR(Debug::LYXVC) << "LyXVC::RCS: retrieve.\n\t" << file << endl;
110 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
115 void RCS::scanMaster()
117 LYXERR(Debug::LYXVC) << "LyXVC::RCS: scanMaster." << endl;
119 ifstream ifs(master_.toFilesystemEncoding().c_str());
122 bool read_enough = false;
124 while (!read_enough && ifs >> token) {
126 << "LyXVC::scanMaster: current lex text: `"
127 << token << '\'' << endl;
131 else if (token == "head") {
135 tmv = rtrim(tmv, ";");
137 LYXERR(Debug::LYXVC) << "LyXVC: version found to be "
139 } else if (contains(token, "access")
140 || contains(token, "symbols")
141 || contains(token, "strict")) {
143 } else if (contains(token, "locks")) {
145 if (contains(token, ';')) {
146 locker_ = "Unlocked";
155 s1 = rtrim(tmpt, ";");
156 // tmp is now in the format <user>:<version>
157 s1 = split(s1, s2, ':');
158 // s2 is user, and s1 is version
159 if (s1 == version_) {
164 } while (!contains(tmpt, ';'));
166 } else if (token == "comment") {
167 // we don't need to read any further than this.
172 << "LyXVC::scanMaster(): unexpected token"
179 void RCS::registrer(string const & msg)
181 string cmd = "ci -q -u -i -t-\"";
184 cmd += quoteName(onlyFilename(owner_->absFileName()));
185 doVCCommand(cmd, FileName(owner_->filePath()));
189 void RCS::checkIn(string const & msg)
191 doVCCommand("ci -q -u -m\"" + msg + "\" "
192 + quoteName(onlyFilename(owner_->absFileName())),
193 FileName(owner_->filePath()));
200 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
201 FileName(owner_->filePath()));
207 doVCCommand("co -f -u" + version() + " "
208 + quoteName(onlyFilename(owner_->absFileName())),
209 FileName(owner_->filePath()));
210 // We ignore changes and just reload!
217 LYXERR(Debug::LYXVC) << "LyXVC: undoLast" << endl;
218 doVCCommand("rcs -o" + version() + " "
219 + quoteName(onlyFilename(owner_->absFileName())),
220 FileName(owner_->filePath()));
224 void RCS::getLog(FileName const & tmpf)
226 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
227 + " > " + tmpf.toFilesystemEncoding(),
228 FileName(owner_->filePath()));
232 /////////////////////////////////////////////////////////////////////
236 /////////////////////////////////////////////////////////////////////
238 CVS::CVS(FileName const & m, FileName const & f)
246 FileName const CVS::find_file(FileName const & file)
248 // First we look for the CVS/Entries in the same dir
249 // where we have file.
250 FileName const dir(onlyPath(file.absFilename()) + "/CVS/Entries");
251 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
252 LYXERR(Debug::LYXVC) << "LyXVC: checking in `" << dir
253 << "' for `" << tmpf << '\'' << endl;
254 if (fs::is_readable(dir.toFilesystemEncoding())) {
255 // Ok we are at least in a CVS dir. Parse the CVS/Entries
256 // and see if we can find this file. We do a fast and
258 ifstream ifs(dir.toFilesystemEncoding().c_str());
260 while (getline(ifs, line)) {
261 LYXERR(Debug::LYXVC) << "\tEntries: " << line << endl;
262 if (contains(line, tmpf))
270 void CVS::scanMaster()
272 LYXERR(Debug::LYXVC) << "LyXVC::CVS: scanMaster. \n Checking: "
274 // Ok now we do the real scan...
275 ifstream ifs(master_.toFilesystemEncoding().c_str());
276 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
277 LYXERR(Debug::LYXVC) << "\tlooking for `" << tmpf << '\'' << endl;
279 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
280 while (getline(ifs, line)) {
281 LYXERR(Debug::LYXVC) << "\t line: " << line << endl;
282 if (contains(line, tmpf)) {
283 // Ok extract the fields.
286 regex_match(line, sm, reg);
288 //sm[0]; // whole matched string
290 version_ = sm.str(2);
291 string const file_date = sm.str(3);
294 //sm[5]; // tag or tagdate
295 // FIXME: must double check file is stattable/existing
296 time_t mod = fs::last_write_time(file_.toFilesystemEncoding());
297 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
299 << "Date in Entries: `" << file_date
300 << "'\nModification date of file: `"
301 << mod_date << '\'' << endl;
302 if (file_date == mod_date) {
303 locker_ = "Unlocked";
306 // Here we should also to some more checking
307 // to see if there are conflicts or not.
317 void CVS::registrer(string const & msg)
319 doVCCommand("cvs -q add -m \"" + msg + "\" "
320 + quoteName(onlyFilename(owner_->absFileName())),
321 FileName(owner_->filePath()));
325 void CVS::checkIn(string const & msg)
327 doVCCommand("cvs -q commit -m \"" + msg + "\" "
328 + quoteName(onlyFilename(owner_->absFileName())),
329 FileName(owner_->filePath()));
335 // cvs update or perhaps for cvs this should be a noop
336 lyxerr << "Sorry not implemented." << endl;
342 // Reverts to the version in CVS repository and
343 // gets the updated version from the repository.
344 string const fil = quoteName(onlyFilename(owner_->absFileName()));
346 doVCCommand("rm -f " + fil + "; cvs update " + fil,
347 FileName(owner_->filePath()));
354 // merge the current with the previous version
355 // in a reverse patch kind of way, so that the
356 // result is to revert the last changes.
357 lyxerr << "Sorry not implemented." << endl;
361 void CVS::getLog(FileName const & tmpf)
363 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
364 + " > " + tmpf.toFilesystemEncoding(),
365 FileName(owner_->filePath()));