4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
16 #include "support/FileName.h"
28 /// A simple version control system interface
31 /// the status of the managed file
38 VCS(Buffer * b) : vcstatus_(NOLOCKING), owner_(b) {}
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;
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.
82 virtual void undoLast() = 0;
83 /// can this operation be processed in the current VCS?
84 virtual bool undoLastEnabled() = 0;
86 * getLog - read the revision log into the given file
87 * @param fname file name to read into
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;
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);
117 /// parse information from the version file
118 virtual void scanMaster() = 0;
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;
124 /// GUI container for doVCCommandCall
125 int doVCCommand(std::string const & cmd, support::FileName const & path, bool reportError = true);
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
132 static int doVCCommandCall(std::string const & cmd, support::FileName const & path);
134 /// The status of the VC controlled file.
137 /// The buffer using this VC
138 Buffer * const owner_;
143 class RCS : public VCS {
147 RCS(support::FileName const & m, Buffer * b);
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);
153 /// get file from repo, the caller must ensure that it does not exist locally
154 static bool retrieve(support::FileName const & file);
156 std::string vcname() const override { return "RCS"; };
158 void registrer(std::string const & msg) override;
160 bool renameEnabled() override;
162 std::string rename(support::FileName const &, std::string const &) override;
164 bool copyEnabled() override;
166 std::string copy(support::FileName const &, std::string const &) override;
169 checkIn(std::string const & msg, std::string & log) override;
171 bool checkInEnabled() override;
173 bool isCheckInWithConfirmation() override;
175 std::string checkOut() override;
177 bool checkOutEnabled() override;
179 std::string repoUpdate() override;
181 bool repoUpdateEnabled() override;
183 std::string lockingToggle() override;
185 bool lockingToggleEnabled() override;
187 bool revert() override;
189 bool isRevertWithConfirmation() override;
191 void undoLast() override;
193 bool undoLastEnabled() override;
195 void getLog(support::FileName const &) override;
197 std::string const versionString() const override {
198 return "RCS: " + version_;
201 bool toggleReadOnlyEnabled() override;
203 std::string revisionInfo(LyXVC::RevisionInfo const info) override;
205 bool prepareFileRevision(std::string const & rev, std::string & f) override;
207 bool prepareFileRevisionEnabled() override;
210 void scanMaster() override;
212 bool getRevisionInfo();
214 * The master VC file. For RCS this is *,v or RCS/ *,v.
215 * master should have full path.
217 support::FileName master_;
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.
223 std::string version_;
224 /// The user currently keeping the lock on the VC file (or "Unlocked").
226 /// Cache for revision info.
227 std::string rev_date_cache_;
229 std::string rev_time_cache_;
231 std::string rev_author_cache_;
236 class CVS : public VCS {
240 CVS(support::FileName const & m, Buffer * b);
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);
246 /// get file from repo, the caller must ensure that it does not exist locally
247 static bool retrieve(support::FileName const & file);
249 std::string vcname() const override { return "CVS"; };
251 void registrer(std::string const & msg) override;
253 bool renameEnabled() override;
255 std::string rename(support::FileName const &, std::string const &) override;
257 bool copyEnabled() override;
259 std::string copy(support::FileName const &, std::string const &) override;
262 checkIn(std::string const & msg, std::string & log) override;
264 bool checkInEnabled() override;
266 bool isCheckInWithConfirmation() override;
268 std::string checkOut() override;
270 bool checkOutEnabled() override;
272 std::string repoUpdate() override;
274 bool repoUpdateEnabled() override;
276 std::string lockingToggle() override;
278 bool lockingToggleEnabled() override;
280 bool isRevertWithConfirmation() override;
282 bool revert() override;
284 void undoLast() override;
286 bool undoLastEnabled() override;
288 void getLog(support::FileName const &) override;
290 /// Check for messages in cvs output.
291 /// Returns conflict line.
292 std::string scanLogFile(support::FileName const & f, std::string & status);
294 std::string const versionString() const override {
295 return "CVS: " + version_;
298 bool toggleReadOnlyEnabled() override;
300 std::string revisionInfo(LyXVC::RevisionInfo const info) override;
302 bool prepareFileRevision(std::string const & rev, std::string & f) override;
304 bool prepareFileRevisionEnabled() override;
307 void scanMaster() override;
308 /// the mode of operation for some VC commands
313 /// possible status values of file
326 * The master VC file. For CVS this is CVS/Entries
327 * master should have full path.
329 support::FileName master_;
330 // revision number from scanMaster
331 std::string version_;
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
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);
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
359 /// revert the edit operation
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;
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();
383 class SVN : public VCS {
389 /// Determine whether the file is under SVN control
390 static bool findFile(support::FileName const & file);
392 /// get file from repo, the caller must ensure that it does not exist locally
393 static bool retrieve(support::FileName const & file);
395 std::string vcname() const override { return "SVN"; };
397 void registrer(std::string const & msg) override;
399 bool renameEnabled() override;
401 std::string rename(support::FileName const &, std::string const &) override;
403 bool copyEnabled() override;
405 std::string copy(support::FileName const &, std::string const &) override;
408 checkIn(std::string const & msg, std::string & log) override;
410 bool checkInEnabled() override;
412 bool isCheckInWithConfirmation() override;
414 std::string checkOut() override;
416 bool checkOutEnabled() override;
418 std::string repoUpdate() override;
420 bool repoUpdateEnabled() override;
422 std::string lockingToggle() override;
424 bool lockingToggleEnabled() override;
426 bool revert() override;
428 bool isRevertWithConfirmation() override;
430 void undoLast() override;
432 bool undoLastEnabled() override;
434 void getLog(support::FileName const &) override;
436 std::string const versionString() const override {
437 return "SVN: " + rev_file_cache_;
440 bool toggleReadOnlyEnabled() override;
442 std::string revisionInfo(LyXVC::RevisionInfo const info) override;
444 bool prepareFileRevision(std::string const & rev, std::string & f) override;
446 bool prepareFileRevisionEnabled() override;
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);
463 /// is the loaded file under locking policy?
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.
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_;
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
493 class GIT : public VCS {
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);
503 /// get file from repo, the caller must ensure that it does not exist locally
504 static bool retrieve(support::FileName const & file);
506 std::string vcname() const override { return "GIT"; };
508 void registrer(std::string const & msg) override;
510 bool renameEnabled() override;
512 std::string rename(support::FileName const &, std::string const &) override;
514 bool copyEnabled() override;
516 std::string copy(support::FileName const &, std::string const &) override;
519 checkIn(std::string const & msg, std::string & log) override;
521 bool checkInEnabled() override;
523 bool isCheckInWithConfirmation() override;
525 std::string checkOut() override;
527 bool checkOutEnabled() override;
529 std::string repoUpdate() override;
531 bool repoUpdateEnabled() override;
533 std::string lockingToggle() override;
535 bool lockingToggleEnabled() override;
537 bool revert() override;
539 bool isRevertWithConfirmation() override;
541 void undoLast() override;
543 bool undoLastEnabled() override;
545 void getLog(support::FileName const &) override;
547 std::string const versionString() const override {
551 bool toggleReadOnlyEnabled() override;
553 std::string revisionInfo(LyXVC::RevisionInfo const info) override;
555 bool prepareFileRevision(std::string const & rev, std::string & f) override;
557 bool prepareFileRevisionEnabled() override;
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);
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.
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_;
593 #endif // VCBACKEND_H