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