]>
Commit | Line | Data |
---|---|---|
ac250c56 | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
ac250c56 BS |
3 | * |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
7 | ||
8 | #include <assert.h> | |
9 | ||
10 | #include "common.h" | |
11 | #include "buffer.h" | |
244d2f6b | 12 | #include "tree.h" |
d00d5464 | 13 | #include "refdb.h" |
ac250c56 | 14 | |
a346992f | 15 | #include "git2.h" |
ac250c56 | 16 | |
0e8e5a61 | 17 | static int maybe_sha_or_abbrev(git_object** out, git_repository *repo, const char *spec, size_t speclen) |
8f17ed80 | 18 | { |
19 | git_oid oid; | |
8f17ed80 | 20 | |
21 | if (git_oid_fromstrn(&oid, spec, speclen) < 0) | |
22 | return GIT_ENOTFOUND; | |
23 | ||
24 | return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJ_ANY); | |
25 | } | |
3e82d6c6 | 26 | |
0e8e5a61 | 27 | static int maybe_sha(git_object** out, git_repository *repo, const char *spec) |
28 | { | |
29 | size_t speclen = strlen(spec); | |
30 | ||
31 | if (speclen != GIT_OID_HEXSZ) | |
32 | return GIT_ENOTFOUND; | |
33 | ||
34 | return maybe_sha_or_abbrev(out, repo, spec, speclen); | |
35 | } | |
36 | ||
37 | static int maybe_abbrev(git_object** out, git_repository *repo, const char *spec) | |
38 | { | |
39 | size_t speclen = strlen(spec); | |
40 | ||
41 | return maybe_sha_or_abbrev(out, repo, spec, speclen); | |
42 | } | |
43 | ||
b8748c12 | 44 | static int build_regex(regex_t *regex, const char *pattern) |
45 | { | |
46 | int error; | |
47 | ||
48 | if (*pattern == '\0') { | |
49 | giterr_set(GITERR_REGEX, "Empty pattern"); | |
cc146626 | 50 | return GIT_EINVALIDSPEC; |
b8748c12 | 51 | } |
52 | ||
53 | error = regcomp(regex, pattern, REG_EXTENDED); | |
54 | if (!error) | |
55 | return 0; | |
8480eef7 | 56 | |
cc146626 | 57 | error = giterr_set_regex(regex, error); |
58 | ||
b8748c12 | 59 | regfree(regex); |
60 | ||
cc146626 | 61 | return error; |
b8748c12 | 62 | } |
63 | ||
3e82d6c6 | 64 | static int maybe_describe(git_object**out, git_repository *repo, const char *spec) |
e7279381 | 65 | { |
e28dd29b | 66 | const char *substr; |
b8748c12 | 67 | int error; |
68 | regex_t regex; | |
e28dd29b | 69 | |
e28dd29b | 70 | substr = strstr(spec, "-g"); |
e28dd29b | 71 | |
3e82d6c6 | 72 | if (substr == NULL) |
73 | return GIT_ENOTFOUND; | |
8480eef7 | 74 | |
b8748c12 | 75 | if (build_regex(®ex, ".+-[0-9]+-g[0-9a-fA-F]+") < 0) |
76 | return -1; | |
3e82d6c6 | 77 | |
b8748c12 | 78 | error = regexec(®ex, spec, 0, NULL, 0); |
79 | regfree(®ex); | |
80 | ||
81 | if (error) | |
3e82d6c6 | 82 | return GIT_ENOTFOUND; |
83 | ||
0e8e5a61 | 84 | return maybe_abbrev(out, repo, substr+2); |
3e82d6c6 | 85 | } |
86 | ||
f672cd2a | 87 | static int revparse_lookup_object( |
88 | git_object **object_out, | |
89 | git_reference **reference_out, | |
90 | git_repository *repo, | |
91 | const char *spec) | |
3e82d6c6 | 92 | { |
93 | int error; | |
94 | git_reference *ref; | |
95 | ||
f672cd2a | 96 | error = maybe_sha(object_out, repo, spec); |
0e8e5a61 | 97 | if (!error) |
98 | return 0; | |
99 | ||
100 | if (error < 0 && error != GIT_ENOTFOUND) | |
101 | return error; | |
102 | ||
98d633cc | 103 | error = git_reference_dwim(&ref, repo, spec); |
e7279381 | 104 | if (!error) { |
f672cd2a | 105 | |
106 | error = git_object_lookup( | |
107 | object_out, repo, git_reference_target(ref), GIT_OBJ_ANY); | |
108 | ||
109 | if (!error) | |
110 | *reference_out = ref; | |
111 | ||
b5f90115 | 112 | return error; |
e28dd29b | 113 | } |
114 | ||
545b479a | 115 | if (error < 0 && error != GIT_ENOTFOUND) |
116 | return error; | |
117 | ||
f672cd2a | 118 | error = maybe_abbrev(object_out, repo, spec); |
545b479a | 119 | if (!error) |
120 | return 0; | |
121 | ||
d1b7921a | 122 | if (error < 0 && error != GIT_ENOTFOUND) |
123 | return error; | |
124 | ||
f672cd2a | 125 | error = maybe_describe(object_out, repo, spec); |
d1b7921a | 126 | if (!error) |
127 | return 0; | |
128 | ||
3e82d6c6 | 129 | if (error < 0 && error != GIT_ENOTFOUND) |
e7279381 | 130 | return error; |
e28dd29b | 131 | |
132 | giterr_set(GITERR_REFERENCE, "Refspec '%s' not found.", spec); | |
4de89ce7 | 133 | return GIT_ENOTFOUND; |
ac250c56 BS |
134 | } |
135 | ||
b8748c12 | 136 | static int try_parse_numeric(int *n, const char *curly_braces_content) |
a346992f | 137 | { |
a8122b5d | 138 | int32_t content; |
b8748c12 | 139 | const char *end_ptr; |
140 | ||
141 | if (git__strtol32(&content, curly_braces_content, &end_ptr, 10) < 0) | |
142 | return -1; | |
29f72aa6 | 143 | |
b8748c12 | 144 | if (*end_ptr != '\0') |
145 | return -1; | |
29f72aa6 | 146 | |
a8122b5d | 147 | *n = (int)content; |
b8748c12 | 148 | return 0; |
a346992f BS |
149 | } |
150 | ||
cc146626 | 151 | static int retrieve_previously_checked_out_branch_or_revision(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position) |
ac250c56 | 152 | { |
b8748c12 | 153 | git_reference *ref = NULL; |
e28dd29b | 154 | git_reflog *reflog = NULL; |
b8748c12 | 155 | regex_t preg; |
a8122b5d RB |
156 | int error = -1; |
157 | size_t i, numentries, cur; | |
e28dd29b | 158 | const git_reflog_entry *entry; |
b8748c12 | 159 | const char *msg; |
160 | regmatch_t regexmatches[2]; | |
e28dd29b | 161 | git_buf buf = GIT_BUF_INIT; |
e28dd29b | 162 | |
b8748c12 | 163 | cur = position; |
e28dd29b | 164 | |
b8748c12 | 165 | if (*identifier != '\0' || *base_ref != NULL) |
cc146626 | 166 | return GIT_EINVALIDSPEC; |
e28dd29b | 167 | |
b8748c12 | 168 | if (build_regex(&preg, "checkout: moving from (.*) to .*") < 0) |
169 | return -1; | |
cab65c2b | 170 | |
b8748c12 | 171 | if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) |
172 | goto cleanup; | |
e28dd29b | 173 | |
b8748c12 | 174 | if (git_reflog_read(&reflog, ref) < 0) |
175 | goto cleanup; | |
176 | ||
177 | numentries = git_reflog_entrycount(reflog); | |
178 | ||
b15df1d9 | 179 | for (i = 0; i < numentries; i++) { |
b8748c12 | 180 | entry = git_reflog_entry_byindex(reflog, i); |
2508cc66 | 181 | msg = git_reflog_entry_message(entry); |
a8122b5d | 182 | |
b8748c12 | 183 | if (regexec(&preg, msg, 2, regexmatches, 0)) |
184 | continue; | |
185 | ||
186 | cur--; | |
187 | ||
188 | if (cur > 0) | |
189 | continue; | |
a8122b5d | 190 | |
b8748c12 | 191 | git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so); |
192 | ||
98d633cc | 193 | if ((error = git_reference_dwim(base_ref, repo, git_buf_cstr(&buf))) == 0) |
b8748c12 | 194 | goto cleanup; |
195 | ||
196 | if (error < 0 && error != GIT_ENOTFOUND) | |
197 | goto cleanup; | |
198 | ||
0e8e5a61 | 199 | error = maybe_abbrev(out, repo, git_buf_cstr(&buf)); |
b8748c12 | 200 | |
201 | goto cleanup; | |
202 | } | |
a8122b5d | 203 | |
b8748c12 | 204 | error = GIT_ENOTFOUND; |
205 | ||
206 | cleanup: | |
207 | git_reference_free(ref); | |
208 | git_buf_free(&buf); | |
209 | regfree(&preg); | |
210 | git_reflog_free(reflog); | |
211 | return error; | |
212 | } | |
213 | ||
a8122b5d | 214 | static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t identifier) |
b8748c12 | 215 | { |
216 | git_reflog *reflog; | |
217 | int error = -1; | |
a8122b5d | 218 | size_t numentries; |
b8748c12 | 219 | const git_reflog_entry *entry; |
220 | bool search_by_pos = (identifier <= 100000000); | |
221 | ||
222 | if (git_reflog_read(&reflog, ref) < 0) | |
223 | return -1; | |
224 | ||
a8122b5d | 225 | numentries = git_reflog_entrycount(reflog); |
b8748c12 | 226 | |
227 | if (search_by_pos) { | |
228 | if (numentries < identifier + 1) { | |
229 | giterr_set( | |
230 | GITERR_REFERENCE, | |
a8122b5d RB |
231 | "Reflog for '%s' has only "PRIuZ" entries, asked for "PRIuZ, |
232 | git_reference_name(ref), numentries, identifier); | |
b8748c12 | 233 | |
234 | error = GIT_ENOTFOUND; | |
235 | goto cleanup; | |
e28dd29b | 236 | } |
b8748c12 | 237 | |
238 | entry = git_reflog_entry_byindex(reflog, identifier); | |
2508cc66 | 239 | git_oid_cpy(oid, git_reflog_entry_id_new(entry)); |
b8748c12 | 240 | error = 0; |
241 | goto cleanup; | |
242 | ||
e28dd29b | 243 | } else { |
a8122b5d | 244 | size_t i; |
b8748c12 | 245 | git_time commit_time; |
e28dd29b | 246 | |
b15df1d9 | 247 | for (i = 0; i < numentries; i++) { |
b8748c12 | 248 | entry = git_reflog_entry_byindex(reflog, i); |
249 | commit_time = git_reflog_entry_committer(entry)->when; | |
a8122b5d RB |
250 | |
251 | if (commit_time.time > (git_time_t)identifier) | |
b8748c12 | 252 | continue; |
e7279381 | 253 | |
2508cc66 | 254 | git_oid_cpy(oid, git_reflog_entry_id_new(entry)); |
b8748c12 | 255 | error = 0; |
e7279381 | 256 | goto cleanup; |
257 | } | |
258 | ||
b8748c12 | 259 | error = GIT_ENOTFOUND; |
260 | } | |
e28dd29b | 261 | |
b8748c12 | 262 | cleanup: |
263 | git_reflog_free(reflog); | |
264 | return error; | |
265 | } | |
e28dd29b | 266 | |
a8122b5d | 267 | static int retrieve_revobject_from_reflog(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position) |
b8748c12 | 268 | { |
269 | git_reference *ref; | |
270 | git_oid oid; | |
271 | int error = -1; | |
e28dd29b | 272 | |
b8748c12 | 273 | if (*base_ref == NULL) { |
98d633cc | 274 | if ((error = git_reference_dwim(&ref, repo, identifier)) < 0) |
b8748c12 | 275 | return error; |
276 | } else { | |
277 | ref = *base_ref; | |
278 | *base_ref = NULL; | |
279 | } | |
e28dd29b | 280 | |
b8748c12 | 281 | if (position == 0) { |
2508cc66 | 282 | error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJ_ANY); |
b8748c12 | 283 | goto cleanup; |
284 | } | |
e28dd29b | 285 | |
b8748c12 | 286 | if ((error = retrieve_oid_from_reflog(&oid, ref, position)) < 0) |
287 | goto cleanup; | |
e28dd29b | 288 | |
b8748c12 | 289 | error = git_object_lookup(out, repo, &oid, GIT_OBJ_ANY); |
290 | ||
291 | cleanup: | |
292 | git_reference_free(ref); | |
293 | return error; | |
294 | } | |
295 | ||
296 | static int retrieve_remote_tracking_reference(git_reference **base_ref, const char *identifier, git_repository *repo) | |
297 | { | |
298 | git_reference *tracking, *ref; | |
299 | int error = -1; | |
300 | ||
301 | if (*base_ref == NULL) { | |
98d633cc | 302 | if ((error = git_reference_dwim(&ref, repo, identifier)) < 0) |
b8748c12 | 303 | return error; |
304 | } else { | |
305 | ref = *base_ref; | |
306 | *base_ref = NULL; | |
e28dd29b | 307 | } |
308 | ||
cc146626 | 309 | if (!git_reference_is_branch(ref)) { |
310 | error = GIT_EINVALIDSPEC; | |
311 | goto cleanup; | |
312 | } | |
313 | ||
a258d8e3 | 314 | if ((error = git_branch_upstream(&tracking, ref)) < 0) |
b8748c12 | 315 | goto cleanup; |
8480eef7 | 316 | |
b8748c12 | 317 | *base_ref = tracking; |
318 | ||
e7279381 | 319 | cleanup: |
b8748c12 | 320 | git_reference_free(ref); |
321 | return error; | |
322 | } | |
323 | ||
b8457baa | 324 | static int handle_at_syntax(git_object **out, git_reference **ref, const char *spec, size_t identifier_len, git_repository* repo, const char *curly_braces_content) |
b8748c12 | 325 | { |
326 | bool is_numeric; | |
7e48635d | 327 | int parsed = 0, error = -1; |
b8748c12 | 328 | git_buf identifier = GIT_BUF_INIT; |
329 | git_time_t timestamp; | |
330 | ||
331 | assert(*out == NULL); | |
332 | ||
333 | if (git_buf_put(&identifier, spec, identifier_len) < 0) | |
334 | return -1; | |
335 | ||
336 | is_numeric = !try_parse_numeric(&parsed, curly_braces_content); | |
337 | ||
338 | if (*curly_braces_content == '-' && (!is_numeric || parsed == 0)) { | |
cc146626 | 339 | error = GIT_EINVALIDSPEC; |
b8748c12 | 340 | goto cleanup; |
341 | } | |
342 | ||
343 | if (is_numeric) { | |
344 | if (parsed < 0) | |
cc146626 | 345 | error = retrieve_previously_checked_out_branch_or_revision(out, ref, repo, git_buf_cstr(&identifier), -parsed); |
b8748c12 | 346 | else |
347 | error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), parsed); | |
348 | ||
349 | goto cleanup; | |
350 | } | |
351 | ||
352 | if (!strcmp(curly_braces_content, "u") || !strcmp(curly_braces_content, "upstream")) { | |
353 | error = retrieve_remote_tracking_reference(ref, git_buf_cstr(&identifier), repo); | |
354 | ||
355 | goto cleanup; | |
356 | } | |
357 | ||
358 | if (git__date_parse(×tamp, curly_braces_content) < 0) | |
359 | goto cleanup; | |
360 | ||
a8122b5d | 361 | error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), (size_t)timestamp); |
b8748c12 | 362 | |
363 | cleanup: | |
364 | git_buf_free(&identifier); | |
365 | return error; | |
023c6f69 BS |
366 | } |
367 | ||
9d7bdf71 BS |
368 | static git_otype parse_obj_type(const char *str) |
369 | { | |
b8748c12 | 370 | if (!strcmp(str, "commit")) |
371 | return GIT_OBJ_COMMIT; | |
372 | ||
373 | if (!strcmp(str, "tree")) | |
374 | return GIT_OBJ_TREE; | |
a8122b5d | 375 | |
b8748c12 | 376 | if (!strcmp(str, "blob")) |
377 | return GIT_OBJ_BLOB; | |
378 | ||
379 | if (!strcmp(str, "tag")) | |
380 | return GIT_OBJ_TAG; | |
381 | ||
e28dd29b | 382 | return GIT_OBJ_BAD; |
9d7bdf71 BS |
383 | } |
384 | ||
b8748c12 | 385 | static int dereference_to_non_tag(git_object **out, git_object *obj) |
023c6f69 | 386 | { |
b8748c12 | 387 | if (git_object_type(obj) == GIT_OBJ_TAG) |
388 | return git_tag_peel(out, (git_tag *)obj); | |
e28dd29b | 389 | |
575a54db | 390 | return git_object_dup(out, obj); |
b8748c12 | 391 | } |
e28dd29b | 392 | |
b8748c12 | 393 | static int handle_caret_parent_syntax(git_object **out, git_object *obj, int n) |
394 | { | |
395 | git_object *temp_commit = NULL; | |
396 | int error; | |
e28dd29b | 397 | |
cc146626 | 398 | if ((error = git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT)) < 0) |
399 | return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ? | |
400 | GIT_EINVALIDSPEC : error; | |
e28dd29b | 401 | |
e28dd29b | 402 | if (n == 0) { |
b8748c12 | 403 | *out = temp_commit; |
e28dd29b | 404 | return 0; |
405 | } | |
406 | ||
b8748c12 | 407 | error = git_commit_parent((git_commit **)out, (git_commit*)temp_commit, n - 1); |
e28dd29b | 408 | |
b8748c12 | 409 | git_object_free(temp_commit); |
410 | return error; | |
023c6f69 | 411 | } |
ac250c56 | 412 | |
b8748c12 | 413 | static int handle_linear_syntax(git_object **out, git_object *obj, int n) |
38533d5a | 414 | { |
b8748c12 | 415 | git_object *temp_commit = NULL; |
416 | int error; | |
e28dd29b | 417 | |
cc146626 | 418 | if ((error = git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT)) < 0) |
419 | return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ? | |
420 | GIT_EINVALIDSPEC : error; | |
e28dd29b | 421 | |
b8748c12 | 422 | error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n); |
e28dd29b | 423 | |
b8748c12 | 424 | git_object_free(temp_commit); |
425 | return error; | |
38533d5a BS |
426 | } |
427 | ||
b8748c12 | 428 | static int handle_colon_syntax( |
429 | git_object **out, | |
e28dd29b | 430 | git_object *obj, |
431 | const char *path) | |
244d2f6b | 432 | { |
b8748c12 | 433 | git_object *tree; |
bb89cf94 | 434 | int error = -1; |
435 | git_tree_entry *entry = NULL; | |
244d2f6b | 436 | |
cc146626 | 437 | if ((error = git_object_peel(&tree, obj, GIT_OBJ_TREE)) < 0) |
438 | return error == GIT_ENOTFOUND ? GIT_EINVALIDSPEC : error; | |
244d2f6b | 439 | |
b8748c12 | 440 | if (*path == '\0') { |
441 | *out = tree; | |
442 | return 0; | |
443 | } | |
053b5096 | 444 | |
bb89cf94 | 445 | /* |
446 | * TODO: Handle the relative path syntax | |
447 | * (:./relative/path and :../relative/path) | |
448 | */ | |
449 | if ((error = git_tree_entry_bypath(&entry, (git_tree *)tree, path)) < 0) | |
450 | goto cleanup; | |
451 | ||
b8748c12 | 452 | error = git_tree_entry_to_object(out, git_object_owner(tree), entry); |
053b5096 | 453 | |
bb89cf94 | 454 | cleanup: |
455 | git_tree_entry_free(entry); | |
b8748c12 | 456 | git_object_free(tree); |
bb89cf94 | 457 | |
458 | return error; | |
244d2f6b BS |
459 | } |
460 | ||
b8748c12 | 461 | static int walk_and_search(git_object **out, git_revwalk *walk, regex_t *regex) |
734efe4b | 462 | { |
b8748c12 | 463 | int error; |
464 | git_oid oid; | |
465 | git_object *obj; | |
8480eef7 | 466 | |
b8748c12 | 467 | while (!(error = git_revwalk_next(&oid, walk))) { |
e28dd29b | 468 | |
10c06114 AS |
469 | error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJ_COMMIT); |
470 | if ((error < 0) && (error != GIT_ENOTFOUND)) | |
b8748c12 | 471 | return -1; |
472 | ||
473 | if (!regexec(regex, git_commit_message((git_commit*)obj), 0, NULL, 0)) { | |
474 | *out = obj; | |
475 | return 0; | |
476 | } | |
477 | ||
478 | git_object_free(obj); | |
e28dd29b | 479 | } |
480 | ||
f335ecd6 | 481 | if (error < 0 && error == GIT_ITEROVER) |
b8748c12 | 482 | error = GIT_ENOTFOUND; |
483 | ||
484 | return error; | |
485 | } | |
486 | ||
487 | static int handle_grep_syntax(git_object **out, git_repository *repo, const git_oid *spec_oid, const char *pattern) | |
488 | { | |
489 | regex_t preg; | |
490 | git_revwalk *walk = NULL; | |
cc146626 | 491 | int error; |
b8748c12 | 492 | |
cc146626 | 493 | if ((error = build_regex(&preg, pattern)) < 0) |
494 | return error; | |
8480eef7 | 495 | |
cc146626 | 496 | if ((error = git_revwalk_new(&walk, repo)) < 0) |
b8748c12 | 497 | goto cleanup; |
498 | ||
499 | git_revwalk_sorting(walk, GIT_SORT_TIME); | |
500 | ||
501 | if (spec_oid == NULL) { | |
502 | // TODO: @carlosmn: The glob should be refs/* but this makes git_revwalk_next() fails | |
cc146626 | 503 | if ((error = git_revwalk_push_glob(walk, GIT_REFS_HEADS_DIR "*")) < 0) |
b8748c12 | 504 | goto cleanup; |
cc146626 | 505 | } else if ((error = git_revwalk_push(walk, spec_oid)) < 0) |
b8748c12 | 506 | goto cleanup; |
507 | ||
508 | error = walk_and_search(out, walk, &preg); | |
8480eef7 | 509 | |
b8748c12 | 510 | cleanup: |
511 | regfree(&preg); | |
512 | git_revwalk_free(walk); | |
513 | ||
514 | return error; | |
515 | } | |
516 | ||
517 | static int handle_caret_curly_syntax(git_object **out, git_object *obj, const char *curly_braces_content) | |
518 | { | |
519 | git_otype expected_type; | |
520 | ||
521 | if (*curly_braces_content == '\0') | |
522 | return dereference_to_non_tag(out, obj); | |
523 | ||
524 | if (*curly_braces_content == '/') | |
525 | return handle_grep_syntax(out, git_object_owner(obj), git_object_id(obj), curly_braces_content + 1); | |
526 | ||
527 | expected_type = parse_obj_type(curly_braces_content); | |
528 | ||
529 | if (expected_type == GIT_OBJ_BAD) | |
cc146626 | 530 | return GIT_EINVALIDSPEC; |
b8748c12 | 531 | |
e2c81fca | 532 | return git_object_peel(out, obj, expected_type); |
b8748c12 | 533 | } |
534 | ||
b8457baa | 535 | static int extract_curly_braces_content(git_buf *buf, const char *spec, size_t *pos) |
b8748c12 | 536 | { |
537 | git_buf_clear(buf); | |
538 | ||
539 | assert(spec[*pos] == '^' || spec[*pos] == '@'); | |
540 | ||
541 | (*pos)++; | |
542 | ||
543 | if (spec[*pos] == '\0' || spec[*pos] != '{') | |
cc146626 | 544 | return GIT_EINVALIDSPEC; |
b8748c12 | 545 | |
546 | (*pos)++; | |
547 | ||
548 | while (spec[*pos] != '}') { | |
549 | if (spec[*pos] == '\0') | |
cc146626 | 550 | return GIT_EINVALIDSPEC; |
b8748c12 | 551 | |
552 | git_buf_putc(buf, spec[(*pos)++]); | |
553 | } | |
554 | ||
555 | (*pos)++; | |
556 | ||
557 | return 0; | |
558 | } | |
559 | ||
b8457baa | 560 | static int extract_path(git_buf *buf, const char *spec, size_t *pos) |
b8748c12 | 561 | { |
562 | git_buf_clear(buf); | |
563 | ||
564 | assert(spec[*pos] == ':'); | |
565 | ||
566 | (*pos)++; | |
567 | ||
568 | if (git_buf_puts(buf, spec + *pos) < 0) | |
569 | return -1; | |
570 | ||
571 | *pos += git_buf_len(buf); | |
572 | ||
573 | return 0; | |
574 | } | |
575 | ||
b8457baa | 576 | static int extract_how_many(int *n, const char *spec, size_t *pos) |
b8748c12 | 577 | { |
578 | const char *end_ptr; | |
579 | int parsed, accumulated; | |
580 | char kind = spec[*pos]; | |
581 | ||
582 | assert(spec[*pos] == '^' || spec[*pos] == '~'); | |
583 | ||
584 | accumulated = 0; | |
585 | ||
586 | do { | |
587 | do { | |
588 | (*pos)++; | |
589 | accumulated++; | |
590 | } while (spec[(*pos)] == kind && kind == '~'); | |
591 | ||
592 | if (git__isdigit(spec[*pos])) { | |
10c06114 | 593 | if (git__strtol32(&parsed, spec + *pos, &end_ptr, 10) < 0) |
cc146626 | 594 | return GIT_EINVALIDSPEC; |
b8748c12 | 595 | |
596 | accumulated += (parsed - 1); | |
597 | *pos = end_ptr - spec; | |
e28dd29b | 598 | } |
b8748c12 | 599 | |
600 | } while (spec[(*pos)] == kind && kind == '~'); | |
601 | ||
602 | *n = accumulated; | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
607 | static int object_from_reference(git_object **object, git_reference *reference) | |
608 | { | |
609 | git_reference *resolved = NULL; | |
610 | int error; | |
611 | ||
612 | if (git_reference_resolve(&resolved, reference) < 0) | |
613 | return -1; | |
614 | ||
d00d5464 | 615 | error = git_object_lookup(object, reference->db->repo, git_reference_target(resolved), GIT_OBJ_ANY); |
b8748c12 | 616 | git_reference_free(resolved); |
617 | ||
618 | return error; | |
619 | } | |
620 | ||
f672cd2a | 621 | static int ensure_base_rev_loaded(git_object **object, git_reference **reference, const char *spec, size_t identifier_len, git_repository *repo, bool allow_empty_identifier) |
b8748c12 | 622 | { |
623 | int error; | |
624 | git_buf identifier = GIT_BUF_INIT; | |
625 | ||
626 | if (*object != NULL) | |
627 | return 0; | |
628 | ||
f672cd2a | 629 | if (*reference != NULL) |
630 | return object_from_reference(object, *reference); | |
e28dd29b | 631 | |
b8748c12 | 632 | if (!allow_empty_identifier && identifier_len == 0) |
cc146626 | 633 | return GIT_EINVALIDSPEC; |
b8748c12 | 634 | |
635 | if (git_buf_put(&identifier, spec, identifier_len) < 0) | |
636 | return -1; | |
637 | ||
f672cd2a | 638 | error = revparse_lookup_object(object, reference, repo, git_buf_cstr(&identifier)); |
b8748c12 | 639 | git_buf_free(&identifier); |
640 | ||
641 | return error; | |
642 | } | |
643 | ||
cc146626 | 644 | static int ensure_base_rev_is_not_known_yet(git_object *object) |
b8748c12 | 645 | { |
646 | if (object == NULL) | |
647 | return 0; | |
648 | ||
cc146626 | 649 | return GIT_EINVALIDSPEC; |
b8748c12 | 650 | } |
651 | ||
b8457baa | 652 | static bool any_left_hand_identifier(git_object *object, git_reference *reference, size_t identifier_len) |
b8748c12 | 653 | { |
654 | if (object != NULL) | |
655 | return true; | |
656 | ||
657 | if (reference != NULL) | |
658 | return true; | |
659 | ||
660 | if (identifier_len > 0) | |
661 | return true; | |
662 | ||
663 | return false; | |
664 | } | |
665 | ||
cc146626 | 666 | static int ensure_left_hand_identifier_is_not_known_yet(git_object *object, git_reference *reference) |
b8748c12 | 667 | { |
cc146626 | 668 | if (!ensure_base_rev_is_not_known_yet(object) && reference == NULL) |
b8748c12 | 669 | return 0; |
670 | ||
cc146626 | 671 | return GIT_EINVALIDSPEC; |
734efe4b BS |
672 | } |
673 | ||
e841c533 | 674 | int revparse__ext( |
675 | git_object **object_out, | |
676 | git_reference **reference_out, | |
677 | int *identifier_len_out, | |
678 | git_repository *repo, | |
679 | const char *spec) | |
ac250c56 | 680 | { |
b8457baa | 681 | size_t pos = 0, identifier_len = 0; |
b8748c12 | 682 | int error = -1, n; |
683 | git_buf buf = GIT_BUF_INIT; | |
684 | ||
685 | git_reference *reference = NULL; | |
686 | git_object *base_rev = NULL; | |
e28dd29b | 687 | |
e841c533 | 688 | assert(object_out && reference_out && repo && spec); |
e28dd29b | 689 | |
e841c533 | 690 | *object_out = NULL; |
691 | *reference_out = NULL; | |
e28dd29b | 692 | |
279b45b0 | 693 | while (spec[pos]) { |
b8748c12 | 694 | switch (spec[pos]) { |
695 | case '^': | |
f672cd2a | 696 | if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) |
b8748c12 | 697 | goto cleanup; |
e28dd29b | 698 | |
b8748c12 | 699 | if (spec[pos+1] == '{') { |
700 | git_object *temp_object = NULL; | |
e28dd29b | 701 | |
b8748c12 | 702 | if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0) |
703 | goto cleanup; | |
e28dd29b | 704 | |
b8748c12 | 705 | if ((error = handle_caret_curly_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0) |
706 | goto cleanup; | |
707 | ||
708 | git_object_free(base_rev); | |
709 | base_rev = temp_object; | |
e28dd29b | 710 | } else { |
b8748c12 | 711 | git_object *temp_object = NULL; |
712 | ||
713 | if ((error = extract_how_many(&n, spec, &pos)) < 0) | |
714 | goto cleanup; | |
715 | ||
716 | if ((error = handle_caret_parent_syntax(&temp_object, base_rev, n)) < 0) | |
717 | goto cleanup; | |
718 | ||
719 | git_object_free(base_rev); | |
720 | base_rev = temp_object; | |
e28dd29b | 721 | } |
e28dd29b | 722 | break; |
723 | ||
b8748c12 | 724 | case '~': |
725 | { | |
726 | git_object *temp_object = NULL; | |
727 | ||
728 | if ((error = extract_how_many(&n, spec, &pos)) < 0) | |
729 | goto cleanup; | |
730 | ||
f672cd2a | 731 | if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) |
b8748c12 | 732 | goto cleanup; |
733 | ||
734 | if ((error = handle_linear_syntax(&temp_object, base_rev, n)) < 0) | |
735 | goto cleanup; | |
736 | ||
737 | git_object_free(base_rev); | |
738 | base_rev = temp_object; | |
e28dd29b | 739 | break; |
b8748c12 | 740 | } |
e28dd29b | 741 | |
b8748c12 | 742 | case ':': |
743 | { | |
744 | git_object *temp_object = NULL; | |
745 | ||
746 | if ((error = extract_path(&buf, spec, &pos)) < 0) | |
747 | goto cleanup; | |
748 | ||
749 | if (any_left_hand_identifier(base_rev, reference, identifier_len)) { | |
f672cd2a | 750 | if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, true)) < 0) |
b8748c12 | 751 | goto cleanup; |
752 | ||
753 | if ((error = handle_colon_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0) | |
754 | goto cleanup; | |
e28dd29b | 755 | } else { |
b8748c12 | 756 | if (*git_buf_cstr(&buf) == '/') { |
757 | if ((error = handle_grep_syntax(&temp_object, repo, NULL, git_buf_cstr(&buf) + 1)) < 0) | |
758 | goto cleanup; | |
759 | } else { | |
760 | ||
761 | /* | |
762 | * TODO: support merge-stage path lookup (":2:Makefile") | |
763 | * and plain index blob lookup (:i-am/a/blob) | |
764 | */ | |
765 | giterr_set(GITERR_INVALID, "Unimplemented"); | |
766 | error = GIT_ERROR; | |
767 | goto cleanup; | |
768 | } | |
e28dd29b | 769 | } |
b8748c12 | 770 | |
771 | git_object_free(base_rev); | |
772 | base_rev = temp_object; | |
e28dd29b | 773 | break; |
b8748c12 | 774 | } |
775 | ||
776 | case '@': | |
777 | { | |
5912d74c | 778 | if (spec[pos+1] == '{') { |
779 | git_object *temp_object = NULL; | |
e28dd29b | 780 | |
5912d74c | 781 | if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0) |
782 | goto cleanup; | |
b8748c12 | 783 | |
cc146626 | 784 | if ((error = ensure_base_rev_is_not_known_yet(base_rev)) < 0) |
5912d74c | 785 | goto cleanup; |
b8748c12 | 786 | |
5912d74c | 787 | if ((error = handle_at_syntax(&temp_object, &reference, spec, identifier_len, repo, git_buf_cstr(&buf))) < 0) |
788 | goto cleanup; | |
b8748c12 | 789 | |
5912d74c | 790 | if (temp_object != NULL) |
791 | base_rev = temp_object; | |
792 | break; | |
793 | } else { | |
794 | /* Fall through */ | |
795 | } | |
e28dd29b | 796 | } |
797 | ||
b8748c12 | 798 | default: |
cc146626 | 799 | if ((error = ensure_left_hand_identifier_is_not_known_yet(base_rev, reference)) < 0) |
b8748c12 | 800 | goto cleanup; |
801 | ||
802 | pos++; | |
803 | identifier_len++; | |
e28dd29b | 804 | } |
279b45b0 | 805 | } |
e28dd29b | 806 | |
f672cd2a | 807 | if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0) |
b8748c12 | 808 | goto cleanup; |
809 | ||
e841c533 | 810 | *object_out = base_rev; |
811 | *reference_out = reference; | |
812 | *identifier_len_out = identifier_len; | |
b8748c12 | 813 | error = 0; |
e28dd29b | 814 | |
b8748c12 | 815 | cleanup: |
cc146626 | 816 | if (error) { |
817 | if (error == GIT_EINVALIDSPEC) | |
818 | giterr_set(GITERR_INVALID, | |
819 | "Failed to parse revision specifier - Invalid pattern '%s'", spec); | |
820 | ||
b8748c12 | 821 | git_object_free(base_rev); |
e841c533 | 822 | git_reference_free(reference); |
cc146626 | 823 | } |
e841c533 | 824 | |
b8748c12 | 825 | git_buf_free(&buf); |
826 | return error; | |
ac250c56 | 827 | } |
b208d900 | 828 | |
e841c533 | 829 | int git_revparse_ext( |
830 | git_object **object_out, | |
831 | git_reference **reference_out, | |
832 | git_repository *repo, | |
833 | const char *spec) | |
834 | { | |
835 | int error, identifier_len; | |
836 | git_object *obj = NULL; | |
837 | git_reference *ref = NULL; | |
838 | ||
839 | if ((error = revparse__ext(&obj, &ref, &identifier_len, repo, spec)) < 0) | |
840 | goto cleanup; | |
841 | ||
842 | *object_out = obj; | |
843 | *reference_out = ref; | |
844 | ||
845 | return 0; | |
846 | ||
847 | cleanup: | |
848 | git_object_free(obj); | |
849 | git_reference_free(ref); | |
850 | return error; | |
851 | } | |
852 | ||
853 | int git_revparse_single(git_object **out, git_repository *repo, const char *spec) | |
854 | { | |
855 | int error; | |
856 | git_object *obj = NULL; | |
857 | git_reference *ref = NULL; | |
858 | ||
859 | *out = NULL; | |
860 | ||
861 | if ((error = git_revparse_ext(&obj, &ref, repo, spec)) < 0) | |
862 | goto cleanup; | |
863 | ||
864 | git_reference_free(ref); | |
865 | ||
866 | *out = obj; | |
867 | ||
868 | return 0; | |
869 | ||
870 | cleanup: | |
871 | git_object_free(obj); | |
872 | git_reference_free(ref); | |
873 | return error; | |
874 | } | |
8480eef7 BS |
875 | |
876 | int git_revparse( | |
cbda09d0 | 877 | git_revspec *revspec, |
36c2dfed VM |
878 | git_repository *repo, |
879 | const char *spec) | |
b208d900 | 880 | { |
8480eef7 | 881 | const char *dotdot; |
b208d900 | 882 | int error = 0; |
b208d900 | 883 | |
cbda09d0 | 884 | assert(revspec && repo && spec); |
36c2dfed | 885 | |
cbda09d0 | 886 | memset(revspec, 0x0, sizeof(*revspec)); |
8480eef7 BS |
887 | |
888 | if ((dotdot = strstr(spec, "..")) != NULL) { | |
889 | char *lstr; | |
890 | const char *rstr; | |
cbda09d0 | 891 | revspec->flags = GIT_REVPARSE_RANGE; |
8480eef7 | 892 | |
36c2dfed | 893 | lstr = git__substrdup(spec, dotdot - spec); |
8480eef7 BS |
894 | rstr = dotdot + 2; |
895 | if (dotdot[2] == '.') { | |
cbda09d0 | 896 | revspec->flags |= GIT_REVPARSE_MERGE_BASE; |
8480eef7 BS |
897 | rstr++; |
898 | } | |
899 | ||
cbda09d0 | 900 | if ((error = git_revparse_single(&revspec->from, repo, lstr)) < 0) { |
299a224b | 901 | return error; |
8480eef7 | 902 | } |
36c2dfed | 903 | |
cbda09d0 | 904 | if ((error = git_revparse_single(&revspec->to, repo, rstr)) < 0) { |
299a224b | 905 | return error; |
8480eef7 BS |
906 | } |
907 | ||
908 | git__free((void*)lstr); | |
b208d900 | 909 | } else { |
cbda09d0 VM |
910 | revspec->flags = GIT_REVPARSE_SINGLE; |
911 | error = git_revparse_single(&revspec->from, repo, spec); | |
b208d900 GP |
912 | } |
913 | ||
b208d900 GP |
914 | return error; |
915 | } |