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