From 50147aa3891845295d81a1fe68ae583d86f516b3 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Sat, 13 Jan 2024 19:34:45 +0100 Subject: [PATCH] chg: [internal] Log errors for git --- app/Lib/Tools/GitTool.php | 4 ++++ app/Model/Server.php | 32 ++++++++++++++------------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/Lib/Tools/GitTool.php b/app/Lib/Tools/GitTool.php index f9a77f5c8..375ae9d83 100644 --- a/app/Lib/Tools/GitTool.php +++ b/app/Lib/Tools/GitTool.php @@ -97,12 +97,14 @@ class GitTool /** * @param string $submodule Path to Git repo * @return string|null + * @throws Exception */ public static function submoduleCurrentCommit($submodule) { try { $commit = ProcessTool::execute(['git', 'rev-parse', 'HEAD'], $submodule); } catch (ProcessException $e) { + CakeLog::notice("Could not get Git commit for $submodule: {$e->getMessage()}"); return null; } return rtrim($commit); @@ -112,12 +114,14 @@ class GitTool * @param string $commit * @param string|null $submodule Path to Git repo * @return int|null + * @throws Exception */ public static function commitTimestamp($commit, $submodule = null) { try { $timestamp = ProcessTool::execute(['git', 'show', '-s', '--pretty=format:%ct', $commit], $submodule); } catch (ProcessException $e) { + CakeLog::notice("Could not get Git commit timestamp for $submodule: {$e->getMessage()}"); return null; } return (int)rtrim($timestamp); diff --git a/app/Model/Server.php b/app/Model/Server.php index f4bdc64cd..18e09b44a 100644 --- a/app/Model/Server.php +++ b/app/Model/Server.php @@ -4264,38 +4264,38 @@ class Server extends AppModel 'app/files/scripts/misp-opendata', 'app/files/scripts/python-maec', 'app/files/scripts/python-stix', - ); return in_array($submodule, $accepted_submodules_names, true); } /** - * @param string $submodule_name - * @param string $superproject_submodule_commit_id + * @param string $submoduleName + * @param string $superprojectSubmoduleCommitId * @return array + * @throws Exception */ - private function getSubmoduleGitStatus($submodule_name, $superproject_submodule_commit_id) + private function getSubmoduleGitStatus($submoduleName, $superprojectSubmoduleCommitId) { - $path = APP . '../' . $submodule_name; - $submodule_name = (strpos($submodule_name, '/') >= 0 ? explode('/', $submodule_name) : $submodule_name); - $submodule_name = end($submodule_name); + $path = APP . '../' . $submoduleName; + $submoduleName = (strpos($submoduleName, '/') >= 0 ? explode('/', $submoduleName) : $submoduleName); + $submoduleName = end($submoduleName); $submoduleCurrentCommitId = GitTool::submoduleCurrentCommit($path); $currentTimestamp = GitTool::commitTimestamp($submoduleCurrentCommitId, $path); - if ($submoduleCurrentCommitId !== $superproject_submodule_commit_id) { - $remoteTimestamp = GitTool::commitTimestamp($superproject_submodule_commit_id, $path); + if ($submoduleCurrentCommitId !== $superprojectSubmoduleCommitId) { + $remoteTimestamp = GitTool::commitTimestamp($superprojectSubmoduleCommitId, $path); } else { $remoteTimestamp = $currentTimestamp; } $status = array( - 'moduleName' => $submodule_name, + 'moduleName' => $submoduleName, 'current' => $submoduleCurrentCommitId, 'currentTimestamp' => $currentTimestamp, - 'remote' => $superproject_submodule_commit_id, + 'remote' => $superprojectSubmoduleCommitId, 'remoteTimestamp' => $remoteTimestamp, - 'upToDate' => '', + 'upToDate' => 'error', 'isReadable' => is_readable($path) && is_readable($path . '/.git'), ); @@ -4307,15 +4307,11 @@ class Server extends AppModel } else { $status['upToDate'] = 'younger'; } - } else { - $status['upToDate'] = 'error'; } if ($status['isReadable'] && !empty($status['remoteTimestamp']) && !empty($status['currentTimestamp'])) { - $date1 = new DateTime(); - $date1->setTimestamp($status['remoteTimestamp']); - $date2 = new DateTime(); - $date2->setTimestamp($status['currentTimestamp']); + $date1 = new DateTime("@{$status['remoteTimestamp']}"); + $date2 = new DateTime("@{$status['currentTimestamp']}"); $status['timeDiff'] = $date1->diff($date2); } else { $status['upToDate'] = 'error';