Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't use git show -c on versions of git earlier than 1.7.2
  • Loading branch information
jcheng5 committed Nov 5, 2011
1 parent c1620f6 commit 8269ef4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/cpp/core/StringUtils.cpp
Expand Up @@ -17,10 +17,13 @@
#include <ostream>

#include <algorithm>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/regex.hpp>

#include <core/Log.hpp>
#include <core/SafeConvert.hpp>
#include <core/json/Json.hpp>

#ifdef _WIN32
Expand Down Expand Up @@ -294,6 +297,29 @@ bool isalnum(wchar_t c)
return lookup.at(c);
}

bool parseVersion(const std::string& str, uint64_t* pVersion)
{
uint64_t version = 0;

std::vector<std::string> chunks;
boost::algorithm::split(chunks, str, boost::algorithm::is_any_of("."));

if (chunks.empty())
return false;

for (size_t i = 0; i < chunks.size() && i < 4; i++)
{
uint16_t value = core::safe_convert::stringTo<uint16_t>(
chunks[i], std::numeric_limits<uint16_t>::max());
if (value == std::numeric_limits<uint16_t>::max())
return false;
version += static_cast<uint64_t>(value) << ((3-i) * 16);
}
if (pVersion)
*pVersion = version;
return true;
}

} // namespace string_utils
} // namespace core

Expand Down
2 changes: 2 additions & 0 deletions src/cpp/core/include/core/StringUtils.hpp
Expand Up @@ -44,6 +44,8 @@ void convertLineEndings(std::string* str, LineEnding type);

std::string filterControlChars(const std::string& str);

bool parseVersion(const std::string& str, uint64_t* pVersion);

template<typename T>
T hashStable(const std::string& str)
{
Expand Down
34 changes: 32 additions & 2 deletions src/cpp/session/modules/SessionSourceControl.cpp
Expand Up @@ -77,6 +77,10 @@ const size_t WARN_SIZE = 200 * 1024;
// git bin dir which we detect at startup. note that if the git bin
// is already in the path then this will be empty
std::string s_gitBinDir;
uint64_t s_gitVersion;
const uint64_t GIT_1_7_2 = ((uint64_t)1 << 48) |
((uint64_t)7 << 32) |
((uint64_t)2 << 16);

core::system::ProcessOptions procOptions()
{
Expand Down Expand Up @@ -1060,8 +1064,12 @@ class GitVCSImpl : public VCSImpl
virtual core::Error show(const std::string& rev,
std::string* pOutput)
{
return runCommand(git() << "show" << "--pretty=oneline" << "-c" << "-M" << rev,
pOutput, NULL);
ShellCommand cmd = git() << "show" << "--pretty=oneline" << "-M";
if (s_gitVersion >= GIT_1_7_2)
cmd << "-c";
cmd << rev;

return runCommand(cmd, pOutput, NULL);
}

virtual core::Error hasRemote(bool *pHasRemote)
Expand Down Expand Up @@ -2498,6 +2506,28 @@ bool tryGit(const FilePath& workingDir)
if (error)
LOG_ERROR(error);

// Save version
s_gitVersion = GIT_1_7_2;
core::system::ProcessResult result;
error = core::system::runCommand(git() << "--version",
procOptions(),
&result);
if (error)
LOG_ERROR(error);
else
{
if (result.exitStatus == 0)
{
boost::smatch matches;
if (boost::regex_search(result.stdOut,
matches,
boost::regex("\\d+(\\.\\d+)+")))
{
string_utils::parseVersion(matches[0], &s_gitVersion);
}
}
}

return true;
}

Expand Down

0 comments on commit 8269ef4

Please sign in to comment.