Skip to content

Commit

Permalink
refs: make unborn-ness checking available outside of HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
tiennou committed Apr 10, 2018
1 parent 2dc5485 commit 37b69a5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/refs.c
Expand Up @@ -1430,3 +1430,28 @@ const char *git_reference_shorthand(const git_reference *ref)
{
return git_reference__shorthand(ref->name);
}

int git_reference__resolve_unborn(git_reference **out_ref, git_repository *repo, const git_reference *ref)
{
git_reference *tmp_ref = NULL;
int error;

if (ref->type == GIT_REF_OID) {
giterr_set(GITERR_REFERENCE, "direct references cannot be unborn");
return GIT_EINVALID;
}

if (out_ref == NULL) {
out_ref = &tmp_ref;
}

error = git_reference_lookup_resolved(out_ref, repo, git_reference_symbolic_target(ref), -1);

if (error == GIT_ENOTFOUND) {
error = GIT_EUNBORNBRANCH;
}

git_reference_free(tmp_ref);

return error;
}
12 changes: 12 additions & 0 deletions src/refs.h
Expand Up @@ -136,4 +136,16 @@ int git_reference__update_for_commit(
const git_oid *id,
const char *operation);

/**
* Lookup a reference, with unborn semantics.
*
* @param out_ref Pointer to the resolved reference. Can be null.
* @param repo The repository to use for resolution.
* @param ref The reference to resolve.
* @return 0 on success and out_ref (if provided) will be populated.
* Otherwise, returns GIT_EINVALID if the reference is not symbolic,
* GIT_EUNBORNBRANCH if the reference is unborn, or another error code.
*/
int git_reference__resolve_unborn(git_reference **out_ref, git_repository *repo, const git_reference *ref);

#endif
8 changes: 5 additions & 3 deletions src/repository.c
Expand Up @@ -2119,6 +2119,8 @@ int git_repository_head(git_reference **head_out, git_repository *repo)
git_reference *head;
int error;

assert(head_out);

if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
return error;

Expand All @@ -2127,10 +2129,10 @@ int git_repository_head(git_reference **head_out, git_repository *repo)
return 0;
}

error = git_reference_lookup_resolved(head_out, repo, git_reference_symbolic_target(head), -1);
error = git_reference__resolve_unborn(head_out, repo, head);
git_reference_free(head);

return error == GIT_ENOTFOUND ? GIT_EUNBORNBRANCH : error;
return error;
}

int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name)
Expand All @@ -2150,7 +2152,7 @@ int git_repository_head_for_worktree(git_reference **out, git_repository *repo,
if (git_reference_type(head) != GIT_REF_OID) {
git_reference *resolved;

error = git_reference_lookup_resolved(&resolved, repo, git_reference_symbolic_target(head), -1);
error = git_reference__resolve_unborn(&resolved, repo, head);
git_reference_free(head);
head = resolved;
}
Expand Down

0 comments on commit 37b69a5

Please sign in to comment.