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