diff --git a/src/util/util.cc b/src/util/util.cc index 33bac99d..847155c8 100644 --- a/src/util/util.cc +++ b/src/util/util.cc @@ -38,6 +38,55 @@ void setup_logging(bool verbose) } } +std::string get_process_output(const std::string &command) +{ + std::array buffer; + std::string result; + std::unique_ptr 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); diff --git a/src/util/util.h b/src/util/util.h index 8a63a5e6..26ad5506 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -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 *