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/FileInfo.h"
18 #include "support/path.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/systemcall.h"
23 #include <boost/regex.hpp>
27 using namespace lyx::support;
29 #ifndef CXX_GLOBAL_CSTD
38 using boost::regex_match;
40 #ifndef USE_INCLUDED_STRING
47 int VCS::doVCCommand(string const & cmd, string const & path)
49 lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
52 int const ret = one.startscript(Systemcall::Wait, cmd);
57 RCS::RCS(string const & m)
64 string const RCS::find_file(string const & file)
67 // Check if *,v exists.
70 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
72 if (f.newFile(tmp).readable()) {
73 lyxerr[Debug::LYXVC] << "Yes " << file
74 << " is under rcs." << endl;
77 // Check if RCS/*,v exists.
78 tmp = AddName(AddPath(OnlyPath(file), "RCS"), file);
80 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
82 if (f.newFile(tmp).readable()) {
83 lyxerr[Debug::LYXVC] << "Yes " << file
84 << " it is under rcs."<< endl;
92 void RCS::retrieve(string const & file)
94 lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
95 VCS::doVCCommand("co -q -r \""
101 void RCS::scanMaster()
103 lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
105 ifstream ifs(master_.c_str());
108 bool read_enough = false;
110 while (!read_enough && ifs >> token) {
112 << "LyXVC::scanMaster: current lex text: `"
113 << token << '\'' << endl;
117 else if (token == "head") {
121 tmv = rtrim(tmv, ";");
123 lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
125 } else if (contains(token, "access")
126 || contains(token, "symbols")
127 || contains(token, "strict")) {
129 } else if (contains(token, "locks")) {
131 if (contains(token, ";")) {
132 locker_ = "Unlocked";
141 s1 = rtrim(tmpt, ";");
142 // tmp is now in the format <user>:<version>
143 s1 = split(s1, s2, ':');
144 // s2 is user, and s1 is version
145 if (s1 == version_) {
150 } while (!contains(tmpt, ";"));
152 } else if (token == "comment") {
153 // we don't need to read any further than this.
158 << "LyXVC::scanMaster(): unexpected token"
165 void RCS::registrer(string const & msg)
167 string cmd = "ci -q -u -i -t-\"";
170 cmd += OnlyFilename(owner_->fileName());
172 doVCCommand(cmd, owner_->filePath());
176 void RCS::checkIn(string const & msg)
178 doVCCommand("ci -q -u -m\"" + msg + "\" \""
179 + OnlyFilename(owner_->fileName()) + '"',
187 doVCCommand("co -q -l \""
188 + OnlyFilename(owner_->fileName()) + '"',
195 doVCCommand("co -f -u" + version() + " \""
196 + OnlyFilename(owner_->fileName()) + '"',
198 // We ignore changes and just reload!
205 lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
206 doVCCommand("rcs -o" + version() + " \""
207 + OnlyFilename(owner_->fileName()) + '"',
212 void RCS::getLog(string const & tmpf)
214 doVCCommand("rlog \""
215 + OnlyFilename(owner_->fileName()) + "\" > "
216 + tmpf, owner_->filePath());
220 CVS::CVS(string const & m, string const & f)
228 string const CVS::find_file(string const & file)
230 // First we look for the CVS/Entries in the same dir
231 // where we have file.
232 string const dir = OnlyPath(file) + "/CVS/Entries";
233 string const tmpf = "/" + OnlyFilename(file) + "/";
234 lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
235 << "' for `" << tmpf << '\'' << endl;
236 FileInfo const f(dir);
238 // Ok we are at least in a CVS dir. Parse the CVS/Entries
239 // and see if we can find this file. We do a fast and
241 ifstream ifs(dir.c_str());
243 while (getline(ifs, line)) {
244 lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
245 if (contains(line, tmpf)) return dir;
252 void CVS::scanMaster()
254 lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n Checking: "
256 // Ok now we do the real scan...
257 ifstream ifs(master_.c_str());
258 string tmpf = "/" + OnlyFilename(file_) + "/";
259 lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
261 regex reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
262 while (getline(ifs, line)) {
263 lyxerr[Debug::LYXVC] << "\t line: " << line << endl;
264 if (contains(line, tmpf)) {
265 // Ok extract the fields.
266 #ifndef USE_INCLUDED_STRING
271 regex_match(STRCONV(line), sm, reg);
273 //sm[0]; // whole matched string
275 version_ = STRCONV(sm.str(2));
276 string const file_date = STRCONV(sm.str(3));
279 //sm[5]; // tag or tagdate
281 // FIXME: must double check file is stattable/existing
282 time_t mod = fi.getModificationTime();
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 + OnlyFilename(owner_->fileName()) + '"',
311 void CVS::checkIn(string const & msg)
313 doVCCommand("cvs -q commit -m \"" + msg + "\" \""
314 + 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 = 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 \""
350 + OnlyFilename(owner_->fileName()) + "\" > " + tmpf,