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