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 "vc-backend.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::onlyFilename;
35 using support::onlyPath;
36 using support::quoteName;
39 using support::Systemcall;
42 using boost::regex_match;
45 #ifndef CXX_GLOBAL_CSTD
55 namespace fs = boost::filesystem;
58 int VCS::doVCCommand(string const & cmd, string const & path)
60 lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
62 support::Path p(path);
63 int const ret = one.startscript(Systemcall::Wait, cmd);
68 RCS::RCS(string const & m)
75 string const RCS::find_file(string const & file)
78 // Check if *,v exists.
80 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
82 if (fs::is_readable(tmp)) {
83 lyxerr[Debug::LYXVC] << "Yes " << file
84 << " is under rcs." << endl;
87 // Check if RCS/*,v exists.
88 tmp = addName(addPath(onlyPath(file), "RCS"), file);
90 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
92 if (fs::is_readable(tmp)) {
93 lyxerr[Debug::LYXVC] << "Yes " << file
94 << " it is under rcs."<< endl;
102 void RCS::retrieve(string const & file)
104 lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
105 VCS::doVCCommand("co -q -r " + quoteName(file),
110 void RCS::scanMaster()
112 lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
114 ifstream ifs(master_.c_str());
117 bool read_enough = false;
119 while (!read_enough && ifs >> token) {
121 << "LyXVC::scanMaster: current lex text: `"
122 << token << '\'' << endl;
126 else if (token == "head") {
130 tmv = rtrim(tmv, ";");
132 lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
134 } else if (contains(token, "access")
135 || contains(token, "symbols")
136 || contains(token, "strict")) {
138 } else if (contains(token, "locks")) {
140 if (contains(token, ';')) {
141 locker_ = "Unlocked";
150 s1 = rtrim(tmpt, ";");
151 // tmp is now in the format <user>:<version>
152 s1 = split(s1, s2, ':');
153 // s2 is user, and s1 is version
154 if (s1 == version_) {
159 } while (!contains(tmpt, ';'));
161 } else if (token == "comment") {
162 // we don't need to read any further than this.
167 << "LyXVC::scanMaster(): unexpected token"
174 void RCS::registrer(string const & msg)
176 string cmd = "ci -q -u -i -t-\"";
179 cmd += quoteName(onlyFilename(owner_->fileName()));
180 doVCCommand(cmd, owner_->filePath());
184 void RCS::checkIn(string const & msg)
186 doVCCommand("ci -q -u -m\"" + msg + "\" "
187 + quoteName(onlyFilename(owner_->fileName())),
195 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->fileName())),
202 doVCCommand("co -f -u" + version() + " "
203 + quoteName(onlyFilename(owner_->fileName())),
205 // We ignore changes and just reload!
212 lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
213 doVCCommand("rcs -o" + version() + " "
214 + quoteName(onlyFilename(owner_->fileName())),
219 void RCS::getLog(string const & tmpf)
221 doVCCommand("rlog " + quoteName(onlyFilename(owner_->fileName()))
227 CVS::CVS(string const & m, string const & f)
235 string const CVS::find_file(string const & file)
237 // First we look for the CVS/Entries in the same dir
238 // where we have file.
239 string const dir = onlyPath(file) + "/CVS/Entries";
240 string const tmpf = "/" + onlyFilename(file) + "/";
241 lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
242 << "' for `" << tmpf << '\'' << endl;
243 if (fs::is_readable(dir)) {
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.c_str());
249 while (getline(ifs, line)) {
250 lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
251 if (contains(line, tmpf)) return dir;
258 void CVS::scanMaster()
260 lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n Checking: "
262 // Ok now we do the real scan...
263 ifstream ifs(master_.c_str());
264 string tmpf = "/" + onlyFilename(file_) + "/";
265 lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
267 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
268 while (getline(ifs, line)) {
269 lyxerr[Debug::LYXVC] << "\t line: " << line << endl;
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 = fs::last_write_time(file_);
285 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
287 << "Date in Entries: `" << file_date
288 << "'\nModification date of file: `"
289 << mod_date << '\'' << endl;
290 if (file_date == mod_date) {
291 locker_ = "Unlocked";
294 // Here we should also to some more checking
295 // to see if there are conflicts or not.
305 void CVS::registrer(string const & msg)
307 doVCCommand("cvs -q add -m \"" + msg + "\" "
308 + quoteName(onlyFilename(owner_->fileName())),
313 void CVS::checkIn(string const & msg)
315 doVCCommand("cvs -q commit -m \"" + msg + "\" "
316 + quoteName(onlyFilename(owner_->fileName())),
323 // cvs update or perhaps for cvs this should be a noop
324 lyxerr << "Sorry not implemented." << endl;
330 // Reverts to the version in CVS repository and
331 // gets the updated version from the repository.
332 string const fil = quoteName(onlyFilename(owner_->fileName()));
334 doVCCommand("rm -f " + fil + "; cvs update " + fil,
342 // merge the current with the previous version
343 // in a reverse patch kind of way, so that the
344 // result is to revert the last changes.
345 lyxerr << "Sorry not implemented." << endl;
349 void CVS::getLog(string const & tmpf)
351 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->fileName()))