summaryrefslogtreecommitdiff
path: root/git.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-07-27 08:48:22 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-07-27 08:58:27 -0300
commit93c6d10ea0048a32009395a2154978ce3f538da7 (patch)
tree67ef5aaccdf2efbec9898a1e322c23b3b6949f71 /git.c
parent1d6d32d044e67fc984e5c1b8361460844fec52db (diff)
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.
Diffstat (limited to 'git.c')
-rw-r--r--git.c44
1 files changed, 9 insertions, 35 deletions
diff --git a/git.c b/git.c
index 4ba9f97..ed4805a 100644
--- a/git.c
+++ b/git.c
@@ -12,21 +12,11 @@
#include <unistd.h>
#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.");
}