From 93c6d10ea0048a32009395a2154978ce3f538da7 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Wed, 27 Jul 2022 08:48:22 -0300 Subject: git: Replace all uses of `git_repo` with the more generic `linked_list` We don't need a type specific linked list anymore, so we can just get rid of it, plus it makes the code much cleaner in the process. --- git.c | 44 +++++++++----------------------------------- git.h | 6 ------ 2 files changed, 9 insertions(+), 41 deletions(-) diff --git a/git.c b/git.c index 4ba9f97..ed4805a 100644 --- a/git.c +++ b/git.c @@ -12,21 +12,11 @@ #include #include "config.h" #include "git.h" +#include "linked_list.h" #include "macros.h" #include "misc.h" -git_repo *add_repo(git_repository *repo, git_repo **first, git_repo **last) { - git_repo *new_repo = calloc(1, sizeof(git_repo)); - (*last != NULL) ? ((*last)->next = new_repo) : (*first = new_repo); - - new_repo->repo = repo; - - *last = new_repo; - - return new_repo; -} - int find_ignore_file(const char *path) { DIR *root = opendir(path); struct dirent entry, *result; @@ -41,16 +31,6 @@ int find_ignore_file(const char *path) { return 0; } -void cleanup_linked_list(git_repo *root) { - if (root != NULL) { - git_repo *repo = root; - cleanup_linked_list(root->next); - repo->repo = NULL; - repo->next = NULL; - free(repo); - } -} - void cleanup_git_repos(git_repository **repos) { for (int i = 0; repos[i] != NULL; i++) { git_repository_free(repos[i]); @@ -393,11 +373,9 @@ int create_pull_request_dir(pull_request *pr, index *idx, const char *root) { git_repository **init_git(config *cfg) { git_repository **repos = NULL; - git_repository *repo = NULL; - git_repo *first = NULL, *last = NULL, *current = NULL; + linked_list *repo_list = NULL; DIR *root = opendir(cfg->git_root); struct dirent entry, *result; - int repo_count = 0; log(LOG_INFO, "Initializing libgit2."); int ret = git_libgit2_init(); @@ -420,13 +398,13 @@ git_repository **init_git(config *cfg) { sprintf(repo_dir, "%s/%s", cfg->git_root, entry.d_name); /* Was no ignore file found? */ if (!find_ignore_file(repo_dir)) { + git_repository *repo = NULL; /* Did we fail to open the git repo? */ if (git_repository_open(&repo, repo_dir)) { log(LOG_ERR, "Failed to open git repository %s, ignoring.", entry.d_name); } else { log(LOG_INFO, "Successfully opened git repository %s", entry.d_name); - current = add_repo(repo, &first, &last); - repo_count++; + repo_list = add_node(&repo_list, repo); } } free(repo_dir); @@ -435,20 +413,16 @@ git_repository **init_git(config *cfg) { } /* Did we find any repos? */ - if (repo_count) { + if (repo_list != NULL) { + int repo_count = linked_list_size(repo_list); log(LOG_INFO, "Found, and opened %i repositories.", repo_count); /* Allocate repo_count + 1 git repos, since we need a NULL entry to denote the end. */ repos = calloc(repo_count+1, sizeof(git_repository *)); /* Add the repos to the array. */ - int i = 0; - for (git_repo *r = first; r != NULL; /*repos[i] = r->repo,*/ r = r->next, i++) { - repos[i] = r->repo; + for (linked_list *node = repo_list; node != NULL; /*repos[repo_count] = (git_repository *)node->data,*/ node = node->prev, --repo_count) { + repos[repo_count] = (git_repository *)node->data; } - cleanup_linked_list(first); - first = NULL; - last = NULL; - current = NULL; - + cleanup_linked_list(repo_list); } else { log(LOG_ERR, "Couldn't find, and/or open any repositories."); } diff --git a/git.h b/git.h index 351b92d..dd770e2 100644 --- a/git.h +++ b/git.h @@ -5,17 +5,11 @@ #include #include -typedef struct git_repo git_repo; typedef struct pull_request pull_request; typedef struct file file; typedef struct comment comment; typedef struct git_branch git_branch; -struct git_repo { - git_repo *next; - git_repository *repo; -}; - struct file { char *name; /* Name of file. */ char *buf; /* Buffer containing contents of file. */ -- cgit v1.2.3-13-gbd6f