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