|
21 | 21 |
|
22 | 22 | #include "writer-base.hh" |
23 | 23 |
|
24 | | -static std::vector<std::string> run_command(const std::string& command) |
| 24 | +static std::vector<std::string> run_command(const std::string& command, bool stderr_enabled = true) |
25 | 25 | { |
26 | 26 | std::array<char, 128> buffer; |
27 | 27 | std::vector<std::string> stdoutLines; |
28 | 28 |
|
29 | | - FILE* pipe = popen(command.c_str(), "r"); |
| 29 | + std::string full_command = command; |
| 30 | + if (!stderr_enabled) |
| 31 | + full_command += " 2>/dev/null"; |
| 32 | + |
| 33 | + FILE* pipe = popen(full_command.c_str(), "r"); |
30 | 34 |
|
31 | 35 | if (!pipe) { |
32 | 36 | std::cerr << "[run_command] Failed calling popen()\n"; |
@@ -147,7 +151,7 @@ class CoverallsWriter : public WriterBase |
147 | 151 |
|
148 | 152 | void onStartup() |
149 | 153 | { |
150 | | - m_gitInfo = getGitInfoMap(); |
| 154 | + m_gitInfo = getGitInfoMap(); |
151 | 155 | } |
152 | 156 |
|
153 | 157 | void onStop() |
@@ -213,7 +217,7 @@ class CoverallsWriter : public WriterBase |
213 | 217 | } |
214 | 218 |
|
215 | 219 | if (!m_gitInfo.empty() && !m_gitInfo["gitRootPath"].empty()) |
216 | | - strip_path = m_gitInfo["gitRootPath"] + "/"; |
| 220 | + strip_path = m_gitInfo["gitRootPath"] + "/"; |
217 | 221 |
|
218 | 222 | unsigned int filesLeft = m_files.size(); |
219 | 223 | for (FileMap_t::const_iterator it = m_files.begin(); |
@@ -283,13 +287,18 @@ class CoverallsWriter : public WriterBase |
283 | 287 |
|
284 | 288 | std::unordered_map<std::string, std::string> getGitInfoMap() |
285 | 289 | { |
286 | | - std::unordered_map<std::string, std::string> gitInfoMap; |
| 290 | + std::unordered_map<std::string, std::string> gitInfoMap; |
| 291 | + |
| 292 | + // check whether we are inside a git work tree before collecting git metadata |
| 293 | + auto insideWorkTree = run_command("git rev-parse --is-inside-work-tree", false); |
| 294 | + if (insideWorkTree.size() != 1 || insideWorkTree[0] != "true") |
| 295 | + return gitInfoMap; |
287 | 296 |
|
288 | 297 | auto optionalGitInfo = run_command("git log -1 --pretty=format:'%H%n%aN%n%aE%n%cN%n%cE%n%s'"); |
289 | 298 | auto optionalGitBranch = run_command("git rev-parse --abbrev-ref HEAD"); |
290 | 299 | auto optionalGitRootPath = run_command("git rev-parse --show-toplevel"); |
291 | 300 | if (6 != optionalGitInfo.size()) |
292 | | - return gitInfoMap; |
| 301 | + return gitInfoMap; |
293 | 302 |
|
294 | 303 | gitInfoMap["commitHash"] = optionalGitInfo[0]; |
295 | 304 | gitInfoMap["authorName"] = optionalGitInfo[1]; |
|
0 commit comments