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