]> git.lyx.org Git - lyx.git/blob - src/VCBackend.cpp
Push latest Andre's changes toward their true direction:
[lyx.git] / src / VCBackend.cpp
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.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "VCBackend.h"
14 #include "debug.h"
15 #include "Buffer.h"
16
17 #include "support/Path.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/Systemcall.h"
21
22 #include <boost/regex.hpp>
23
24 #include <fstream>
25
26
27 namespace lyx {
28
29 using support::addName;
30 using support::addPath;
31 using support::contains;
32 using support::FileName;
33 using support::onlyFilename;
34 using support::onlyPath;
35 using support::quoteName;
36 using support::rtrim;
37 using support::split;
38 using support::Systemcall;
39
40 using boost::regex;
41 using boost::regex_match;
42 using boost::smatch;
43
44 #ifndef CXX_GLOBAL_CSTD
45 using std::asctime;
46 using std::gmtime;
47 #endif
48
49 using std::endl;
50 using std::getline;
51 using std::string;
52 using std::ifstream;
53
54
55 int VCS::doVCCommand(string const & cmd, FileName const & path)
56 {
57         LYXERR(Debug::LYXVC) << "doVCCommand: " << cmd << endl;
58         Systemcall one;
59         support::PathChanger p(path);
60         int const ret = one.startscript(Systemcall::Wait, cmd);
61         return ret;
62 }
63
64
65 /////////////////////////////////////////////////////////////////////
66 //
67 // RCS
68 //
69 /////////////////////////////////////////////////////////////////////
70
71 RCS::RCS(FileName const & m)
72 {
73         master_ = m;
74         scanMaster();
75 }
76
77
78 FileName const RCS::find_file(FileName const & file)
79 {
80         // Check if *,v exists.
81         FileName tmp(file.absFilename() + ",v");
82         LYXERR(Debug::LYXVC) << "Checking if file is under rcs: "
83                              << tmp << endl;
84         if (tmp.isReadable()) {
85                 LYXERR(Debug::LYXVC) << "Yes " << file
86                                      << " is under rcs." << endl;
87                 return tmp;
88         }
89
90         // Check if RCS/*,v exists.
91         tmp = FileName(addName(addPath(onlyPath(file.absFilename()), "RCS"), file.absFilename()) + ",v");
92         LYXERR(Debug::LYXVC) << "Checking if file is under rcs: "
93                                          << tmp << endl;
94         if (tmp.isReadable()) {
95                 LYXERR(Debug::LYXVC) << "Yes " << file
96                                                  << " it is under rcs."<< endl;
97                 return tmp;
98         }
99
100         return FileName();
101 }
102
103
104 void RCS::retrieve(FileName const & file)
105 {
106         LYXERR(Debug::LYXVC) << "LyXVC::RCS: retrieve.\n\t" << file << endl;
107         VCS::doVCCommand("co -q -r " + quoteName(file.toFilesystemEncoding()),
108                          FileName());
109 }
110
111
112 void RCS::scanMaster()
113 {
114         LYXERR(Debug::LYXVC) << "LyXVC::RCS: scanMaster." << endl;
115
116         ifstream ifs(master_.toFilesystemEncoding().c_str());
117
118         string token;
119         bool read_enough = false;
120
121         while (!read_enough && ifs >> token) {
122                 LYXERR(Debug::LYXVC)
123                         << "LyXVC::scanMaster: current lex text: `"
124                         << token << '\'' << endl;
125
126                 if (token.empty())
127                         continue;
128                 else if (token == "head") {
129                         // get version here
130                         string tmv;
131                         ifs >> tmv;
132                         tmv = rtrim(tmv, ";");
133                         version_ = tmv;
134                         LYXERR(Debug::LYXVC) << "LyXVC: version found to be "
135                                              << tmv << endl;
136                 } else if (contains(token, "access")
137                            || contains(token, "symbols")
138                            || contains(token, "strict")) {
139                         // nothing
140                 } else if (contains(token, "locks")) {
141                         // get locker here
142                         if (contains(token, ';')) {
143                                 locker_ = "Unlocked";
144                                 vcstatus = UNLOCKED;
145                                 continue;
146                         }
147                         string tmpt;
148                         string s1;
149                         string s2;
150                         do {
151                                 ifs >> tmpt;
152                                 s1 = rtrim(tmpt, ";");
153                                 // tmp is now in the format <user>:<version>
154                                 s1 = split(s1, s2, ':');
155                                 // s2 is user, and s1 is version
156                                 if (s1 == version_) {
157                                         locker_ = s2;
158                                         vcstatus = LOCKED;
159                                         break;
160                                 }
161                         } while (!contains(tmpt, ';'));
162
163                 } else if (token == "comment") {
164                         // we don't need to read any further than this.
165                         read_enough = true;
166                 } else {
167                         // unexpected
168                         LYXERR(Debug::LYXVC)
169                                 << "LyXVC::scanMaster(): unexpected token"
170                                 << endl;
171                 }
172         }
173 }
174
175
176 void RCS::registrer(string const & msg)
177 {
178         string cmd = "ci -q -u -i -t-\"";
179         cmd += msg;
180         cmd += "\" ";
181         cmd += quoteName(onlyFilename(owner_->absFileName()));
182         doVCCommand(cmd, FileName(owner_->filePath()));
183 }
184
185
186 void RCS::checkIn(string const & msg)
187 {
188         doVCCommand("ci -q -u -m\"" + msg + "\" "
189                     + quoteName(onlyFilename(owner_->absFileName())),
190                     FileName(owner_->filePath()));
191 }
192
193
194 void RCS::checkOut()
195 {
196         owner_->markClean();
197         doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->absFileName())),
198                     FileName(owner_->filePath()));
199 }
200
201
202 void RCS::revert()
203 {
204         doVCCommand("co -f -u" + version() + " "
205                     + quoteName(onlyFilename(owner_->absFileName())),
206                     FileName(owner_->filePath()));
207         // We ignore changes and just reload!
208         owner_->markClean();
209 }
210
211
212 void RCS::undoLast()
213 {
214         LYXERR(Debug::LYXVC) << "LyXVC: undoLast" << endl;
215         doVCCommand("rcs -o" + version() + " "
216                     + quoteName(onlyFilename(owner_->absFileName())),
217                     FileName(owner_->filePath()));
218 }
219
220
221 void RCS::getLog(FileName const & tmpf)
222 {
223         doVCCommand("rlog " + quoteName(onlyFilename(owner_->absFileName()))
224                     + " > " + tmpf.toFilesystemEncoding(),
225                     FileName(owner_->filePath()));
226 }
227
228
229 /////////////////////////////////////////////////////////////////////
230 //
231 // CVS
232 //
233 /////////////////////////////////////////////////////////////////////
234
235 CVS::CVS(FileName const & m, FileName const & f)
236 {
237         master_ = m;
238         file_ = f;
239         scanMaster();
240 }
241
242
243 FileName const CVS::find_file(FileName const & file)
244 {
245         // First we look for the CVS/Entries in the same dir
246         // where we have file.
247         FileName const dir(onlyPath(file.absFilename()) + "/CVS/Entries");
248         string const tmpf = '/' + onlyFilename(file.absFilename()) + '/';
249         LYXERR(Debug::LYXVC) << "LyXVC: checking in `" << dir
250                              << "' for `" << tmpf << '\'' << endl;
251         if (dir.isReadable()) {
252                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
253                 // and see if we can find this file. We do a fast and
254                 // dirty parse here.
255                 ifstream ifs(dir.toFilesystemEncoding().c_str());
256                 string line;
257                 while (getline(ifs, line)) {
258                         LYXERR(Debug::LYXVC) << "\tEntries: " << line << endl;
259                         if (contains(line, tmpf))
260                                 return dir;
261                 }
262         }
263         return FileName();
264 }
265
266
267 void CVS::scanMaster()
268 {
269         LYXERR(Debug::LYXVC) << "LyXVC::CVS: scanMaster. \n     Checking: "
270                              << master_ << endl;
271         // Ok now we do the real scan...
272         ifstream ifs(master_.toFilesystemEncoding().c_str());
273         string tmpf = '/' + onlyFilename(file_.absFilename()) + '/';
274         LYXERR(Debug::LYXVC) << "\tlooking for `" << tmpf << '\'' << endl;
275         string line;
276         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
277         while (getline(ifs, line)) {
278                 LYXERR(Debug::LYXVC) << "\t  line: " << line << endl;
279                 if (contains(line, tmpf)) {
280                         // Ok extract the fields.
281                         smatch sm;
282
283                         regex_match(line, sm, reg);
284
285                         //sm[0]; // whole matched string
286                         //sm[1]; // filename
287                         version_ = sm.str(2);
288                         string const file_date = sm.str(3);
289
290                         //sm[4]; // options
291                         //sm[5]; // tag or tagdate
292                         // FIXME: must double check file is stattable/existing
293                         time_t mod = file_.lastModified();
294                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
295                         LYXERR(Debug::LYXVC)
296                                 <<  "Date in Entries: `" << file_date
297                                 << "'\nModification date of file: `"
298                                 << mod_date << '\'' << endl;
299                         if (file_date == mod_date) {
300                                 locker_ = "Unlocked";
301                                 vcstatus = UNLOCKED;
302                         } else {
303                                 // Here we should also to some more checking
304                                 // to see if there are conflicts or not.
305                                 locker_ = "Locked";
306                                 vcstatus = LOCKED;
307                         }
308                         break;
309                 }
310         }
311 }
312
313
314 void CVS::registrer(string const & msg)
315 {
316         doVCCommand("cvs -q add -m \"" + msg + "\" "
317                     + quoteName(onlyFilename(owner_->absFileName())),
318                     FileName(owner_->filePath()));
319 }
320
321
322 void CVS::checkIn(string const & msg)
323 {
324         doVCCommand("cvs -q commit -m \"" + msg + "\" "
325                     + quoteName(onlyFilename(owner_->absFileName())),
326                     FileName(owner_->filePath()));
327 }
328
329
330 void CVS::checkOut()
331 {
332         // cvs update or perhaps for cvs this should be a noop
333         lyxerr << "Sorry not implemented." << endl;
334 }
335
336
337 void CVS::revert()
338 {
339         // Reverts to the version in CVS repository and
340         // gets the updated version from the repository.
341         string const fil = quoteName(onlyFilename(owner_->absFileName()));
342
343         doVCCommand("rm -f " + fil + "; cvs update " + fil,
344                     FileName(owner_->filePath()));
345         owner_->markClean();
346 }
347
348
349 void CVS::undoLast()
350 {
351         // merge the current with the previous version
352         // in a reverse patch kind of way, so that the
353         // result is to revert the last changes.
354         lyxerr << "Sorry not implemented." << endl;
355 }
356
357
358 void CVS::getLog(FileName const & tmpf)
359 {
360         doVCCommand("cvs log " + quoteName(onlyFilename(owner_->absFileName()))
361                     + " > " + tmpf.toFilesystemEncoding(),
362                     FileName(owner_->filePath()));
363 }
364
365
366 } // namespace lyx