summaryrefslogtreecommitdiff
path: root/pkgrel.c
blob: fe25fe4fd7a22e1e8fd30824d9d7d9cc98e83abe (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <git2.h>


enum match_result
{
	MATCH_RESULT_INIT,
	MATCH_RESULT_ROOT,
	MATCH_RESULT_COMMIT,
	MATCH_RESULT_ANYTHING,
	MATCH_RESULT_PKGVER,
};


static inline size_t minimum(size_t lhs, size_t rhs);
static char* relative_path_in_current_dir(git_repository* repo, const char* filename);
static int try_git(int err, const char* msg);
static int match_line(const git_diff_delta* delta, const git_diff_hunk* hunk, const git_diff_line* line, void* payload);


/// Returns the smaller value from *lhs* and *rhs*.
size_t
minimum(const size_t lhs, const size_t rhs)
{
	return lhs > rhs ? rhs : lhs;
}


/// Finds the relative path of *filename* from the workdir of *repo*.
char*
relative_path_in_current_dir(git_repository* repo, const char* filename)
{
	const long maximum = pathconf(".", _PC_PATH_MAX);
	size_t len = 0 > maximum ? 1024 : maximum;
	char* buf = NULL;
	char* cwd = NULL;
	for (; NULL == cwd && 1024 * 50 > len; len *= 2) {
		if (NULL == (cwd = realloc(buf, len))) {
			if (NULL != buf)
				free(buf);
			return NULL;
		}
		buf = cwd;
		cwd = getcwd(buf, len);
		if (NULL == cwd && ERANGE != errno) {
			free(buf);
			return NULL;
		}
	}
	const char* workdir = git_repository_workdir(repo);
	const size_t prefix = minimum(strlen(cwd), strlen(workdir));
	if (0 != strncmp(cwd, workdir, prefix)) {
		free(cwd);
		return NULL;
	}
	const size_t relative = strlen(cwd + prefix);
	if (NULL == (buf = malloc(relative + strlen(filename) + 2))) {
		free(cwd);
		return NULL;
	}
	strcpy(buf, cwd + prefix);
	if (0 < relative)
		strcat(buf, "/");
	strcat(buf, filename);
	free(cwd);
	return buf;
}


/// If *err* is considered erroneous in context of libgit2 return code, print a message describing it, and prefix it
/// with *msg* if it is not NULL.
int
try_git(const int err, const char* msg)
{
	if (0 > err) {
		const git_error* e = git_error_last();
		if (NULL != msg)
			dprintf(2, "%s: ", msg);
		dprintf(2, "%s\n", e->message);
		exit(1);
	}
	return err;
}


/// Callback for git_diff_foreach. Check each line reported by the diff looking for "pkgver" occurrance. If found, set
/// *payload* to MATCH_RESULT_PKGVER, otherwise set it to MATCH_RESULT_ANYTHING, but only if it is not set to
/// MATCH_RESULT_PKGVER already.
int
match_line(const git_diff_delta* delta, const git_diff_hunk* hunk, const git_diff_line* line, void* payload)
{
	(void) delta;
	(void) hunk;
	static const char* const PKGVER = "pkgver";
	static const size_t PKGVER_LEN = 6;
	enum match_result* match = (enum match_result*) payload;
	switch (line->origin) {
	case GIT_DIFF_LINE_ADDITION:
	case GIT_DIFF_LINE_DELETION:
		if (PKGVER_LEN <= line->content_len && 0 == strncmp(line->content, PKGVER, PKGVER_LEN))
			*match = MATCH_RESULT_PKGVER;
		else if (MATCH_RESULT_PKGVER != *match)
			*match = MATCH_RESULT_ANYTHING;
		break;
	}
	return 0;
}


/// Parses an optional output prefix from *args* and returns it.
const char*
parse_args(const int argc, char* argv[])
{
	int opt;
	const char* prefix = NULL;
	while (NULL == prefix && -1 != (opt = getopt(argc, argv, ":d"))) {
		switch (opt) {
		case 'd':
			prefix = "pkgrel=";
			break;
		default:
			prefix = argv[optind - 1];  // Use first option that is not -d as a normal prefix, too.
			break;
		}
	}
	if (NULL == prefix) {
		if (argc > optind)
			prefix = argv[optind];
		else
			prefix = "";
	}
	return prefix;
}


int
main(int argc, char* argv[])
{
	const char* prefix = parse_args(argc, argv);
	FILE* file = fopen("PKGBUILD", "r");
	if (NULL == file) {
		dprintf(2, "PKGBUILD not found or cannot be opened\n");
		exit(1);
	}
	fclose(file);
	git_libgit2_init();
	git_repository* repo = NULL;
	try_git(git_repository_open_ext(&repo, NULL, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), "opening repo");
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
#pragma GCC diagnostic pop
	diffopts.pathspec.strings = (char*[1]){relative_path_in_current_dir(repo, "PKGBUILD")};
	diffopts.pathspec.count = 1;
	diffopts.flags |= GIT_DIFF_SHOW_UNTRACKED_CONTENT;
	if (NULL == diffopts.pathspec.strings[0]) {
		dprintf(2, "finding PKGBUILD in repo\n");
		exit(1);
	}
	git_pathspec* pathspec = NULL;
	try_git(git_pathspec_new(&pathspec, &diffopts.pathspec), "creating pathspec");
	git_revwalk* walker = NULL;
	try_git(git_revwalk_new(&walker, repo), "creating revwalker");
	try_git(git_revwalk_push_head(walker), "finding head");
	git_oid oid;
	git_tree* earlier = NULL;
	git_tree* later = NULL;
	int relpkg = 1;
	enum match_result match = MATCH_RESULT_INIT;
	while (0 == git_revwalk_next(&oid, walker)) {
		git_commit* commit;
		try_git(git_commit_lookup(&commit, repo, &oid), "finding commit");
		if (0 == git_commit_parentcount(commit))
			match = MATCH_RESULT_ROOT;
		if (NULL != earlier)
			git_tree_free(earlier);
		earlier = later;
		try_git(git_commit_tree(&later, commit), "finding tree");
		git_diff* diff;
		if (NULL == earlier)
			try_git(git_diff_tree_to_workdir_with_index(&diff, repo, later, &diffopts), "diff");
		else
			try_git(git_diff_tree_to_tree(&diff, repo, later, earlier, &diffopts), "diff");
		if (MATCH_RESULT_INIT != match)
			match = MATCH_RESULT_COMMIT;
		try_git(git_diff_foreach(diff, NULL, NULL, NULL, &match_line, &match), "searching history");
		git_diff_free(diff);
		git_commit_free(commit);
		if (MATCH_RESULT_PKGVER == match)
			break;
		if (MATCH_RESULT_ANYTHING == match)
			relpkg++;
	}
	if (MATCH_RESULT_INIT == match) {
		dprintf(2, "PKGBUILD not found in history\n");
		exit(1);
	}
	else
		printf("%s%d\n", prefix, relpkg);
}