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 of file and all parent directories
107 /// for the existence of repository-info like .git or .svn
108 static bool checkparentdirs(support::FileName const & file, std::string const & vcsdir);
111 /// parse information from the version file
112 virtual void scanMaster() = 0;
114 /// Prepare a version identifier suitable for RCS and CVS.
115 /// If needed converts last or relative number to the absolute revision.
116 bool makeRCSRevision(std::string const &version, std::string &revis) const;
118 /// GUI container for doVCCommandCall
119 int doVCCommand(std::string const & cmd, support::FileName const & path, bool reportError = true);
121 * doVCCommandCall - call out to the version control utility
122 * @param cmd the command to execute
123 * @param path the path from which to execute
124 * @return exit status
126 static int doVCCommandCall(std::string const & cmd, support::FileName const & path);
129 * The master VC file. For RCS this is *,v or RCS/ *,v. master should
132 support::FileName master_;
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 /// return the revision file for the given file, if found
150 static support::FileName const findFile(support::FileName const & file);
152 /// get file from repo, the caller must ensure that it does not exist locally
153 static bool retrieve(support::FileName const & file);
155 std::string vcname() const override { return "RCS"; };
157 void registrer(std::string const & msg) override;
159 bool renameEnabled() override;
161 std::string rename(support::FileName const &, std::string const &) override;
163 bool copyEnabled() override;
165 std::string copy(support::FileName const &, std::string const &) override;
168 checkIn(std::string const & msg, std::string & log) override;
170 bool checkInEnabled() override;
172 bool isCheckInWithConfirmation() override;
174 std::string checkOut() override;
176 bool checkOutEnabled() override;
178 std::string repoUpdate() override;
180 bool repoUpdateEnabled() override;
182 std::string lockingToggle() override;
184 bool lockingToggleEnabled() override;
186 bool revert() override;
188 bool isRevertWithConfirmation() override;
190 void undoLast() override;
192 bool undoLastEnabled() override;
194 void getLog(support::FileName const &) override;
196 std::string const versionString() const override {
197 return "RCS: " + version_;
200 bool toggleReadOnlyEnabled() override;
202 std::string revisionInfo(LyXVC::RevisionInfo const info) override;
204 bool prepareFileRevision(std::string const & rev, std::string & f) override;
206 bool prepareFileRevisionEnabled() override;
209 void scanMaster() override;
211 bool getRevisionInfo();
213 * The version of the VC file. I am not sure if this can be a
214 * string or if it must be a float/int.
216 std::string version_;
217 /// The user currently keeping the lock on the VC file (or "Unlocked").
219 /// Cache for revision info.
220 std::string rev_date_cache_;
222 std::string rev_time_cache_;
224 std::string rev_author_cache_;
229 class CVS : public VCS {
233 CVS(support::FileName const & m, Buffer * b);
235 /// return the revision file for the given file, if found
236 static support::FileName const findFile(support::FileName const & file);
238 /// get file from repo, the caller must ensure that it does not exist locally
239 static bool retrieve(support::FileName const & file);
241 std::string vcname() const override { return "CVS"; };
243 void registrer(std::string const & msg) override;
245 bool renameEnabled() override;
247 std::string rename(support::FileName const &, std::string const &) override;
249 bool copyEnabled() override;
251 std::string copy(support::FileName const &, std::string const &) override;
254 checkIn(std::string const & msg, std::string & log) override;
256 bool checkInEnabled() override;
258 bool isCheckInWithConfirmation() override;
260 std::string checkOut() override;
262 bool checkOutEnabled() override;
264 std::string repoUpdate() override;
266 bool repoUpdateEnabled() override;
268 std::string lockingToggle() override;
270 bool lockingToggleEnabled() override;
272 bool isRevertWithConfirmation() override;
274 bool revert() override;
276 void undoLast() override;
278 bool undoLastEnabled() override;
280 void getLog(support::FileName const &) override;
282 /// Check for messages in cvs output.
283 /// Returns conflict line.
284 std::string scanLogFile(support::FileName const & f, std::string & status);
286 std::string const versionString() const override {
287 return "CVS: " + version_;
290 bool toggleReadOnlyEnabled() override;
292 std::string revisionInfo(LyXVC::RevisionInfo const info) override;
294 bool prepareFileRevision(std::string const & rev, std::string & f) override;
296 bool prepareFileRevisionEnabled() override;
299 void scanMaster() override;
300 /// the mode of operation for some VC commands
305 /// possible status values of file
317 // revision number from scanMaster
318 std::string version_;
321 * doVCCommandWithOutput
322 * - call out to the version control utility
323 * - it is able to collect output in a file
324 * @param cmd the command to execute
325 * @param path the path from which to execute
326 * @param output the path where to store output
327 * @param reportError display of low level error message dialog
328 * @return exit status
330 int doVCCommandWithOutput(std::string const & cmd,
331 support::FileName const & path,
332 support::FileName const & output,
333 bool reportError = true);
334 static int doVCCommandCallWithOutput(std::string const & cmd,
335 support::FileName const & path,
336 support::FileName const & output);
338 /// return the quoted pathname if Directory or filename if File
339 std::string const getTarget(OperationMode opmode) const;
340 /// collect the diff of file or directory against repository
341 /// result is placed in temporary file
342 void getDiff(OperationMode opmode, support::FileName const & tmpf);
343 /// make the file ready for editing:
344 /// save a copy in CVS/Base and change file permissions to rw if needed
346 /// revert the edit operation
348 /// retrieve repository changes into working copy
349 int update(OperationMode opmode, support::FileName const & tmpf);
350 /// check readonly state for file
351 /// assume true when file is writable
352 bool isLocked() const;
353 /// query and parse the cvs status of file
354 CvsStatus getStatus();
355 /// convert enum to string
356 docstring toString(CvsStatus status) const;
358 /// cache the info values of current file revision
359 /// author, date and time of commit
360 std::string rev_author_cache_;
361 std::string rev_date_cache_;
362 std::string rev_time_cache_;
363 /// fills the cache values, returns true if successfull.
364 void getRevisionInfo();
370 class SVN : public VCS {
374 SVN(support::FileName const & m, Buffer * b);
376 /// return the revision file for the given file, if found
377 static support::FileName const findFile(support::FileName const & file);
379 /// get file from repo, the caller must ensure that it does not exist locally
380 static bool retrieve(support::FileName const & file);
382 std::string vcname() const override { return "SVN"; };
384 void registrer(std::string const & msg) override;
386 bool renameEnabled() override;
388 std::string rename(support::FileName const &, std::string const &) override;
390 bool copyEnabled() override;
392 std::string copy(support::FileName const &, std::string const &) override;
395 checkIn(std::string const & msg, std::string & log) override;
397 bool checkInEnabled() override;
399 bool isCheckInWithConfirmation() override;
401 std::string checkOut() override;
403 bool checkOutEnabled() override;
405 std::string repoUpdate() override;
407 bool repoUpdateEnabled() override;
409 std::string lockingToggle() override;
411 bool lockingToggleEnabled() override;
413 bool revert() override;
415 bool isRevertWithConfirmation() override;
417 void undoLast() override;
419 bool undoLastEnabled() override;
421 void getLog(support::FileName const &) override;
423 std::string const versionString() const override {
424 return "SVN: " + rev_file_cache_;
427 bool toggleReadOnlyEnabled() override;
429 std::string revisionInfo(LyXVC::RevisionInfo const info) override;
431 bool prepareFileRevision(std::string const & rev, std::string & f) override;
433 bool prepareFileRevisionEnabled() override;
436 void scanMaster() override;
437 /// Check for messages in svn output. Returns error.
438 std::string scanLogFile(support::FileName const & f, std::string & status);
439 /// checks locking policy and setup locked_mode_
440 bool checkLockMode();
441 /// is the loaded file locked?
442 bool isLocked() const;
443 /// acquire/release write lock for the current file
444 bool fileLock(bool lock, support::FileName const & tmpf, std::string & status);
445 /// Check in files \p f with log \p msg
446 LyXVC::CommandResult checkIn(std::vector<support::FileName> const & f,
447 std::string const & msg, std::string & log);
450 /// is the loaded file under locking policy?
453 * Real code for obtaining file revision info. Fills all file-related caches
454 * and returns true if successfull.
455 * "?" is stored in rev_file_cache_ as a signal if request for obtaining info
456 * was already unsuccessful.
458 bool getFileRevisionInfo();
459 /// cache for file revision number, "?" if already unsuccessful, isNumber==true
460 std::string rev_file_cache_;
461 /// cache for author of last commit
462 std::string rev_author_cache_;
463 /// cache for date of last commit
464 std::string rev_date_cache_;
465 /// cache for time of last commit
466 std::string rev_time_cache_;
467 /// fills rev_tree_cache_, returns true if successfull.
468 bool getTreeRevisionInfo();
469 /// cache for tree revision number, "?" if already unsuccessful
470 std::string rev_tree_cache_;
475 * Very basic git support:
476 * Remote repos are completely ignored, only the local tree is considered.
477 * How push and pull could be integrated with the LyX VCS interface needs
480 class GIT : public VCS {
484 GIT(support::FileName const & m, Buffer * b);
486 /// return the revision file for the given file, if found
487 static support::FileName const findFile(support::FileName const & file);
489 /// get file from repo, the caller must ensure that it does not exist locally
490 static bool retrieve(support::FileName const & file);
492 std::string vcname() const override { return "GIT"; };
494 void registrer(std::string const & msg) override;
496 bool renameEnabled() override;
498 std::string rename(support::FileName const &, std::string const &) override;
500 bool copyEnabled() override;
502 std::string copy(support::FileName const &, std::string const &) override;
505 checkIn(std::string const & msg, std::string & log) override;
507 bool checkInEnabled() override;
509 bool isCheckInWithConfirmation() override;
511 std::string checkOut() override;
513 bool checkOutEnabled() override;
515 std::string repoUpdate() override;
517 bool repoUpdateEnabled() override;
519 std::string lockingToggle() override;
521 bool lockingToggleEnabled() override;
523 bool revert() override;
525 bool isRevertWithConfirmation() override;
527 void undoLast() override;
529 bool undoLastEnabled() override;
531 void getLog(support::FileName const &) override;
533 std::string const versionString() const override {
537 bool toggleReadOnlyEnabled() override;
539 std::string revisionInfo(LyXVC::RevisionInfo const info) override;
541 bool prepareFileRevision(std::string const & rev, std::string & f) override;
543 bool prepareFileRevisionEnabled() override;
546 void scanMaster() override;
547 /// Check for messages in svn output. Returns error.
548 std::string scanLogFile(support::FileName const & f, std::string & status);
549 /// Check in files \p f with log \p msg
550 LyXVC::CommandResult checkIn(std::vector<support::FileName> const & f,
551 std::string const & msg, std::string & log);
555 * Real code for obtaining file revision info. Fills all file-related caches
556 * and returns true if successfull.
557 * "?" is stored in rev_file_cache_ as a signal if request for obtaining info
558 * was already unsuccessful.
560 bool getFileRevisionInfo();
561 /// cache for file revision number, "?" if already unsuccessful, isNumber==true
562 std::string rev_file_cache_;
563 /// cache for abbreviated file revision number, "?" if already unsuccessful, isNumber==true
564 std::string rev_file_abbrev_cache_;
565 /// cache for author of last commit
566 std::string rev_author_cache_;
567 /// cache for date of last commit
568 std::string rev_date_cache_;
569 /// cache for time of last commit
570 std::string rev_time_cache_;
571 /// fills rev_tree_cache_, returns true if successfull.
572 bool getTreeRevisionInfo();
573 /// cache for tree revision number, "?" if already unsuccessful
574 std::string rev_tree_cache_;
579 #endif // VCBACKEND_H