summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-08-06 12:23:03 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-08-06 12:23:03 -0300
commitc8a1f90c7a329b2e2a3c6622f9bddaea9ac56dda (patch)
tree75edbbde8c169955fafebbd74d61a7802db549f4
parent50a36e69eedaf9a4438d939e66ed5d09aa0f65c1 (diff)
git: Make use of `create_key_value_file()` in `create_info_file()`
This greatly simplifies the code for it, and helps make the codebase more generic.
-rw-r--r--git.c61
1 files changed, 7 insertions, 54 deletions
diff --git a/git.c b/git.c
index 8f99f53..e1ea501 100644
--- a/git.c
+++ b/git.c
@@ -415,39 +415,10 @@ int get_patch_list(void *ctx, void *data, const keyword *key, keyword_val *val)
}
}
-int get_info_len(pull_request *pr) {
- struct tm tm;
- int len = format_len("title: %s\n", pr->title);
-
- localtime_r(&pr->date, &tm);
-
- len += format_len("author: %s\n", pr->author);
- len += format_len("date: ") + strftime(NULL, -1, "%a %b %e %H:%M:%S %Y %z\n", &tm);
- len += format_len("description: %s\n", pr->desc);
- len += format_len("type: %i\n", pr->pr_type);
-
- /* Is this PR using a remote branch? */
- if (pr->pr_type) {
- len += format_len("branch: %s\n", pr->branch->name);
- len += format_len("repo: %s", pr->branch->repo);
- } else {
- len += format_len("patches:");
- for (int i = 0; pr->patches[i] != NULL; i++) {
- len += format_len(" %s", pr->patches[i]->name);
- }
- }
- return len;
-
-}
-
int create_info_file(pull_request *pr, const char *pr_root) {
- int len;
- int buf_len;
- int j = 0;
char *filename;
char *file_buf;
FILE *fp;
- struct tm tm;
/* Is the PR NULL? */
if (pr == NULL) {
@@ -460,30 +431,9 @@ int create_info_file(pull_request *pr, const char *pr_root) {
return 0;
}
- localtime_r(&pr->date, &tm);
-
- len = strlen(pr_root) + strlen("/info");
- buf_len = get_info_len(pr);
- filename = calloc(len+1, sizeof(char));
- file_buf = calloc(buf_len+1, sizeof(char));
-
- j = sprintf(file_buf, "title: %s\n", pr->title);
- j += sprintf(file_buf+j, "author: %s\n", pr->author);
- j += sprintf(file_buf+j, "date: ");
- j += strftime(file_buf+j, -1, "%a %b %e %H:%M:%S %Y %z\n", &tm);
- j += sprintf(file_buf+j, "description: %s\n", pr->desc);
- j += sprintf(file_buf+j, "type: %i\n", pr->pr_type);
-
- /* Is this PR using a remote branch? */
- if (pr->pr_type) {
- j += sprintf(file_buf+j, "branch: %s\n", pr->branch->name);
- j += sprintf(file_buf+j, "repo: %s", pr->branch->repo);
- } else {
- j += sprintf(file_buf+j, "patches:");
- for (int i = 0; pr->patches[i] != NULL; i++) {
- j += sprintf(file_buf+j, " %s", pr->patches[i]->name);
- }
- }
+ filename = calloc(format_len("%s/info", pr_root), sizeof(char));
+ /* Create the contents of the new info file. */
+ file_buf = create_key_value_file(pr, (void *)pr_root, info_keywords, &(const delimiter){":", 0, 1}, &(const delimiter){"\n", 0, 0});
/* Append /info to the PR root. */
sprintf(filename, "%s/info", pr_root);
@@ -521,9 +471,12 @@ int create_info_file(pull_request *pr, const char *pr_root) {
return 0;
}
- fwrite(file_buf, sizeof(char), buf_len, fp);
+ fwrite(file_buf, sizeof(char), strlen(file_buf), fp);
fclose(fp);
+ free(filename);
+ free(file_buf);
+
return 1;
}