]> git.proxmox.com Git - libgit2.git/blob - src/revparse.c
Merge branch 'pr/3957'
[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 = p_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 size_t numentries;
209 const git_reflog_entry *entry;
210 bool search_by_pos = (identifier <= 100000000);
211
212 if (git_reflog_read(&reflog, git_reference_owner(ref), git_reference_name(ref)) < 0)
213 return -1;
214
215 numentries = git_reflog_entrycount(reflog);
216
217 if (search_by_pos) {
218 if (numentries < identifier + 1)
219 goto notfound;
220
221 entry = git_reflog_entry_byindex(reflog, identifier);
222 git_oid_cpy(oid, git_reflog_entry_id_new(entry));
223 } else {
224 size_t i;
225 git_time commit_time;
226
227 for (i = 0; i < numentries; i++) {
228 entry = git_reflog_entry_byindex(reflog, i);
229 commit_time = git_reflog_entry_committer(entry)->when;
230
231 if (commit_time.time > (git_time_t)identifier)
232 continue;
233
234 git_oid_cpy(oid, git_reflog_entry_id_new(entry));
235 break;
236 }
237
238 if (i == numentries)
239 goto notfound;
240 }
241
242 git_reflog_free(reflog);
243 return 0;
244
245 notfound:
246 giterr_set(
247 GITERR_REFERENCE,
248 "reflog for '%s' has only %"PRIuZ" entries, asked for %"PRIuZ,
249 git_reference_name(ref), numentries, identifier);
250
251 git_reflog_free(reflog);
252 return GIT_ENOTFOUND;
253 }
254
255 static int retrieve_revobject_from_reflog(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position)
256 {
257 git_reference *ref;
258 git_oid oid;
259 int error = -1;
260
261 if (*base_ref == NULL) {
262 if ((error = git_reference_dwim(&ref, repo, identifier)) < 0)
263 return error;
264 } else {
265 ref = *base_ref;
266 *base_ref = NULL;
267 }
268
269 if (position == 0) {
270 error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJ_ANY);
271 goto cleanup;
272 }
273
274 if ((error = retrieve_oid_from_reflog(&oid, ref, position)) < 0)
275 goto cleanup;
276
277 error = git_object_lookup(out, repo, &oid, GIT_OBJ_ANY);
278
279 cleanup:
280 git_reference_free(ref);
281 return error;
282 }
283
284 static int retrieve_remote_tracking_reference(git_reference **base_ref, const char *identifier, git_repository *repo)
285 {
286 git_reference *tracking, *ref;
287 int error = -1;
288
289 if (*base_ref == NULL) {
290 if ((error = git_reference_dwim(&ref, repo, identifier)) < 0)
291 return error;
292 } else {
293 ref = *base_ref;
294 *base_ref = NULL;
295 }
296
297 if (!git_reference_is_branch(ref)) {
298 error = GIT_EINVALIDSPEC;
299 goto cleanup;
300 }
301
302 if ((error = git_branch_upstream(&tracking, ref)) < 0)
303 goto cleanup;
304
305 *base_ref = tracking;
306
307 cleanup:
308 git_reference_free(ref);
309 return error;
310 }
311
312 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)
313 {
314 bool is_numeric;
315 int parsed = 0, error = -1;
316 git_buf identifier = GIT_BUF_INIT;
317 git_time_t timestamp;
318
319 assert(*out == NULL);
320
321 if (git_buf_put(&identifier, spec, identifier_len) < 0)
322 return -1;
323
324 is_numeric = !try_parse_numeric(&parsed, curly_braces_content);
325
326 if (*curly_braces_content == '-' && (!is_numeric || parsed == 0)) {
327 error = GIT_EINVALIDSPEC;
328 goto cleanup;
329 }
330
331 if (is_numeric) {
332 if (parsed < 0)
333 error = retrieve_previously_checked_out_branch_or_revision(out, ref, repo, git_buf_cstr(&identifier), -parsed);
334 else
335 error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), parsed);
336
337 goto cleanup;
338 }
339
340 if (!strcmp(curly_braces_content, "u") || !strcmp(curly_braces_content, "upstream")) {
341 error = retrieve_remote_tracking_reference(ref, git_buf_cstr(&identifier), repo);
342
343 goto cleanup;
344 }
345
346 if (git__date_parse(&timestamp, curly_braces_content) < 0)
347 goto cleanup;
348
349 error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), (size_t)timestamp);
350
351 cleanup:
352 git_buf_free(&identifier);
353 return error;
354 }
355
356 static git_otype parse_obj_type(const char *str)
357 {
358 if (!strcmp(str, "commit"))
359 return GIT_OBJ_COMMIT;
360
361 if (!strcmp(str, "tree"))
362 return GIT_OBJ_TREE;
363
364 if (!strcmp(str, "blob"))
365 return GIT_OBJ_BLOB;
366
367 if (!strcmp(str, "tag"))
368 return GIT_OBJ_TAG;
369
370 return GIT_OBJ_BAD;
371 }
372
373 static int dereference_to_non_tag(git_object **out, git_object *obj)
374 {
375 if (git_object_type(obj) == GIT_OBJ_TAG)
376 return git_tag_peel(out, (git_tag *)obj);
377
378 return git_object_dup(out, obj);
379 }
380
381 static int handle_caret_parent_syntax(git_object **out, git_object *obj, int n)
382 {
383 git_object *temp_commit = NULL;
384 int error;
385
386 if ((error = git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT)) < 0)
387 return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ?
388 GIT_EINVALIDSPEC : error;
389
390 if (n == 0) {
391 *out = temp_commit;
392 return 0;
393 }
394
395 error = git_commit_parent((git_commit **)out, (git_commit*)temp_commit, n - 1);
396
397 git_object_free(temp_commit);
398 return error;
399 }
400
401 static int handle_linear_syntax(git_object **out, git_object *obj, int n)
402 {
403 git_object *temp_commit = NULL;
404 int error;
405
406 if ((error = git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT)) < 0)
407 return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ?
408 GIT_EINVALIDSPEC : error;
409
410 error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n);
411
412 git_object_free(temp_commit);
413 return error;
414 }
415
416 static int handle_colon_syntax(
417 git_object **out,
418 git_object *obj,
419 const char *path)
420 {
421 git_object *tree;
422 int error = -1;
423 git_tree_entry *entry = NULL;
424
425 if ((error = git_object_peel(&tree, obj, GIT_OBJ_TREE)) < 0)
426 return error == GIT_ENOTFOUND ? GIT_EINVALIDSPEC : error;
427
428 if (*path == '\0') {
429 *out = tree;
430 return 0;
431 }
432
433 /*
434 * TODO: Handle the relative path syntax
435 * (:./relative/path and :../relative/path)
436 */
437 if ((error = git_tree_entry_bypath(&entry, (git_tree *)tree, path)) < 0)
438 goto cleanup;
439
440 error = git_tree_entry_to_object(out, git_object_owner(tree), entry);
441
442 cleanup:
443 git_tree_entry_free(entry);
444 git_object_free(tree);
445
446 return error;
447 }
448
449 static int walk_and_search(git_object **out, git_revwalk *walk, regex_t *regex)
450 {
451 int error;
452 git_oid oid;
453 git_object *obj;
454
455 while (!(error = git_revwalk_next(&oid, walk))) {
456
457 error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJ_COMMIT);
458 if ((error < 0) && (error != GIT_ENOTFOUND))
459 return -1;
460
461 if (!regexec(regex, git_commit_message((git_commit*)obj), 0, NULL, 0)) {
462 *out = obj;
463 return 0;
464 }
465
466 git_object_free(obj);
467 }
468
469 if (error < 0 && error == GIT_ITEROVER)
470 error = GIT_ENOTFOUND;
471
472 return error;
473 }
474
475 static int handle_grep_syntax(git_object **out, git_repository *repo, const git_oid *spec_oid, const char *pattern)
476 {
477 regex_t preg;
478 git_revwalk *walk = NULL;
479 int error;
480
481 if ((error = build_regex(&preg, pattern)) < 0)
482 return error;
483
484 if ((error = git_revwalk_new(&walk, repo)) < 0)
485 goto cleanup;
486
487 git_revwalk_sorting(walk, GIT_SORT_TIME);
488
489 if (spec_oid == NULL) {
490 if ((error = git_revwalk_push_glob(walk, "refs/*")) < 0)
491 goto cleanup;
492 } else if ((error = git_revwalk_push(walk, spec_oid)) < 0)
493 goto cleanup;
494
495 error = walk_and_search(out, walk, &preg);
496
497 cleanup:
498 regfree(&preg);
499 git_revwalk_free(walk);
500
501 return error;
502 }
503
504 static int handle_caret_curly_syntax(git_object **out, git_object *obj, const char *curly_braces_content)
505 {
506 git_otype expected_type;
507
508 if (*curly_braces_content == '\0')
509 return dereference_to_non_tag(out, obj);
510
511 if (*curly_braces_content == '/')
512 return handle_grep_syntax(out, git_object_owner(obj), git_object_id(obj), curly_braces_content + 1);
513
514 expected_type = parse_obj_type(curly_braces_content);
515
516 if (expected_type == GIT_OBJ_BAD)
517 return GIT_EINVALIDSPEC;
518
519 return git_object_peel(out, obj, expected_type);
520 }
521
522 static int extract_curly_braces_content(git_buf *buf, const char *spec, size_t *pos)
523 {
524 git_buf_clear(buf);
525
526 assert(spec[*pos] == '^' || spec[*pos] == '@');
527
528 (*pos)++;
529
530 if (spec[*pos] == '\0' || spec[*pos] != '{')
531 return GIT_EINVALIDSPEC;
532
533 (*pos)++;
534
535 while (spec[*pos] != '}') {
536 if (spec[*pos] == '\0')
537 return GIT_EINVALIDSPEC;
538
539 git_buf_putc(buf, spec[(*pos)++]);
540 }
541
542 (*pos)++;
543
544 return 0;
545 }
546
547 static int extract_path(git_buf *buf, const char *spec, size_t *pos)
548 {
549 git_buf_clear(buf);
550
551 assert(spec[*pos] == ':');
552
553 (*pos)++;
554
555 if (git_buf_puts(buf, spec + *pos) < 0)
556 return -1;
557
558 *pos += git_buf_len(buf);
559
560 return 0;
561 }
562
563 static int extract_how_many(int *n, const char *spec, size_t *pos)
564 {
565 const char *end_ptr;
566 int parsed, accumulated;
567 char kind = spec[*pos];
568
569 assert(spec[*pos] == '^' || spec[*pos] == '~');
570
571 accumulated = 0;
572
573 do {
574 do {
575 (*pos)++;
576 accumulated++;
577 } while (spec[(*pos)] == kind && kind == '~');
578
579 if (git__isdigit(spec[*pos])) {
580 if (git__strtol32(&parsed, spec + *pos, &end_ptr, 10) < 0)
581 return GIT_EINVALIDSPEC;
582
583 accumulated += (parsed - 1);
584 *pos = end_ptr - spec;
585 }
586
587 } while (spec[(*pos)] == kind && kind == '~');
588
589 *n = accumulated;
590
591 return 0;
592 }
593
594 static int object_from_reference(git_object **object, git_reference *reference)
595 {
596 git_reference *resolved = NULL;
597 int error;
598
599 if (git_reference_resolve(&resolved, reference) < 0)
600 return -1;
601
602 error = git_object_lookup(object, reference->db->repo, git_reference_target(resolved), GIT_OBJ_ANY);
603 git_reference_free(resolved);
604
605 return error;
606 }
607
608 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)
609 {
610 int error;
611 git_buf identifier = GIT_BUF_INIT;
612
613 if (*object != NULL)
614 return 0;
615
616 if (*reference != NULL)
617 return object_from_reference(object, *reference);
618
619 if (!allow_empty_identifier && identifier_len == 0)
620 return GIT_EINVALIDSPEC;
621
622 if (git_buf_put(&identifier, spec, identifier_len) < 0)
623 return -1;
624
625 error = revparse_lookup_object(object, reference, repo, git_buf_cstr(&identifier));
626 git_buf_free(&identifier);
627
628 return error;
629 }
630
631 static int ensure_base_rev_is_not_known_yet(git_object *object)
632 {
633 if (object == NULL)
634 return 0;
635
636 return GIT_EINVALIDSPEC;
637 }
638
639 static bool any_left_hand_identifier(git_object *object, git_reference *reference, size_t identifier_len)
640 {
641 if (object != NULL)
642 return true;
643
644 if (reference != NULL)
645 return true;
646
647 if (identifier_len > 0)
648 return true;
649
650 return false;
651 }
652
653 static int ensure_left_hand_identifier_is_not_known_yet(git_object *object, git_reference *reference)
654 {
655 if (!ensure_base_rev_is_not_known_yet(object) && reference == NULL)
656 return 0;
657
658 return GIT_EINVALIDSPEC;
659 }
660
661 int revparse__ext(
662 git_object **object_out,
663 git_reference **reference_out,
664 size_t *identifier_len_out,
665 git_repository *repo,
666 const char *spec)
667 {
668 size_t pos = 0, identifier_len = 0;
669 int error = -1, n;
670 git_buf buf = GIT_BUF_INIT;
671
672 git_reference *reference = NULL;
673 git_object *base_rev = NULL;
674
675 bool should_return_reference = true;
676
677 assert(object_out && reference_out && repo && spec);
678
679 *object_out = NULL;
680 *reference_out = NULL;
681
682 while (spec[pos]) {
683 switch (spec[pos]) {
684 case '^':
685 should_return_reference = false;
686
687 if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
688 goto cleanup;
689
690 if (spec[pos+1] == '{') {
691 git_object *temp_object = NULL;
692
693 if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0)
694 goto cleanup;
695
696 if ((error = handle_caret_curly_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0)
697 goto cleanup;
698
699 git_object_free(base_rev);
700 base_rev = temp_object;
701 } else {
702 git_object *temp_object = NULL;
703
704 if ((error = extract_how_many(&n, spec, &pos)) < 0)
705 goto cleanup;
706
707 if ((error = handle_caret_parent_syntax(&temp_object, base_rev, n)) < 0)
708 goto cleanup;
709
710 git_object_free(base_rev);
711 base_rev = temp_object;
712 }
713 break;
714
715 case '~':
716 {
717 git_object *temp_object = NULL;
718
719 should_return_reference = false;
720
721 if ((error = extract_how_many(&n, spec, &pos)) < 0)
722 goto cleanup;
723
724 if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
725 goto cleanup;
726
727 if ((error = handle_linear_syntax(&temp_object, base_rev, n)) < 0)
728 goto cleanup;
729
730 git_object_free(base_rev);
731 base_rev = temp_object;
732 break;
733 }
734
735 case ':':
736 {
737 git_object *temp_object = NULL;
738
739 should_return_reference = false;
740
741 if ((error = extract_path(&buf, spec, &pos)) < 0)
742 goto cleanup;
743
744 if (any_left_hand_identifier(base_rev, reference, identifier_len)) {
745 if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, true)) < 0)
746 goto cleanup;
747
748 if ((error = handle_colon_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0)
749 goto cleanup;
750 } else {
751 if (*git_buf_cstr(&buf) == '/') {
752 if ((error = handle_grep_syntax(&temp_object, repo, NULL, git_buf_cstr(&buf) + 1)) < 0)
753 goto cleanup;
754 } else {
755
756 /*
757 * TODO: support merge-stage path lookup (":2:Makefile")
758 * and plain index blob lookup (:i-am/a/blob)
759 */
760 giterr_set(GITERR_INVALID, "unimplemented");
761 error = GIT_ERROR;
762 goto cleanup;
763 }
764 }
765
766 git_object_free(base_rev);
767 base_rev = temp_object;
768 break;
769 }
770
771 case '@':
772 {
773 if (spec[pos+1] == '{') {
774 git_object *temp_object = NULL;
775
776 if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0)
777 goto cleanup;
778
779 if ((error = ensure_base_rev_is_not_known_yet(base_rev)) < 0)
780 goto cleanup;
781
782 if ((error = handle_at_syntax(&temp_object, &reference, spec, identifier_len, repo, git_buf_cstr(&buf))) < 0)
783 goto cleanup;
784
785 if (temp_object != NULL)
786 base_rev = temp_object;
787 break;
788 } else {
789 /* Fall through */
790 }
791 }
792
793 default:
794 if ((error = ensure_left_hand_identifier_is_not_known_yet(base_rev, reference)) < 0)
795 goto cleanup;
796
797 pos++;
798 identifier_len++;
799 }
800 }
801
802 if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
803 goto cleanup;
804
805 if (!should_return_reference) {
806 git_reference_free(reference);
807 reference = NULL;
808 }
809
810 *object_out = base_rev;
811 *reference_out = reference;
812 *identifier_len_out = identifier_len;
813 error = 0;
814
815 cleanup:
816 if (error) {
817 if (error == GIT_EINVALIDSPEC)
818 giterr_set(GITERR_INVALID,
819 "failed to parse revision specifier - Invalid pattern '%s'", spec);
820
821 git_object_free(base_rev);
822 git_reference_free(reference);
823 }
824
825 git_buf_free(&buf);
826 return error;
827 }
828
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;
836 size_t identifier_len;
837 git_object *obj = NULL;
838 git_reference *ref = NULL;
839
840 if ((error = revparse__ext(&obj, &ref, &identifier_len, repo, spec)) < 0)
841 goto cleanup;
842
843 *object_out = obj;
844 *reference_out = ref;
845 GIT_UNUSED(identifier_len);
846
847 return 0;
848
849 cleanup:
850 git_object_free(obj);
851 git_reference_free(ref);
852 return error;
853 }
854
855 int git_revparse_single(git_object **out, git_repository *repo, const char *spec)
856 {
857 int error;
858 git_object *obj = NULL;
859 git_reference *ref = NULL;
860
861 *out = NULL;
862
863 if ((error = git_revparse_ext(&obj, &ref, repo, spec)) < 0)
864 goto cleanup;
865
866 git_reference_free(ref);
867
868 *out = obj;
869
870 return 0;
871
872 cleanup:
873 git_object_free(obj);
874 git_reference_free(ref);
875 return error;
876 }
877
878 int git_revparse(
879 git_revspec *revspec,
880 git_repository *repo,
881 const char *spec)
882 {
883 const char *dotdot;
884 int error = 0;
885
886 assert(revspec && repo && spec);
887
888 memset(revspec, 0x0, sizeof(*revspec));
889
890 if ((dotdot = strstr(spec, "..")) != NULL) {
891 char *lstr;
892 const char *rstr;
893 revspec->flags = GIT_REVPARSE_RANGE;
894
895 lstr = git__substrdup(spec, dotdot - spec);
896 rstr = dotdot + 2;
897 if (dotdot[2] == '.') {
898 revspec->flags |= GIT_REVPARSE_MERGE_BASE;
899 rstr++;
900 }
901
902 error = git_revparse_single(&revspec->from, repo, lstr);
903 if (!error)
904 error = git_revparse_single(&revspec->to, repo, rstr);
905
906 git__free((void*)lstr);
907 } else {
908 revspec->flags = GIT_REVPARSE_SINGLE;
909 error = git_revparse_single(&revspec->from, repo, spec);
910 }
911
912 return error;
913 }