diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2022-07-13 16:08:59 -0300 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2022-07-13 16:41:50 -0300 |
commit | b71a05e0bacff4f03fe75fba22197c3100d58c09 (patch) | |
tree | 24a4398d58282d05b5d31dbac697da2ff322652f /git.c | |
parent | b93ca4a3a70cad569e5ab8056923f90043b4b0e9 (diff) |
git: Use an index instead of an id for creating PR directories
PR's will now be identified by index, rather than by id.
If no index is found, then it falls back to using the PR title as the
directory name.
Diffstat (limited to 'git.c')
-rw-r--r-- | git.c | 93 |
1 files changed, 84 insertions, 9 deletions
@@ -284,26 +284,101 @@ file **get_branch_commits(git_branch *br) { return NULL; } -int create_pull_request_dir(pull_request *pr, int id, const char *root) { - int ret = (id >= 0) ? id : 0; +int is_valid_index(index *idx, char **reason) { + int did_alloc = 0; + char *err_str = NULL; + + /* Is the index NULL? */ + if (idx == NULL) { + err_str = "Index is NULL."; + /* Is the index type invalid? */ + } else if (idx->type > 1) { + did_alloc = 1; + err_str = calloc(format_len("Invalid index type: %u.", idx->type)+1, sizeof(char)); + sprintf(err_str, "Invalid index type: %u.", idx->type); + /* Is this named index empty? */ + } else if (idx->type == 0 && is_empty(idx->name)) { + err_str = "Named index is empty."; + } + + /* Did we get an error? */ + if (err_str != NULL) { + *reason = err_str; + /* Return -1 if error string was allocated, and false if not. */ + return (did_alloc) ? -1 : 0; + /* Is there another index after this one? */ + } else if (idx->next != NULL) { + /* Return the validity of the next index. */ + return is_valid_index(idx->next, reason); + } + /* Valid index, return true. */ + return 1; +} + +char *make_index_path(const char *root, index *idx, int path_type) { + char *path; + char *tmp; + int path_len = strlen(root) + 1; + /* Is the index invalid? */ + const int valid_index = is_valid_index(idx, &reason); + if (valid_index <= 0) { + log_reason(LOG_NOTICE, "Invalid index.", reason); + /* Was the reason string allocated? */ + if (valid_index < 0) { + free(reason); + } + return NULL; + } + + /* Get the length of the path. */ + for (index *i = idx; i != NULL; i = i->next) { + const int is_last = (i->next == NULL); + const char *fmt = (i->type) "%llu" : "%s"; + path_len += format_len(fmt, (i->type) ? i->num : i->name) + !is_last; + } + + path = calloc(path_len+1, sizeof(char)); + tmp = path; + + /* Create the path. */ + tmp += sprintf(tmp, "%s/", root); + for (index *i = idx; i != NULL; i = i->next) { + const int is_last = (i->next == NULL); + const char *delm = (path_type) ? "-" : "/"; + const char *fmt = (i->type) "%llu%s" : "%s%s"; + tmp += sprintf(tmp, fmt, (i->type) ? i->num : i->name, (!is_last) ? delm : ""); + } + + return path; +} + +int create_pull_request_dir(pull_request *pr, index *idx, const char *root) { + int ret = 0; struct stat st; file **commits; char *pr_dir; + char *reason; /* Is this a NULL PR? */ if (pr == NULL) { log(LOG_ERR, "Pull Request is NULL."); return -1; } - /* Is the ID negative, and the PR title blank, or NULL? */ - if (id < 0 && (pr->title == NULL || pr->title[0] == '\0')) { - log(LOG_ERR, "Negative ID, and either blank, or NULL Pull Request title."); - return -1; - } - /* Get the path of the PR's directory. */ - pr_dir = (pr->title != NULL && pr->title[0] != '\0') ? sanitized_dir_path_name(root, pr->title) : dir_path_num(root, id); + /* Use the index as the directory path. */ + pr_dir = make_index_path(root, idx); + /* Did we fail to make an indexed path? */ + if (pr_dir == NULL) { + /* Is the PR title empty? */ + if (is_empty(pr->title)) { + log(LOG_ERR, "Empty PR title."); + return -1; + } + + /* Use the title of the PR as the directory path. */ + pr_dir = sanitized_dir_path_name(root, pr->title); + } /* Is there no existing directory? */ if (stat(pr_dir, &st) < 0) { |