]> git.lyx.org Git - features.git/blob - src/VCBackend.h
improve documentation
[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 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 void 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         virtual void registrer(std::string const & msg);
214
215         virtual std::string checkIn(std::string const & msg);
216
217         virtual bool checkInEnabled();
218
219         virtual bool isCheckInWithConfirmation();
220
221         virtual std::string checkOut();
222
223         virtual bool checkOutEnabled();
224
225         virtual std::string repoUpdate();
226
227         virtual bool repoUpdateEnabled();
228
229         virtual std::string lockingToggle();
230
231         virtual bool lockingToggleEnabled();
232
233         virtual bool isRevertWithConfirmation();
234
235         virtual bool revert();
236
237         virtual void undoLast();
238
239         virtual bool undoLastEnabled();
240
241         virtual void getLog(support::FileName const &);
242
243         /// Check for messages in cvs output. 
244         /// Returns conflict line.
245         std::string scanLogFile(support::FileName const & f, std::string & status);
246
247         virtual std::string const versionString() const {
248                 return "CVS: " + version_;
249         }
250
251         virtual bool toggleReadOnlyEnabled();
252
253         virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
254
255         virtual bool prepareFileRevision(std::string const & rev, std::string & f);
256
257         virtual bool prepareFileRevisionEnabled();
258
259 protected:
260         virtual void scanMaster();
261         /// the mode of operation for some VC commands
262         enum OperationMode {
263                 Directory = 0,
264                 File = 1
265         };
266         /// possible status values of file
267         enum CvsStatus {
268                 UpToDate = 0,
269                 LocallyModified = 1,
270                 LocallyAdded = 2,
271                 NeedsMerge = 3,
272                 NeedsCheckout = 4,
273                 NoCvsFile = 5,
274                 StatusError = 6
275         };
276
277 private:
278         // revision number from scanMaster
279         std::string version_;
280
281         /**
282          * doVCCommandWithOutput
283          * - call out to the version control utility
284          * - it is able to collect output in a file
285          * @param cmd the command to execute
286          * @param path the path from which to execute
287          * @param output the path where to store output
288          * @param reportError display of low level error message dialog
289          * @return exit status
290          */
291         int doVCCommandWithOutput(std::string const & cmd,
292                         support::FileName const & path,
293                         support::FileName const & output,
294                         bool reportError = true);
295         static int doVCCommandCallWithOutput(std::string const & cmd,
296                         support::FileName const & path,
297                         support::FileName const & output);
298                                                 
299         /// return the quoted pathname if Directory or filename if File
300         virtual std::string const getTarget(OperationMode opmode) const;
301         /// collect the diff of file or directory against repository
302         /// result is placed in temporary file
303         void getDiff(OperationMode opmode, support::FileName const & tmpf);
304         /// make the file ready for editing:
305         /// save a copy in CVS/Base and change file permissions to rw if needed
306         virtual int edit();
307         /// revert the edit operation
308         virtual int unedit();
309         /// retrieve repository changes into working copy
310         virtual int update(OperationMode opmode, support::FileName const & tmpf);
311         /// check readonly state for file
312         /// assume true when file is writable
313         virtual bool isLocked() const;
314         /// query and parse the cvs status of file
315         virtual CvsStatus getStatus();
316         /// convert enum to string
317         virtual docstring toString(CvsStatus status) const;
318
319         /// cache the info values of current file revision
320         /// author, date and time of commit
321         std::string rev_author_cache_;
322         std::string rev_date_cache_;
323         std::string rev_time_cache_;
324         /// fills the cache values, returns true if successfull.
325         void getRevisionInfo();
326         bool have_rev_info_;
327 };
328
329
330 ///
331 class SVN : public VCS {
332 public:
333         ///
334         explicit
335         SVN(support::FileName const & m, Buffer * b);
336
337         /// return the revision file for the given file, if found
338         static support::FileName const findFile(support::FileName const & file);
339
340         virtual void registrer(std::string const & msg);
341
342         virtual std::string checkIn(std::string const & msg);
343
344         virtual bool checkInEnabled();
345
346         virtual bool isCheckInWithConfirmation();
347
348         virtual std::string checkOut();
349
350         virtual bool checkOutEnabled();
351
352         virtual std::string repoUpdate();
353
354         virtual bool repoUpdateEnabled();
355
356         virtual std::string lockingToggle();
357
358         virtual bool lockingToggleEnabled();
359
360         virtual bool revert();
361
362         virtual bool isRevertWithConfirmation();
363
364         virtual void undoLast();
365
366         virtual bool undoLastEnabled();
367
368         virtual void getLog(support::FileName const &);
369
370         virtual std::string const versionString() const {
371                 return "SVN: " + rev_file_cache_;
372         }
373
374         virtual bool toggleReadOnlyEnabled();
375
376         virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
377
378         virtual bool prepareFileRevision(std::string const & rev, std::string & f);
379
380         virtual bool prepareFileRevisionEnabled();
381
382 protected:
383         virtual void scanMaster();
384         /// Check for messages in svn output. Returns error.
385         std::string scanLogFile(support::FileName const & f, std::string & status);
386         /// checks locking policy and setup locked_mode_
387         bool checkLockMode();
388         /// is the loaded file locked?
389         bool isLocked() const;
390         /// acquire/release write lock for the current file
391         void fileLock(bool lock, support::FileName const & tmpf, std::string & status);
392
393 private:
394         /// is the loaded file under locking policy?
395         bool locked_mode_;
396         /**
397          * Real code for obtaining file revision info. Fills all file-related caches
398          * and returns true if successfull.
399          * "?" is stored in rev_file_cache_ as a signal if request for obtaining info
400          * was already unsuccessful.
401          */
402         bool getFileRevisionInfo();
403         /// cache for file revision number, "?" if already unsuccessful, isNumber==true
404         std::string rev_file_cache_;
405         /// cache for author of last commit
406         std::string rev_author_cache_;
407         /// cache for date of last commit
408         std::string rev_date_cache_;
409         /// cache for time of last commit
410         std::string rev_time_cache_;
411         /// fills rev_tree_cache_, returns true if successfull.
412         bool getTreeRevisionInfo();
413         /// cache for tree revision number, "?" if already unsuccessful
414         std::string rev_tree_cache_;
415 };
416
417 } // namespace lyx
418
419 #endif // VCBACKEND_H