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