From c889b852a652115f4a6936a96ebdee9410571997 Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Mon, 19 Aug 2024 16:01:16 +0200 Subject: GitOps: Pass source directory to GitInitialCommit operation While there, ensure optional Git operation parameters are checked before use for the operations that require them. --- src/other_tools/git_operations/git_operations.cpp | 38 ++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'src/other_tools/git_operations/git_operations.cpp') diff --git a/src/other_tools/git_operations/git_operations.cpp b/src/other_tools/git_operations/git_operations.cpp index bfd1d18f..5135743f 100644 --- a/src/other_tools/git_operations/git_operations.cpp +++ b/src/other_tools/git_operations/git_operations.cpp @@ -25,6 +25,19 @@ auto CriticalGitOps::GitInitialCommit(GitOpParams const& crit_op_params, AsyncMapConsumerLoggerPtr const& logger) -> GitOpValue { +#ifndef NDEBUG + // Check required fields have been set + if (not crit_op_params.message) { + (*logger)("missing message for operation creating commit", + true /*fatal*/); + return {.git_cas = nullptr, .result = std::nullopt}; + } + if (not crit_op_params.source_path) { + (*logger)("missing source_path for operation creating commit", + true /*fatal*/); + return {.git_cas = nullptr, .result = std::nullopt}; + } +#endif // Create and open a GitRepoRemote at given target location auto git_repo = GitRepoRemote::InitAndOpen(crit_op_params.target_path, /*is_bare=*/false); @@ -41,10 +54,11 @@ auto CriticalGitOps::GitInitialCommit(GitOpParams const& crit_op_params, fmt::format("While doing initial commit Git op:\n{}", msg), fatal); }); - // Stage and commit all at the target location - auto commit_hash = git_repo->CommitDirectory(crit_op_params.target_path, - crit_op_params.message.value(), - wrapped_logger); + // Commit all at the target directory + auto commit_hash = + git_repo->CommitDirectory(crit_op_params.source_path.value(), + crit_op_params.message.value(), + wrapped_logger); if (commit_hash == std::nullopt) { return {.git_cas = nullptr, .result = std::nullopt}; } @@ -81,6 +95,14 @@ auto CriticalGitOps::GitEnsureInit(GitOpParams const& crit_op_params, auto CriticalGitOps::GitKeepTag(GitOpParams const& crit_op_params, AsyncMapConsumerLoggerPtr const& logger) -> GitOpValue { +#ifndef NDEBUG + // Check required fields have been set + if (not crit_op_params.message) { + (*logger)("missing message for operation tagging a commit", + true /*fatal*/); + return {.git_cas = nullptr, .result = std::nullopt}; + } +#endif // Make sure folder we want to access exists if (not FileSystemManager::Exists(crit_op_params.target_path)) { (*logger)(fmt::format("target directory {} does not exist!", @@ -149,6 +171,14 @@ auto CriticalGitOps::GitGetHeadId(GitOpParams const& crit_op_params, auto CriticalGitOps::GitKeepTree(GitOpParams const& crit_op_params, AsyncMapConsumerLoggerPtr const& logger) -> GitOpValue { +#ifndef NDEBUG + // Check required fields have been set + if (not crit_op_params.message) { + (*logger)("missing message for operation keeping a tree comitted", + true /*fatal*/); + return {.git_cas = nullptr, .result = std::nullopt}; + } +#endif // Make sure folder we want to access exists if (not FileSystemManager::Exists(crit_op_params.target_path)) { (*logger)(fmt::format("target directory {} does not exist!", -- cgit v1.2.3