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