From 31bb4276c730c70fcdaa89d270d32ab15c4975d2 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Sat, 30 Jul 2022 14:21:31 -0300 Subject: git: Add `cleanup_pull_request()` --- git.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ git.h | 1 + 2 files changed, 69 insertions(+) diff --git a/git.c b/git.c index befdaa3..ea1b59a 100644 --- a/git.c +++ b/git.c @@ -72,6 +72,74 @@ void free_files(file **files) { free(files); } +static void free_git_branch(git_branch *branch) { + if (branch->name != NULL) { + free(branch->name); + branch->name = NULL; + } + if (branch->repo != NULL) { + free(branch->repo); + branch->repo = NULL; + } +} + +static void free_comment(comment *comment) { + if (comment->author != NULL) { + free(comment->author); + comment->author = NULL; + } + if (comment->desc != NULL) { + free(comment->desc); + comment->desc = NULL; + } + comment->reply = NULL; +} + +static void free_comments(comment **comments) { + for (int i = 0; comments[i] != NULL; ++i) { + free_comment(comments[i]); + comments[i] = NULL; + } + free(comments); +} + +void cleanup_pull_request(pull_request *pr) { + if (pr != NULL) { + if (pr->title != NULL) { + free(pr->title); + pr->title = NULL; + } + if (pr->desc != NULL) { + free(pr->desc); + pr->desc = NULL; + } + if (pr->author != NULL) { + free(pr->author); + pr->author = NULL; + } + if (pr->comments != NULL) { + free_comments(pr->comments); + pr->comments = NULL; + } + if (pr->merge_branch != NULL) { + free_git_branch(pr->merge_branch); + pr->merge_branch = NULL; + } + if (!pr->pr_type) { + if (pr->patches != NULL) { + free_files(pr->patches); + pr->patches = NULL; + } + } else { + if (pr->branch != NULL) { + free_git_branch(pr->branch); + pr->branch = NULL; + } + } + free(pr); + } +} + static linked_list *parse_dsv_str(const char *str, const char *delm) { linked_list *tail = NULL; for (; *str != '\0'; str += strspn(str, delm)) { diff --git a/git.h b/git.h index 3bd79bd..ac3455b 100644 --- a/git.h +++ b/git.h @@ -45,6 +45,7 @@ struct pull_request { }; extern void cleanup_git(git_repository **repos); +extern void cleanup_pull_request(pull_request *pr); extern git_repository **init_git(config *cfg); extern pull_request *get_pull_request(index_t *idx, const char *root); extern int add_comment(comment *comment, const char *pr_root); -- cgit v1.2.3-13-gbd6f