]> git.lyx.org Git - lyx.git/blob - src/vc-backend.C
hopefully fix tex2lyx linking.
[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
29 namespace lyx {
30
31 using support::addName;
32 using support::addPath;
33 using support::contains;
34 using support::onlyFilename;
35 using support::onlyPath;
36 using support::quoteName;
37 using support::rtrim;
38 using support::split;
39 using support::Systemcall;
40
41 using boost::regex;
42 using boost::regex_match;
43 using boost::smatch;
44
45 #ifndef CXX_GLOBAL_CSTD
46 using std::asctime;
47 using std::gmtime;
48 #endif
49
50 using std::endl;
51 using std::getline;
52 using std::string;
53 using std::ifstream;
54
55 namespace fs = boost::filesystem;
56
57
58 int VCS::doVCCommand(string const & cmd, string const & path)
59 {
60         lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
61         Systemcall one;
62         support::Path p(path);
63         int const ret = one.startscript(Systemcall::Wait, cmd);
64         return ret;
65 }
66
67
68 RCS::RCS(string const & m)
69 {
70         master_ = m;
71         scanMaster();
72 }
73
74
75 string const RCS::find_file(string const & file)
76 {
77         string tmp = file;
78         // Check if *,v exists.
79         tmp += ",v";
80         lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
81                              << tmp << endl;
82         if (fs::is_readable(tmp)) {
83                 lyxerr[Debug::LYXVC] << "Yes " << file
84                                      << " is under rcs." << endl;
85                 return tmp;
86         } else {
87                 // Check if RCS/*,v exists.
88                 tmp = addName(addPath(onlyPath(file), "RCS"), file);
89                 tmp += ",v";
90                 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
91                                      << tmp << endl;
92                 if (fs::is_readable(tmp)) {
93                         lyxerr[Debug::LYXVC] << "Yes " << file
94                                              << " it is under rcs."<< endl;
95                         return tmp;
96                 }
97         }
98         return string();
99 }
100
101
102 void RCS::retrieve(string const & file)
103 {
104         lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
105         VCS::doVCCommand("co -q -r " + quoteName(file),
106                          string());
107 }
108
109
110 void RCS::scanMaster()
111 {
112         lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
113
114         ifstream ifs(master_.c_str());
115
116         string token;
117         bool read_enough = false;
118
119         while (!read_enough && ifs >> token) {
120                 lyxerr[Debug::LYXVC]
121                         << "LyXVC::scanMaster: current lex text: `"
122                         << token << '\'' << endl;
123
124                 if (token.empty())
125                         continue;
126                 else if (token == "head") {
127                         // get version here
128                         string tmv;
129                         ifs >> tmv;
130                         tmv = rtrim(tmv, ";");
131                         version_ = tmv;
132                         lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
133                                              << tmv << endl;
134                 } else if (contains(token, "access")
135                            || contains(token, "symbols")
136                            || contains(token, "strict")) {
137                         // nothing
138                 } else if (contains(token, "locks")) {
139                         // get locker here
140                         if (contains(token, ';')) {
141                                 locker_ = "Unlocked";
142                                 vcstatus = UNLOCKED;
143                                 continue;
144                         }
145                         string tmpt;
146                         string s1;
147                         string s2;
148                         do {
149                                 ifs >> tmpt;
150                                 s1 = rtrim(tmpt, ";");
151                                 // tmp is now in the format <user>:<version>
152                                 s1 = split(s1, s2, ':');
153                                 // s2 is user, and s1 is version
154                                 if (s1 == version_) {
155                                         locker_ = s2;
156                                         vcstatus = LOCKED;
157                                         break;
158                                 }
159                         } while (!contains(tmpt, ';'));
160
161                 } else if (token == "comment") {
162                         // we don't need to read any further than this.
163                         read_enough = true;
164                 } else {
165                         // unexpected
166                         lyxerr[Debug::LYXVC]
167                                 << "LyXVC::scanMaster(): unexpected token"
168                                 << endl;
169                 }
170         }
171 }
172
173
174 void RCS::registrer(string const & msg)
175 {
176         string cmd = "ci -q -u -i -t-\"";
177         cmd += msg;
178         cmd += "\" ";
179         cmd += quoteName(onlyFilename(owner_->fileName()));
180         doVCCommand(cmd, owner_->filePath());
181 }
182
183
184 void RCS::checkIn(string const & msg)
185 {
186         doVCCommand("ci -q -u -m\"" + msg + "\" "
187                     + quoteName(onlyFilename(owner_->fileName())),
188                     owner_->filePath());
189 }
190
191
192 void RCS::checkOut()
193 {
194         owner_->markClean();
195         doVCCommand("co -q -l " + quoteName(onlyFilename(owner_->fileName())),
196                     owner_->filePath());
197 }
198
199
200 void RCS::revert()
201 {
202         doVCCommand("co -f -u" + version() + " "
203                     + quoteName(onlyFilename(owner_->fileName())),
204                     owner_->filePath());
205         // We ignore changes and just reload!
206         owner_->markClean();
207 }
208
209
210 void RCS::undoLast()
211 {
212         lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
213         doVCCommand("rcs -o" + version() + " "
214                     + quoteName(onlyFilename(owner_->fileName())),
215                     owner_->filePath());
216 }
217
218
219 void RCS::getLog(string const & tmpf)
220 {
221         doVCCommand("rlog " + quoteName(onlyFilename(owner_->fileName()))
222                     + " > " + tmpf,
223                     owner_->filePath());
224 }
225
226
227 CVS::CVS(string const & m, string const & f)
228 {
229         master_ = m;
230         file_ = f;
231         scanMaster();
232 }
233
234
235 string const CVS::find_file(string const & file)
236 {
237         // First we look for the CVS/Entries in the same dir
238         // where we have file.
239         string const dir = onlyPath(file) + "/CVS/Entries";
240         string const tmpf = "/" + onlyFilename(file) + "/";
241         lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
242                              << "' for `" << tmpf << '\'' << endl;
243         if (fs::is_readable(dir)) {
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                         // FIXME: must double check file is stattable/existing
284                         time_t mod = fs::last_write_time(file_);
285                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
286                         lyxerr[Debug::LYXVC]
287                                 <<  "Date in Entries: `" << file_date
288                                 << "'\nModification date of file: `"
289                                 << mod_date << '\'' << endl;
290                         if (file_date == mod_date) {
291                                 locker_ = "Unlocked";
292                                 vcstatus = UNLOCKED;
293                         } else {
294                                 // Here we should also to some more checking
295                                 // to see if there are conflicts or not.
296                                 locker_ = "Locked";
297                                 vcstatus = LOCKED;
298                         }
299                         break;
300                 }
301         }
302 }
303
304
305 void CVS::registrer(string const & msg)
306 {
307         doVCCommand("cvs -q add -m \"" + msg + "\" "
308                     + quoteName(onlyFilename(owner_->fileName())),
309                     owner_->filePath());
310 }
311
312
313 void CVS::checkIn(string const & msg)
314 {
315         doVCCommand("cvs -q commit -m \"" + msg + "\" "
316                     + quoteName(onlyFilename(owner_->fileName())),
317                     owner_->filePath());
318 }
319
320
321 void CVS::checkOut()
322 {
323         // cvs update or perhaps for cvs this should be a noop
324         lyxerr << "Sorry not implemented." << endl;
325 }
326
327
328 void CVS::revert()
329 {
330         // Reverts to the version in CVS repository and
331         // gets the updated version from the repository.
332         string const fil = quoteName(onlyFilename(owner_->fileName()));
333
334         doVCCommand("rm -f " + fil + "; cvs update " + fil,
335                     owner_->filePath());
336         owner_->markClean();
337 }
338
339
340 void CVS::undoLast()
341 {
342         // merge the current with the previous version
343         // in a reverse patch kind of way, so that the
344         // result is to revert the last changes.
345         lyxerr << "Sorry not implemented." << endl;
346 }
347
348
349 void CVS::getLog(string const & tmpf)
350 {
351         doVCCommand("cvs log " + quoteName(onlyFilename(owner_->fileName()))
352                     + " > " + tmpf,
353                     owner_->filePath());
354 }
355
356
357 } // namespace lyx