Skip to content

Commit

Permalink
revparse: deploy git_object_peel()
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Jul 17, 2012
1 parent db9be94 commit e2c81fc
Showing 1 changed file with 4 additions and 55 deletions.
59 changes: 4 additions & 55 deletions src/revparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,57 +382,6 @@ static int handle_at_syntax(git_object **out, git_reference **ref, const char *s
return error;
}

static int dereference_object(git_object **dereferenced, git_object *obj)
{
git_otype type = git_object_type(obj);

switch (type) {
case GIT_OBJ_COMMIT:
return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj);
break;

case GIT_OBJ_TAG:
return git_tag_target(dereferenced, (git_tag*)obj);
break;

default:
return GIT_ENOTFOUND;
break;
}
}

static int dereference_to_type(git_object **out, git_object *obj, git_otype target_type)
{
git_object *source, *deref = NULL;

if (git_object_type(obj) == target_type)
return git_object_lookup(out, git_object_owner(obj), git_object_id(obj), target_type);

source = obj;

while (true) {
if (dereference_object(&deref, source) < 0)
goto cleanup;

if (source != obj)
git_object_free(source);

if (git_object_type(deref) == target_type) {
*out = deref;
return 0;
}

source = deref;
deref = NULL;
}

cleanup:
if (source != obj)
git_object_free(source);
git_object_free(deref);
return -1;
}

static git_otype parse_obj_type(const char *str)
{
if (!strcmp(str, "commit"))
Expand Down Expand Up @@ -463,7 +412,7 @@ static int handle_caret_parent_syntax(git_object **out, git_object *obj, int n)
git_object *temp_commit = NULL;
int error;

if (dereference_to_type(&temp_commit, obj, GIT_OBJ_COMMIT) < 0)
if (git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT) < 0)
return -1;

if (n == 0) {
Expand All @@ -482,7 +431,7 @@ static int handle_linear_syntax(git_object **out, git_object *obj, int n)
git_object *temp_commit = NULL;
int error;

if (dereference_to_type(&temp_commit, obj, GIT_OBJ_COMMIT) < 0)
if (git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT) < 0)
return -1;

error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n);
Expand All @@ -500,7 +449,7 @@ static int handle_colon_syntax(
int error = -1;
git_tree_entry *entry = NULL;

if (dereference_to_type(&tree, obj, GIT_OBJ_TREE) < 0)
if (git_object_peel(&tree, obj, GIT_OBJ_TREE) < 0)
return -1;

if (*path == '\0') {
Expand Down Expand Up @@ -595,7 +544,7 @@ static int handle_caret_curly_syntax(git_object **out, git_object *obj, const ch
if (expected_type == GIT_OBJ_BAD)
return -1;

return dereference_to_type(out, obj, expected_type);
return git_object_peel(out, obj, expected_type);
}

static int extract_curly_braces_content(git_buf *buf, const char *spec, int *pos)
Expand Down

0 comments on commit e2c81fc

Please sign in to comment.