2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
17 static int maybe_sha_or_abbrev(git_object
** out
, git_repository
*repo
, const char *spec
, size_t speclen
)
21 if (git_oid_fromstrn(&oid
, spec
, speclen
) < 0)
24 return git_object_lookup_prefix(out
, repo
, &oid
, speclen
, GIT_OBJECT_ANY
);
27 static int maybe_sha(git_object
** out
, git_repository
*repo
, const char *spec
)
29 size_t speclen
= strlen(spec
);
31 if (speclen
!= GIT_OID_HEXSZ
)
34 return maybe_sha_or_abbrev(out
, repo
, spec
, speclen
);
37 static int maybe_abbrev(git_object
** out
, git_repository
*repo
, const char *spec
)
39 size_t speclen
= strlen(spec
);
41 return maybe_sha_or_abbrev(out
, repo
, spec
, speclen
);
44 static int build_regex(git_regexp
*regex
, const char *pattern
)
48 if (*pattern
== '\0') {
49 git_error_set(GIT_ERROR_REGEX
, "empty pattern");
50 return GIT_EINVALIDSPEC
;
53 error
= git_regexp_compile(regex
, pattern
, 0);
57 git_regexp_dispose(regex
);
62 static int maybe_describe(git_object
**out
, git_repository
*repo
, const char *spec
)
68 substr
= strstr(spec
, "-g");
73 if (build_regex(®ex
, ".+-[0-9]+-g[0-9a-fA-F]+") < 0)
76 error
= git_regexp_match(®ex
, spec
);
77 git_regexp_dispose(®ex
);
82 return maybe_abbrev(out
, repo
, substr
+2);
85 static int revparse_lookup_object(
86 git_object
**object_out
,
87 git_reference
**reference_out
,
94 if ((error
= maybe_sha(object_out
, repo
, spec
)) != GIT_ENOTFOUND
)
97 error
= git_reference_dwim(&ref
, repo
, spec
);
100 error
= git_object_lookup(
101 object_out
, repo
, git_reference_target(ref
), GIT_OBJECT_ANY
);
104 *reference_out
= ref
;
109 if (error
!= GIT_ENOTFOUND
)
112 if ((strlen(spec
) < GIT_OID_HEXSZ
) &&
113 ((error
= maybe_abbrev(object_out
, repo
, spec
)) != GIT_ENOTFOUND
))
116 if ((error
= maybe_describe(object_out
, repo
, spec
)) != GIT_ENOTFOUND
)
119 git_error_set(GIT_ERROR_REFERENCE
, "revspec '%s' not found", spec
);
120 return GIT_ENOTFOUND
;
123 static int try_parse_numeric(int *n
, const char *curly_braces_content
)
128 if (git__strntol32(&content
, curly_braces_content
, strlen(curly_braces_content
),
132 if (*end_ptr
!= '\0')
139 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 git_reference
*ref
= NULL
;
142 git_reflog
*reflog
= NULL
;
145 size_t i
, numentries
, cur
;
146 const git_reflog_entry
*entry
;
148 git_buf buf
= GIT_BUF_INIT
;
152 if (*identifier
!= '\0' || *base_ref
!= NULL
)
153 return GIT_EINVALIDSPEC
;
155 if (build_regex(&preg
, "checkout: moving from (.*) to .*") < 0)
158 if (git_reference_lookup(&ref
, repo
, GIT_HEAD_FILE
) < 0)
161 if (git_reflog_read(&reflog
, repo
, GIT_HEAD_FILE
) < 0)
164 numentries
= git_reflog_entrycount(reflog
);
166 for (i
= 0; i
< numentries
; i
++) {
167 git_regmatch regexmatches
[2];
169 entry
= git_reflog_entry_byindex(reflog
, i
);
170 msg
= git_reflog_entry_message(entry
);
174 if (git_regexp_search(&preg
, msg
, 2, regexmatches
) < 0)
182 if ((git_buf_put(&buf
, msg
+regexmatches
[1].start
, regexmatches
[1].end
- regexmatches
[1].start
)) < 0)
185 if ((error
= git_reference_dwim(base_ref
, repo
, git_buf_cstr(&buf
))) == 0)
188 if (error
< 0 && error
!= GIT_ENOTFOUND
)
191 error
= maybe_abbrev(out
, repo
, git_buf_cstr(&buf
));
196 error
= GIT_ENOTFOUND
;
199 git_reference_free(ref
);
200 git_buf_dispose(&buf
);
201 git_regexp_dispose(&preg
);
202 git_reflog_free(reflog
);
206 static int retrieve_oid_from_reflog(git_oid
*oid
, git_reference
*ref
, size_t identifier
)
210 const git_reflog_entry
*entry
;
211 bool search_by_pos
= (identifier
<= 100000000);
213 if (git_reflog_read(&reflog
, git_reference_owner(ref
), git_reference_name(ref
)) < 0)
216 numentries
= git_reflog_entrycount(reflog
);
219 if (numentries
< identifier
+ 1)
222 entry
= git_reflog_entry_byindex(reflog
, identifier
);
223 git_oid_cpy(oid
, git_reflog_entry_id_new(entry
));
226 git_time commit_time
;
228 for (i
= 0; i
< numentries
; i
++) {
229 entry
= git_reflog_entry_byindex(reflog
, i
);
230 commit_time
= git_reflog_entry_committer(entry
)->when
;
232 if (commit_time
.time
> (git_time_t
)identifier
)
235 git_oid_cpy(oid
, git_reflog_entry_id_new(entry
));
243 git_reflog_free(reflog
);
249 "reflog for '%s' has only %"PRIuZ
" entries, asked for %"PRIuZ
,
250 git_reference_name(ref
), numentries
, identifier
);
252 git_reflog_free(reflog
);
253 return GIT_ENOTFOUND
;
256 static int retrieve_revobject_from_reflog(git_object
**out
, git_reference
**base_ref
, git_repository
*repo
, const char *identifier
, size_t position
)
262 if (*base_ref
== NULL
) {
263 if ((error
= git_reference_dwim(&ref
, repo
, identifier
)) < 0)
271 error
= git_object_lookup(out
, repo
, git_reference_target(ref
), GIT_OBJECT_ANY
);
275 if ((error
= retrieve_oid_from_reflog(&oid
, ref
, position
)) < 0)
278 error
= git_object_lookup(out
, repo
, &oid
, GIT_OBJECT_ANY
);
281 git_reference_free(ref
);
285 static int retrieve_remote_tracking_reference(git_reference
**base_ref
, const char *identifier
, git_repository
*repo
)
287 git_reference
*tracking
, *ref
;
290 if (*base_ref
== NULL
) {
291 if ((error
= git_reference_dwim(&ref
, repo
, identifier
)) < 0)
298 if (!git_reference_is_branch(ref
)) {
299 error
= GIT_EINVALIDSPEC
;
303 if ((error
= git_branch_upstream(&tracking
, ref
)) < 0)
306 *base_ref
= tracking
;
309 git_reference_free(ref
);
313 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 int parsed
= 0, error
= -1;
317 git_buf identifier
= GIT_BUF_INIT
;
318 git_time_t timestamp
;
320 assert(*out
== NULL
);
322 if (git_buf_put(&identifier
, spec
, identifier_len
) < 0)
325 is_numeric
= !try_parse_numeric(&parsed
, curly_braces_content
);
327 if (*curly_braces_content
== '-' && (!is_numeric
|| parsed
== 0)) {
328 error
= GIT_EINVALIDSPEC
;
334 error
= retrieve_previously_checked_out_branch_or_revision(out
, ref
, repo
, git_buf_cstr(&identifier
), -parsed
);
336 error
= retrieve_revobject_from_reflog(out
, ref
, repo
, git_buf_cstr(&identifier
), parsed
);
341 if (!strcmp(curly_braces_content
, "u") || !strcmp(curly_braces_content
, "upstream")) {
342 error
= retrieve_remote_tracking_reference(ref
, git_buf_cstr(&identifier
), repo
);
347 if (git__date_parse(×tamp
, curly_braces_content
) < 0)
350 error
= retrieve_revobject_from_reflog(out
, ref
, repo
, git_buf_cstr(&identifier
), (size_t)timestamp
);
353 git_buf_dispose(&identifier
);
357 static git_object_t
parse_obj_type(const char *str
)
359 if (!strcmp(str
, "commit"))
360 return GIT_OBJECT_COMMIT
;
362 if (!strcmp(str
, "tree"))
363 return GIT_OBJECT_TREE
;
365 if (!strcmp(str
, "blob"))
366 return GIT_OBJECT_BLOB
;
368 if (!strcmp(str
, "tag"))
369 return GIT_OBJECT_TAG
;
371 return GIT_OBJECT_INVALID
;
374 static int dereference_to_non_tag(git_object
**out
, git_object
*obj
)
376 if (git_object_type(obj
) == GIT_OBJECT_TAG
)
377 return git_tag_peel(out
, (git_tag
*)obj
);
379 return git_object_dup(out
, obj
);
382 static int handle_caret_parent_syntax(git_object
**out
, git_object
*obj
, int n
)
384 git_object
*temp_commit
= NULL
;
387 if ((error
= git_object_peel(&temp_commit
, obj
, GIT_OBJECT_COMMIT
)) < 0)
388 return (error
== GIT_EAMBIGUOUS
|| error
== GIT_ENOTFOUND
) ?
389 GIT_EINVALIDSPEC
: error
;
396 error
= git_commit_parent((git_commit
**)out
, (git_commit
*)temp_commit
, n
- 1);
398 git_object_free(temp_commit
);
402 static int handle_linear_syntax(git_object
**out
, git_object
*obj
, int n
)
404 git_object
*temp_commit
= NULL
;
407 if ((error
= git_object_peel(&temp_commit
, obj
, GIT_OBJECT_COMMIT
)) < 0)
408 return (error
== GIT_EAMBIGUOUS
|| error
== GIT_ENOTFOUND
) ?
409 GIT_EINVALIDSPEC
: error
;
411 error
= git_commit_nth_gen_ancestor((git_commit
**)out
, (git_commit
*)temp_commit
, n
);
413 git_object_free(temp_commit
);
417 static int handle_colon_syntax(
424 git_tree_entry
*entry
= NULL
;
426 if ((error
= git_object_peel(&tree
, obj
, GIT_OBJECT_TREE
)) < 0)
427 return error
== GIT_ENOTFOUND
? GIT_EINVALIDSPEC
: error
;
435 * TODO: Handle the relative path syntax
436 * (:./relative/path and :../relative/path)
438 if ((error
= git_tree_entry_bypath(&entry
, (git_tree
*)tree
, path
)) < 0)
441 error
= git_tree_entry_to_object(out
, git_object_owner(tree
), entry
);
444 git_tree_entry_free(entry
);
445 git_object_free(tree
);
450 static int walk_and_search(git_object
**out
, git_revwalk
*walk
, git_regexp
*regex
)
456 while (!(error
= git_revwalk_next(&oid
, walk
))) {
458 error
= git_object_lookup(&obj
, git_revwalk_repository(walk
), &oid
, GIT_OBJECT_COMMIT
);
459 if ((error
< 0) && (error
!= GIT_ENOTFOUND
))
462 if (!git_regexp_match(regex
, git_commit_message((git_commit
*)obj
))) {
467 git_object_free(obj
);
470 if (error
< 0 && error
== GIT_ITEROVER
)
471 error
= GIT_ENOTFOUND
;
476 static int handle_grep_syntax(git_object
**out
, git_repository
*repo
, const git_oid
*spec_oid
, const char *pattern
)
479 git_revwalk
*walk
= NULL
;
482 if ((error
= build_regex(&preg
, pattern
)) < 0)
485 if ((error
= git_revwalk_new(&walk
, repo
)) < 0)
488 git_revwalk_sorting(walk
, GIT_SORT_TIME
);
490 if (spec_oid
== NULL
) {
491 if ((error
= git_revwalk_push_glob(walk
, "refs/*")) < 0)
493 } else if ((error
= git_revwalk_push(walk
, spec_oid
)) < 0)
496 error
= walk_and_search(out
, walk
, &preg
);
499 git_regexp_dispose(&preg
);
500 git_revwalk_free(walk
);
505 static int handle_caret_curly_syntax(git_object
**out
, git_object
*obj
, const char *curly_braces_content
)
507 git_object_t expected_type
;
509 if (*curly_braces_content
== '\0')
510 return dereference_to_non_tag(out
, obj
);
512 if (*curly_braces_content
== '/')
513 return handle_grep_syntax(out
, git_object_owner(obj
), git_object_id(obj
), curly_braces_content
+ 1);
515 expected_type
= parse_obj_type(curly_braces_content
);
517 if (expected_type
== GIT_OBJECT_INVALID
)
518 return GIT_EINVALIDSPEC
;
520 return git_object_peel(out
, obj
, expected_type
);
523 static int extract_curly_braces_content(git_buf
*buf
, const char *spec
, size_t *pos
)
527 assert(spec
[*pos
] == '^' || spec
[*pos
] == '@');
531 if (spec
[*pos
] == '\0' || spec
[*pos
] != '{')
532 return GIT_EINVALIDSPEC
;
536 while (spec
[*pos
] != '}') {
537 if (spec
[*pos
] == '\0')
538 return GIT_EINVALIDSPEC
;
540 if (git_buf_putc(buf
, spec
[(*pos
)++]) < 0)
549 static int extract_path(git_buf
*buf
, const char *spec
, size_t *pos
)
553 assert(spec
[*pos
] == ':');
557 if (git_buf_puts(buf
, spec
+ *pos
) < 0)
560 *pos
+= git_buf_len(buf
);
565 static int extract_how_many(int *n
, const char *spec
, size_t *pos
)
568 int parsed
, accumulated
;
569 char kind
= spec
[*pos
];
571 assert(spec
[*pos
] == '^' || spec
[*pos
] == '~');
579 } while (spec
[(*pos
)] == kind
&& kind
== '~');
581 if (git__isdigit(spec
[*pos
])) {
582 if (git__strntol32(&parsed
, spec
+ *pos
, strlen(spec
+ *pos
), &end_ptr
, 10) < 0)
583 return GIT_EINVALIDSPEC
;
585 accumulated
+= (parsed
- 1);
586 *pos
= end_ptr
- spec
;
589 } while (spec
[(*pos
)] == kind
&& kind
== '~');
596 static int object_from_reference(git_object
**object
, git_reference
*reference
)
598 git_reference
*resolved
= NULL
;
601 if (git_reference_resolve(&resolved
, reference
) < 0)
604 error
= git_object_lookup(object
, reference
->db
->repo
, git_reference_target(resolved
), GIT_OBJECT_ANY
);
605 git_reference_free(resolved
);
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
)
613 git_buf identifier
= GIT_BUF_INIT
;
618 if (*reference
!= NULL
)
619 return object_from_reference(object
, *reference
);
621 if (!allow_empty_identifier
&& identifier_len
== 0)
622 return GIT_EINVALIDSPEC
;
624 if (git_buf_put(&identifier
, spec
, identifier_len
) < 0)
627 error
= revparse_lookup_object(object
, reference
, repo
, git_buf_cstr(&identifier
));
628 git_buf_dispose(&identifier
);
633 static int ensure_base_rev_is_not_known_yet(git_object
*object
)
638 return GIT_EINVALIDSPEC
;
641 static bool any_left_hand_identifier(git_object
*object
, git_reference
*reference
, size_t identifier_len
)
646 if (reference
!= NULL
)
649 if (identifier_len
> 0)
655 static int ensure_left_hand_identifier_is_not_known_yet(git_object
*object
, git_reference
*reference
)
657 if (!ensure_base_rev_is_not_known_yet(object
) && reference
== NULL
)
660 return GIT_EINVALIDSPEC
;
664 git_object
**object_out
,
665 git_reference
**reference_out
,
666 size_t *identifier_len_out
,
667 git_repository
*repo
,
670 size_t pos
= 0, identifier_len
= 0;
672 git_buf buf
= GIT_BUF_INIT
;
674 git_reference
*reference
= NULL
;
675 git_object
*base_rev
= NULL
;
677 bool should_return_reference
= true;
679 assert(object_out
&& reference_out
&& repo
&& spec
);
682 *reference_out
= NULL
;
687 should_return_reference
= false;
689 if ((error
= ensure_base_rev_loaded(&base_rev
, &reference
, spec
, identifier_len
, repo
, false)) < 0)
692 if (spec
[pos
+1] == '{') {
693 git_object
*temp_object
= NULL
;
695 if ((error
= extract_curly_braces_content(&buf
, spec
, &pos
)) < 0)
698 if ((error
= handle_caret_curly_syntax(&temp_object
, base_rev
, git_buf_cstr(&buf
))) < 0)
701 git_object_free(base_rev
);
702 base_rev
= temp_object
;
704 git_object
*temp_object
= NULL
;
706 if ((error
= extract_how_many(&n
, spec
, &pos
)) < 0)
709 if ((error
= handle_caret_parent_syntax(&temp_object
, base_rev
, n
)) < 0)
712 git_object_free(base_rev
);
713 base_rev
= temp_object
;
719 git_object
*temp_object
= NULL
;
721 should_return_reference
= false;
723 if ((error
= extract_how_many(&n
, spec
, &pos
)) < 0)
726 if ((error
= ensure_base_rev_loaded(&base_rev
, &reference
, spec
, identifier_len
, repo
, false)) < 0)
729 if ((error
= handle_linear_syntax(&temp_object
, base_rev
, n
)) < 0)
732 git_object_free(base_rev
);
733 base_rev
= temp_object
;
739 git_object
*temp_object
= NULL
;
741 should_return_reference
= false;
743 if ((error
= extract_path(&buf
, spec
, &pos
)) < 0)
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)
750 if ((error
= handle_colon_syntax(&temp_object
, base_rev
, git_buf_cstr(&buf
))) < 0)
753 if (*git_buf_cstr(&buf
) == '/') {
754 if ((error
= handle_grep_syntax(&temp_object
, repo
, NULL
, git_buf_cstr(&buf
) + 1)) < 0)
759 * TODO: support merge-stage path lookup (":2:Makefile")
760 * and plain index blob lookup (:i-am/a/blob)
762 git_error_set(GIT_ERROR_INVALID
, "unimplemented");
768 git_object_free(base_rev
);
769 base_rev
= temp_object
;
774 if (spec
[pos
+1] == '{') {
775 git_object
*temp_object
= NULL
;
777 if ((error
= extract_curly_braces_content(&buf
, spec
, &pos
)) < 0)
780 if ((error
= ensure_base_rev_is_not_known_yet(base_rev
)) < 0)
783 if ((error
= handle_at_syntax(&temp_object
, &reference
, spec
, identifier_len
, repo
, git_buf_cstr(&buf
))) < 0)
786 if (temp_object
!= NULL
)
787 base_rev
= temp_object
;
793 if ((error
= ensure_left_hand_identifier_is_not_known_yet(base_rev
, reference
)) < 0)
801 if ((error
= ensure_base_rev_loaded(&base_rev
, &reference
, spec
, identifier_len
, repo
, false)) < 0)
804 if (!should_return_reference
) {
805 git_reference_free(reference
);
809 *object_out
= base_rev
;
810 *reference_out
= reference
;
811 *identifier_len_out
= identifier_len
;
816 if (error
== GIT_EINVALIDSPEC
)
817 git_error_set(GIT_ERROR_INVALID
,
818 "failed to parse revision specifier - Invalid pattern '%s'", spec
);
820 git_object_free(base_rev
);
821 git_reference_free(reference
);
824 git_buf_dispose(&buf
);
828 int git_revparse_ext(
829 git_object
**object_out
,
830 git_reference
**reference_out
,
831 git_repository
*repo
,
835 size_t identifier_len
;
836 git_object
*obj
= NULL
;
837 git_reference
*ref
= NULL
;
839 if ((error
= revparse(&obj
, &ref
, &identifier_len
, repo
, spec
)) < 0)
843 *reference_out
= ref
;
844 GIT_UNUSED(identifier_len
);
849 git_object_free(obj
);
850 git_reference_free(ref
);
854 int git_revparse_single(git_object
**out
, git_repository
*repo
, const char *spec
)
857 git_object
*obj
= NULL
;
858 git_reference
*ref
= NULL
;
862 if ((error
= git_revparse_ext(&obj
, &ref
, repo
, spec
)) < 0)
865 git_reference_free(ref
);
872 git_object_free(obj
);
873 git_reference_free(ref
);
878 git_revspec
*revspec
,
879 git_repository
*repo
,
885 assert(revspec
&& repo
&& spec
);
887 memset(revspec
, 0x0, sizeof(*revspec
));
889 if ((dotdot
= strstr(spec
, "..")) != NULL
) {
892 revspec
->flags
= GIT_REVPARSE_RANGE
;
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
900 if (!git__strcmp(spec
, "..")) {
901 git_error_set(GIT_ERROR_INVALID
, "Invalid pattern '..'");
902 return GIT_EINVALIDSPEC
;
905 lstr
= git__substrdup(spec
, dotdot
- spec
);
907 if (dotdot
[2] == '.') {
908 revspec
->flags
|= GIT_REVPARSE_MERGE_BASE
;
912 error
= git_revparse_single(
915 *lstr
== '\0' ? "HEAD" : lstr
);
918 error
= git_revparse_single(
921 *rstr
== '\0' ? "HEAD" : rstr
);
924 git__free((void*)lstr
);
926 revspec
->flags
= GIT_REVPARSE_SINGLE
;
927 error
= git_revparse_single(&revspec
->from
, repo
, spec
);