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