]> git.lyx.org Git - lyx.git/blob - src/vc-backend.C
small missing bit from reload fix
[lyx.git] / src / vc-backend.C
1 #include <config.h>
2
3 #include "vc-backend.h"
4 #include "debug.h"
5 #include "buffer.h"
6 #include "BufferView.h"
7 #include "frontends/LyXView.h"
8 #include "funcrequest.h"
9
10 #include "support/FileInfo.h"
11 #include "support/path.h"
12 #include "support/filetools.h"
13 #include "support/lstrings.h"
14 #include "support/systemcall.h"
15
16 #include <boost/regex.hpp>
17
18 #include <fstream>
19
20 #ifndef CXX_GLOBAL_CSTD
21 using std::asctime;
22 using std::gmtime;
23 #endif
24
25 using std::endl;
26 using std::ifstream;
27 using std::getline;
28 using boost::regex;
29 using boost::regex_match;
30
31 #ifndef USE_INCLUDED_STRING
32 using boost::smatch;
33 #else
34 using boost::cmatch;
35 #endif
36
37
38 int VCS::doVCCommand(string const & cmd, string const & path)
39 {
40         lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
41         Systemcall one;
42         Path p(path);
43         int const ret = one.startscript(Systemcall::Wait, cmd);
44         return ret;
45 }
46
47
48 void VCS::reload()
49 {
50         owner_->getUser()->reload();
51         /* Watch out ! We have deleted ourselves here
52          * via the ->reload() !
53          */
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         reload();
174 }
175
176
177 void RCS::checkIn(string const & msg)
178 {
179         doVCCommand("ci -q -u -m\"" + msg + "\" \""
180                     + OnlyFilename(owner_->fileName()) + '"',
181                     owner_->filePath());
182         reload();
183 }
184
185
186 void RCS::checkOut()
187 {
188         owner_->markClean();
189         doVCCommand("co -q -l \""
190                     + OnlyFilename(owner_->fileName()) + '"',
191                     owner_->filePath());
192         reload();
193 }
194
195
196 void RCS::revert()
197 {
198         doVCCommand("co -f -u" + version() + " \""
199                     + OnlyFilename(owner_->fileName()) + '"',
200                     owner_->filePath());
201         // We ignore changes and just reload!
202         owner_->markClean();
203         reload();
204 }
205
206
207 void RCS::undoLast()
208 {
209         lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
210         doVCCommand("rcs -o" + version() + " \""
211                     + OnlyFilename(owner_->fileName()) + '"',
212                     owner_->filePath());
213 }
214
215
216 void RCS::getLog(string const & tmpf)
217 {
218         doVCCommand("rlog \""
219                     + OnlyFilename(owner_->fileName()) + "\" > "
220                     + tmpf, owner_->filePath());
221 }
222
223
224 CVS::CVS(string const & m, string const & f)
225 {
226         master_ = m;
227         file_ = f;
228         scanMaster();
229 }
230
231
232 string const CVS::find_file(string const & file)
233 {
234         // First we look for the CVS/Entries in the same dir
235         // where we have file.
236         string const dir = OnlyPath(file) + "/CVS/Entries";
237         string const tmpf = "/" + OnlyFilename(file) + "/";
238         lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
239                              << "' for `" << tmpf << '\'' << endl;
240         FileInfo const f(dir);
241         if (f.readable()) {
242                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
243                 // and see if we can find this file. We do a fast and
244                 // dirty parse here.
245                 ifstream ifs(dir.c_str());
246                 string line;
247                 while (getline(ifs, line)) {
248                         lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
249                         if (contains(line, tmpf)) return dir;
250                 }
251         }
252         return string();
253 }
254
255
256 void CVS::scanMaster()
257 {
258         lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n     Checking: "
259                              << master_ << endl;
260         // Ok now we do the real scan...
261         ifstream ifs(master_.c_str());
262         string tmpf = "/" + OnlyFilename(file_) + "/";
263         lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
264         string line;
265         regex reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
266         while (getline(ifs, line)) {
267                 lyxerr[Debug::LYXVC] << "\t  line: " << line << endl;
268                 if (contains(line, tmpf)) {
269                         // Ok extract the fields.
270 #ifndef USE_INCLUDED_STRING
271                         smatch sm;
272 #else
273                         cmatch sm;
274 #endif
275                         regex_match(STRCONV(line), sm, reg);
276
277                         //sm[0]; // whole matched string
278                         //sm[1]; // filename
279                         version_ = STRCONV(sm.str(2));
280                         string const file_date = STRCONV(sm.str(3));
281
282                         //sm[4]; // options
283                         //sm[5]; // tag or tagdate
284                         FileInfo fi(file_);
285                         // FIXME: must double check file is stattable/existing
286                         time_t mod = fi.getModificationTime();
287                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
288                         lyxerr[Debug::LYXVC]
289                                 <<  "Date in Entries: `" << file_date
290                                 << "'\nModification date of file: `"
291                                 << mod_date << '\'' << endl;
292                         if (file_date == mod_date) {
293                                 locker_ = "Unlocked";
294                                 vcstatus = UNLOCKED;
295                         } else {
296                                 // Here we should also to some more checking
297                                 // to see if there are conflicts or not.
298                                 locker_ = "Locked";
299                                 vcstatus = LOCKED;
300                         }
301                         break;
302                 }
303         }
304 }
305
306
307 void CVS::registrer(string const & msg)
308 {
309         doVCCommand("cvs -q add -m \"" + msg + "\" \""
310                     + OnlyFilename(owner_->fileName()) + '"',
311                     owner_->filePath());
312         reload();
313 }
314
315
316 void CVS::checkIn(string const & msg)
317 {
318         doVCCommand("cvs -q commit -m \"" + msg + "\" \""
319                     + OnlyFilename(owner_->fileName()) + '"',
320                     owner_->filePath());
321         reload();
322 }
323
324
325 void CVS::checkOut()
326 {
327         // cvs update or perhaps for cvs this should be a noop
328         lyxerr << "Sorry not implemented." << endl;
329 }
330
331
332 void CVS::revert()
333 {
334         // Reverts to the version in CVS repository and
335         // gets the updated version from the repository.
336         string const fil = OnlyFilename(owner_->fileName());
337
338         doVCCommand("rm -f \"" + fil + "\"; cvs update \"" + fil + '"',
339                     owner_->filePath());
340         owner_->markClean();
341         reload();
342 }
343
344
345 void CVS::undoLast()
346 {
347         // merge the current with the previous version
348         // in a reverse patch kind of way, so that the
349         // result is to revert the last changes.
350         lyxerr << "Sorry not implemented." << endl;
351 }
352
353
354 void CVS::getLog(string const & tmpf)
355 {
356         doVCCommand("cvs log \""
357                     + OnlyFilename(owner_->fileName()) + "\" > " + tmpf,
358                     owner_->filePath());
359 }