]> git.lyx.org Git - features.git/commitdiff
Implement revision info for git
authorGeorg Baum <baum@lyx.org>
Thu, 7 Feb 2013 21:07:22 +0000 (22:07 +0100)
committerGeorg Baum <baum@lyx.org>
Fri, 8 Feb 2013 20:02:37 +0000 (21:02 +0100)
src/VCBackend.cpp
src/VCBackend.h

index a6ce1c36f9554c816473fe596a5a45fdb6341138..599e33136439dbe1255e8315aa22602b6004ee33 100644 (file)
@@ -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()))
index 6d3903c68698476125b465a436011ac7f9b28abf..ab7f52fd9d6399bc06a71172d5034b18da1f5472 100644 (file)
@@ -542,6 +542,27 @@ protected:
        /// Check in files \p f with log \p msg
        LyXVC::CommandResult checkIn(std::vector<support::FileName> 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