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