This commit was manufactured by cvs2svn to create tag 'lyx-1_2_1'.
[lyx.git] / src / vc-backend.C
blob61c59fc3b3a5ff0c52a8e306603444253d3be09a
1 #include <config.h>
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
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"
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 #include "support/systemcall.h"
22 #include <fstream>
24 #ifndef CXX_GLOBAL_CSTD
25 using std::asctime;
26 using std::gmtime;
27 #endif
29 using std::endl;
30 using std::ifstream;
31 using std::getline;
33 int VCS::doVCCommand(string const & cmd, string const & path)
35         lyxerr[Debug::LYXVC] << "doVCCommand: " << cmd << endl;
36         Systemcall one;
37         Path p(path);
38         int const ret = one.startscript(Systemcall::Wait, cmd);
39         return ret;
43 RCS::RCS(string const & m)
45         master_ = m;
46         scanMaster();
50 string const RCS::find_file(string const & file)
52         string tmp(file);
53         // Check if *,v exists.
54         tmp += ",v";
55         FileInfo f;
56         lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
57                              << tmp << endl;
58         if (f.newFile(tmp).readable()) {
59                 lyxerr[Debug::LYXVC] << "Yes " << file
60                                      << " is under rcs." << endl;
61                 return tmp;
62         } else {
63                 // Check if RCS/*,v exists.
64                 tmp = AddName(AddPath(OnlyPath(file), "RCS"), file);
65                 tmp += ",v";
66                 lyxerr[Debug::LYXVC] << "Checking if file is under rcs: "
67                                      << tmp << endl;
68                 if (f.newFile(tmp).readable()) {
69                         lyxerr[Debug::LYXVC] << "Yes " << file
70                                              << " it is under rcs."<< endl;
71                         return tmp;
72                 }
73         }
74         return string();
78 void RCS::retrieve(string const & file)
80         lyxerr[Debug::LYXVC] << "LyXVC::RCS: retrieve.\n\t" << file << endl;
81         VCS::doVCCommand("co -q -r \""
82                          + file + "\"",
83                          string());
87 void RCS::scanMaster()
89         lyxerr[Debug::LYXVC] << "LyXVC::RCS: scanMaster." << endl;
91         ifstream ifs(master_.c_str());
93         string token;
94         bool read_enough = false;
96         while (!read_enough && ifs >> token) {
97                 lyxerr[Debug::LYXVC]
98                         << "LyXVC::scanMaster: current lex text: `"
99                         << token << "'" << endl;
101                 if (token.empty())
102                         continue;
103                 else if (token == "head") {
104                         // get version here
105                         string tmv;
106                         ifs >> tmv;
107                         tmv = strip(tmv, ';');
108                         version_ = tmv;
109                         lyxerr[Debug::LYXVC] << "LyXVC: version found to be "
110                                              << tmv << endl;
111                 } else if (contains(token, "access")
112                            || contains(token, "symbols")
113                            || contains(token, "strict")) {
114                         // nothing
115                 } else if (contains(token, "locks")) {
116                         // get locker here
117                         if (contains(token, ";")) {
118                                 locker_ = "Unlocked";
119                                 vcstatus = UNLOCKED;
120                                 continue;
121                         }
122                         string tmpt;
123                         string s1;
124                         string s2;
125                         do {
126                                 ifs >> tmpt;
127                                 s1 = strip(tmpt, ';');
128                                 // tmp is now in the format <user>:<version>
129                                 s1 = split(s1, s2, ':');
130                                 // s2 is user, and s1 is version
131                                 if (s1 == version_) {
132                                         locker_ = s2;
133                                         vcstatus = LOCKED;
134                                         break;
135                                 }
136                         } while (!contains(tmpt, ";"));
138                 } else if (token == "comment") {
139                         // we don't need to read any further than this.
140                         read_enough = true;
141                 } else {
142                         // unexpected
143                         lyxerr[Debug::LYXVC]
144                                 << "LyXVC::scanMaster(): unexpected token"
145                                 << endl;
146                 }
147         }
151 void RCS::registrer(string const & msg)
153         string cmd = "ci -q -u -i -t-\"";
154         cmd += msg;
155         cmd += "\" \"";
156         cmd += OnlyFilename(owner_->fileName());
157         cmd += "\"";
158         doVCCommand(cmd, owner_->filePath());
159         owner_->getUser()->owner()->getLyXFunc()->dispatch(LFUN_MENURELOAD);
163 void RCS::checkIn(string const & msg)
165         doVCCommand("ci -q -u -m\"" + msg + "\" \""
166                     + OnlyFilename(owner_->fileName()) + "\"", owner_->filePath());
167         owner_->getUser()->owner()->getLyXFunc()->dispatch(LFUN_MENURELOAD);
171 void RCS::checkOut()
173         owner_->markLyxClean();
174         doVCCommand("co -q -l \""
175                     + OnlyFilename(owner_->fileName()) + "\"", owner_->filePath());
176         owner_->getUser()->owner()->getLyXFunc()->dispatch(LFUN_MENURELOAD);
180 void RCS::revert()
182         doVCCommand("co -f -u" + version() + " \""
183                     + OnlyFilename(owner_->fileName()) + "\"", owner_->filePath());
184         // We ignore changes and just reload!
185         owner_->markLyxClean();
186         owner_->getUser()->owner()
187                 ->getLyXFunc()->dispatch(LFUN_MENURELOAD);
191 void RCS::undoLast()
193         lyxerr[Debug::LYXVC] << "LyXVC: undoLast" << endl;
194         doVCCommand("rcs -o" + version() + " \""
195                     + OnlyFilename(owner_->fileName()) + "\"",
196                     owner_->filePath());
200 void RCS::getLog(string const & tmpf)
202         doVCCommand("rlog \""
203                     + OnlyFilename(owner_->fileName()) + "\" > " + tmpf, owner_->filePath());
207 CVS::CVS(string const & m, string const & f)
209         master_ = m;
210         file_ = f;
211         scanMaster();
215 string const CVS::find_file(string const & file)
217         // First we look for the CVS/Entries in the same dir
218         // where we have file.
219         string const dir = OnlyPath(file) + "/CVS/Entries";
220         string const tmpf = "/" + OnlyFilename(file) + "/";
221         lyxerr[Debug::LYXVC] << "LyXVC: checking in `" << dir
222                              << "' for `" << tmpf << "'" << endl;
223         FileInfo const f(dir);
224         if (f.readable()) {
225                 // Ok we are at least in a CVS dir. Parse the CVS/Entries
226                 // and see if we can find this file. We do a fast and
227                 // dirty parse here.
228                 ifstream ifs(dir.c_str());
229                 string line;
230                 while (getline(ifs, line)) {
231                         lyxerr[Debug::LYXVC] << "\tEntries: " << line << endl;
232                         if (contains(line, tmpf)) return dir;
233                 }
234         }
235         return string();
239 void CVS::scanMaster()
241         lyxerr[Debug::LYXVC] << "LyXVC::CVS: scanMaster. \n     Checking: "
242                              << master_ << endl;
243         // Ok now we do the real scan...
244         ifstream ifs(master_.c_str());
245         string tmpf = "/" + OnlyFilename(file_) + "/";
246         lyxerr[Debug::LYXVC] << "\tlooking for `" << tmpf << "'" << endl;
247         string line;
248         LRegex reg("/(.*)/(.*)/(.*)/(.*)/(.*)");
249         while (getline(ifs, line)) {
250                 lyxerr[Debug::LYXVC] << "\t  line: " << line << endl;
251                 if (contains(line, tmpf)) {
252                         // Ok extract the fields.
253                         LRegex::SubMatches const & sm = reg.exec(line);
254                         //sm[0]; // whole matched string
255                         //sm[1]; // filename
256                         version_ = LSubstring(line, sm[2].first,
257                                               sm[2].second);
258                         string file_date = LSubstring(line, sm[3].first,
259                                                       sm[3].second);
260                         //sm[4]; // options
261                         //sm[5]; // tag or tagdate
262                         FileInfo fi(file_);
263                         // FIXME: must double check file is stattable/existing
264                         time_t mod = fi.getModificationTime();
265                         string mod_date = strip(asctime(gmtime(&mod)), '\n');
266                         lyxerr[Debug::LYXVC]
267                                 <<  "Date in Entries: `" << file_date
268                                 << "'\nModification date of file: `"
269                                 << mod_date << "'" << endl;
270                         if (file_date == mod_date) {
271                                 locker_ = "Unlocked";
272                                 vcstatus = UNLOCKED;
273                         } else {
274                                 // Here we should also to some more checking
275                                 // to see if there are conflicts or not.
276                                 locker_ = "Locked";
277                                 vcstatus = LOCKED;
278                         }
279                         break;
280                 }
281         }
285 void CVS::registrer(string const & msg)
287         doVCCommand("cvs -q add -m \"" + msg + "\" \""
288                     + OnlyFilename(owner_->fileName()) + "\"", owner_->filePath());
289         owner_->getUser()->owner()->getLyXFunc()->dispatch(LFUN_MENURELOAD);
293 void CVS::checkIn(string const & msg)
295         doVCCommand("cvs -q commit -m \"" + msg + "\" \""
296                     + OnlyFilename(owner_->fileName()) + "\"",
297                     owner_->filePath());
298         owner_->getUser()->owner()->getLyXFunc()->dispatch(LFUN_MENURELOAD);
302 void CVS::checkOut()
304         // cvs update or perhaps for cvs this should be a noop
305         lyxerr << "Sorry not implemented." << endl;
309 void CVS::revert()
311         // Reverts to the version in CVS repository and
312         // gets the updated version from the repository.
313         string const fil = OnlyFilename(owner_->fileName());
315         doVCCommand("rm -f \"" + fil + "\"; cvs update \"" + fil + "\"",
316                     owner_->filePath());
317         owner_->markLyxClean();
318         owner_->getUser()->owner()
319                 ->getLyXFunc()->dispatch(LFUN_MENURELOAD);
323 void CVS::undoLast()
325         // merge the current with the previous version
326         // in a reverse patch kind of way, so that the
327         // result is to revert the last changes.
328         lyxerr << "Sorry not implemented." << endl;
332 void CVS::getLog(string const & tmpf)
334         doVCCommand("cvs log \""
335                     + OnlyFilename(owner_->fileName()) + "\" > " + tmpf,
336                     owner_->filePath());