]> git.lyx.org Git - lyx.git/blob - src/vc-backend.C
cleanup after svn hang-up, #undef CursorShape. Should be compilable ganin now.
[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/path.h"
18 #include "support/filetools.h"
19 #include "support/fs_extras.h"
20 #include "support/lstrings.h"
21 #include "support/systemcall.h"
22
23 #include <boost/filesystem/operations.hpp>
24 #include <boost/regex.hpp>
25
26 #include <fstream>
27
28 using lyx::support::addName;
29 using lyx::support::addPath;
30 using lyx::support::contains;
31 using lyx::support::onlyFilename;
32 using lyx::support::onlyPath;
33 using lyx::support::quoteName;
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 namespace fs = boost::filesystem;
53
54
55 int VCS::doVCCommand(string const & cmd, string const & path)
56 {
57         lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
58         Systemcall one;
59         lyx::support::Path p(path);
60         int const ret = one.startscript(Systemcall::Wait, cmd);
61         return ret;
62 }
63
64
65 RCS::RCS(string const & m)
66 {
67         master_ = m;
68         scanMaster();
69 }
70
71
72 string const RCS::find_file(string const & file)
73 {
74         string tmp(file);
75         // Check if *,v exists.
76         tmp += ",v";
77         lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
78                              << tmp << endl;
79         if (fs::is_readable(tmp)) {
80                 lyxerr[Debug::LYXVC] << "Yes " << file
81                                      << " is under rcs." << endl;
82                 return tmp;
83         } else {
84                 // Check if RCS/*,v exists.
85                 tmp = addName(addPath(onlyPath(file), "RCS"), file);
86                 tmp += ",v";
87                 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
88                                      << tmp << endl;
89                 if (fs::is_readable(tmp)) {
90                         lyxerr[Debug::LYXVC] << "Yes " << file
91                                              << " it is under rcs."<< endl;
92                         return tmp;
93                 }
94         }
95         return string();
96 }
97
98
99 void RCS::retrieve(string const & file)
100 {
101         lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
102         VCS::doVCCommand("co -q -r " + quoteName(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 += quoteName(onlyFilename(owner_->fileName()));
177         doVCCommand(cmd, owner_->filePath());
178 }
179
180
181 void RCS::checkIn(string const & msg)
182 {
183         doVCCommand("ci -q -u -m\"" + msg + "\" "
184                     + quoteName(onlyFilename(owner_->fileName())),
185                     owner_->filePath());
186 }
187
188
189 void RCS::checkOut()
190 {
191         owner_->markClean();
192         doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->fileName())),
193                     owner_->filePath());
194 }
195
196
197 void RCS::revert()
198 {
199         doVCCommand("co -f -u" + version() + " "
200                     + quoteName(onlyFilename(owner_->fileName())),
201                     owner_->filePath());
202         // We ignore changes and just reload!
203         owner_->markClean();
204 }
205
206
207 void RCS::undoLast()
208 {
209         lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
210         doVCCommand("rcs -o" + version() + " "
211                     + quoteName(onlyFilename(owner_->fileName())),
212                     owner_->filePath());
213 }
214
215
216 void RCS::getLog(string const & tmpf)
217 {
218         doVCCommand("rlog " + quoteName(onlyFilename(owner_->fileName()))
219                     + " > " + tmpf,
220                     owner_->filePath());
221 }
222
223
224 CVS::CVS(string const & m, string const & f)
225 {
226         master_ = m;
227         file_ = f;
228         scanMaster();
229 }
230
231
232 string const CVS::find_file(string const & file)
233 {
234         // First we look for the CVS/Entries in the same dir
235         // where we have file.
236         string const dir = onlyPath(file) + "/CVS/Entries";
237         string const tmpf = "/" + onlyFilename(file) + "/";
238         lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
239                              << "' for `" << tmpf << '\'' << endl;
240         if (fs::is_readable(dir)) {
241                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
242                 // and see if we can find this file. We do a fast and
243                 // dirty parse here.
244                 ifstream ifs(dir.c_str());
245                 string line;
246                 while (getline(ifs, line)) {
247                         lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
248                         if (contains(line, tmpf)) return dir;
249                 }
250         }
251         return string();
252 }
253
254
255 void CVS::scanMaster()
256 {
257         lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n     Checking: "
258                              << master_ << endl;
259         // Ok now we do the real scan...
260         ifstream ifs(master_.c_str());
261         string tmpf = "/" + onlyFilename(file_) + "/";
262         lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
263         string line;
264         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
265         while (getline(ifs, line)) {
266                 lyxerr[Debug::LYXVC] << "\t  line: " << line << endl;
267                 if (contains(line, tmpf)) {
268                         // Ok extract the fields.
269                         smatch sm;
270
271                         regex_match(line, sm, reg);
272
273                         //sm[0]; // whole matched string
274                         //sm[1]; // filename
275                         version_ = sm.str(2);
276                         string const file_date = sm.str(3);
277
278                         //sm[4]; // options
279                         //sm[5]; // tag or tagdate
280                         // FIXME: must double check file is stattable/existing
281                         time_t mod = fs::last_write_time(file_);
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                     + quoteName(onlyFilename(owner_->fileName())),
306                     owner_->filePath());
307 }
308
309
310 void CVS::checkIn(string const & msg)
311 {
312         doVCCommand("cvs -q commit -m \"" + msg + "\" "
313                     + quoteName(onlyFilename(owner_->fileName())),
314                     owner_->filePath());
315 }
316
317
318 void CVS::checkOut()
319 {
320         // cvs update or perhaps for cvs this should be a noop
321         lyxerr << "Sorry not implemented." << endl;
322 }
323
324
325 void CVS::revert()
326 {
327         // Reverts to the version in CVS repository and
328         // gets the updated version from the repository.
329         string const fil = quoteName(onlyFilename(owner_->fileName()));
330
331         doVCCommand("rm -f " + fil + "; cvs update " + fil,
332                     owner_->filePath());
333         owner_->markClean();
334 }
335
336
337 void CVS::undoLast()
338 {
339         // merge the current with the previous version
340         // in a reverse patch kind of way, so that the
341         // result is to revert the last changes.
342         lyxerr << "Sorry not implemented." << endl;
343 }
344
345
346 void CVS::getLog(string const & tmpf)
347 {
348         doVCCommand("cvs log " + quoteName(onlyFilename(owner_->fileName()))
349                     + " > " + tmpf,
350                     owner_->filePath());
351 }