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 /// 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;
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.
80 virtual void undoLast() = 0;
81 /// can this operation be processed in the current VCS?
82 virtual bool undoLastEnabled() = 0;
84 * getLog - read the revision log into the given file
85 * @param fname file name to read into
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;
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);
109 /// parse information from the version file
110 virtual void scanMaster() = 0;
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;
116 /// GUI container for doVCCommandCall
117 int doVCCommand(std::string const & cmd, support::FileName const & path, bool reportError = true);
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
124 static int doVCCommandCall(std::string const & cmd, support::FileName const & path);
127 * The master VC file. For RCS this is *,v or RCS/ *,v. master should
130 support::FileName master_;
132 /// The status of the VC controlled file.
135 /// The buffer using this VC
136 Buffer * const owner_;
141 class RCS : public VCS {
145 RCS(support::FileName const & m, Buffer * b);
147 /// return the revision file for the given file, if found
148 static support::FileName const findFile(support::FileName const & file);
150 /// get file from repo, the caller must ensure that it does not exist locally
151 static bool retrieve(support::FileName const & file);
153 virtual void registrer(std::string const & msg);
155 virtual bool renameEnabled();
157 virtual std::string rename(support::FileName const &, std::string const &);
159 virtual bool copyEnabled();
161 virtual std::string copy(support::FileName const &, std::string const &);
163 virtual LyXVC::CommandResult
164 checkIn(std::string const & msg, std::string & log);
166 virtual bool checkInEnabled();
168 virtual bool isCheckInWithConfirmation();
170 virtual std::string checkOut();
172 virtual bool checkOutEnabled();
174 virtual std::string repoUpdate();
176 virtual bool repoUpdateEnabled();
178 virtual std::string lockingToggle();
180 virtual bool lockingToggleEnabled();
182 virtual bool revert();
184 virtual bool isRevertWithConfirmation();
186 virtual void undoLast();
188 virtual bool undoLastEnabled();
190 virtual void getLog(support::FileName const &);
192 virtual std::string const versionString() const {
193 return "RCS: " + version_;
196 virtual bool toggleReadOnlyEnabled();
198 virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
200 virtual bool prepareFileRevision(std::string const & rev, std::string & f);
202 virtual bool prepareFileRevisionEnabled();
205 virtual void scanMaster();
207 bool getRevisionInfo();
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.
212 std::string version_;
213 /// The user currently keeping the lock on the VC file (or "Unlocked").
215 /// Cache for revision info.
216 std::string rev_date_cache_;
218 std::string rev_time_cache_;
220 std::string rev_author_cache_;
225 class CVS : public VCS {
229 CVS(support::FileName const & m, Buffer * b);
231 /// return the revision file for the given file, if found
232 static support::FileName const findFile(support::FileName const & file);
234 /// get file from repo, the caller must ensure that it does not exist locally
235 static bool retrieve(support::FileName const & file);
237 virtual void registrer(std::string const & msg);
239 virtual bool renameEnabled();
241 virtual std::string rename(support::FileName const &, std::string const &);
243 virtual bool copyEnabled();
245 virtual std::string copy(support::FileName const &, std::string const &);
247 virtual LyXVC::CommandResult
248 checkIn(std::string const & msg, std::string & log);
250 virtual bool checkInEnabled();
252 virtual bool isCheckInWithConfirmation();
254 virtual std::string checkOut();
256 virtual bool checkOutEnabled();
258 virtual std::string repoUpdate();
260 virtual bool repoUpdateEnabled();
262 virtual std::string lockingToggle();
264 virtual bool lockingToggleEnabled();
266 virtual bool isRevertWithConfirmation();
268 virtual bool revert();
270 virtual void undoLast();
272 virtual bool undoLastEnabled();
274 virtual void getLog(support::FileName const &);
276 /// Check for messages in cvs output.
277 /// Returns conflict line.
278 std::string scanLogFile(support::FileName const & f, std::string & status);
280 virtual std::string const versionString() const {
281 return "CVS: " + version_;
284 virtual bool toggleReadOnlyEnabled();
286 virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
288 virtual bool prepareFileRevision(std::string const & rev, std::string & f);
290 virtual bool prepareFileRevisionEnabled();
293 virtual void scanMaster();
294 /// the mode of operation for some VC commands
299 /// possible status values of file
311 // revision number from scanMaster
312 std::string version_;
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
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);
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
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;
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();
364 class SVN : public VCS {
368 SVN(support::FileName const & m, Buffer * b);
370 /// return the revision file for the given file, if found
371 static support::FileName const findFile(support::FileName const & file);
373 /// get file from repo, the caller must ensure that it does not exist locally
374 static bool retrieve(support::FileName const & file);
376 virtual void registrer(std::string const & msg);
378 virtual bool renameEnabled();
380 virtual std::string rename(support::FileName const &, std::string const &);
382 virtual bool copyEnabled();
384 virtual std::string copy(support::FileName const &, std::string const &);
386 virtual LyXVC::CommandResult
387 checkIn(std::string const & msg, std::string & log);
389 virtual bool checkInEnabled();
391 virtual bool isCheckInWithConfirmation();
393 virtual std::string checkOut();
395 virtual bool checkOutEnabled();
397 virtual std::string repoUpdate();
399 virtual bool repoUpdateEnabled();
401 virtual std::string lockingToggle();
403 virtual bool lockingToggleEnabled();
405 virtual bool revert();
407 virtual bool isRevertWithConfirmation();
409 virtual void undoLast();
411 virtual bool undoLastEnabled();
413 virtual void getLog(support::FileName const &);
415 virtual std::string const versionString() const {
416 return "SVN: " + rev_file_cache_;
419 virtual bool toggleReadOnlyEnabled();
421 virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
423 virtual bool prepareFileRevision(std::string const & rev, std::string & f);
425 virtual bool prepareFileRevisionEnabled();
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);
442 /// is the loaded file under locking policy?
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.
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_;
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
472 class GIT : public VCS {
476 GIT(support::FileName const & m, Buffer * b);
478 /// return the revision file for the given file, if found
479 static support::FileName const findFile(support::FileName const & file);
481 /// get file from repo, the caller must ensure that it does not exist locally
482 static bool retrieve(support::FileName const & file);
484 virtual void registrer(std::string const & msg);
486 virtual bool renameEnabled();
488 virtual std::string rename(support::FileName const &, std::string const &);
490 virtual bool copyEnabled();
492 virtual std::string copy(support::FileName const &, std::string const &);
494 virtual LyXVC::CommandResult
495 checkIn(std::string const & msg, std::string & log);
497 virtual bool checkInEnabled();
499 virtual bool isCheckInWithConfirmation();
501 virtual std::string checkOut();
503 virtual bool checkOutEnabled();
505 virtual std::string repoUpdate();
507 virtual bool repoUpdateEnabled();
509 virtual std::string lockingToggle();
511 virtual bool lockingToggleEnabled();
513 virtual bool revert();
515 virtual bool isRevertWithConfirmation();
517 virtual void undoLast();
519 virtual bool undoLastEnabled();
521 virtual void getLog(support::FileName const &);
523 virtual std::string const versionString() const {
527 virtual bool toggleReadOnlyEnabled();
529 virtual std::string revisionInfo(LyXVC::RevisionInfo const info);
531 virtual bool prepareFileRevision(std::string const & rev, std::string & f);
533 virtual bool prepareFileRevisionEnabled();
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);
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.
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_;
567 #endif // VCBACKEND_H