Commit cbc02c10 by Ben Straub

Merge pull request #788 from nulltoken/topix/revparse

Small revparse colon syntax improvements
parents 1d8943c6 0e7af9e7
......@@ -485,7 +485,7 @@ static int handle_caret_syntax(git_object **out, git_repository *repo, git_objec
}
if (git_commit_parent(&commit, commit, n-1) < 0) {
return GIT_ERROR;
return GIT_ENOTFOUND;
}
*out = (git_object*)commit;
......@@ -533,27 +533,56 @@ static int handle_linear_syntax(git_object **out, git_object *obj, const char *m
static int oid_for_tree_path(git_oid *out, git_tree *tree, git_repository *repo, const char *path)
{
char *str = git__strdup(path);
char *tok;
void *alloc = str;
char *str, *tok;
void *alloc;
git_tree *tree2 = tree;
const git_tree_entry *entry = NULL;
git_otype type;
if (*path == '\0') {
git_oid_cpy(out, git_object_id((git_object *)tree));
return 0;
}
alloc = str = git__strdup(path);
while ((tok = git__strtok(&str, "/\\")) != NULL) {
entry = git_tree_entry_byname(tree2, tok);
if (tree2 != tree) git_tree_free(tree2);
if (git_tree_entry__is_tree(entry)) {
if (entry == NULL)
break;
type = git_tree_entry_type(entry);
switch (type) {
case GIT_OBJ_TREE:
if (*str == '\0')
break;
if (git_tree_lookup(&tree2, repo, &entry->oid) < 0) {
git__free(alloc);
return GIT_ERROR;
}
break;
case GIT_OBJ_BLOB:
if (*str != '\0') {
entry = NULL;
goto out;
}
break;
default:
/* TODO: support submodules? */
giterr_set(GITERR_INVALID, "Unimplemented");
git__free(alloc);
return GIT_ERROR;
}
}
out:
if (!entry) {
giterr_set(GITERR_INVALID, "Invalid tree path '%s'", path);
git__free(alloc);
return GIT_ERROR;
return GIT_ENOTFOUND;
}
git_oid_cpy(out, git_tree_entry_id(entry));
......@@ -576,7 +605,7 @@ static int handle_colon_syntax(git_object **out,
}
tree = (git_tree*)obj;
/* Find the blob at the given path. */
/* Find the blob or tree at the given path. */
error = oid_for_tree_path(&oid, tree, repo, path);
git_tree_free(tree);
......@@ -623,6 +652,7 @@ static int revparse_global_grep(git_object **out, git_repository *repo, const ch
}
if (!resultobj) {
giterr_set(GITERR_REFERENCE, "Couldn't find a match for %s", pattern);
retcode = GIT_ENOTFOUND;
git_object_free(walkobj);
} else {
*out = resultobj;
......@@ -702,6 +732,10 @@ int git_revparse_single(git_object **out, git_repository *repo, const char *spec
if (retcode < 0) {
next_state = REVPARSE_STATE_DONE;
}
} else if (*spec_cur == ':') {
retcode = handle_caret_syntax(&next_obj, repo, cur_obj, git_buf_cstr(&stepbuffer));
git_buf_clear(&stepbuffer);
next_state = !retcode ? REVPARSE_STATE_COLON : REVPARSE_STATE_DONE;
} else {
git_buf_putc(&stepbuffer, *spec_cur);
}
......
......@@ -85,6 +85,8 @@ void test_refs_revparse__nth_parent(void)
test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "be3563a^42"));
}
void test_refs_revparse__not_tag(void)
......@@ -164,10 +166,20 @@ void test_refs_revparse__date(void)
void test_refs_revparse__colon(void)
{
cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/not found in any commit"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README"));
cl_git_fail(git_revparse_single(&g_obj, g_repo, "master:"));
cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, ":/not found in any commit"));
cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "subtrees:ab/42.txt"));
cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "subtrees:ab/4.txt/nope"));
cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "subtrees:nope"));
cl_assert_equal_i(GIT_ENOTFOUND, git_revparse_single(&g_obj, g_repo, "test/master^1:branch_file.txt"));
/* Trees */
test_object("master:", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
test_object("subtrees:", "ae90f12eea699729ed24555e40b9fd669da12a12");
test_object("subtrees:ab", "f1425cef211cc08caa31e7b545ffb232acb098c3");
/* Blobs */
test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f");
test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b");
test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6");
......@@ -175,4 +187,5 @@ void test_refs_revparse__colon(void)
test_object(":/Merge", "a4a7dce85cf63874e984719f4fdd239f5145052f");
test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd");
test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9");
test_object("test/master^2:branch_file.txt", "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment