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