Implement revision info for git

This commit is contained in:
Georg Baum 2013-02-07 22:07:22 +01:00
parent 5a62a1ccb8
commit 22b7ad2b0a
2 changed files with 109 additions and 1 deletions

View 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(); 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) void GIT::getLog(FileName const & tmpf)
{ {
doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName())) doVCCommand("git log " + quoteName(onlyFileName(owner_->absFileName()))

View File

@ -542,6 +542,27 @@ protected:
/// Check in files \p f with log \p msg /// Check in files \p f with log \p msg
LyXVC::CommandResult checkIn(std::vector<support::FileName> const & f, LyXVC::CommandResult checkIn(std::vector<support::FileName> const & f,
std::string const & msg, std::string & log); 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 } // namespace lyx