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