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