From e33f48a1797691ae0432e2b90bf0755c1493d8a4 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Fri, 25 Jun 2021 20:44:31 -0400 Subject: Add error checking, and move the code for getting the PR path, and getting the patch files to after the error checks. --- git.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/git.c b/git.c index 4a35165..52ef13a 100644 --- a/git.c +++ b/git.c @@ -71,9 +71,27 @@ pull_request *get_pull_request(int id, const char *root) { int create_pull_request_dir(pull_request *pr, int id, const char *root) { int ret = id; struct stat st; - file **commits = (pr->pr_type) ? get_branch_commits(pr->branch) : pr->patches; + file **commits; + char *pr_dir; + + /* 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; + } + /* Did we fail to create the info file? */ + if (!create_info(pr, pr_dir)) { + log(LOG_ERR, "Failed to create info file."); + return -1; + } + /* Get the path of the PR's directory. */ - char *pr_dir = (id >= 0) ? dir_path_num(root, id) : dir_path_name(root, sanitize_str(pr->title)); + pr_dir = (pr->title != NULL && pr->title[0] != '\0') ? dir_path_name(root, sanitize_str(pr->title)) : dir_path_num(root, id); /* Is there no existing directory? */ if (stat(pr_dir, &st) < -1) { @@ -81,6 +99,9 @@ int create_pull_request_dir(pull_request *pr, int id, const char *root) { mkdir(pr_dir, 0755); } + /* Get the patch files. */ + commits = (pr->pr_type) ? get_branch_commits(pr->branch) : pr->patches; + /* Make patch files for each commit of PR. */ for (int i = 0; commits[i] != NULL; i++) { FILE *fp; -- cgit v1.2.3-13-gbd6f