]> git.lyx.org Git - lyx.git/blob - src/VCBackend.h
Implement extractFromVC() for CVS and SVN.
[lyx.git] / src / VCBackend.h
1 // -*- C++ -*-
2 /**
3  * \file VCBackend.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Pavel Sanda
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef VC_BACKEND_H
14 #define VC_BACKEND_H
15
16 #include "support/FileName.h"
17
18 #include <string>
19
20 #include "LyXVC.h"
21
22
23 namespace lyx {
24
25 class Buffer;
26
27 /// A simple version control system interface
28 class VCS {
29 public:
30         /// the status of the managed file
31         enum VCStatus {
32                 UNLOCKED,
33                 LOCKED,
34                 NOLOCKING
35         };
36
37         VCS(Buffer * b) : owner_(b) {}
38         virtual ~VCS() {}
39
40         /// register a file for version control
41         virtual void registrer(std::string const & msg) = 0;
42         /// check in the current revision, returns log
43         virtual std::string checkIn(std::string const & msg) = 0;
44         /// can this operation be processed in the current VCS?
45         virtual bool checkInEnabled() = 0;
46         /// should a log message provided for next checkin?
47         virtual bool isCheckInWithConfirmation() = 0;
48         /// check out for editing, returns log
49         virtual std::string checkOut() = 0;
50         /// can this operation be processed in the current VCS?
51         virtual bool checkOutEnabled() = 0;
52         /// synchronize with repository, returns log
53         virtual std::string repoUpdate() = 0;
54         /// can this operation be processed in the current VCS?
55         virtual bool repoUpdateEnabled() = 0;
56         /// toggle locking property of the file
57         virtual std::string lockingToggle() = 0;
58         /// can this operation be processed in the current VCS?
59         virtual bool lockingToggleEnabled() = 0;
60         /// revert current edits
61         virtual bool revert() = 0;
62         /// should a confirmation before revert requested?
63         virtual bool isRevertWithConfirmation() = 0;
64         /**
65          * Merge the current with the previous version
66          * in a reverse patch kind of way, so that the
67          * result is to revert the last changes.
68          */
69         virtual void undoLast() = 0;
70         /// can this operation be processed in the current VCS?
71         virtual bool undoLastEnabled() = 0;
72         /**
73          * getLog - read the revision log into the given file
74          * @param fname file name to read into
75          */
76         virtual void getLog(support::FileName const &) = 0;
77         /// return the current version description
78         virtual std::string const versionString() const = 0;
79         /// return the owning buffer
80         Buffer * owner() const { return owner_; }
81         /// return the lock status of this file
82         VCStatus status() const { return vcstatus; }
83         /// do we need special handling for read-only toggling?
84         /// (also used for check-out operation)
85         virtual bool toggleReadOnlyEnabled() = 0;
86         /// Return revision info specified by the argument.
87         virtual std::string revisionInfo(LyXVC::RevisionInfo const info) = 0;
88         /// can this operation be processed in the current VCS?
89         virtual bool prepareFileRevision(std::string const & rev, std::string & f) = 0;
90         /// can this operation be processed in the current VCS?
91         virtual bool prepareFileRevisionEnabled() = 0;
92
93         /// Check the directory of file and all parent directories
94         /// for the existence of the given pathname
95         static bool checkparentdirs(support::FileName const & file, std::string const & pathname);
96         
97 protected:
98         /// parse information from the version file
99         virtual void scanMaster() = 0;
100
101         /// Prepare a version identifier suitable for RCS and CVS.
102         /// If needed converts last or relative number to the absolute revision.
103         bool makeRCSRevision(std::string const &version, std::string &revis) const;
104         
105         /// GUI container for doVCCommandCall
106         int doVCCommand(std::string const & cmd, support::FileName const & path, bool reportError = true);
107         /**
108          * doVCCommandCall - call out to the version control utility
109          * @param cmd the command to execute
110          * @param path the path from which to execute
111          * @return exit status
112          */
113         static int doVCCommandCall(std::string const & cmd, support::FileName const & path);
114
115         /**
116          * The master VC file. For RCS this is *,v or RCS/ *,v. master should
117          * have full path.
118          */
119         support::FileName master_;
120
121         /// The status of the VC controlled file.
122         VCStatus vcstatus;
123
124         /// The buffer using this VC
125         Buffer * const owner_;
126 };
127
128
129 ///
130 class RCS : public VCS {
131 public:
132
133         explicit
134         RCS(support::FileName const & m, Buffer * b);
135
136         /// return the revision file for the given file, if found
137         static support::FileName const findFile(support::FileName const & file);
138
139         static bool retrieve(support::FileName const & file);
140
141         virtual void registrer(std::string const & msg);
142
143         virtual std::string checkIn(std::string const & msg);
144
145         virtual bool checkInEnabled();
146
147         virtual bool isCheckInWithConfirmation();
148
149         virtual std::string checkOut();
150
151         virtual bool checkOutEnabled();
152
153         virtual std::string repoUpdate();
154
155         virtual bool repoUpdateEnabled();
156
157         virtual std::string lockingToggle();
158
159         virtual bool lockingToggleEnabled();
160
161         virtual bool revert();
162
163         virtual bool isRevertWithConfirmation();
164
165         virtual void undoLast();
166
167         virtual bool undoLastEnabled();
168
169         virtual void getLog(support::FileName const &);
170
171         virtual std::string const versionString() const {
172                 return "RCS: " + version_;
173         }
174
175         virtual bool toggleReadOnlyEnabled();
176
177         virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
178
179         virtual bool prepareFileRevision(std::string const & rev, std::string & f);
180
181         virtual bool prepareFileRevisionEnabled();
182
183 protected:
184         virtual void scanMaster();
185 private:
186         bool getRevisionInfo();
187         /**
188          * The version of the VC file. I am not sure if this can be a
189          * string or if it must be a float/int.
190          */
191         std::string version_;
192         /// The user currently keeping the lock on the VC file (or "Unlocked").
193         std::string locker_;
194         /// Cache for revision info.
195         std::string rev_date_cache_;
196         ///
197         std::string rev_time_cache_;
198         ///
199         std::string rev_author_cache_;
200 };
201
202
203 ///
204 class CVS : public VCS {
205 public:
206         ///
207         explicit
208         CVS(support::FileName const & m, Buffer * b);
209
210         /// return the revision file for the given file, if found
211         static support::FileName const findFile(support::FileName const & file);
212
213         static bool retrieve(support::FileName const & file);
214
215         virtual void registrer(std::string const & msg);
216
217         virtual std::string checkIn(std::string const & msg);
218
219         virtual bool checkInEnabled();
220
221         virtual bool isCheckInWithConfirmation();
222
223         virtual std::string checkOut();
224
225         virtual bool checkOutEnabled();
226
227         virtual std::string repoUpdate();
228
229         virtual bool repoUpdateEnabled();
230
231         virtual std::string lockingToggle();
232
233         virtual bool lockingToggleEnabled();
234
235         virtual bool isRevertWithConfirmation();
236
237         virtual bool revert();
238
239         virtual void undoLast();
240
241         virtual bool undoLastEnabled();
242
243         virtual void getLog(support::FileName const &);
244
245         /// Check for messages in cvs output. 
246         /// Returns conflict line.
247         std::string scanLogFile(support::FileName const & f, std::string & status);
248
249         virtual std::string const versionString() const {
250                 return "CVS: " + version_;
251         }
252
253         virtual bool toggleReadOnlyEnabled();
254
255         virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
256
257         virtual bool prepareFileRevision(std::string const & rev, std::string & f);
258
259         virtual bool prepareFileRevisionEnabled();
260
261 protected:
262         virtual void scanMaster();
263         /// the mode of operation for some VC commands
264         enum OperationMode {
265                 Directory = 0,
266                 File = 1
267         };
268         /// possible status values of file
269         enum CvsStatus {
270                 UpToDate = 0,
271                 LocallyModified = 1,
272                 LocallyAdded = 2,
273                 NeedsMerge = 3,
274                 NeedsCheckout = 4,
275                 NoCvsFile = 5,
276                 StatusError = 6
277         };
278
279 private:
280         // revision number from scanMaster
281         std::string version_;
282
283         /**
284          * doVCCommandWithOutput
285          * - call out to the version control utility
286          * - it is able to collect output in a file
287          * @param cmd the command to execute
288          * @param path the path from which to execute
289          * @param output the path where to store output
290          * @param reportError display of low level error message dialog
291          * @return exit status
292          */
293         int doVCCommandWithOutput(std::string const & cmd,
294                         support::FileName const & path,
295                         support::FileName const & output,
296                         bool reportError = true);
297         static int doVCCommandCallWithOutput(std::string const & cmd,
298                         support::FileName const & path,
299                         support::FileName const & output);
300                                                 
301         /// return the quoted pathname if Directory or filename if File
302         virtual std::string const getTarget(OperationMode opmode) const;
303         /// collect the diff of file or directory against repository
304         /// result is placed in temporary file
305         void getDiff(OperationMode opmode, support::FileName const & tmpf);
306         /// make the file ready for editing:
307         /// save a copy in CVS/Base and change file permissions to rw if needed
308         virtual int edit();
309         /// revert the edit operation
310         virtual int unedit();
311         /// retrieve repository changes into working copy
312         virtual int update(OperationMode opmode, support::FileName const & tmpf);
313         /// check readonly state for file
314         /// assume true when file is writable
315         virtual bool isLocked() const;
316         /// query and parse the cvs status of file
317         virtual CvsStatus getStatus();
318         /// convert enum to string
319         virtual docstring toString(CvsStatus status) const;
320
321         /// cache the info values of current file revision
322         /// author, date and time of commit
323         std::string rev_author_cache_;
324         std::string rev_date_cache_;
325         std::string rev_time_cache_;
326         /// fills the cache values, returns true if successfull.
327         void getRevisionInfo();
328         bool have_rev_info_;
329 };
330
331
332 ///
333 class SVN : public VCS {
334 public:
335         ///
336         explicit
337         SVN(support::FileName const & m, Buffer * b);
338
339         /// return the revision file for the given file, if found
340         static support::FileName const findFile(support::FileName const & file);
341
342         static bool retrieve(support::FileName const & file);
343
344         virtual void registrer(std::string const & msg);
345
346         virtual std::string checkIn(std::string const & msg);
347
348         virtual bool checkInEnabled();
349
350         virtual bool isCheckInWithConfirmation();
351
352         virtual std::string checkOut();
353
354         virtual bool checkOutEnabled();
355
356         virtual std::string repoUpdate();
357
358         virtual bool repoUpdateEnabled();
359
360         virtual std::string lockingToggle();
361
362         virtual bool lockingToggleEnabled();
363
364         virtual bool revert();
365
366         virtual bool isRevertWithConfirmation();
367
368         virtual void undoLast();
369
370         virtual bool undoLastEnabled();
371
372         virtual void getLog(support::FileName const &);
373
374         virtual std::string const versionString() const {
375                 return "SVN: " + rev_file_cache_;
376         }
377
378         virtual bool toggleReadOnlyEnabled();
379
380         virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
381
382         virtual bool prepareFileRevision(std::string const & rev, std::string & f);
383
384         virtual bool prepareFileRevisionEnabled();
385
386 protected:
387         virtual void scanMaster();
388         /// Check for messages in svn output. Returns error.
389         std::string scanLogFile(support::FileName const & f, std::string & status);
390         /// checks locking policy and setup locked_mode_
391         bool checkLockMode();
392         /// is the loaded file locked?
393         bool isLocked() const;
394         /// acquire/release write lock for the current file
395         void fileLock(bool lock, support::FileName const & tmpf, std::string & status);
396
397 private:
398         /// is the loaded file under locking policy?
399         bool locked_mode_;
400         /**
401          * Real code for obtaining file revision info. Fills all file-related caches
402          * and returns true if successfull.
403          * "?" is stored in rev_file_cache_ as a signal if request for obtaining info
404          * was already unsuccessful.
405          */
406         bool getFileRevisionInfo();
407         /// cache for file revision number, "?" if already unsuccessful, isNumber==true
408         std::string rev_file_cache_;
409         /// cache for author of last commit
410         std::string rev_author_cache_;
411         /// cache for date of last commit
412         std::string rev_date_cache_;
413         /// cache for time of last commit
414         std::string rev_time_cache_;
415         /// fills rev_tree_cache_, returns true if successfull.
416         bool getTreeRevisionInfo();
417         /// cache for tree revision number, "?" if already unsuccessful
418         std::string rev_tree_cache_;
419 };
420
421 } // namespace lyx
422
423 #endif // VCBACKEND_H