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