]> git.lyx.org Git - lyx.git/blob - src/vc-backend.C
Really fix start_of_appendix output
[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::QuoteName;
35 using lyx::support::rtrim;
36 using lyx::support::split;
37 using lyx::support::Systemcall;
38
39 using boost::regex;
40 using boost::regex_match;
41 using boost::smatch;
42
43 #ifndef CXX_GLOBAL_CSTD
44 using std::asctime;
45 using std::gmtime;
46 #endif
47
48 using std::endl;
49 using std::getline;
50 using std::string;
51 using std::ifstream;
52
53 namespace fs = boost::filesystem;
54
55
56 int VCS::doVCCommand(string const & cmd, string const & path)
57 {
58         lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
59         Systemcall one;
60         Path p(path);
61         int const ret = one.startscript(Systemcall::Wait, cmd);
62         return ret;
63 }
64
65
66 RCS::RCS(string const & m)
67 {
68         master_ = m;
69         scanMaster();
70 }
71
72
73 string const RCS::find_file(string const & file)
74 {
75         string tmp(file);
76         // Check if *,v exists.
77         tmp += ",v";
78         lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
79                              << tmp << endl;
80         if (fs::is_readable(tmp)) {
81                 lyxerr[Debug::LYXVC] << "Yes " << file
82                                      << " is under rcs." << endl;
83                 return tmp;
84         } else {
85                 // Check if RCS/*,v exists.
86                 tmp = AddName(AddPath(OnlyPath(file), "RCS"), file);
87                 tmp += ",v";
88                 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
89                                      << tmp << endl;
90                 if (fs::is_readable(tmp)) {
91                         lyxerr[Debug::LYXVC] << "Yes " << file
92                                              << " it is under rcs."<< endl;
93                         return tmp;
94                 }
95         }
96         return string();
97 }
98
99
100 void RCS::retrieve(string const & file)
101 {
102         lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
103         VCS::doVCCommand("co -q -r " + QuoteName(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 += QuoteName(OnlyFilename(owner_->fileName()));
178         doVCCommand(cmd, owner_->filePath());
179 }
180
181
182 void RCS::checkIn(string const & msg)
183 {
184         doVCCommand("ci -q -u -m\"" + msg + "\" "
185                     + QuoteName(OnlyFilename(owner_->fileName())),
186                     owner_->filePath());
187 }
188
189
190 void RCS::checkOut()
191 {
192         owner_->markClean();
193         doVCCommand("co -q -l " + QuoteName(OnlyFilename(owner_->fileName())),
194                     owner_->filePath());
195 }
196
197
198 void RCS::revert()
199 {
200         doVCCommand("co -f -u" + version() + " "
201                     + QuoteName(OnlyFilename(owner_->fileName())),
202                     owner_->filePath());
203         // We ignore changes and just reload!
204         owner_->markClean();
205 }
206
207
208 void RCS::undoLast()
209 {
210         lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
211         doVCCommand("rcs -o" + version() + " "
212                     + QuoteName(OnlyFilename(owner_->fileName())),
213                     owner_->filePath());
214 }
215
216
217 void RCS::getLog(string const & tmpf)
218 {
219         doVCCommand("rlog " + QuoteName(OnlyFilename(owner_->fileName())) 
220                     + " > " + tmpf, 
221                     owner_->filePath());
222 }
223
224
225 CVS::CVS(string const & m, string const & f)
226 {
227         master_ = m;
228         file_ = f;
229         scanMaster();
230 }
231
232
233 string const CVS::find_file(string const & file)
234 {
235         // First we look for the CVS/Entries in the same dir
236         // where we have file.
237         string const dir = OnlyPath(file) + "/CVS/Entries";
238         string const tmpf = "/" + OnlyFilename(file) + "/";
239         lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
240                              << "' for `" << tmpf << '\'' << endl;
241         if (fs::is_readable(dir)) {
242                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
243                 // and see if we can find this file. We do a fast and
244                 // dirty parse here.
245                 ifstream ifs(dir.c_str());
246                 string line;
247                 while (getline(ifs, line)) {
248                         lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
249                         if (contains(line, tmpf)) return dir;
250                 }
251         }
252         return string();
253 }
254
255
256 void CVS::scanMaster()
257 {
258         lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n     Checking: "
259                              << master_ << endl;
260         // Ok now we do the real scan...
261         ifstream ifs(master_.c_str());
262         string tmpf = "/" + OnlyFilename(file_) + "/";
263         lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << '\'' << endl;
264         string line;
265         static regex const reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
266         while (getline(ifs, line)) {
267                 lyxerr[Debug::LYXVC] << "\t  line: " << line << endl;
268                 if (contains(line, tmpf)) {
269                         // Ok extract the fields.
270                         smatch sm;
271
272                         regex_match(line, sm, reg);
273
274                         //sm[0]; // whole matched string
275                         //sm[1]; // filename
276                         version_ = sm.str(2);
277                         string const file_date = sm.str(3);
278
279                         //sm[4]; // options
280                         //sm[5]; // tag or tagdate
281                         // FIXME: must double check file is stattable/existing
282                         time_t mod = fs::last_write_time(file_);
283                         string mod_date = rtrim(asctime(gmtime(&mod)), "\n");
284                         lyxerr[Debug::LYXVC]
285                                 <<  "Date in Entries: `" << file_date
286                                 << "'\nModification date of file: `"
287                                 << mod_date << '\'' << endl;
288                         if (file_date == mod_date) {
289                                 locker_ = "Unlocked";
290                                 vcstatus = UNLOCKED;
291                         } else {
292                                 // Here we should also to some more checking
293                                 // to see if there are conflicts or not.
294                                 locker_ = "Locked";
295                                 vcstatus = LOCKED;
296                         }
297                         break;
298                 }
299         }
300 }
301
302
303 void CVS::registrer(string const & msg)
304 {
305         doVCCommand("cvs -q add -m \"" + msg + "\" "
306                     + QuoteName(OnlyFilename(owner_->fileName())),
307                     owner_->filePath());
308 }
309
310
311 void CVS::checkIn(string const & msg)
312 {
313         doVCCommand("cvs -q commit -m \"" + msg + "\" "
314                     + QuoteName(OnlyFilename(owner_->fileName())),
315                     owner_->filePath());
316 }
317
318
319 void CVS::checkOut()
320 {
321         // cvs update or perhaps for cvs this should be a noop
322         lyxerr << "Sorry not implemented." << endl;
323 }
324
325
326 void CVS::revert()
327 {
328         // Reverts to the version in CVS repository and
329         // gets the updated version from the repository.
330         string const fil = QuoteName(OnlyFilename(owner_->fileName()));
331
332         doVCCommand("rm -f " + fil + "; cvs update " + fil,
333                     owner_->filePath());
334         owner_->markClean();
335 }
336
337
338 void CVS::undoLast()
339 {
340         // merge the current with the previous version
341         // in a reverse patch kind of way, so that the
342         // result is to revert the last changes.
343         lyxerr << "Sorry not implemented." << endl;
344 }
345
346
347 void CVS::getLog(string const & tmpf)
348 {
349         doVCCommand("cvs log " + QuoteName(OnlyFilename(owner_->fileName())) 
350                     + " > " + tmpf,
351                     owner_->filePath());
352 }