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