]> git.lyx.org Git - lyx.git/blob - src/vc-backend.C
2fed252ac11e1c9ce9c68c961a1342c6b29ed1da
[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::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 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         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 \""
103                          + file + '"',
104                          string());
105 }
106
107
108 void RCS::scanMaster()
109 {
110         lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
111
112         ifstream ifs(master_.c_str());
113
114         string token;
115         bool read_enough = false;
116
117         while (!read_enough && ifs >> token) {
118                 lyxerr[Debug::LYXVC]
119                         << "LyXVC::scanMaster: current lex text: `"
120                         << token << '\'' << endl;
121
122                 if (token.empty())
123                         continue;
124                 else if (token == "head") {
125                         // get version here
126                         string tmv;
127                         ifs >> tmv;
128                         tmv = rtrim(tmv, ";");
129                         version_ = tmv;
130                         lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
131                                              << tmv << endl;
132                 } else if (contains(token, "access")
133                            || contains(token, "symbols")
134                            || contains(token, "strict")) {
135                         // nothing
136                 } else if (contains(token, "locks")) {
137                         // get locker here
138                         if (contains(token, ';')) {
139                                 locker_ = "Unlocked";
140                                 vcstatus = UNLOCKED;
141                                 continue;
142                         }
143                         string tmpt;
144                         string s1;
145                         string s2;
146                         do {
147                                 ifs >> tmpt;
148                                 s1 = rtrim(tmpt, ";");
149                                 // tmp is now in the format <user>:<version>
150                                 s1 = split(s1, s2, ':');
151                                 // s2 is user, and s1 is version
152                                 if (s1 == version_) {
153                                         locker_ = s2;
154                                         vcstatus = LOCKED;
155                                         break;
156                                 }
157                         } while (!contains(tmpt, ';'));
158
159                 } else if (token == "comment") {
160                         // we don't need to read any further than this.
161                         read_enough = true;
162                 } else {
163                         // unexpected
164                         lyxerr[Debug::LYXVC]
165                                 << "LyXVC::scanMaster(): unexpected token"
166                                 << endl;
167                 }
168         }
169 }
170
171
172 void RCS::registrer(string const & msg)
173 {
174         string cmd = "ci -q -u -i -t-\"";
175         cmd += msg;
176         cmd += "\" \"";
177         cmd += OnlyFilename(owner_->fileName());
178         cmd += '"';
179         doVCCommand(cmd, owner_->filePath());
180 }
181
182
183 void RCS::checkIn(string const & msg)
184 {
185         doVCCommand("ci -q -u -m\"" + msg + "\" \""
186                     + OnlyFilename(owner_->fileName()) + '"',
187                     owner_->filePath());
188 }
189
190
191 void RCS::checkOut()
192 {
193         owner_->markClean();
194         doVCCommand("co -q -l \""
195                     + OnlyFilename(owner_->fileName()) + '"',
196                     owner_->filePath());
197 }
198
199
200 void RCS::revert()
201 {
202         doVCCommand("co -f -u" + version() + " \""
203                     + 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                     + OnlyFilename(owner_->fileName()) + '"',
215                     owner_->filePath());
216 }
217
218
219 void RCS::getLog(string const & tmpf)
220 {
221         doVCCommand("rlog \""
222                     + OnlyFilename(owner_->fileName()) + "\" > "
223                     + tmpf, 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                     + 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                     + 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 = 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 \""
352                     + OnlyFilename(owner_->fileName()) + "\" > " + tmpf,
353                     owner_->filePath());
354 }