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