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