]>
git.proxmox.com Git - libgit2.git/blob - src/revparse.c
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_OBJ_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(regex_t
*regex
, const char *pattern
)
48 if (*pattern
== '\0') {
49 giterr_set(GITERR_REGEX
, "Empty pattern");
50 return GIT_EINVALIDSPEC
;
53 error
= regcomp(regex
, pattern
, REG_EXTENDED
);
57 error
= giterr_set_regex(regex
, error
);
64 static int maybe_describe(git_object
**out
, git_repository
*repo
, const char *spec
)
70 substr
= strstr(spec
, "-g");
75 if (build_regex(®ex
, ".+-[0-9]+-g[0-9a-fA-F]+") < 0)
78 error
= regexec(®ex
, spec
, 0, NULL
, 0);
84 return maybe_abbrev(out
, repo
, substr
+2);
87 static int revparse_lookup_object(git_object
**out
, git_repository
*repo
, const char *spec
)
92 error
= maybe_sha(out
, repo
, spec
);
96 if (error
< 0 && error
!= GIT_ENOTFOUND
)
99 error
= git_reference_dwim(&ref
, repo
, spec
);
101 error
= git_object_lookup(out
, repo
, git_reference_target(ref
), GIT_OBJ_ANY
);
102 git_reference_free(ref
);
106 if (error
< 0 && error
!= GIT_ENOTFOUND
)
109 error
= maybe_abbrev(out
, repo
, spec
);
113 if (error
< 0 && error
!= GIT_ENOTFOUND
)
116 error
= maybe_describe(out
, repo
, spec
);
120 if (error
< 0 && error
!= GIT_ENOTFOUND
)
123 giterr_set(GITERR_REFERENCE
, "Refspec '%s' not found.", spec
);
124 return GIT_ENOTFOUND
;
127 static int try_parse_numeric(int *n
, const char *curly_braces_content
)
132 if (git__strtol32(&content
, curly_braces_content
, &end_ptr
, 10) < 0)
135 if (*end_ptr
!= '\0')
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
)
144 git_reference
*ref
= NULL
;
145 git_reflog
*reflog
= NULL
;
148 size_t i
, numentries
, cur
;
149 const git_reflog_entry
*entry
;
151 regmatch_t regexmatches
[2];
152 git_buf buf
= GIT_BUF_INIT
;
156 if (*identifier
!= '\0' || *base_ref
!= NULL
)
157 return GIT_EINVALIDSPEC
;
159 if (build_regex(&preg
, "checkout: moving from (.*) to .*") < 0)
162 if (git_reference_lookup(&ref
, repo
, GIT_HEAD_FILE
) < 0)
165 if (git_reflog_read(&reflog
, ref
) < 0)
168 numentries
= git_reflog_entrycount(reflog
);
170 for (i
= 0; i
< numentries
; i
++) {
171 entry
= git_reflog_entry_byindex(reflog
, i
);
172 msg
= git_reflog_entry_message(entry
);
174 if (regexec(&preg
, msg
, 2, regexmatches
, 0))
182 git_buf_put(&buf
, msg
+regexmatches
[1].rm_so
, regexmatches
[1].rm_eo
- regexmatches
[1].rm_so
);
184 if ((error
= git_reference_dwim(base_ref
, repo
, git_buf_cstr(&buf
))) == 0)
187 if (error
< 0 && error
!= GIT_ENOTFOUND
)
190 error
= maybe_abbrev(out
, repo
, git_buf_cstr(&buf
));
195 error
= GIT_ENOTFOUND
;
198 git_reference_free(ref
);
201 git_reflog_free(reflog
);
205 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
, ref
) < 0)
216 numentries
= git_reflog_entrycount(reflog
);
219 if (numentries
< identifier
+ 1) {
222 "Reflog for '%s' has only "PRIuZ
" entries, asked for "PRIuZ
,
223 git_reference_name(ref
), numentries
, identifier
);
225 error
= GIT_ENOTFOUND
;
229 entry
= git_reflog_entry_byindex(reflog
, identifier
);
230 git_oid_cpy(oid
, git_reflog_entry_id_new(entry
));
236 git_time commit_time
;
238 for (i
= 0; i
< numentries
; i
++) {
239 entry
= git_reflog_entry_byindex(reflog
, i
);
240 commit_time
= git_reflog_entry_committer(entry
)->when
;
242 if (commit_time
.time
> (git_time_t
)identifier
)
245 git_oid_cpy(oid
, git_reflog_entry_id_new(entry
));
250 error
= GIT_ENOTFOUND
;
254 git_reflog_free(reflog
);
258 static int retrieve_revobject_from_reflog(git_object
**out
, git_reference
**base_ref
, git_repository
*repo
, const char *identifier
, size_t position
)
264 if (*base_ref
== NULL
) {
265 if ((error
= git_reference_dwim(&ref
, repo
, identifier
)) < 0)
273 error
= git_object_lookup(out
, repo
, git_reference_target(ref
), GIT_OBJ_ANY
);
277 if ((error
= retrieve_oid_from_reflog(&oid
, ref
, position
)) < 0)
280 error
= git_object_lookup(out
, repo
, &oid
, GIT_OBJ_ANY
);
283 git_reference_free(ref
);
287 static int retrieve_remote_tracking_reference(git_reference
**base_ref
, const char *identifier
, git_repository
*repo
)
289 git_reference
*tracking
, *ref
;
292 if (*base_ref
== NULL
) {
293 if ((error
= git_reference_dwim(&ref
, repo
, identifier
)) < 0)
300 if (!git_reference_is_branch(ref
)) {
301 error
= GIT_EINVALIDSPEC
;
305 if ((error
= git_branch_upstream(&tracking
, ref
)) < 0)
308 *base_ref
= tracking
;
311 git_reference_free(ref
);
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
)
318 int parsed
= 0, error
= -1;
319 git_buf identifier
= GIT_BUF_INIT
;
320 git_time_t timestamp
;
322 assert(*out
== NULL
);
324 if (git_buf_put(&identifier
, spec
, identifier_len
) < 0)
327 is_numeric
= !try_parse_numeric(&parsed
, curly_braces_content
);
329 if (*curly_braces_content
== '-' && (!is_numeric
|| parsed
== 0)) {
330 error
= GIT_EINVALIDSPEC
;
336 error
= retrieve_previously_checked_out_branch_or_revision(out
, ref
, repo
, git_buf_cstr(&identifier
), -parsed
);
338 error
= retrieve_revobject_from_reflog(out
, ref
, repo
, git_buf_cstr(&identifier
), parsed
);
343 if (!strcmp(curly_braces_content
, "u") || !strcmp(curly_braces_content
, "upstream")) {
344 error
= retrieve_remote_tracking_reference(ref
, git_buf_cstr(&identifier
), repo
);
349 if (git__date_parse(×tamp
, curly_braces_content
) < 0)
352 error
= retrieve_revobject_from_reflog(out
, ref
, repo
, git_buf_cstr(&identifier
), (size_t)timestamp
);
355 git_buf_free(&identifier
);
359 static git_otype
parse_obj_type(const char *str
)
361 if (!strcmp(str
, "commit"))
362 return GIT_OBJ_COMMIT
;
364 if (!strcmp(str
, "tree"))
367 if (!strcmp(str
, "blob"))
370 if (!strcmp(str
, "tag"))
376 static int dereference_to_non_tag(git_object
**out
, git_object
*obj
)
378 if (git_object_type(obj
) == GIT_OBJ_TAG
)
379 return git_tag_peel(out
, (git_tag
*)obj
);
381 return git_object_dup(out
, obj
);
384 static int handle_caret_parent_syntax(git_object
**out
, git_object
*obj
, int n
)
386 git_object
*temp_commit
= NULL
;
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
;
398 error
= git_commit_parent((git_commit
**)out
, (git_commit
*)temp_commit
, n
- 1);
400 git_object_free(temp_commit
);
404 static int handle_linear_syntax(git_object
**out
, git_object
*obj
, int n
)
406 git_object
*temp_commit
= NULL
;
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
;
413 error
= git_commit_nth_gen_ancestor((git_commit
**)out
, (git_commit
*)temp_commit
, n
);
415 git_object_free(temp_commit
);
419 static int handle_colon_syntax(
426 git_tree_entry
*entry
= NULL
;
428 if ((error
= git_object_peel(&tree
, obj
, GIT_OBJ_TREE
)) < 0)
429 return error
== GIT_ENOTFOUND
? GIT_EINVALIDSPEC
: error
;
437 * TODO: Handle the relative path syntax
438 * (:./relative/path and :../relative/path)
440 if ((error
= git_tree_entry_bypath(&entry
, (git_tree
*)tree
, path
)) < 0)
443 error
= git_tree_entry_to_object(out
, git_object_owner(tree
), entry
);
446 git_tree_entry_free(entry
);
447 git_object_free(tree
);
452 static int walk_and_search(git_object
**out
, git_revwalk
*walk
, regex_t
*regex
)
458 while (!(error
= git_revwalk_next(&oid
, walk
))) {
460 error
= git_object_lookup(&obj
, git_revwalk_repository(walk
), &oid
, GIT_OBJ_COMMIT
);
461 if ((error
< 0) && (error
!= GIT_ENOTFOUND
))
464 if (!regexec(regex
, git_commit_message((git_commit
*)obj
), 0, NULL
, 0)) {
469 git_object_free(obj
);
472 if (error
< 0 && error
== GIT_ITEROVER
)
473 error
= GIT_ENOTFOUND
;
478 static int handle_grep_syntax(git_object
**out
, git_repository
*repo
, const git_oid
*spec_oid
, const char *pattern
)
481 git_revwalk
*walk
= NULL
;
484 if ((error
= build_regex(&preg
, pattern
)) < 0)
487 if ((error
= git_revwalk_new(&walk
, repo
)) < 0)
490 git_revwalk_sorting(walk
, GIT_SORT_TIME
);
492 if (spec_oid
== NULL
) {
493 // TODO: @carlosmn: The glob should be refs/* but this makes git_revwalk_next() fails
494 if ((error
= git_revwalk_push_glob(walk
, GIT_REFS_HEADS_DIR
"*")) < 0)
496 } else if ((error
= git_revwalk_push(walk
, spec_oid
)) < 0)
499 error
= walk_and_search(out
, walk
, &preg
);
503 git_revwalk_free(walk
);
508 static int handle_caret_curly_syntax(git_object
**out
, git_object
*obj
, const char *curly_braces_content
)
510 git_otype expected_type
;
512 if (*curly_braces_content
== '\0')
513 return dereference_to_non_tag(out
, obj
);
515 if (*curly_braces_content
== '/')
516 return handle_grep_syntax(out
, git_object_owner(obj
), git_object_id(obj
), curly_braces_content
+ 1);
518 expected_type
= parse_obj_type(curly_braces_content
);
520 if (expected_type
== GIT_OBJ_BAD
)
521 return GIT_EINVALIDSPEC
;
523 return git_object_peel(out
, obj
, expected_type
);
526 static int extract_curly_braces_content(git_buf
*buf
, const char *spec
, size_t *pos
)
530 assert(spec
[*pos
] == '^' || spec
[*pos
] == '@');
534 if (spec
[*pos
] == '\0' || spec
[*pos
] != '{')
535 return GIT_EINVALIDSPEC
;
539 while (spec
[*pos
] != '}') {
540 if (spec
[*pos
] == '\0')
541 return GIT_EINVALIDSPEC
;
543 git_buf_putc(buf
, spec
[(*pos
)++]);
551 static int extract_path(git_buf
*buf
, const char *spec
, size_t *pos
)
555 assert(spec
[*pos
] == ':');
559 if (git_buf_puts(buf
, spec
+ *pos
) < 0)
562 *pos
+= git_buf_len(buf
);
567 static int extract_how_many(int *n
, const char *spec
, size_t *pos
)
570 int parsed
, accumulated
;
571 char kind
= spec
[*pos
];
573 assert(spec
[*pos
] == '^' || spec
[*pos
] == '~');
581 } while (spec
[(*pos
)] == kind
&& kind
== '~');
583 if (git__isdigit(spec
[*pos
])) {
584 if (git__strtol32(&parsed
, spec
+ *pos
, &end_ptr
, 10) < 0)
585 return GIT_EINVALIDSPEC
;
587 accumulated
+= (parsed
- 1);
588 *pos
= end_ptr
- spec
;
591 } while (spec
[(*pos
)] == kind
&& kind
== '~');
598 static int object_from_reference(git_object
**object
, git_reference
*reference
)
600 git_reference
*resolved
= NULL
;
603 if (git_reference_resolve(&resolved
, reference
) < 0)
606 error
= git_object_lookup(object
, reference
->db
->repo
, git_reference_target(resolved
), GIT_OBJ_ANY
);
607 git_reference_free(resolved
);
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
)
615 git_buf identifier
= GIT_BUF_INIT
;
620 if (reference
!= NULL
)
621 return object_from_reference(object
, reference
);
623 if (!allow_empty_identifier
&& identifier_len
== 0)
624 return GIT_EINVALIDSPEC
;
626 if (git_buf_put(&identifier
, spec
, identifier_len
) < 0)
629 error
= revparse_lookup_object(object
, repo
, git_buf_cstr(&identifier
));
630 git_buf_free(&identifier
);
635 static int ensure_base_rev_is_not_known_yet(git_object
*object
)
640 return GIT_EINVALIDSPEC
;
643 static bool any_left_hand_identifier(git_object
*object
, git_reference
*reference
, size_t identifier_len
)
648 if (reference
!= NULL
)
651 if (identifier_len
> 0)
657 static int ensure_left_hand_identifier_is_not_known_yet(git_object
*object
, git_reference
*reference
)
659 if (!ensure_base_rev_is_not_known_yet(object
) && reference
== NULL
)
662 return GIT_EINVALIDSPEC
;
666 git_object
**object_out
,
667 git_reference
**reference_out
,
668 int *identifier_len_out
,
669 git_repository
*repo
,
672 size_t pos
= 0, identifier_len
= 0;
674 git_buf buf
= GIT_BUF_INIT
;
676 git_reference
*reference
= NULL
;
677 git_object
*base_rev
= NULL
;
679 assert(object_out
&& reference_out
&& repo
&& spec
);
682 *reference_out
= NULL
;
687 if ((error
= ensure_base_rev_loaded(&base_rev
, reference
, spec
, identifier_len
, repo
, false)) < 0)
690 if (spec
[pos
+1] == '{') {
691 git_object
*temp_object
= NULL
;
693 if ((error
= extract_curly_braces_content(&buf
, spec
, &pos
)) < 0)
696 if ((error
= handle_caret_curly_syntax(&temp_object
, base_rev
, git_buf_cstr(&buf
))) < 0)
699 git_object_free(base_rev
);
700 base_rev
= temp_object
;
702 git_object
*temp_object
= NULL
;
704 if ((error
= extract_how_many(&n
, spec
, &pos
)) < 0)
707 if ((error
= handle_caret_parent_syntax(&temp_object
, base_rev
, n
)) < 0)
710 git_object_free(base_rev
);
711 base_rev
= temp_object
;
717 git_object
*temp_object
= NULL
;
719 if ((error
= extract_how_many(&n
, spec
, &pos
)) < 0)
722 if ((error
= ensure_base_rev_loaded(&base_rev
, reference
, spec
, identifier_len
, repo
, false)) < 0)
725 if ((error
= handle_linear_syntax(&temp_object
, base_rev
, n
)) < 0)
728 git_object_free(base_rev
);
729 base_rev
= temp_object
;
735 git_object
*temp_object
= NULL
;
737 if ((error
= extract_path(&buf
, spec
, &pos
)) < 0)
740 if (any_left_hand_identifier(base_rev
, reference
, identifier_len
)) {
741 if ((error
= ensure_base_rev_loaded(&base_rev
, reference
, spec
, identifier_len
, repo
, true)) < 0)
744 if ((error
= handle_colon_syntax(&temp_object
, base_rev
, git_buf_cstr(&buf
))) < 0)
747 if (*git_buf_cstr(&buf
) == '/') {
748 if ((error
= handle_grep_syntax(&temp_object
, repo
, NULL
, git_buf_cstr(&buf
) + 1)) < 0)
753 * TODO: support merge-stage path lookup (":2:Makefile")
754 * and plain index blob lookup (:i-am/a/blob)
756 giterr_set(GITERR_INVALID
, "Unimplemented");
762 git_object_free(base_rev
);
763 base_rev
= temp_object
;
769 if (spec
[pos
+1] == '{') {
770 git_object
*temp_object
= NULL
;
772 if ((error
= extract_curly_braces_content(&buf
, spec
, &pos
)) < 0)
775 if ((error
= ensure_base_rev_is_not_known_yet(base_rev
)) < 0)
778 if ((error
= handle_at_syntax(&temp_object
, &reference
, spec
, identifier_len
, repo
, git_buf_cstr(&buf
))) < 0)
781 if (temp_object
!= NULL
)
782 base_rev
= temp_object
;
790 if ((error
= ensure_left_hand_identifier_is_not_known_yet(base_rev
, reference
)) < 0)
798 if ((error
= ensure_base_rev_loaded(&base_rev
, reference
, spec
, identifier_len
, repo
, false)) < 0)
801 *object_out
= base_rev
;
802 *reference_out
= reference
;
803 *identifier_len_out
= identifier_len
;
808 if (error
== GIT_EINVALIDSPEC
)
809 giterr_set(GITERR_INVALID
,
810 "Failed to parse revision specifier - Invalid pattern '%s'", spec
);
812 git_object_free(base_rev
);
813 git_reference_free(reference
);
820 int git_revparse_ext(
821 git_object
**object_out
,
822 git_reference
**reference_out
,
823 git_repository
*repo
,
826 int error
, identifier_len
;
827 git_object
*obj
= NULL
;
828 git_reference
*ref
= NULL
;
830 if ((error
= revparse__ext(&obj
, &ref
, &identifier_len
, repo
, spec
)) < 0)
834 *reference_out
= ref
;
839 git_object_free(obj
);
840 git_reference_free(ref
);
844 int git_revparse_single(git_object
**out
, git_repository
*repo
, const char *spec
)
847 git_object
*obj
= NULL
;
848 git_reference
*ref
= NULL
;
852 if ((error
= git_revparse_ext(&obj
, &ref
, repo
, spec
)) < 0)
855 git_reference_free(ref
);
862 git_object_free(obj
);
863 git_reference_free(ref
);
868 git_revspec
*revspec
,
869 git_repository
*repo
,
875 assert(revspec
&& repo
&& spec
);
877 memset(revspec
, 0x0, sizeof(*revspec
));
879 if ((dotdot
= strstr(spec
, "..")) != NULL
) {
882 revspec
->flags
= GIT_REVPARSE_RANGE
;
884 lstr
= git__substrdup(spec
, dotdot
- spec
);
886 if (dotdot
[2] == '.') {
887 revspec
->flags
|= GIT_REVPARSE_MERGE_BASE
;
891 if ((error
= git_revparse_single(&revspec
->from
, repo
, lstr
)) < 0) {
895 if ((error
= git_revparse_single(&revspec
->to
, repo
, rstr
)) < 0) {
899 git__free((void*)lstr
);
901 revspec
->flags
= GIT_REVPARSE_SINGLE
;
902 error
= git_revparse_single(&revspec
->from
, repo
, spec
);