File: keyword.c

package info (click to toggle)
got 0.119-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,448 kB
  • sloc: ansic: 124,378; sh: 50,814; yacc: 4,353; makefile: 2,241; perl: 357
file content (264 lines) | stat: -rw-r--r-- 6,124 bytes parent folder | download | duplicates (2)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (c) 2023 Mark Jamsek <mark@jamsek.dev>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "got_compat.h"

#include <sys/queue.h>

#include <ctype.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "got_reference.h"
#include "got_error.h"
#include "got_object.h"
#include "got_repository.h"
#include "got_cancel.h"
#include "got_worktree.h"
#include "got_commit_graph.h"
#include "got_keyword.h"

struct keyword_mod {
	char		*kw;
	uint64_t	 n;
	uint8_t		 sym;
	uint8_t		 iskeyword;
	uint8_t		 ismodified;
};

#define GOT_KEYWORD_DESCENDANT	'+'
#define GOT_KEYWORD_ANCESTOR	'-'

static const struct got_error *
parse_keyword(struct keyword_mod *kwm, const char *keyword)
{
	const char	*kw;
	char		*p;

	if (keyword == NULL)
		return NULL;

	/* check if it is a (modified) keyword or modified reference */
	if (*keyword == ':') {
		kwm->iskeyword = 1;
		kw = keyword + 1;
	} else
		kw = keyword;

	kwm->kw = strdup(kw);
	if (kwm->kw == NULL)
		return got_error_from_errno("strdup");

	p = strchr(kwm->kw, ':');

	if (p != NULL) {
		*p = '\0';
		++p;
		if (*p != GOT_KEYWORD_DESCENDANT && *p != GOT_KEYWORD_ANCESTOR)
			return got_error_fmt(GOT_ERR_BAD_KEYWORD,
			    "'%s'", keyword);

		kwm->ismodified = 1;
		kwm->sym = *p;
		++p;

		if (*p) {
			const char	*errstr;
			long long	 n;

			n = strtonum(p, 0, LLONG_MAX, &errstr);
			if (errstr != NULL)
				return got_error_fmt(GOT_ERR_BAD_KEYWORD,
				    "'%s'", keyword);

			kwm->n = n;
		} else
			kwm->n = 1;	/* :(+/-) == :(+/-)1 */
	}

	return NULL;
}

const struct got_error *
got_keyword_to_idstr(char **ret, const char *keyword,
    struct got_repository *repo, struct got_worktree *wt)
{
	const struct got_error		*err = NULL;
	struct got_commit_graph		*graph = NULL;
	struct got_object_id		*head_id = NULL, *kwid = NULL;
	struct got_object_id		 iter_id;
	struct got_reflist_head		 refs;
	struct got_object_id_queue	 commits;
	struct got_object_qid		*qid;
	struct keyword_mod		 kwm;
	const char			*kw = NULL;
	char				*kwid_str = NULL;
	uint64_t			 n = 0;

	*ret = NULL;
	TAILQ_INIT(&refs);
	STAILQ_INIT(&commits);
	memset(&kwm, 0, sizeof(kwm));

	err = parse_keyword(&kwm, keyword);
	if (err != NULL)
		goto done;

	kw = kwm.kw;

	if (kwm.iskeyword) {
		if (strcmp(kw, GOT_KEYWORD_BASE) == 0) {
			if (wt == NULL) {
				err = got_error_msg(GOT_ERR_NOT_WORKTREE,
				    "'-c :base' requires work tree");
				goto done;
			}

			err = got_object_id_str(&kwid_str,
			    got_worktree_get_base_commit_id(wt));
			if (err != NULL)
				goto done;
		} else if (strcmp(kw, GOT_KEYWORD_HEAD) == 0) {
			struct got_reference *head_ref;

			err = got_ref_open(&head_ref, repo, wt != NULL ?
			    got_worktree_get_head_ref_name(wt) :
			    GOT_REF_HEAD, 0);
			if (err != NULL)
				goto done;

			kwid_str = got_ref_to_str(head_ref);
			got_ref_close(head_ref);
			if (kwid_str == NULL) {
				err = got_error_from_errno("got_ref_to_str");
				goto done;
			}
		} else {
			err = got_error_fmt(GOT_ERR_BAD_KEYWORD, "'%s'", kw);
			goto done;
		}
	} else if (kwm.ismodified) {
		/* reference:[(+|-)[N]] */
		kwid_str = strdup(kw);
		if (kwid_str == NULL) {
			err = got_error_from_errno("strdup");
			goto done;
		}
	} else
		goto done;

	if (kwm.n == 0)
		goto done;	/* unmodified keyword */

	err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
	if (err)
		goto done;

	err = got_repo_match_object_id(&kwid, NULL, kwid_str,
	    GOT_OBJ_TYPE_COMMIT, &refs, repo);
	if (err != NULL)
		goto done;

	/*
	 * If looking for a descendant, we need to iterate from
	 * HEAD so grab its id now if it's not already in kwid.
	 */
	if (kwm.sym == GOT_KEYWORD_DESCENDANT && kw != NULL &&
	    strcmp(kw, GOT_KEYWORD_HEAD) != 0) {
		struct got_reference *head_ref;

		err = got_ref_open(&head_ref, repo, wt != NULL ?
		    got_worktree_get_head_ref_name(wt) : GOT_REF_HEAD, 0);
		if (err != NULL)
			goto done;
		err = got_ref_resolve(&head_id, repo, head_ref);
		got_ref_close(head_ref);
		if (err != NULL)
			goto done;
	}

	err = got_commit_graph_open(&graph, "/", 1);
	if (err)
		goto done;

	err = got_commit_graph_bfsort(graph,
	    head_id != NULL ? head_id : kwid, repo, NULL, NULL);
	if (err)
		goto done;

	while (n <= kwm.n) {
		err = got_commit_graph_iter_next(&iter_id, graph, repo,
		    NULL, NULL);
		if (err) {
			if (err->code == GOT_ERR_ITER_COMPLETED)
				err = NULL;
			break;
		}

		if (kwm.sym == GOT_KEYWORD_DESCENDANT) {
			/*
			 * We want the Nth generation descendant of KEYWORD,
			 * so queue all commits from HEAD to KEYWORD then we
			 * can walk from KEYWORD to its Nth gen descendent.
			 */
			err = got_object_qid_alloc(&qid, &iter_id);
			if (err)
				goto done;
			STAILQ_INSERT_HEAD(&commits, qid, entry);

			if (got_object_id_cmp(&iter_id, kwid) == 0)
				break;
			continue;
		}
		++n;
	}

	if (kwm.sym == GOT_KEYWORD_DESCENDANT) {
		n = 0;

		STAILQ_FOREACH(qid, &commits, entry) {
			if (qid == STAILQ_LAST(&commits, got_object_qid, entry)
			    || n == kwm.n)
				break;
			++n;
		}

		memcpy(&iter_id, &qid->id, sizeof(iter_id));
	}

	free(kwid_str);
	err = got_object_id_str(&kwid_str, &iter_id);

done:
	free(kwid);
	free(kwm.kw);
	free(head_id);
	got_ref_list_free(&refs);
	got_object_id_queue_free(&commits);
	if (graph != NULL)
		got_commit_graph_close(graph);

	if (err != NULL) {
		free(kwid_str);
		return err;
	}

	*ret = kwid_str;
	return NULL;
}