base: parse remote_urls in git project

issue #2
This commit is contained in:
tylen 2024-09-27 12:33:14 +00:00 committed by Vasily Davydov
parent 29620fe35a
commit 37b02e144d
2 changed files with 44 additions and 9 deletions

View File

@ -23,7 +23,10 @@ int main(int argc, char* argv[]) {
PlatformIdList* project_platforms = identify_platfroms_from_project( PlatformIdList* project_platforms = identify_platfroms_from_project(
project_path, project_path,
extract_value_from_arg(args, ARG_REMOTE)); 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); cdo_free((void*)project_path);
free_arg_list(args); free_arg_list(args);
return 0; return 0;

View File

@ -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) { static Lines* get_git_remote_urls_from_lines(Lines* lines, const char* remote) {
LOG_DEBUG("Entering function %s", __func__); LOG_DEBUG("Entering function %s", __func__);
char config_remote_line_rg[GIT_CONFIG_REMOTE_STRING_MAX_LEN] = {0}; 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++) { for (size_t i = 0; i < GIT_CFG_RG_COUNT; i++) {
int value = regcomp(&(regexes[i].regex_holder), regexes[i].regex_pattern, 0); 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 (value != 0) throw_regex_error(value, &(regexes[i].regex_holder), regexes[i].regex_pattern);
} }
bool remote_section_found = false; bool remote_section_found = false;
bool inside_remote_section = 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* 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); Lines* git_remote_urls = get_git_remote_urls_from_lines(config_file_lines, remote);
PlatformIdList* platforms = identify_remote_urls(git_remote_urls);
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
lines_free(config_file_lines); lines_free(config_file_lines);
lines_flush(git_remote_urls); lines_free(git_remote_urls);
buffer_free(git_config_path_buffer); buffer_free(git_config_path_buffer);
return NULL; return platforms;
} }