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