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