diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2022-07-26 17:46:54 -0300 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2022-07-26 17:46:54 -0300 |
commit | 45b91d6057fc67db1491d301cf44f4f93764c783 (patch) | |
tree | 4649e17b0d569136e48ec20cc9c91a8026218e31 | |
parent | 6fec7170544009fe3ce95b0d2b7e6ab65c97922d (diff) |
git, index: Move index related functions to `index.{c,h}`
-rw-r--r-- | git.c | 65 | ||||
-rw-r--r-- | git.h | 1 | ||||
-rw-r--r-- | index.c | 70 | ||||
-rw-r--r-- | index.h | 20 |
4 files changed, 90 insertions, 66 deletions
@@ -284,71 +284,6 @@ file **get_branch_commits(git_branch *br) { return NULL; } -int is_valid_index(index *idx) { - int did_alloc = 0; - char *err_str = NULL; - - /* Is the index NULL? */ - if (idx == NULL) { - err_str = "Index is NULL."; - /* Is the index type invalid? */ - } else if (idx->type > 1) { - did_alloc = 1; - err_str = calloc(format_len("Invalid index type: %u.", idx->type)+1, sizeof(char)); - sprintf(err_str, "Invalid index type: %u.", idx->type); - /* Is this named index empty? */ - } else if (idx->type == 0 && is_empty(idx->name)) { - err_str = "Named index is empty."; - } - - /* Did we get an error? */ - if (err_str != NULL) { - log_reason(LOG_ERR, "Invalid index.", reason); - if (did_alloc) { - free(err_str); - } - /* Invalid index, return false. */ - return 0; - /* Is there another index after this one? */ - } else if (idx->next != NULL) { - /* Return the validity of the next index. */ - return is_valid_index(idx->next); - } - /* Valid index, return true. */ - return 1; -} - -char *make_index_path(const char *root, index *idx, int path_type) { - char *path; - char *tmp; - int path_len = strlen(root) + 1; - /* Is the index invalid? */ - if (!is_valid_index(idx)) { - return NULL; - } - - /* Get the length of the path. */ - for (index *i = idx; i != NULL; i = i->next) { - const int is_last = (i->next == NULL); - const char *fmt = (i->type) "%llu" : "%s"; - path_len += format_len(fmt, (i->type) ? i->num : i->name) + !is_last; - } - - path = calloc(path_len+1, sizeof(char)); - tmp = path; - - /* Create the path. */ - tmp += sprintf(tmp, "%s/", root); - for (index *i = idx; i != NULL; i = i->next) { - const int is_last = (i->next == NULL); - const char *delm = (path_type) ? "-" : "/"; - const char *fmt = (i->type) "%llu%s" : "%s%s"; - tmp += sprintf(tmp, fmt, (i->type) ? i->num : i->name, (!is_last) ? delm : ""); - } - - return path; -} - int create_pull_request_dir(pull_request *pr, index *idx, const char *root) { int ret = 0; struct stat st; @@ -7,7 +7,6 @@ typedef struct git_repo git_repo; typedef struct pull_request pull_request; -typedef struct index index; typedef struct file file; typedef struct comment comment; typedef struct git_branch git_branch; @@ -0,0 +1,70 @@ +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include "index.h" +#include "macros.h" + +int is_valid_index(index *idx) { + int did_alloc = 0; + char *err_str = NULL; + + /* Is the index NULL? */ + if (idx == NULL) { + err_str = "Index is NULL."; + /* Is the index type invalid? */ + } else if (idx->type > 1) { + did_alloc = 1; + err_str = calloc(format_len("Invalid index type: %u.", idx->type)+1, sizeof(char)); + sprintf(err_str, "Invalid index type: %u.", idx->type); + /* Is this named index empty? */ + } else if (idx->type == 0 && is_empty(idx->name)) { + err_str = "Named index is empty."; + } + + /* Did we get an error? */ + if (err_str != NULL) { + log_reason(LOG_ERR, "Invalid index.", reason); + if (did_alloc) { + free(err_str); + } + /* Invalid index, return false. */ + return 0; + /* Is there another index after this one? */ + } else if (idx->next != NULL) { + /* Return the validity of the next index. */ + return is_valid_index(idx->next); + } + /* Valid index, return true. */ + return 1; +} + +char *make_index_path(const char *root, index *idx, int path_type) { + char *path; + char *tmp; + int path_len = strlen(root) + 1; + /* Is the index invalid? */ + if (!is_valid_index(idx)) { + return NULL; + } + + /* Get the length of the path. */ + for (index *i = idx; i != NULL; i = i->next) { + const int is_last = (i->next == NULL); + const char *fmt = (i->type) "%llu" : "%s"; + path_len += format_len(fmt, (i->type) ? i->num : i->name) + !is_last; + } + + path = calloc(path_len+1, sizeof(char)); + tmp = path; + + /* Create the path. */ + tmp += sprintf(tmp, "%s/", root); + for (index *i = idx; i != NULL; i = i->next) { + const int is_last = (i->next == NULL); + const char *delm = (path_type) ? "-" : "/"; + const char *fmt = (i->type) "%llu%s" : "%s%s"; + tmp += sprintf(tmp, fmt, (i->type) ? i->num : i->name, (!is_last) ? delm : ""); + } + + return path; +} @@ -0,0 +1,20 @@ +#ifndef INDEX_H +#define INDEX_H + +#include <stdint.h> + +typedef struct index index; + +struct index { + uint8_t type; /* Index type. (0 = Named index, 1 = Numbered index) */ + union { + char *name; /* Named index. */ + uint64_t num; /* Numbered index. */ + }; + index *next; /* Next index. */ +}; + +extern int is_valid_index(index *idx); +extern char *make_index_path(const char *root, index *idx, int path_type); + +#endif |