vendor/libgit2/src/annotated_commit.c in rugged-0.25.0b2 vs vendor/libgit2/src/annotated_commit.c in rugged-0.25.0b3

- old
+ new

@@ -18,42 +18,105 @@ #include "git2/tree.h" #include "git2/index.h" static int annotated_commit_init( git_annotated_commit **out, + git_commit *commit, + const char *description) +{ + git_annotated_commit *annotated_commit; + int error = 0; + + assert(out && commit); + + *out = NULL; + + annotated_commit = git__calloc(1, sizeof(git_annotated_commit)); + GITERR_CHECK_ALLOC(annotated_commit); + + annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL; + + if ((error = git_commit_dup(&annotated_commit->commit, commit)) < 0) + goto done; + + git_oid_fmt(annotated_commit->id_str, git_commit_id(commit)); + annotated_commit->id_str[GIT_OID_HEXSZ] = '\0'; + + if (!description) + description = annotated_commit->id_str; + + annotated_commit->description = git__strdup(description); + GITERR_CHECK_ALLOC(annotated_commit->description); + +done: + if (!error) + *out = annotated_commit; + + return error; +} + +static int annotated_commit_init_from_id( + git_annotated_commit **out, git_repository *repo, const git_oid *id, - const char *ref_name, - const char *remote_url) + const char *description) { - git_annotated_commit *annotated_commit; git_commit *commit = NULL; int error = 0; - assert(out && id); + assert(out && repo && id); *out = NULL; - if ((error = git_commit_lookup(&commit, repo, id)) < 0 || - (error = git_annotated_commit_from_commit(&annotated_commit, - commit)) < 0) + if ((error = git_commit_lookup(&commit, repo, id)) < 0) goto done; - if (ref_name) { - annotated_commit->ref_name = git__strdup(ref_name); - GITERR_CHECK_ALLOC(annotated_commit->ref_name); - } + error = annotated_commit_init(out, commit, description); - if (remote_url) { - annotated_commit->remote_url = git__strdup(remote_url); - GITERR_CHECK_ALLOC(annotated_commit->remote_url); +done: + git_commit_free(commit); + return error; +} + +int git_annotated_commit_lookup( + git_annotated_commit **out, + git_repository *repo, + const git_oid *id) +{ + return annotated_commit_init_from_id(out, repo, id, NULL); +} + +int git_annotated_commit_from_commit( + git_annotated_commit **out, + git_commit *commit) +{ + return annotated_commit_init(out, commit, NULL); +} + +int git_annotated_commit_from_revspec( + git_annotated_commit **out, + git_repository *repo, + const char *revspec) +{ + git_object *obj, *commit; + int error; + + assert(out && repo && revspec); + + if ((error = git_revparse_single(&obj, repo, revspec)) < 0) + return error; + + if ((error = git_object_peel(&commit, obj, GIT_OBJ_COMMIT))) { + git_object_free(obj); + return error; } - *out = annotated_commit; + error = annotated_commit_init(out, (git_commit *)commit, revspec); -done: - git_commit_free(commit); + git_object_free(obj); + git_object_free(commit); + return error; } int git_annotated_commit_from_ref( git_annotated_commit **out, @@ -68,13 +131,20 @@ *out = NULL; if ((error = git_reference_resolve(&resolved, ref)) < 0) return error; - error = annotated_commit_init(out, repo, git_reference_target(resolved), - git_reference_name(ref), NULL); + error = annotated_commit_init_from_id(out, + repo, + git_reference_target(resolved), + git_reference_name(ref)); + if (!error) { + (*out)->ref_name = git__strdup(git_reference_name(ref)); + GITERR_CHECK_ALLOC((*out)->ref_name); + } + git_reference_free(resolved); return error; } int git_annotated_commit_from_head( @@ -95,81 +165,29 @@ git_reference_free(head); return error; } -int git_annotated_commit_from_commit( - git_annotated_commit **out, - git_commit *commit) -{ - git_annotated_commit *annotated_commit; - - assert(out && commit); - - *out = NULL; - - annotated_commit = git__calloc(1, sizeof(git_annotated_commit)); - GITERR_CHECK_ALLOC(annotated_commit); - - annotated_commit->type = GIT_ANNOTATED_COMMIT_REAL; - - git_cached_obj_incref(commit); - annotated_commit->commit = commit; - - git_oid_fmt(annotated_commit->id_str, git_commit_id(commit)); - annotated_commit->id_str[GIT_OID_HEXSZ] = '\0'; - - *out = annotated_commit; - return 0; -} - -int git_annotated_commit_lookup( - git_annotated_commit **out, - git_repository *repo, - const git_oid *id) -{ - assert(out && repo && id); - - return annotated_commit_init(out, repo, id, NULL, NULL); -} - int git_annotated_commit_from_fetchhead( git_annotated_commit **out, git_repository *repo, const char *branch_name, const char *remote_url, const git_oid *id) { assert(repo && id && branch_name && remote_url); - return annotated_commit_init(out, repo, id, branch_name, remote_url); -} + if (annotated_commit_init_from_id(out, repo, id, branch_name) < 0) + return -1; -int git_annotated_commit_from_revspec( - git_annotated_commit **out, - git_repository *repo, - const char *revspec) -{ - git_object *obj, *commit; - int error; + (*out)->ref_name = git__strdup(branch_name); + GITERR_CHECK_ALLOC((*out)->ref_name); - assert(out && repo && revspec); + (*out)->remote_url = git__strdup(remote_url); + GITERR_CHECK_ALLOC((*out)->remote_url); - if ((error = git_revparse_single(&obj, repo, revspec)) < 0) - return error; - - if ((error = git_object_peel(&commit, obj, GIT_OBJ_COMMIT))) { - git_object_free(obj); - return error; - } - - error = annotated_commit_init(out, repo, git_object_id(commit), revspec, NULL); - - git_object_free(obj); - git_object_free(commit); - - return error; + return 0; } const git_oid *git_annotated_commit_id( const git_annotated_commit *annotated_commit) @@ -185,11 +203,12 @@ switch (annotated_commit->type) { case GIT_ANNOTATED_COMMIT_REAL: git_commit_free(annotated_commit->commit); git_tree_free(annotated_commit->tree); - git__free(annotated_commit->ref_name); - git__free(annotated_commit->remote_url); + git__free((char *)annotated_commit->description); + git__free((char *)annotated_commit->ref_name); + git__free((char *)annotated_commit->remote_url); break; case GIT_ANNOTATED_COMMIT_VIRTUAL: git_index_free(annotated_commit->index); git_array_clear(annotated_commit->parents); break;