Added git util methods
This commit is contained in:
@@ -38,6 +38,55 @@ void setup_logging(bool verbose)
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_process_output(const std::string &command)
|
||||
{
|
||||
std::array<char, 1024> buffer;
|
||||
std::string result;
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe(
|
||||
popen(command.c_str(), "r"), pclose);
|
||||
|
||||
if (!pipe) {
|
||||
throw std::runtime_error("popen() failed!");
|
||||
}
|
||||
|
||||
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
|
||||
result += buffer.data();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool is_git_repository()
|
||||
{
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
return false;
|
||||
#else
|
||||
return contains(
|
||||
trim(get_process_output("git rev-parse --git-dir 2> /dev/null")),
|
||||
".git");
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string get_git_branch()
|
||||
{
|
||||
return trim(get_process_output("git rev-parse --abbrev-ref HEAD"));
|
||||
}
|
||||
|
||||
std::string get_git_revision()
|
||||
{
|
||||
return trim(get_process_output("git describe --tags --always"));
|
||||
}
|
||||
|
||||
std::string get_git_commit()
|
||||
{
|
||||
return trim(get_process_output("git rev-parse HEAD"));
|
||||
}
|
||||
|
||||
std::string get_git_toplevel_dir()
|
||||
{
|
||||
return trim(get_process_output("git rev-parse --show-toplevel"));
|
||||
}
|
||||
|
||||
std::string ltrim(const std::string &s)
|
||||
{
|
||||
size_t start = s.find_first_not_of(WHITESPACE);
|
||||
|
||||
@@ -59,6 +59,18 @@ std::string trim(const std::string &s);
|
||||
*/
|
||||
void setup_logging(bool verbose);
|
||||
|
||||
std::string get_process_output(const std::string &command);
|
||||
|
||||
bool is_git_repository();
|
||||
|
||||
std::string get_git_branch();
|
||||
|
||||
std::string get_git_revision();
|
||||
|
||||
std::string get_git_commit();
|
||||
|
||||
std::string get_git_toplevel_dir();
|
||||
|
||||
/**
|
||||
* @brief Split a string using delimiter
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user