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