From 0d74accf6fca824570fdb05b9a7644bf52fdb172 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Fri, 25 Jun 2021 20:36:47 -0400 Subject: Allow `create_pull_request_dir()` to use either the ID, or title of the PR as the directory name. --- git.c | 103 +++++++++++++++++++++++++----------------------------------------- 1 file changed, 39 insertions(+), 64 deletions(-) diff --git a/git.c b/git.c index 418a525..4a35165 100644 --- a/git.c +++ b/git.c @@ -68,79 +68,54 @@ pull_request *get_pull_request(int id, const char *root) { } -int create_pull_request_dir(pull_request *pr, const char *root) { - int id = 0; - int is_num = 0; - DIR *dir = opendir(root); - struct dirent entry, *result; - - while (readdir_r(dir, &entry, &result) == 0 && result != NULL) { - if (entry.d_type == DT_DIR) { - char *tmp = entry.d_name; - - /* Is this a blank directory name? */ - if (*tmp == '\0') { - continue; - } - - for (is_num = 1; *tmp && is_num; is_num = isdigit(*tmp++)); - - - /* Is this a numbered directory? */ - if (is_num) { - int val = strtol(entry.d_name, NULL, 0); - /* Set the current ID to the numbered directory, if it's larger than it. */ - id = (id < val) ? val : id; - } - } +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; + /* 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)); + + /* Is there no existing directory? */ + if (stat(pr_dir, &st) < -1) { + /* Create a directory with a name of the stringified ID. */ + mkdir(pr_dir, 0755); } - /* Did we find any numbered directories? */ - if (id > 0) { - struct stat st; - file **commits = (pr->pr_type) ? get_branch_commits(pr->branch) : pr->patches; - /* Get the length of the stringified ID path. */ - int len = snprintf(NULL, 0, "%s/%i", root, id); - /* Convert the ID to a string. */ - char *pr_dir = calloc(len+1, sizeof(char)); - sprintf(pr_dir, "%s/%i", root, id); - - /* Is there no existing directory? */ - if (stat(pr_dir, &st) < -1) { - /* Create a directory with a name of the stringified ID. */ - mkdir(pr_dir, 0755); - } - - /* Make patch files for each commit of PR. */ - for (int i = 0; commits[i] != NULL; i++) { - FILE *fp; - /* Does this patch file exists? */ - if (access(commits[i]->name, F_OK) == 0) { - long size = 0; - char *buf = read_file(commits[i]->name, &size); - /* Did we read the file? */ - if (buf != NULL) { - /* Are the contents of the patch file the same? */ - if (strcmp(commits[i]->buf, buf) == 0) { - /* Skip creating this patch file. */ - free(buf); - continue; - } + /* Make patch files for each commit of PR. */ + for (int i = 0; commits[i] != NULL; i++) { + FILE *fp; + /* Does this patch file exists? */ + if (access(commits[i]->name, F_OK) == 0) { + long size = 0; + char *buf = read_file(commits[i]->name, &size); + /* Did we read the file? */ + if (buf != NULL) { + /* Are the contents of the patch file the same? */ + if (strcmp(commits[i]->buf, buf) == 0) { + /* Skip creating this patch file. */ free(buf); - } else { continue; } + free(buf); + } else { + continue; } - - /* Create the format patch file for this commit. */ - fp = fopen(commits[i]->name, "w"); - fwrite(commits[i]->buf, sizeof(char), strlen(commits[i]->buf), fp); - fclose(fp); } - /* TODO: Implement rest of code. */ + + /* Create the format patch file for this commit. */ + fp = fopen(commits[i]->name, "w"); + fwrite(commits[i]->buf, sizeof(char), strlen(commits[i]->buf), fp); + fclose(fp); + } + + /* TODO: Implement rest of code. */ + free(pr_dir); + + if (pr->pr_type) { + free_files(commits); } - return id; + return ret; } git_repository **init_git(config *cfg) { -- cgit v1.2.3-13-gbd6f