From: Georg Baum Date: Thu, 7 Feb 2013 21:07:22 +0000 (+0100) Subject: Implement revision info for git X-Git-Tag: 2.1.0beta1~774 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=22b7ad2b0a6c228f234bcf7f5a0b164fca17e122;p=features.git Implement revision info for git --- diff --git a/src/VCBackend.cpp b/src/VCBackend.cpp index a6ce1c36f9..599e331364 100644 --- a/src/VCBackend.cpp +++ b/src/VCBackend.cpp @@ -2112,12 +2112,99 @@ bool GIT::undoLastEnabled() } -string GIT::revisionInfo(LyXVC::RevisionInfo const /*info*/) +string GIT::revisionInfo(LyXVC::RevisionInfo const info) { + if (info == LyXVC::Tree) { + if (rev_tree_cache_.empty()) + if (!getTreeRevisionInfo()) + rev_tree_cache_ = "?"; + if (rev_tree_cache_ == "?") + return string(); + + return rev_tree_cache_; + } + + // fill the rest of the attributes for a single file + if (rev_file_cache_.empty()) + if (!getFileRevisionInfo()) + rev_file_cache_ = "?"; + + switch (info) { + case LyXVC::File: + if (rev_file_cache_ == "?") + return string(); + return rev_file_cache_; + case LyXVC::Author: + return rev_author_cache_; + case LyXVC::Date: + return rev_date_cache_; + case LyXVC::Time: + return rev_time_cache_; + default: ; + + } + return string(); } +bool GIT::getFileRevisionInfo() +{ + FileName tmpf = FileName::tempName("lyxvcout"); + if (tmpf.empty()) { + LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf); + return false; + } + + doVCCommand("git log -n 1 --pretty=format:%H%n%an%n%ai " + quoteName(onlyFileName(owner_->absFileName())) + + " > " + quoteName(tmpf.toFilesystemEncoding()), + FileName(owner_->filePath())); + + if (tmpf.empty()) + return false; + + ifstream ifs(tmpf.toFilesystemEncoding().c_str()); + + if (ifs) + getline(ifs, rev_file_cache_); + if (ifs) + getline(ifs, rev_author_cache_); + if (ifs) { + string line; + getline(ifs, line); + rev_time_cache_ = split(line, rev_date_cache_, ' '); + } + + ifs.close(); + tmpf.removeFile(); + return !rev_file_cache_.empty(); +} + + +bool GIT::getTreeRevisionInfo() +{ + FileName tmpf = FileName::tempName("lyxvcout"); + if (tmpf.empty()) { + LYXERR(Debug::LYXVC, "Could not generate logfile " << tmpf); + return false; + } + + doVCCommand("git log -n 1 --pretty=format:%H . > " + quoteName(tmpf.toFilesystemEncoding()), + FileName(owner_->filePath())); + + if (tmpf.empty()) + return false; + + // only first line in case something bad happens. + ifstream ifs(tmpf.toFilesystemEncoding().c_str()); + getline(ifs, rev_tree_cache_); + ifs.close(); + tmpf.removeFile(); + + return !rev_tree_cache_.empty(); +} + + void GIT::getLog(FileName const & tmpf) { doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName())) diff --git a/src/VCBackend.h b/src/VCBackend.h index 6d3903c686..ab7f52fd9d 100644 --- a/src/VCBackend.h +++ b/src/VCBackend.h @@ -542,6 +542,27 @@ protected: /// Check in files \p f with log \p msg LyXVC::CommandResult checkIn(std::vector const & f, std::string const & msg, std::string & log); + +private: + /** + * Real code for obtaining file revision info. Fills all file-related caches + * and returns true if successfull. + * "?" is stored in rev_file_cache_ as a signal if request for obtaining info + * was already unsuccessful. + */ + bool getFileRevisionInfo(); + /// cache for file revision number, "?" if already unsuccessful, isNumber==true + std::string rev_file_cache_; + /// cache for author of last commit + std::string rev_author_cache_; + /// cache for date of last commit + std::string rev_date_cache_; + /// cache for time of last commit + std::string rev_time_cache_; + /// fills rev_tree_cache_, returns true if successfull. + bool getTreeRevisionInfo(); + /// cache for tree revision number, "?" if already unsuccessful + std::string rev_tree_cache_; }; } // namespace lyx