diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/branch.c | 8 | ||||
-rw-r--r-- | src/checkout.c | 16 | ||||
-rw-r--r-- | src/repository.c | 16 |
3 files changed, 28 insertions, 12 deletions
diff --git a/src/branch.c b/src/branch.c index d0bd1c45b..991314508 100644 --- a/src/branch.c +++ b/src/branch.c @@ -265,13 +265,19 @@ int git_branch_is_head( { git_reference *head; bool is_same = false; + int error; assert(branch); if (!git_reference_is_branch(branch)) return false; - if (git_repository_head(&head, git_reference_owner(branch)) < 0) + error = git_repository_head(&head, git_reference_owner(branch)); + + if (error == GIT_EORPHANEDHEAD) + return false; + + if (error < 0) return -1; is_same = strcmp( diff --git a/src/checkout.c b/src/checkout.c index 4782f7724..b56b459d2 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -430,17 +430,23 @@ int git_checkout_head( git_checkout_opts *opts, git_indexer_stats *stats) { + git_reference *head; int error; - git_tree *tree = NULL; + git_object *tree = NULL; assert(repo); - if (git_repository_head_tree(&tree, repo) < 0) - return -1; + if ((error = git_repository_head(&head, repo)) < 0) + return error; + + if ((error = git_reference_peel(&tree, head, GIT_OBJ_TREE)) < 0) + goto cleanup; - error = git_checkout_tree(repo, (git_object *)tree, opts, stats); + error = git_checkout_tree(repo, tree, opts, stats); - git_tree_free(tree); +cleanup: + git_reference_free(head); + git_object_free(tree); return error; } diff --git a/src/repository.c b/src/repository.c index 5f7fa3cea..db0888a89 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1206,7 +1206,11 @@ int git_repository_head_detached(git_repository *repo) int git_repository_head(git_reference **head_out, git_repository *repo) { - return git_reference_lookup_resolved(head_out, repo, GIT_HEAD_FILE, -1); + int error; + + error = git_reference_lookup_resolved(head_out, repo, GIT_HEAD_FILE, -1); + + return error == GIT_ENOTFOUND ? GIT_EORPHANEDHEAD : error; } int git_repository_head_orphan(git_repository *repo) @@ -1217,7 +1221,7 @@ int git_repository_head_orphan(git_repository *repo) error = git_repository_head(&ref, repo); git_reference_free(ref); - if (error == GIT_ENOTFOUND) + if (error == GIT_EORPHANEDHEAD) return 1; if (error < 0) @@ -1519,14 +1523,14 @@ int git_repository_detach_head( git_reference *old_head = NULL, *new_head = NULL; git_object *object = NULL; - int error = -1; + int error; assert(repo); - if (git_repository_head(&old_head, repo) < 0) - return -1; + if ((error = git_repository_head(&old_head, repo)) < 0) + return error; - if (git_object_lookup(&object, repo, git_reference_oid(old_head), GIT_OBJ_COMMIT) < 0) + if ((error = git_object_lookup(&object, repo, git_reference_oid(old_head), GIT_OBJ_COMMIT)) < 0) goto cleanup; error = git_reference_create_oid(&new_head, repo, GIT_HEAD_FILE, git_reference_oid(old_head), 1); |