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::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, string const & path)
61 lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
63 support::Path p(path);
64 int const ret = one.startscript(Systemcall::Wait, cmd);
69 RCS::RCS(FileName const & m)
76 FileName const RCS::find_file(FileName const & file)
78 // Check if *,v exists.
79 FileName tmp(file.absFilename() + ",v");
80 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
82 if (fs::is_readable(tmp.toFilesystemEncoding())) {
83 lyxerr[Debug::LYXVC] << "Yes " << file
84 << " is under rcs." << endl;
87 // Check if RCS/*,v exists.
88 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
89 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
91 if (fs::is_readable(tmp.toFilesystemEncoding())) {
92 lyxerr[Debug::LYXVC] << "Yes " << file
93 << " it is under rcs."<< endl;
101 void RCS::retrieve(FileName const & file)
103 lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
104 VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
109 void RCS::scanMaster()
111 lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
113 ifstream ifs(master_.toFilesystemEncoding().c_str());
116 bool read_enough = false;
118 while (!read_enough && ifs >> token) {
120 << "LyXVC::scanMaster: current lex text: `"
121 << token << '\'' << endl;
125 else if (token == "head") {
129 tmv = rtrim(tmv, ";");
131 lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
133 } else if (contains(token, "access")
134 || contains(token, "symbols")
135 || contains(token, "strict")) {
137 } else if (contains(token, "locks")) {
139 if (contains(token, ';')) {
140 locker_ = "Unlocked";
149 s1 = rtrim(tmpt, ";");
150 // tmp is now in the format <user>:<version>
151 s1 = split(s1, s2, ':');
152 // s2 is user, and s1 is version
153 if (s1 == version_) {
158 } while (!contains(tmpt, ';'));
160 } else if (token == "comment") {
161 // we don't need to read any further than this.
166 << "LyXVC::scanMaster(): unexpected token"
173 void RCS::registrer(string const & msg)
175 string cmd = "ci -q -u -i -t-\"";
178 cmd += quoteName(onlyFilename(owner_->fileName()));
179 doVCCommand(cmd, owner_->filePath());
183 void RCS::checkIn(string const & msg)
185 doVCCommand("ci -q -u -m\"" + msg + "\" "
186 + quoteName(onlyFilename(owner_->fileName())),
194 doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->fileName())),
201 doVCCommand("co -f -u" + version() + " "
202 + quoteName(onlyFilename(owner_->fileName())),
204 // We ignore changes and just reload!
211 lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
212 doVCCommand("rcs -o" + version() + " "
213 + quoteName(onlyFilename(owner_->fileName())),
218 void RCS::getLog(FileName const & tmpf)
220 doVCCommand("rlog " + quoteName(onlyFilename(owner_->fileName()))
221 + " > " + tmpf.toFilesystemEncoding(),
226 CVS::CVS(FileName const & m, FileName const & f)
234 FileName const CVS::find_file(FileName const & file)
236 // First we look for the CVS/Entries in the same dir
237 // where we have file.
238 FileName const dir(onlyPath(file.absFilename()) + "/CVS/Entries");
239 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
240 lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
241 << "' for `" << tmpf << '\'' << endl;
242 if (fs::is_readable(dir.toFilesystemEncoding())) {
243 // Ok we are at least in a CVS dir. Parse the CVS/Entries
244 // and see if we can find this file. We do a fast and
246 ifstream ifs(dir.toFilesystemEncoding().c_str());
248 while (getline(ifs, line)) {
249 lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
250 if (contains(line, tmpf))
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_.toFilesystemEncoding().c_str());
264 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
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_.toFilesystemEncoding());
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(FileName const & tmpf)
351 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->fileName()))
352 + " > " + tmpf.toFilesystemEncoding(),