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