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