Prepare ANNOUNCE and NEWS for rc2
[lyx.git] / src / VCBackend.cpp
blobd05e4428ac2e35b244078e0455f6548b391b6d9f
1 /**
2 * \file VCBackend.cpp
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.
9 */
11 #include <config.h>
13 #include "VCBackend.h"
14 #include "Buffer.h"
16 #include "frontends/alert.h"
18 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
22 #include "support/Path.h"
23 #include "support/Systemcall.h"
25 #include <boost/regex.hpp>
27 #include <fstream>
29 using namespace std;
30 using namespace lyx::support;
32 using boost::regex;
33 using boost::regex_match;
34 using boost::smatch;
36 namespace lyx {
39 int VCS::doVCCommandCall(string const & cmd, FileName const & path){
40 LYXERR(Debug::LYXVC, "doVCCommandCall: " << cmd);
41 Systemcall one;
42 support::PathChanger p(path);
43 return one.startscript(Systemcall::Wait, cmd);
46 int VCS::doVCCommand(string const & cmd, FileName const & path)
48 owner_->setBusy(true);
49 int const ret = doVCCommandCall(cmd, path);
50 owner_->setBusy(false);
51 if (ret)
52 frontend::Alert::error(_("Revision control error."),
53 bformat(_("Some problem occured while running the command:\n"
54 "'%1$s'."),
55 from_ascii(cmd)));
56 return ret;
60 /////////////////////////////////////////////////////////////////////
62 // RCS
64 /////////////////////////////////////////////////////////////////////
66 RCS::RCS(FileName const & m)
68 master_ = m;
69 scanMaster();
73 FileName const RCS::findFile(FileName const & file)
75 // Check if *,v exists.
76 FileName tmp(file.absFilename() + ",v");
77 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
78 if (tmp.isReadableFile()) {
79 LYXERR(Debug::LYXVC, "Yes " << file << " is under rcs.");
80 return tmp;
83 // Check if RCS/*,v exists.
84 tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
85 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under rcs: " << tmp);
86 if (tmp.isReadableFile()) {
87 LYXERR(Debug::LYXVC, "Yes " << file << " it is under rcs.");
88 return tmp;
91 return FileName();
95 void RCS::retrieve(FileName const & file)
97 LYXERR(Debug::LYXVC, "LyXVC::RCS: retrieve.\n\t" << file);
98 doVCCommandCall("co -q -r " + quoteName(file.toFilesystemEncoding()),
99 FileName());
103 void RCS::scanMaster()
105 LYXERR(Debug::LYXVC, "LyXVC::RCS: scanMaster.");
107 ifstream ifs(master_.toFilesystemEncoding().c_str());
109 string token;
110 bool read_enough = false;
112 while (!read_enough && ifs >> token) {
113 LYXERR(Debug::LYXVC, "LyXVC::scanMaster: current lex text: `"
114 << token << '\'');
116 if (token.empty())
117 continue;
118 else if (token == "head") {
119 // get version here
120 string tmv;
121 ifs >> tmv;
122 tmv = rtrim(tmv, ";");
123 version_ = tmv;
124 LYXERR(Debug::LYXVC, "LyXVC: version found to be " << tmv);
125 } else if (contains(token, "access")
126 || contains(token, "symbols")
127 || contains(token, "strict")) {
128 // nothing
129 } else if (contains(token, "locks")) {
130 // get locker here
131 if (contains(token, ';')) {
132 locker_ = "Unlocked";
133 vcstatus = UNLOCKED;
134 continue;
136 string tmpt;
137 string s1;
138 string s2;
139 do {
140 ifs >> tmpt;
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_) {
146 locker_ = s2;
147 vcstatus = LOCKED;
148 break;
150 } while (!contains(tmpt, ';'));
152 } else if (token == "comment") {
153 // we don't need to read any further than this.
154 read_enough = true;
155 } else {
156 // unexpected
157 LYXERR(Debug::LYXVC, "LyXVC::scanMaster(): unexpected token");
163 void RCS::registrer(string const & msg)
165 string cmd = "ci -q -u -i -t-\"";
166 cmd += msg;
167 cmd += "\" ";
168 cmd += quoteName(onlyFilename(owner_->absFileName()));
169 doVCCommand(cmd, FileName(owner_->filePath()));
173 string RCS::checkIn(string const & msg)
175 int ret = doVCCommand("ci -q -u -m\"" + msg + "\" "
176 + quoteName(onlyFilename(owner_->absFileName())),
177 FileName(owner_->filePath()));
178 return ret ? string() : "RCS: Proceeded";
181 bool RCS::checkInEnabled()
183 return owner_ && !owner_->isReadonly();
186 string RCS::checkOut()
188 owner_->markClean();
189 int ret = doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
190 FileName(owner_->filePath()));
191 return ret ? string() : "RCS: Proceeded";
195 bool RCS::checkOutEnabled()
197 return owner_ && owner_->isReadonly();
201 void RCS::revert()
203 doVCCommand("co -f -u" + version() + " "
204 + quoteName(onlyFilename(owner_->absFileName())),
205 FileName(owner_->filePath()));
206 // We ignore changes and just reload!
207 owner_->markClean();
211 void RCS::undoLast()
213 LYXERR(Debug::LYXVC, "LyXVC: undoLast");
214 doVCCommand("rcs -o" + version() + " "
215 + quoteName(onlyFilename(owner_->absFileName())),
216 FileName(owner_->filePath()));
220 bool RCS::undoLastEnabled()
222 return true;
226 void RCS::getLog(FileName const & tmpf)
228 doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
229 + " > " + tmpf.toFilesystemEncoding(),
230 FileName(owner_->filePath()));
234 bool RCS::toggleReadOnlyEnabled()
236 return true;
240 /////////////////////////////////////////////////////////////////////
242 // CVS
244 /////////////////////////////////////////////////////////////////////
246 CVS::CVS(FileName const & m, FileName const & f)
248 master_ = m;
249 file_ = f;
250 scanMaster();
254 FileName const CVS::findFile(FileName const & file)
256 // First we look for the CVS/Entries in the same dir
257 // where we have file.
258 FileName const entries(onlyPath(file.absFilename()) + "/CVS/Entries");
259 string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
260 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under cvs in `" << entries
261 << "' for `" << tmpf << '\'');
262 if (entries.isReadableFile()) {
263 // Ok we are at least in a CVS dir. Parse the CVS/Entries
264 // and see if we can find this file. We do a fast and
265 // dirty parse here.
266 ifstream ifs(entries.toFilesystemEncoding().c_str());
267 string line;
268 while (getline(ifs, line)) {
269 LYXERR(Debug::LYXVC, "\tEntries: " << line);
270 if (contains(line, tmpf))
271 return entries;
274 return FileName();
278 void CVS::scanMaster()
280 LYXERR(Debug::LYXVC, "LyXVC::CVS: scanMaster. \n Checking: " << master_);
281 // Ok now we do the real scan...
282 ifstream ifs(master_.toFilesystemEncoding().c_str());
283 string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
284 LYXERR(Debug::LYXVC, "\tlooking for `" << tmpf << '\'');
285 string line;
286 static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
287 while (getline(ifs, line)) {
288 LYXERR(Debug::LYXVC, "\t line: " << line);
289 if (contains(line, tmpf)) {
290 // Ok extract the fields.
291 smatch sm;
293 regex_match(line, sm, reg);
295 //sm[0]; // whole matched string
296 //sm[1]; // filename
297 version_ = sm.str(2);
298 string const file_date = sm.str(3);
300 //sm[4]; // options
301 //sm[5]; // tag or tagdate
302 // FIXME: must double check file is stattable/existing
303 time_t mod = file_.lastModified();
304 string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
305 LYXERR(Debug::LYXVC, "Date in Entries: `" << file_date
306 << "'\nModification date of file: `" << mod_date << '\'');
307 //FIXME this whole locking bussiness is not working under cvs and the machinery
308 // conforms to the ci usage, not cvs.
309 if (file_date == mod_date) {
310 locker_ = "Unlocked";
311 vcstatus = UNLOCKED;
312 } else {
313 // Here we should also to some more checking
314 // to see if there are conflicts or not.
315 locker_ = "Locked";
316 vcstatus = LOCKED;
318 break;
324 void CVS::registrer(string const & msg)
326 doVCCommand("cvs -q add -m \"" + msg + "\" "
327 + quoteName(onlyFilename(owner_->absFileName())),
328 FileName(owner_->filePath()));
332 string CVS::checkIn(string const & msg)
334 int ret = doVCCommand("cvs -q commit -m \"" + msg + "\" "
335 + quoteName(onlyFilename(owner_->absFileName())),
336 FileName(owner_->filePath()));
337 return ret ? string() : "CVS: Proceeded";
341 bool CVS::checkInEnabled()
343 return true;
347 string CVS::checkOut()
349 // cvs update or perhaps for cvs this should be a noop
350 // we need to detect conflict (eg "C" in output)
351 // before we can do this.
352 lyxerr << "Sorry not implemented." << endl;
353 return string();
357 bool CVS::checkOutEnabled()
359 return false;
363 void CVS::revert()
365 // Reverts to the version in CVS repository and
366 // gets the updated version from the repository.
367 string const fil = quoteName(onlyFilename(owner_->absFileName()));
368 // This is sensitive operation, so at lest some check about
369 // existence of cvs program and its file
370 if (doVCCommand("cvs log "+ fil, FileName(owner_->filePath())))
371 return;
372 FileName f(owner_->absFileName());
373 f.removeFile();
374 doVCCommand("cvs update " + fil,
375 FileName(owner_->filePath()));
376 owner_->markClean();
380 void CVS::undoLast()
382 // merge the current with the previous version
383 // in a reverse patch kind of way, so that the
384 // result is to revert the last changes.
385 lyxerr << "Sorry not implemented." << endl;
389 bool CVS::undoLastEnabled()
391 return false;
395 void CVS::getLog(FileName const & tmpf)
397 doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
398 + " > " + tmpf.toFilesystemEncoding(),
399 FileName(owner_->filePath()));
402 bool CVS::toggleReadOnlyEnabled()
404 return false;
407 /////////////////////////////////////////////////////////////////////
409 // SVN
411 /////////////////////////////////////////////////////////////////////
413 SVN::SVN(FileName const & m, FileName const & f)
415 master_ = m;
416 file_ = f;
417 scanMaster();
421 FileName const SVN::findFile(FileName const & file)
423 // First we look for the CVS/Entries in the same dir
424 // where we have file.
425 FileName const entries(onlyPath(file.absFilename()) + "/.svn/entries");
426 string const tmpf = onlyFilename(file.absFilename());
427 LYXERR(Debug::LYXVC, "LyXVC: Checking if file is under svn in `" << entries
428 << "' for `" << tmpf << '\'');
429 if (entries.isReadableFile()) {
430 // Ok we are at least in a CVS dir. Parse the CVS/Entries
431 // and see if we can find this file. We do a fast and
432 // dirty parse here.
433 ifstream ifs(entries.toFilesystemEncoding().c_str());
434 string line, oldline;
435 while (getline(ifs, line)) {
436 if (line == "dir" || line == "file")
437 LYXERR(Debug::LYXVC, "\tEntries: " << oldline);
438 if (oldline == tmpf && line == "file")
439 return entries;
440 oldline = line;
443 return FileName();
447 void SVN::scanMaster()
449 // if we want some locking under svn
450 // we need different infrastructure around
451 locker_ = "Unlocked";
452 vcstatus = UNLOCKED;
456 void SVN::registrer(string const & msg)
458 doVCCommand("svn add -q " + quoteName(onlyFilename(owner_->absFileName())),
459 FileName(owner_->filePath()));
463 string SVN::checkIn(string const & msg)
465 FileName tmpf = FileName::tempName("lyxvcout");
466 if (tmpf.empty()){
467 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
468 return N_("Error: Could not generate logfile.");
471 doVCCommand("svn commit -m \"" + msg + "\" "
472 + quoteName(onlyFilename(owner_->absFileName()))
473 + " > " + tmpf.toFilesystemEncoding(),
474 FileName(owner_->filePath()));
476 string log;
477 string res = scanLogFile(tmpf, log);
478 if (!res.empty())
479 frontend::Alert::error(_("Revision control error."),
480 _("Error when commiting to repository.\n"
481 "You have to manually resolve the problem.\n"
482 "After pressing OK, LyX will reopen the document."));
483 tmpf.erase();
484 return "SVN: " + log;
488 bool SVN::checkInEnabled()
490 return true;
493 // FIXME Correctly return code should be checked instead of this.
494 // This would need another solution than just plain startscript.
495 // Hint from Andre': QProcess::readAllStandardError()...
496 string SVN::scanLogFile(FileName const & f, string & status)
498 ifstream ifs(f.toFilesystemEncoding().c_str());
499 string line;
501 while (ifs) {
502 getline(ifs, line);
503 lyxerr << line << "\n";
504 if (!line.empty()) status += line + "; ";
505 if (prefixIs(line, "C ") || contains(line, "Commit failed")) {
506 ifs.close();
507 return line;
510 ifs.close();
511 return string();
515 string SVN::checkOut()
517 FileName tmpf = FileName::tempName("lyxvcout");
518 if (tmpf.empty()) {
519 LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf);
520 return N_("Error: Could not generate logfile.");
523 doVCCommand("svn update " + quoteName(onlyFilename(owner_->absFileName()))
524 + " > " + tmpf.toFilesystemEncoding(),
525 FileName(owner_->filePath()));
527 string log;
528 string res = scanLogFile(tmpf, log);
529 if (!res.empty())
530 frontend::Alert::error(_("Revision control error."),
531 bformat(_("Error when updating from repository.\n"
532 "You have to manually resolve the conflicts NOW!\n'%1$s'.\n\n"
533 "After pressing OK, LyX will try to reopen resolved document."),
534 from_ascii(res)));
535 tmpf.erase();
536 return "SVN: " + log;
540 bool SVN::checkOutEnabled()
542 return true;
546 void SVN::revert()
548 // Reverts to the version in CVS repository and
549 // gets the updated version from the repository.
550 string const fil = quoteName(onlyFilename(owner_->absFileName()));
552 doVCCommand("svn revert -q " + fil,
553 FileName(owner_->filePath()));
554 owner_->markClean();
558 void SVN::undoLast()
560 // merge the current with the previous version
561 // in a reverse patch kind of way, so that the
562 // result is to revert the last changes.
563 lyxerr << "Sorry not implemented." << endl;
567 bool SVN::undoLastEnabled()
569 return false;
573 void SVN::getLog(FileName const & tmpf)
575 doVCCommand("svn log " + quoteName(onlyFilename(owner_->absFileName()))
576 + " > " + tmpf.toFilesystemEncoding(),
577 FileName(owner_->filePath()));
581 bool SVN::toggleReadOnlyEnabled()
583 return false;
587 } // namespace lyx