From 37b02e144d92deefe1b453a1f21b386a19d49955 Mon Sep 17 00:00:00 2001 From: tylen Date: Fri, 27 Sep 2024 12:33:14 +0000 Subject: [PATCH] base: parse remote_urls in git project issue #2 --- src/cdo.c | 5 ++++- src/platform/base.c | 48 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/cdo.c b/src/cdo.c index e4b08c6..c0e77b4 100644 --- a/src/cdo.c +++ b/src/cdo.c @@ -23,7 +23,10 @@ int main(int argc, char* argv[]) { PlatformIdList* project_platforms = identify_platfroms_from_project( project_path, extract_value_from_arg(args, ARG_REMOTE)); - + LOG_INFO("Project platforms:"); + for (size_t i = 0; i < project_platforms->size; i++) { + LOG_INFO("%s", SUPPORTED_PLATFORM_NAMES[project_platforms->ids[i]]); + } cdo_free((void*)project_path); free_arg_list(args); return 0; diff --git a/src/platform/base.c b/src/platform/base.c index ee54e16..5429445 100644 --- a/src/platform/base.c +++ b/src/platform/base.c @@ -34,6 +34,42 @@ static bool parse_regex_result(int value) { } } +static PlaformId get_platfrom_id_from_string(const char* str) { + PlaformId result = PL_UNRECOGNIZED; + RegexWrapper regexes[PL_UNRECOGNIZED] = { + {.regex_pattern = SUPPORTED_PLATFORM_NAMES[PL_GITHUB]}, + {.regex_pattern = SUPPORTED_PLATFORM_NAMES[PL_GITLAB]}}; + for (size_t i = 0; i < PL_UNRECOGNIZED; i++) { + int value = regcomp(&(regexes[i].regex_holder), regexes[i].regex_pattern, 0); + if (value != 0) throw_regex_error(value, &(regexes[i].regex_holder), regexes[i].regex_pattern); + if (parse_regex_result(regexec( + &(regexes[i].regex_holder), + str, + 0, NULL, 0)) + ) { + result = i; + } + regfree(&(regexes[i].regex_holder)); + } + return result; +} + +static PlatformIdList* identify_remote_urls(Lines* remote_urls) { + if (remote_urls->size == 0) { + LOG_ERROR_EXIT("Git config does not have any remote urls."); + } + + PlatformIdList* platforms = cdo_calloc(1, sizeof(PlatformIdList)); + platforms->size = remote_urls->size; + platforms->ids = cdo_malloc(platforms->size * sizeof(PlaformId)); + + for(size_t i = 0; i < remote_urls->size; i++) { + platforms->ids[i] = get_platfrom_id_from_string(remote_urls->lines[i]->data); + } + + return platforms; +} + static Lines* get_git_remote_urls_from_lines(Lines* lines, const char* remote) { LOG_DEBUG("Entering function %s", __func__); char config_remote_line_rg[GIT_CONFIG_REMOTE_STRING_MAX_LEN] = {0}; @@ -53,7 +89,7 @@ static Lines* get_git_remote_urls_from_lines(Lines* lines, const char* remote) { for (size_t i = 0; i < GIT_CFG_RG_COUNT; i++) { int value = regcomp(&(regexes[i].regex_holder), regexes[i].regex_pattern, 0); if (value != 0) throw_regex_error(value, &(regexes[i].regex_holder), regexes[i].regex_pattern); - } + } bool remote_section_found = false; bool inside_remote_section = false; @@ -118,14 +154,10 @@ PlatformIdList* identify_platfroms_from_project(const char* project_path, const Lines* config_file_lines = read_file_lines(git_config_path_buffer->data); Lines* git_remote_urls = get_git_remote_urls_from_lines(config_file_lines, remote); - - PlatformIdList* platforms = cdo_calloc(1, sizeof(PlatformIdList)); - platforms->size = git_remote_urls->size; - platforms->ids = cdo_malloc(platforms->size * sizeof(PlaformId)); - // TODO: parse remote "origin url" to get platform + PlatformIdList* platforms = identify_remote_urls(git_remote_urls); lines_free(config_file_lines); - lines_flush(git_remote_urls); + lines_free(git_remote_urls); buffer_free(git_config_path_buffer); - return NULL; + return platforms; } \ No newline at end of file