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"
16 #include "frontends/LyXView.h"
17 #include "funcrequest.h"
19 #include "support/FileInfo.h"
20 #include "support/path.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/systemcall.h"
25 #include <boost/regex.hpp>
29 using namespace lyx::support;
31 #ifndef CXX_GLOBAL_CSTD
40 using boost::regex_match;
42 #ifndef USE_INCLUDED_STRING
49 int VCS::doVCCommand(string const & cmd, string const & path)
51 lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
54 int const ret = one.startscript(Systemcall::Wait, cmd);
59 RCS::RCS(string const & m)
66 string const RCS::find_file(string const & file)
69 // Check if *,v exists.
72 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
74 if (f.newFile(tmp).readable()) {
75 lyxerr[Debug::LYXVC] << "Yes " << file
76 << " is under rcs." << endl;
79 // Check if RCS/*,v exists.
80 tmp = AddName(AddPath(OnlyPath(file), "RCS"), file);
82 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
84 if (f.newFile(tmp).readable()) {
85 lyxerr[Debug::LYXVC] << "Yes " << file
86 << " it is under rcs."<< endl;
94 void RCS::retrieve(string const & file)
96 lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
97 VCS::doVCCommand("co -q -r \""
103 void RCS::scanMaster()
105 lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
107 ifstream ifs(master_.c_str());
110 bool read_enough = false;
112 while (!read_enough && ifs >> token) {
114 << "LyXVC::scanMaster: current lex text: `"
115 << token << '\'' << endl;
119 else if (token == "head") {
123 tmv = rtrim(tmv, ";");
125 lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
127 } else if (contains(token, "access")
128 || contains(token, "symbols")
129 || contains(token, "strict")) {
131 } else if (contains(token, "locks")) {
133 if (contains(token, ";")) {
134 locker_ = "Unlocked";
143 s1 = rtrim(tmpt, ";");
144 // tmp is now in the format <user>:<version>
145 s1 = split(s1, s2, ':');
146 // s2 is user, and s1 is version
147 if (s1 == version_) {
152 } while (!contains(tmpt, ";"));
154 } else if (token == "comment") {
155 // we don't need to read any further than this.
160 << "LyXVC::scanMaster(): unexpected token"
167 void RCS::registrer(string const & msg)
169 string cmd = "ci -q -u -i -t-\"";
172 cmd += OnlyFilename(owner_->fileName());
174 doVCCommand(cmd, owner_->filePath());
178 void RCS::checkIn(string const & msg)
180 doVCCommand("ci -q -u -m\"" + msg + "\" \""
181 + OnlyFilename(owner_->fileName()) + '"',
189 doVCCommand("co -q -l \""
190 + OnlyFilename(owner_->fileName()) + '"',
197 doVCCommand("co -f -u" + version() + " \""
198 + OnlyFilename(owner_->fileName()) + '"',
200 // We ignore changes and just reload!
207 lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
208 doVCCommand("rcs -o" + version() + " \""
209 + OnlyFilename(owner_->fileName()) + '"',
214 void RCS::getLog(string const & tmpf)
216 doVCCommand("rlog \""
217 + OnlyFilename(owner_->fileName()) + "\" > "
218 + tmpf, owner_->filePath());
222 CVS::CVS(string const & m, string const & f)
230 string const CVS::find_file(string const & file)
232 // First we look for the CVS/Entries in the same dir
233 // where we have file.
234 string const dir = OnlyPath(file) + "/CVS/Entries";
235 string const tmpf = "/" + OnlyFilename(file) + "/";
236 lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
237 << "' for `" << tmpf << '\'' << endl;
238 FileInfo const f(dir);
240 // Ok we are at least in a CVS dir. Parse the CVS/Entries
241 // and see if we can find this file. We do a fast and
243 ifstream ifs(dir.c_str());
245 while (getline(ifs, line)) {
246 lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
247 if (contains(line, tmpf)) return dir;
254 void CVS::scanMaster()
256 lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n Checking: "
258 // Ok now we do the real scan...
259 ifstream ifs(master_.c_str());
260 string tmpf = "/" + OnlyFilename(file_) + "/";
261 lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
263 regex reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
264 while (getline(ifs, line)) {
265 lyxerr[Debug::LYXVC] << "\t line: " << line << endl;
266 if (contains(line, tmpf)) {
267 // Ok extract the fields.
268 #ifndef USE_INCLUDED_STRING
273 regex_match(STRCONV(line), sm, reg);
275 //sm[0]; // whole matched string
277 version_ = STRCONV(sm.str(2));
278 string const file_date = STRCONV(sm.str(3));
281 //sm[5]; // tag or tagdate
283 // FIXME: must double check file is stattable/existing
284 time_t mod = fi.getModificationTime();
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 + OnlyFilename(owner_->fileName()) + '"',
313 void CVS::checkIn(string const & msg)
315 doVCCommand("cvs -q commit -m \"" + msg + "\" \""
316 + 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 = 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 \""
352 + OnlyFilename(owner_->fileName()) + "\" > " + tmpf,