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