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