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::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
52 namespace fs = boost::filesystem;
55 int VCS::doVCCommand(string const & cmd, string const & path)
57 lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
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 \""
108 void RCS::scanMaster()
110 lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
112 ifstream ifs(master_.c_str());
115 bool read_enough = false;
117 while (!read_enough && ifs >> token) {
119 << "LyXVC::scanMaster: current lex text: `"
120 << token << '\'' << endl;
124 else if (token == "head") {
128 tmv = rtrim(tmv, ";");
130 lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
132 } else if (contains(token, "access")
133 || contains(token, "symbols")
134 || contains(token, "strict")) {
136 } else if (contains(token, "locks")) {
138 if (contains(token, ';')) {
139 locker_ = "Unlocked";
148 s1 = rtrim(tmpt, ";");
149 // tmp is now in the format <user>:<version>
150 s1 = split(s1, s2, ':');
151 // s2 is user, and s1 is version
152 if (s1 == version_) {
157 } while (!contains(tmpt, ';'));
159 } else if (token == "comment") {
160 // we don't need to read any further than this.
165 << "LyXVC::scanMaster(): unexpected token"
172 void RCS::registrer(string const & msg)
174 string cmd = "ci -q -u -i -t-\"";
177 cmd += OnlyFilename(owner_->fileName());
179 doVCCommand(cmd, owner_->filePath());
183 void RCS::checkIn(string const & msg)
185 doVCCommand("ci -q -u -m\"" + msg + "\" \""
186 + OnlyFilename(owner_->fileName()) + '"',
194 doVCCommand("co -q -l \""
195 + OnlyFilename(owner_->fileName()) + '"',
202 doVCCommand("co -f -u" + version() + " \""
203 + OnlyFilename(owner_->fileName()) + '"',
205 // We ignore changes and just reload!
212 lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
213 doVCCommand("rcs -o" + version() + " \""
214 + OnlyFilename(owner_->fileName()) + '"',
219 void RCS::getLog(string const & tmpf)
221 doVCCommand("rlog \""
222 + OnlyFilename(owner_->fileName()) + "\" > "
223 + tmpf, owner_->filePath());
227 CVS::CVS(string const & m, string const & f)
235 string const CVS::find_file(string const & file)
237 // First we look for the CVS/Entries in the same dir
238 // where we have file.
239 string const dir = OnlyPath(file) + "/CVS/Entries";
240 string const tmpf = "/" + OnlyFilename(file) + "/";
241 lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
242 << "' for `" << tmpf << '\'' << endl;
243 if (fs::is_readable(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
283 // FIXME: must double check file is stattable/existing
284 time_t mod = fs::last_write_time(file_);
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,