Improve handling of Git masterlist info.

Caching the revision and date is pointless as calculating them only
takes ~ 20 ms, and the caching can cause problems if the files are
edited unexpectedly. Instead, deliver both through a single static
member function.
This commit is contained in:
Oliver Hamlet
2015-07-14 17:34:27 +01:00
parent a24394a676
commit fbd00111d0
4 changed files with 24 additions and 35 deletions
+3 -3
View File
@@ -515,9 +515,9 @@ LOOT_API unsigned int loot_get_masterlist_revision(loot_db db,
bool edited = false;
try {
loot::Masterlist masterlist;
std::string id = masterlist.GetRevision(masterlistPath, getShortID);
std::string date = masterlist.GetDate(masterlistPath);
loot::Masterlist::Info info = loot::Masterlist::GetInfo(masterlistPath, getShortID);
std::string id = info.revision;
std::string date = info.date;
if (boost::ends_with(id, " (edited)")) {
id = id.substr(0, id.length() - 9);
+10 -20
View File
@@ -35,23 +35,10 @@ namespace fs = boost::filesystem;
namespace lc = boost::locale;
namespace loot {
std::string Masterlist::GetRevision(const boost::filesystem::path& path, bool shortID) {
if (revision.empty() || (shortID && revision.length() == 40) || (!shortID && revision.length() < 40))
GetGitInfo(path, shortID);
return revision;
}
std::string Masterlist::GetDate(const boost::filesystem::path& path) {
if (date.empty())
GetGitInfo(path, true);
return date;
}
void Masterlist::GetGitInfo(const boost::filesystem::path& path, bool shortID) {
Masterlist::Info Masterlist::GetInfo(const boost::filesystem::path& path, bool shortID) {
// Compare HEAD and working copy, and get revision info.
GitHelper git;
Info info;
git.SetErrorMessage((boost::format(lc::translate("An error occurred while trying to read the local masterlist's version. If this error happens again, try deleting the \".git\" folder in %1%.")) % path.parent_path().string()).str());
if (!fs::exists(path)) {
@@ -73,11 +60,11 @@ namespace loot {
BOOST_LOG_TRIVIAL(trace) << "Generating hex string for Git object ID.";
if (shortID) {
git.Call(git_object_short_id(&git.buf, git.obj));
revision = git.buf.ptr;
info.revision = git.buf.ptr;
}
else {
char c_rev[GIT_OID_HEXSZ + 1];
revision = git_oid_tostr(c_rev, GIT_OID_HEXSZ + 1, git_object_id(git.obj));
info.revision = git_oid_tostr(c_rev, GIT_OID_HEXSZ + 1, git_object_id(git.obj));
}
BOOST_LOG_TRIVIAL(trace) << "Getting date for Git object.";
@@ -87,7 +74,7 @@ namespace loot {
boost::locale::date_time dateTime(time);
stringstream out;
out << boost::locale::as::ftime("%Y-%m-%d") << dateTime;
date = out.str();
info.date = out.str();
// Free object memory.
git_object_free(git.obj);
@@ -95,9 +82,11 @@ namespace loot {
BOOST_LOG_TRIVIAL(trace) << "Diffing masterlist HEAD and working copy.";
if (IsFileDifferent(path.parent_path(), path.filename().string())) {
revision += string(" ") + lc::translate("(edited)").str();
date += string(" ") + lc::translate("(edited)").str();
info.revision += string(" ") + lc::translate("(edited)").str();
info.date += string(" ") + lc::translate("(edited)").str();
}
return info;
}
bool Masterlist::Update(const Game& game) {
@@ -344,6 +333,7 @@ namespace loot {
git.SetErrorMessage((boost::format(lc::translate("An error occurred while trying to read information on the updated masterlist. If this error happens again, try deleting the \".git\" folder in %1%.")) % repo_path.string()).str());
do {
// Get some descriptive info about what was checked out.
string revision, date;
BOOST_LOG_TRIVIAL(trace) << "Getting the Git object for HEAD.";
git.Call(git_repository_head(&git.ref, git.repo));
+5 -8
View File
@@ -36,20 +36,17 @@ namespace loot {
class Masterlist : public MetadataList {
public:
struct Info {
std::string revision;
std::string date;
};
bool Update(const Game& game);
bool Update(const boost::filesystem::path& path,
const std::string& repoURL,
const std::string& repoBranch);
std::string GetRevision(const boost::filesystem::path& path, bool shortID);
std::string GetDate(const boost::filesystem::path& path);
private:
void GetGitInfo(const boost::filesystem::path& path, bool shortID);
std::string revision;
std::string date;
static Info GetInfo(const boost::filesystem::path& path, bool shortID);
};
}
+6 -4
View File
@@ -683,8 +683,9 @@ namespace loot {
// Store the masterlist revision and date.
try {
gameNode["masterlist"]["revision"] = _lootState.CurrentGame().masterlist.GetRevision(_lootState.CurrentGame().MasterlistPath(), true);
gameNode["masterlist"]["date"] = _lootState.CurrentGame().masterlist.GetDate(_lootState.CurrentGame().MasterlistPath());
Masterlist::Info info = _lootState.CurrentGame().masterlist.GetInfo(_lootState.CurrentGame().MasterlistPath(), true);
gameNode["masterlist"]["revision"] = info.revision;
gameNode["masterlist"]["date"] = info.date;
}
catch (error &e) {
gameNode["masterlist"]["revision"] = e.what();
@@ -843,8 +844,9 @@ namespace loot {
// Store the masterlist revision and date.
try {
gameNode["masterlist"]["revision"] = _lootState.CurrentGame().masterlist.GetRevision(_lootState.CurrentGame().MasterlistPath(), true);
gameNode["masterlist"]["date"] = _lootState.CurrentGame().masterlist.GetDate(_lootState.CurrentGame().MasterlistPath());
Masterlist::Info info = _lootState.CurrentGame().masterlist.GetInfo(_lootState.CurrentGame().MasterlistPath(), true);
gameNode["masterlist"]["revision"] = info.revision;
gameNode["masterlist"]["date"] = info.date;
}
catch (error &e) {
gameNode["masterlist"]["revision"] = e.what();