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