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