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