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>
28 using lyx::support::addName;
29 using lyx::support::addPath;
30 using lyx::support::contains;
31 using lyx::support::onlyFilename;
32 using lyx::support::onlyPath;
33 using lyx::support::Path;
34 using lyx::support::quoteName;
35 using lyx::support::rtrim;
36 using lyx::support::split;
37 using lyx::support::Systemcall;
40 using boost::regex_match;
43 #ifndef CXX_GLOBAL_CSTD
53 namespace fs = boost::filesystem;
56 int VCS::doVCCommand(string const & cmd, string const & path)
58 lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
61 int const ret = one.startscript(Systemcall::Wait, cmd);
66 RCS::RCS(string const & m)
73 string const RCS::find_file(string const & file)
76 // Check if *,v exists.
78 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
80 if (fs::is_readable(tmp)) {
81 lyxerr[Debug::LYXVC] << "Yes " << file
82 << " is under rcs." << endl;
85 // Check if RCS/*,v exists.
86 tmp = addName(addPath(onlyPath(file), "RCS"), file);
88 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
90 if (fs::is_readable(tmp)) {
91 lyxerr[Debug::LYXVC] << "Yes " << file
92 << " it is under rcs."<< endl;
100 void RCS::retrieve(string const & file)
102 lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
103 VCS::doVCCommand("co -q -r " + quoteName(file),
108 void RCS::scanMaster()
110 lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
112 ifstream ifs(master_.c_str());
115 bool read_enough = false;
117 while (!read_enough && ifs >> token) {
119 << "LyXVC::scanMaster: current lex text: `"
120 << token << '\'' << endl;
124 else if (token == "head") {
128 tmv = rtrim(tmv, ";");
130 lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
132 } else if (contains(token, "access")
133 || contains(token, "symbols")
134 || contains(token, "strict")) {
136 } else if (contains(token, "locks")) {
138 if (contains(token, ';')) {
139 locker_ = "Unlocked";
148 s1 = rtrim(tmpt, ";");
149 // tmp is now in the format <user>:<version>
150 s1 = split(s1, s2, ':');
151 // s2 is user, and s1 is version
152 if (s1 == version_) {
157 } while (!contains(tmpt, ';'));
159 } else if (token == "comment") {
160 // we don't need to read any further than this.
165 << "LyXVC::scanMaster(): unexpected token"
172 void RCS::registrer(string const & msg)
174 string cmd = "ci -q -u -i -t-\"";
177 cmd += quoteName(onlyFilename(owner_->fileName()));
178 doVCCommand(cmd, owner_->filePath());
182 void RCS::checkIn(string const & msg)
184 doVCCommand("ci -q -u -m\"" + msg + "\" "
185 + quoteName(onlyFilename(owner_->fileName())),
193 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->fileName())),
200 doVCCommand("co -f -u" + version() + " "
201 + quoteName(onlyFilename(owner_->fileName())),
203 // We ignore changes and just reload!
210 lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
211 doVCCommand("rcs -o" + version() + " "
212 + quoteName(onlyFilename(owner_->fileName())),
217 void RCS::getLog(string const & tmpf)
219 doVCCommand("rlog " + quoteName(onlyFilename(owner_->fileName()))
225 CVS::CVS(string const & m, string const & f)
233 string const CVS::find_file(string const & file)
235 // First we look for the CVS/Entries in the same dir
236 // where we have file.
237 string const dir = onlyPath(file) + "/CVS/Entries";
238 string const tmpf = "/" + onlyFilename(file) + "/";
239 lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
240 << "' for `" << tmpf << '\'' << endl;
241 if (fs::is_readable(dir)) {
242 // Ok we are at least in a CVS dir. Parse the CVS/Entries
243 // and see if we can find this file. We do a fast and
245 ifstream ifs(dir.c_str());
247 while (getline(ifs, line)) {
248 lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
249 if (contains(line, tmpf)) return dir;
256 void CVS::scanMaster()
258 lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n Checking: "
260 // Ok now we do the real scan...
261 ifstream ifs(master_.c_str());
262 string tmpf = "/" + onlyFilename(file_) + "/";
263 lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
265 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
266 while (getline(ifs, line)) {
267 lyxerr[Debug::LYXVC] << "\t line: " << line << endl;
268 if (contains(line, tmpf)) {
269 // Ok extract the fields.
272 regex_match(line, sm, reg);
274 //sm[0]; // whole matched string
276 version_ = sm.str(2);
277 string const file_date = sm.str(3);
280 //sm[5]; // tag or tagdate
281 // FIXME: must double check file is stattable/existing
282 time_t mod = fs::last_write_time(file_);
283 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
285 << "Date in Entries: `" << file_date
286 << "'\nModification date of file: `"
287 << mod_date << '\'' << endl;
288 if (file_date == mod_date) {
289 locker_ = "Unlocked";
292 // Here we should also to some more checking
293 // to see if there are conflicts or not.
303 void CVS::registrer(string const & msg)
305 doVCCommand("cvs -q add -m \"" + msg + "\" "
306 + quoteName(onlyFilename(owner_->fileName())),
311 void CVS::checkIn(string const & msg)
313 doVCCommand("cvs -q commit -m \"" + msg + "\" "
314 + quoteName(onlyFilename(owner_->fileName())),
321 // cvs update or perhaps for cvs this should be a noop
322 lyxerr << "Sorry not implemented." << endl;
328 // Reverts to the version in CVS repository and
329 // gets the updated version from the repository.
330 string const fil = quoteName(onlyFilename(owner_->fileName()));
332 doVCCommand("rm -f " + fil + "; cvs update " + fil,
340 // merge the current with the previous version
341 // in a reverse patch kind of way, so that the
342 // result is to revert the last changes.
343 lyxerr << "Sorry not implemented." << endl;
347 void CVS::getLog(string const & tmpf)
349 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->fileName()))