summaryrefslogtreecommitdiff
path: root/git.h
blob: 76be91e73dd0465bf97365fcd6ff28b4ab54e731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef GIT_H
#define GIT_H

#include <git2.h>
#include <stdint.h>
#include <time.h>
#include "index.h"
#include "keyword.h"

typedef struct pull_request pull_request;
typedef struct file file;
typedef struct comment comment;
typedef struct git_branch git_branch;
typedef struct git_repo git_repo;

struct file {
	char *name;	/* Name of file. */
	char *buf;	/* Buffer containing contents of file. */
};

struct comment {
	char *author;	/* Author of comment. */
	char *desc;	/* Description/Contents of comment. */
	time_t date;	/* Date of creation. */
	int id;		/* Comment ID. */
	comment *reply;	/* Comment that is being replied to. */
};

struct git_branch {
	char *name;	/* Name of branch. */
	char *repo;	/* Path, or URL to local, or remote repository. */
};

struct git_repo {
	char *name;		/* Name of git repository. */
	git_repository *repo;	/* Git repository. */
};

struct pull_request {
	char *title;			/* Title of the pull request. */
	char *desc;			/* Description of the pull request. */
	char *author;			/* Author of the pull request. */
	comment **comments;		/* Comments of this pull reuqest. */
	time_t date;			/* Date of creation. */
	uint8_t pr_type;		/* Type of pull request. (0 = individual commit patches, 1 = remote branch) */
	union {
		file **patches;		/* Patch files of pull request. */
		git_branch *branch;	/* Branch of pull request. */
	};
	char *merge_branch;		/* Branch to merge pull request with. */
};

extern void cleanup_git(git_repo **repos);
extern void cleanup_pull_request(pull_request *pr);
extern git_repo **init_git(config *cfg);
extern pull_request *get_pull_request(index_t *idx, const char *root, const char *repo);
extern int add_comment(comment *comment, const char *pr_root);
extern git_repo *get_repo(git_repo **repos, const char *name);
extern int create_pull_request_branch(pull_request *pr, index_t *idx, git_repo *repo);
extern int create_pull_request_dir(pull_request *pr, index_t *idx, const char *root, const char *repo);

int parse_remote_branch(void *ctx, void *ret, const keyword *key, keyword_val val);
int parse_patch_list(void *ctx, void *ret, const keyword *key, keyword_val val);
int parse_comment_reply(void *ctx, void *ret, const keyword *key, keyword_val val);


int get_pr_type(void *ctx, void *data, const keyword *key, keyword_val *val);
int has_remote_branch(void *ctx, void *ret, const keyword *key, keyword_val *val);
int get_patch_list(void *ctx, void *data, const keyword *key, keyword_val *val);
int get_comment_reply(void *ctx, void *data, const keyword *key, keyword_val *val);

#define offset_list(...) (size_t []){__VA_ARGS__, -1}
static const keyword *info_keywords[] = {
	&(const keyword){"title", "Title of Pull Request.", NULL, TYPE_STRING, offset_list(offsetof(pull_request, title)), NULL, NULL},
	&(const keyword){"author", "Author of Pull Request.", NULL, TYPE_STRING, offset_list(offsetof(pull_request, author)), NULL, NULL},
	&(const keyword){"date", "Date of Pull Request's creation.", "%a %b %e %H:%M:%S %Y %z", TYPE_TIME, offset_list(offsetof(pull_request, date)), NULL, NULL},
	&(const keyword){"description", "Description of Pull Request.", NULL, TYPE_STRING, offset_list(offsetof(pull_request, desc)), NULL, NULL},
	&(const keyword){"type", "Type of Pull Request (0 = patch files, 1 = remote repo).", NULL, TYPE_INT, offset_list(offsetof(pull_request, pr_type)), NULL, get_pr_type},
	&(const keyword){"branch", "Branch of remote repo.", NULL, TYPE_STRING, offset_list(offsetof(pull_request, branch), offsetof(git_branch, name)), parse_remote_branch, has_remote_branch},
	&(const keyword){"repo", "URL of remote repo.", NULL, TYPE_STRING, offset_list(offsetof(pull_request, branch), offsetof(git_branch, repo)), parse_remote_branch, has_remote_branch},
	&(const keyword){"patches", "Patch file(s) of Pull Request.", NULL, TYPE_STRING, offset_list(offsetof(pull_request, patches), offsetof(file, name)), parse_patch_list, get_patch_list},
	&(const keyword){"merge-branch", "Branch to merge Pull Request with.", NULL, TYPE_STRING, offset_list(offsetof(pull_request, merge_branch)), NULL},
	NULL,
};

static const keyword *comment_keywords[] = {
	&(const keyword){"id", "ID of Comment.", NULL, TYPE_INT, offset_list(offsetof(comment, id)), NULL},
	&(const keyword){"author", "Author of Comment.", NULL, TYPE_STRING, offset_list(offsetof(comment, author)), NULL},
	&(const keyword){"date", "Date of Comment's creation.", "%a %b %e %H:%M:%S %Y %z", TYPE_TIME, offset_list(offsetof(comment, date)), NULL},
	&(const keyword){"reply-to", "ID of replied Comment.", NULL, TYPE_INT, offset_list(offsetof(comment, reply), offsetof(comment, id)), parse_comment_reply, get_comment_reply},
	&(const keyword){"reply-author", "Author of replied Comment.", NULL, TYPE_STRING, offset_list(offsetof(comment, reply), offsetof(comment, author)), parse_comment_reply, get_comment_reply},
	&(const keyword){"reply-date", "Date of replied Comment's creation", "%a %b %e %H:%M:%S %Y %z", TYPE_TIME, offset_list(offsetof(comment, reply), offsetof(comment, date)), parse_comment_reply, get_comment_reply},
	&(const keyword){"description", "Message of Comment.", NULL, TYPE_STRING, offset_list(offsetof(comment, desc)), NULL, NULL},
	NULL,
};
#undef offset_list

#endif