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