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