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