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