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 lyx::support::AddName;
28 using lyx::support::AddPath;
29 using lyx::support::contains;
30 using lyx::support::FileInfo;
31 using lyx::support::OnlyFilename;
32 using lyx::support::OnlyPath;
33 using lyx::support::Path;
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
53 int VCS::doVCCommand(string const & cmd, string const & path)
55 lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
58 int const ret = one.startscript(Systemcall::Wait, cmd);
63 RCS::RCS(string const & m)
70 string const RCS::find_file(string const & file)
73 // Check if *,v exists.
76 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
78 if (f.newFile(tmp).readable()) {
79 lyxerr[Debug::LYXVC] << "Yes " << file
80 << " is under rcs." << endl;
83 // Check if RCS/*,v exists.
84 tmp = AddName(AddPath(OnlyPath(file), "RCS"), file);
86 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
88 if (f.newFile(tmp).readable()) {
89 lyxerr[Debug::LYXVC] << "Yes " << file
90 << " it is under rcs."<< endl;
98 void RCS::retrieve(string const & file)
100 lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
101 VCS::doVCCommand("co -q -r \""
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 += OnlyFilename(owner_->fileName());
178 doVCCommand(cmd, owner_->filePath());
182 void RCS::checkIn(string const & msg)
184 doVCCommand("ci -q -u -m\"" + msg + "\" \""
185 + OnlyFilename(owner_->fileName()) + '"',
193 doVCCommand("co -q -l \""
194 + OnlyFilename(owner_->fileName()) + '"',
201 doVCCommand("co -f -u" + version() + " \""
202 + OnlyFilename(owner_->fileName()) + '"',
204 // We ignore changes and just reload!
211 lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
212 doVCCommand("rcs -o" + version() + " \""
213 + OnlyFilename(owner_->fileName()) + '"',
218 void RCS::getLog(string const & tmpf)
220 doVCCommand("rlog \""
221 + OnlyFilename(owner_->fileName()) + "\" > "
222 + tmpf, owner_->filePath());
226 CVS::CVS(string const & m, string const & f)
234 string const CVS::find_file(string const & file)
236 // First we look for the CVS/Entries in the same dir
237 // where we have file.
238 string const dir = OnlyPath(file) + "/CVS/Entries";
239 string const tmpf = "/" + OnlyFilename(file) + "/";
240 lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
241 << "' for `" << tmpf << '\'' << endl;
242 FileInfo const f(dir);
244 // Ok we are at least in a CVS dir. Parse the CVS/Entries
245 // and see if we can find this file. We do a fast and
247 ifstream ifs(dir.c_str());
249 while (getline(ifs, line)) {
250 lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
251 if (contains(line, tmpf)) return dir;
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_.c_str());
264 string tmpf = "/" + OnlyFilename(file_) + "/";
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
284 // FIXME: must double check file is stattable/existing
285 time_t mod = fi.getModificationTime();
286 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
288 << "Date in Entries: `" << file_date
289 << "'\nModification date of file: `"
290 << mod_date << '\'' << endl;
291 if (file_date == mod_date) {
292 locker_ = "Unlocked";
295 // Here we should also to some more checking
296 // to see if there are conflicts or not.
306 void CVS::registrer(string const & msg)
308 doVCCommand("cvs -q add -m \"" + msg + "\" \""
309 + OnlyFilename(owner_->fileName()) + '"',
314 void CVS::checkIn(string const & msg)
316 doVCCommand("cvs -q commit -m \"" + msg + "\" \""
317 + OnlyFilename(owner_->fileName()) + '"',
324 // cvs update or perhaps for cvs this should be a noop
325 lyxerr << "Sorry not implemented." << endl;
331 // Reverts to the version in CVS repository and
332 // gets the updated version from the repository.
333 string const fil = OnlyFilename(owner_->fileName());
335 doVCCommand("rm -f \"" + fil + "\"; cvs update \"" + fil + '"',
343 // merge the current with the previous version
344 // in a reverse patch kind of way, so that the
345 // result is to revert the last changes.
346 lyxerr << "Sorry not implemented." << endl;
350 void CVS::getLog(string const & tmpf)
352 doVCCommand("cvs log \""
353 + OnlyFilename(owner_->fileName()) + "\" > " + tmpf,