]> git.lyx.org Git - lyx.git/blob - src/VCBackend.h
Revert some recent test changes
[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 #include <vector>
20
21 #include "LyXVC.h"
22
23
24 namespace lyx {
25
26 class Buffer;
27
28 /// A simple version control system interface
29 class VCS {
30 public:
31         /// the status of the managed file
32         enum VCStatus {
33                 UNLOCKED,
34                 LOCKED,
35                 NOLOCKING,
36         };
37
38         VCS(Buffer * b) : owner_(b) {}
39         virtual ~VCS() {}
40
41         /// register a file for version control
42         virtual void registrer(std::string const & msg) = 0;
43         /// can this operation be processed in the current VCS?
44         virtual bool renameEnabled() = 0;
45         /// rename a file. Return non-empty log on success, empty log on failure.
46         virtual std::string rename(support::FileName const &, std::string const &) = 0;
47         /// can this operation be processed in the current VCS?
48         virtual bool copyEnabled() = 0;
49         /// copy a file. Return non-empty log on success, empty log on failure.
50         virtual std::string copy(support::FileName const &, std::string const &) = 0;
51         /// check in the current revision.
52         /// \p log is non-empty on success and may be empty on failure.
53         virtual LyXVC::CommandResult
54         checkIn(std::string const & msg, std::string & log) = 0;
55         /// can this operation be processed in the current VCS?
56         virtual bool checkInEnabled() = 0;
57         /// should a log message provided for next checkin?
58         virtual bool isCheckInWithConfirmation() = 0;
59         /// check out for editing, returns log
60         virtual std::string checkOut() = 0;
61         /// can this operation be processed in the current VCS?
62         virtual bool checkOutEnabled() = 0;
63         /// synchronize with repository, returns log
64         virtual std::string repoUpdate() = 0;
65         /// can this operation be processed in the current VCS?
66         virtual bool repoUpdateEnabled() = 0;
67         /// toggle locking property of the file
68         virtual std::string lockingToggle() = 0;
69         /// can this operation be processed in the current VCS?
70         virtual bool lockingToggleEnabled() = 0;
71         /// revert current edits
72         virtual bool revert() = 0;
73         /// should a confirmation before revert requested?
74         virtual bool isRevertWithConfirmation() = 0;
75         /**
76          * Merge the current with the previous version
77          * in a reverse patch kind of way, so that the
78          * result is to revert the last changes.
79          */
80         virtual void undoLast() = 0;
81         /// can this operation be processed in the current VCS?
82         virtual bool undoLastEnabled() = 0;
83         /**
84          * getLog - read the revision log into the given file
85          * @param fname file name to read into
86          */
87         virtual void getLog(support::FileName const &) = 0;
88         /// return the current version description
89         virtual std::string const versionString() const = 0;
90         /// return the owning buffer
91         Buffer * owner() const { return owner_; }
92         /// return the lock status of this file
93         VCStatus status() const { return vcstatus; }
94         /// do we need special handling for read-only toggling?
95         /// (also used for check-out operation)
96         virtual bool toggleReadOnlyEnabled() = 0;
97         /// Return revision info specified by the argument.
98         virtual std::string revisionInfo(LyXVC::RevisionInfo const info) = 0;
99         /// can this operation be processed in the current VCS?
100         virtual bool prepareFileRevision(std::string const & rev, std::string & f) = 0;
101         /// can this operation be processed in the current VCS?
102         virtual bool prepareFileRevisionEnabled() = 0;
103
104         /// Check the directory of file and all parent directories
105         /// for the existence of repository-info like .git or .svn
106         static bool checkparentdirs(support::FileName const & file, std::string const & vcsdir);
107         
108 protected:
109         /// parse information from the version file
110         virtual void scanMaster() = 0;
111
112         /// Prepare a version identifier suitable for RCS and CVS.
113         /// If needed converts last or relative number to the absolute revision.
114         bool makeRCSRevision(std::string const &version, std::string &revis) const;
115         
116         /// GUI container for doVCCommandCall
117         int doVCCommand(std::string const & cmd, support::FileName const & path, bool reportError = true);
118         /**
119          * doVCCommandCall - call out to the version control utility
120          * @param cmd the command to execute
121          * @param path the path from which to execute
122          * @return exit status
123          */
124         static int doVCCommandCall(std::string const & cmd, support::FileName const & path);
125
126         /**
127          * The master VC file. For RCS this is *,v or RCS/ *,v. master should
128          * have full path.
129          */
130         support::FileName master_;
131
132         /// The status of the VC controlled file.
133         VCStatus vcstatus;
134
135         /// The buffer using this VC
136         Buffer * const owner_;
137 };
138
139
140 ///
141 class RCS : public VCS {
142 public:
143
144         explicit
145         RCS(support::FileName const & m, Buffer * b);
146
147         /// return the revision file for the given file, if found
148         static support::FileName const findFile(support::FileName const & file);
149
150         /// get file from repo, the caller must ensure that it does not exist locally
151         static bool retrieve(support::FileName const & file);
152
153         virtual void registrer(std::string const & msg);
154
155         virtual bool renameEnabled();
156
157         virtual std::string rename(support::FileName const &, std::string const &);
158
159         virtual bool copyEnabled();
160
161         virtual std::string copy(support::FileName const &, std::string const &);
162
163         virtual LyXVC::CommandResult
164         checkIn(std::string const & msg, std::string & log);
165
166         virtual bool checkInEnabled();
167
168         virtual bool isCheckInWithConfirmation();
169
170         virtual std::string checkOut();
171
172         virtual bool checkOutEnabled();
173
174         virtual std::string repoUpdate();
175
176         virtual bool repoUpdateEnabled();
177
178         virtual std::string lockingToggle();
179
180         virtual bool lockingToggleEnabled();
181
182         virtual bool revert();
183
184         virtual bool isRevertWithConfirmation();
185
186         virtual void undoLast();
187
188         virtual bool undoLastEnabled();
189
190         virtual void getLog(support::FileName const &);
191
192         virtual std::string const versionString() const {
193                 return "RCS: " + version_;
194         }
195
196         virtual bool toggleReadOnlyEnabled();
197
198         virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
199
200         virtual bool prepareFileRevision(std::string const & rev, std::string & f);
201
202         virtual bool prepareFileRevisionEnabled();
203
204 protected:
205         virtual void scanMaster();
206 private:
207         bool getRevisionInfo();
208         /**
209          * The version of the VC file. I am not sure if this can be a
210          * string or if it must be a float/int.
211          */
212         std::string version_;
213         /// The user currently keeping the lock on the VC file (or "Unlocked").
214         std::string locker_;
215         /// Cache for revision info.
216         std::string rev_date_cache_;
217         ///
218         std::string rev_time_cache_;
219         ///
220         std::string rev_author_cache_;
221 };
222
223
224 ///
225 class CVS : public VCS {
226 public:
227         ///
228         explicit
229         CVS(support::FileName const & m, Buffer * b);
230
231         /// return the revision file for the given file, if found
232         static support::FileName const findFile(support::FileName const & file);
233
234         /// get file from repo, the caller must ensure that it does not exist locally
235         static bool retrieve(support::FileName const & file);
236
237         virtual void registrer(std::string const & msg);
238
239         virtual bool renameEnabled();
240
241         virtual std::string rename(support::FileName const &, std::string const &);
242
243         virtual bool copyEnabled();
244
245         virtual std::string copy(support::FileName const &, std::string const &);
246
247         virtual LyXVC::CommandResult
248         checkIn(std::string const & msg, std::string & log);
249
250         virtual bool checkInEnabled();
251
252         virtual bool isCheckInWithConfirmation();
253
254         virtual std::string checkOut();
255
256         virtual bool checkOutEnabled();
257
258         virtual std::string repoUpdate();
259
260         virtual bool repoUpdateEnabled();
261
262         virtual std::string lockingToggle();
263
264         virtual bool lockingToggleEnabled();
265
266         virtual bool isRevertWithConfirmation();
267
268         virtual bool revert();
269
270         virtual void undoLast();
271
272         virtual bool undoLastEnabled();
273
274         virtual void getLog(support::FileName const &);
275
276         /// Check for messages in cvs output. 
277         /// Returns conflict line.
278         std::string scanLogFile(support::FileName const & f, std::string & status);
279
280         virtual std::string const versionString() const {
281                 return "CVS: " + version_;
282         }
283
284         virtual bool toggleReadOnlyEnabled();
285
286         virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
287
288         virtual bool prepareFileRevision(std::string const & rev, std::string & f);
289
290         virtual bool prepareFileRevisionEnabled();
291
292 protected:
293         virtual void scanMaster();
294         /// the mode of operation for some VC commands
295         enum OperationMode {
296                 Directory = 0,
297                 File = 1
298         };
299         /// possible status values of file
300         enum CvsStatus {
301                 UpToDate = 0,
302                 LocallyModified = 1,
303                 LocallyAdded = 2,
304                 NeedsMerge = 3,
305                 NeedsCheckout = 4,
306                 NoCvsFile = 5,
307                 StatusError = 6
308         };
309
310 private:
311         // revision number from scanMaster
312         std::string version_;
313
314         /**
315          * doVCCommandWithOutput
316          * - call out to the version control utility
317          * - it is able to collect output in a file
318          * @param cmd the command to execute
319          * @param path the path from which to execute
320          * @param output the path where to store output
321          * @param reportError display of low level error message dialog
322          * @return exit status
323          */
324         int doVCCommandWithOutput(std::string const & cmd,
325                         support::FileName const & path,
326                         support::FileName const & output,
327                         bool reportError = true);
328         static int doVCCommandCallWithOutput(std::string const & cmd,
329                         support::FileName const & path,
330                         support::FileName const & output);
331                                                 
332         /// return the quoted pathname if Directory or filename if File
333         virtual std::string const getTarget(OperationMode opmode) const;
334         /// collect the diff of file or directory against repository
335         /// result is placed in temporary file
336         void getDiff(OperationMode opmode, support::FileName const & tmpf);
337         /// make the file ready for editing:
338         /// save a copy in CVS/Base and change file permissions to rw if needed
339         virtual int edit();
340         /// revert the edit operation
341         virtual int unedit();
342         /// retrieve repository changes into working copy
343         virtual int update(OperationMode opmode, support::FileName const & tmpf);
344         /// check readonly state for file
345         /// assume true when file is writable
346         virtual bool isLocked() const;
347         /// query and parse the cvs status of file
348         virtual CvsStatus getStatus();
349         /// convert enum to string
350         virtual docstring toString(CvsStatus status) const;
351
352         /// cache the info values of current file revision
353         /// author, date and time of commit
354         std::string rev_author_cache_;
355         std::string rev_date_cache_;
356         std::string rev_time_cache_;
357         /// fills the cache values, returns true if successfull.
358         void getRevisionInfo();
359         bool have_rev_info_;
360 };
361
362
363 ///
364 class SVN : public VCS {
365 public:
366         ///
367         explicit
368         SVN(support::FileName const & m, Buffer * b);
369
370         /// return the revision file for the given file, if found
371         static support::FileName const findFile(support::FileName const & file);
372
373         /// get file from repo, the caller must ensure that it does not exist locally
374         static bool retrieve(support::FileName const & file);
375
376         virtual void registrer(std::string const & msg);
377
378         virtual bool renameEnabled();
379
380         virtual std::string rename(support::FileName const &, std::string const &);
381
382         virtual bool copyEnabled();
383
384         virtual std::string copy(support::FileName const &, std::string const &);
385
386         virtual LyXVC::CommandResult
387         checkIn(std::string const & msg, std::string & log);
388
389         virtual bool checkInEnabled();
390
391         virtual bool isCheckInWithConfirmation();
392
393         virtual std::string checkOut();
394
395         virtual bool checkOutEnabled();
396
397         virtual std::string repoUpdate();
398
399         virtual bool repoUpdateEnabled();
400
401         virtual std::string lockingToggle();
402
403         virtual bool lockingToggleEnabled();
404
405         virtual bool revert();
406
407         virtual bool isRevertWithConfirmation();
408
409         virtual void undoLast();
410
411         virtual bool undoLastEnabled();
412
413         virtual void getLog(support::FileName const &);
414
415         virtual std::string const versionString() const {
416                 return "SVN: " + rev_file_cache_;
417         }
418
419         virtual bool toggleReadOnlyEnabled();
420
421         virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
422
423         virtual bool prepareFileRevision(std::string const & rev, std::string & f);
424
425         virtual bool prepareFileRevisionEnabled();
426
427 protected:
428         virtual void scanMaster();
429         /// Check for messages in svn output. Returns error.
430         std::string scanLogFile(support::FileName const & f, std::string & status);
431         /// checks locking policy and setup locked_mode_
432         bool checkLockMode();
433         /// is the loaded file locked?
434         bool isLocked() const;
435         /// acquire/release write lock for the current file
436         bool fileLock(bool lock, support::FileName const & tmpf, std::string & status);
437         /// Check in files \p f with log \p msg
438         LyXVC::CommandResult checkIn(std::vector<support::FileName> const & f,
439                                      std::string const & msg, std::string & log);
440
441 private:
442         /// is the loaded file under locking policy?
443         bool locked_mode_;
444         /**
445          * Real code for obtaining file revision info. Fills all file-related caches
446          * and returns true if successfull.
447          * "?" is stored in rev_file_cache_ as a signal if request for obtaining info
448          * was already unsuccessful.
449          */
450         bool getFileRevisionInfo();
451         /// cache for file revision number, "?" if already unsuccessful, isNumber==true
452         std::string rev_file_cache_;
453         /// cache for author of last commit
454         std::string rev_author_cache_;
455         /// cache for date of last commit
456         std::string rev_date_cache_;
457         /// cache for time of last commit
458         std::string rev_time_cache_;
459         /// fills rev_tree_cache_, returns true if successfull.
460         bool getTreeRevisionInfo();
461         /// cache for tree revision number, "?" if already unsuccessful
462         std::string rev_tree_cache_;
463 };
464
465
466 /**
467  * Very basic git support:
468  * Remote repos are completely ignored, only the local tree is considered.
469  * How push and pull could be integrated with the LyX VCS interface needs
470  * to be discussed.
471  */
472 class GIT : public VCS {
473 public:
474         ///
475         explicit
476         GIT(support::FileName const & m, Buffer * b);
477
478         /// return the revision file for the given file, if found
479         static support::FileName const findFile(support::FileName const & file);
480
481         /// get file from repo, the caller must ensure that it does not exist locally
482         static bool retrieve(support::FileName const & file);
483
484         virtual void registrer(std::string const & msg);
485
486         virtual bool renameEnabled();
487
488         virtual std::string rename(support::FileName const &, std::string const &);
489
490         virtual bool copyEnabled();
491
492         virtual std::string copy(support::FileName const &, std::string const &);
493
494         virtual LyXVC::CommandResult
495         checkIn(std::string const & msg, std::string & log);
496
497         virtual bool checkInEnabled();
498
499         virtual bool isCheckInWithConfirmation();
500
501         virtual std::string checkOut();
502
503         virtual bool checkOutEnabled();
504
505         virtual std::string repoUpdate();
506
507         virtual bool repoUpdateEnabled();
508
509         virtual std::string lockingToggle();
510
511         virtual bool lockingToggleEnabled();
512
513         virtual bool revert();
514
515         virtual bool isRevertWithConfirmation();
516
517         virtual void undoLast();
518
519         virtual bool undoLastEnabled();
520
521         virtual void getLog(support::FileName const &);
522
523         virtual std::string const versionString() const {
524                 return "GIT: ?";
525         }
526
527         virtual bool toggleReadOnlyEnabled();
528
529         virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
530
531         virtual bool prepareFileRevision(std::string const & rev, std::string & f);
532
533         virtual bool prepareFileRevisionEnabled();
534
535 protected:
536         virtual void scanMaster();
537         /// Check for messages in svn output. Returns error.
538         std::string scanLogFile(support::FileName const & f, std::string & status);
539         /// Check in files \p f with log \p msg
540         LyXVC::CommandResult checkIn(std::vector<support::FileName> const & f,
541                                      std::string const & msg, std::string & log);
542
543 private:
544         /**
545          * Real code for obtaining file revision info. Fills all file-related caches
546          * and returns true if successfull.
547          * "?" is stored in rev_file_cache_ as a signal if request for obtaining info
548          * was already unsuccessful.
549          */
550         bool getFileRevisionInfo();
551         /// cache for file revision number, "?" if already unsuccessful, isNumber==true
552         std::string rev_file_cache_;
553         /// cache for author of last commit
554         std::string rev_author_cache_;
555         /// cache for date of last commit
556         std::string rev_date_cache_;
557         /// cache for time of last commit
558         std::string rev_time_cache_;
559         /// fills rev_tree_cache_, returns true if successfull.
560         bool getTreeRevisionInfo();
561         /// cache for tree revision number, "?" if already unsuccessful
562         std::string rev_tree_cache_;
563 };
564
565 } // namespace lyx
566
567 #endif // VCBACKEND_H