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::quoteName;
34 using lyx::support::rtrim;
35 using lyx::support::split;
36 using lyx::support::Systemcall;
39 using boost::regex_match;
42 #ifndef CXX_GLOBAL_CSTD
52 namespace fs = boost::filesystem;
55 int VCS::doVCCommand(string const & cmd, string const & path)
57 lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
59 lyx::support::Path p(path);
60 int const ret = one.startscript(Systemcall::Wait, cmd);
65 RCS::RCS(string const & m)
72 string const RCS::find_file(string const & file)
75 // Check if *,v exists.
77 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
79 if (fs::is_readable(tmp)) {
80 lyxerr[Debug::LYXVC] << "Yes " << file
81 << " is under rcs." << endl;
84 // Check if RCS/*,v exists.
85 tmp = addName(addPath(onlyPath(file), "RCS"), file);
87 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
89 if (fs::is_readable(tmp)) {
90 lyxerr[Debug::LYXVC] << "Yes " << file
91 << " it is under rcs."<< endl;
99 void RCS::retrieve(string const & file)
101 lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
102 VCS::doVCCommand("co -q -r " + quoteName(file),
107 void RCS::scanMaster()
109 lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
111 ifstream ifs(master_.c_str());
114 bool read_enough = false;
116 while (!read_enough && ifs >> token) {
118 << "LyXVC::scanMaster: current lex text: `"
119 << token << '\'' << endl;
123 else if (token == "head") {
127 tmv = rtrim(tmv, ";");
129 lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
131 } else if (contains(token, "access")
132 || contains(token, "symbols")
133 || contains(token, "strict")) {
135 } else if (contains(token, "locks")) {
137 if (contains(token, ';')) {
138 locker_ = "Unlocked";
147 s1 = rtrim(tmpt, ";");
148 // tmp is now in the format <user>:<version>
149 s1 = split(s1, s2, ':');
150 // s2 is user, and s1 is version
151 if (s1 == version_) {
156 } while (!contains(tmpt, ';'));
158 } else if (token == "comment") {
159 // we don't need to read any further than this.
164 << "LyXVC::scanMaster(): unexpected token"
171 void RCS::registrer(string const & msg)
173 string cmd = "ci -q -u -i -t-\"";
176 cmd += quoteName(onlyFilename(owner_->fileName()));
177 doVCCommand(cmd, owner_->filePath());
181 void RCS::checkIn(string const & msg)
183 doVCCommand("ci -q -u -m\"" + msg + "\" "
184 + quoteName(onlyFilename(owner_->fileName())),
192 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->fileName())),
199 doVCCommand("co -f -u" + version() + " "
200 + quoteName(onlyFilename(owner_->fileName())),
202 // We ignore changes and just reload!
209 lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
210 doVCCommand("rcs -o" + version() + " "
211 + quoteName(onlyFilename(owner_->fileName())),
216 void RCS::getLog(string const & tmpf)
218 doVCCommand("rlog " + quoteName(onlyFilename(owner_->fileName()))
224 CVS::CVS(string const & m, string const & f)
232 string const CVS::find_file(string const & file)
234 // First we look for the CVS/Entries in the same dir
235 // where we have file.
236 string const dir = onlyPath(file) + "/CVS/Entries";
237 string const tmpf = "/" + onlyFilename(file) + "/";
238 lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
239 << "' for `" << tmpf << '\'' << endl;
240 if (fs::is_readable(dir)) {
241 // Ok we are at least in a CVS dir. Parse the CVS/Entries
242 // and see if we can find this file. We do a fast and
244 ifstream ifs(dir.c_str());
246 while (getline(ifs, line)) {
247 lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
248 if (contains(line, tmpf)) return dir;
255 void CVS::scanMaster()
257 lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n Checking: "
259 // Ok now we do the real scan...
260 ifstream ifs(master_.c_str());
261 string tmpf = "/" + onlyFilename(file_) + "/";
262 lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
264 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
265 while (getline(ifs, line)) {
266 lyxerr[Debug::LYXVC] << "\t line: " << line << endl;
267 if (contains(line, tmpf)) {
268 // Ok extract the fields.
271 regex_match(line, sm, reg);
273 //sm[0]; // whole matched string
275 version_ = sm.str(2);
276 string const file_date = sm.str(3);
279 //sm[5]; // tag or tagdate
280 // FIXME: must double check file is stattable/existing
281 time_t mod = fs::last_write_time(file_);
282 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
284 << "Date in Entries: `" << file_date
285 << "'\nModification date of file: `"
286 << mod_date << '\'' << endl;
287 if (file_date == mod_date) {
288 locker_ = "Unlocked";
291 // Here we should also to some more checking
292 // to see if there are conflicts or not.
302 void CVS::registrer(string const & msg)
304 doVCCommand("cvs -q add -m \"" + msg + "\" "
305 + quoteName(onlyFilename(owner_->fileName())),
310 void CVS::checkIn(string const & msg)
312 doVCCommand("cvs -q commit -m \"" + msg + "\" "
313 + quoteName(onlyFilename(owner_->fileName())),
320 // cvs update or perhaps for cvs this should be a noop
321 lyxerr << "Sorry not implemented." << endl;
327 // Reverts to the version in CVS repository and
328 // gets the updated version from the repository.
329 string const fil = quoteName(onlyFilename(owner_->fileName()));
331 doVCCommand("rm -f " + fil + "; cvs update " + fil,
339 // merge the current with the previous version
340 // in a reverse patch kind of way, so that the
341 // result is to revert the last changes.
342 lyxerr << "Sorry not implemented." << endl;
346 void CVS::getLog(string const & tmpf)
348 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->fileName()))