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