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