]> git.proxmox.com Git - libgit2.git/blobdiff - src/describe.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / describe.c
index 16e195542abe44f15f60b6c458914b71730eae6a..1033eac500a05c4cbd40c0fd45019a057cf88ad2 100644 (file)
@@ -4,22 +4,24 @@
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
  */
+
+#include "common.h"
+
 #include "git2/describe.h"
 #include "git2/strarray.h"
 #include "git2/diff.h"
 #include "git2/status.h"
 
-#include "common.h"
+#include "buf.h"
 #include "commit.h"
 #include "commit_list.h"
 #include "oidmap.h"
 #include "refs.h"
+#include "repository.h"
 #include "revwalk.h"
 #include "tag.h"
 #include "vector.h"
-#include "repository.h"
-
-GIT__USE_OIDMAP
+#include "wildmatch.h"
 
 /* Ported from https://github.com/git/git/blob/89dde7882f71f846ccd0359756d27bebc31108de/builtin/describe.c */
 
@@ -36,12 +38,7 @@ struct commit_name {
 
 static void *oidmap_value_bykey(git_oidmap *map, const git_oid *key)
 {
-       khint_t pos = git_oidmap_lookup_index(map, key);
-
-       if (!git_oidmap_valid_index(map, pos))
-               return NULL;
-
-       return git_oidmap_value_at(map, pos);
+       return git_oidmap_get(map, key);
 }
 
 static struct commit_name *find_commit_name(
@@ -108,7 +105,7 @@ static int add_to_known_names(
        if (replace_name(&tag, repo, e, prio, sha1)) {
                if (!found) {
                        e = git__malloc(sizeof(struct commit_name));
-                       GITERR_CHECK_ALLOC(e);
+                       GIT_ERROR_CHECK_ALLOC(e);
 
                        e->path = NULL;
                        e->tag = NULL;
@@ -124,13 +121,8 @@ static int add_to_known_names(
                e->path = git__strdup(path);
                git_oid_cpy(&e->peeled, peeled);
 
-               if (!found) {
-                       int ret;
-
-                       git_oidmap_insert(names, &e->peeled, e, ret);
-                       if (ret < 0)
-                               return -1;
-               }
+               if (!found && git_oidmap_set(names, &e->peeled, e) < 0)
+                       return -1;
        }
        else
                git_tag_free(tag);
@@ -151,7 +143,7 @@ static int retrieve_peeled_tag_or_object_oid(
        if ((error = git_reference_lookup_resolved(&ref, repo, refname, -1)) < 0)
                return error;
 
-       if ((error = git_reference_peel(&peeled, ref, GIT_OBJ_ANY)) < 0)
+       if ((error = git_reference_peel(&peeled, ref, GIT_OBJECT_ANY)) < 0)
                goto cleanup;
 
        git_oid_cpy(ref_target_out, git_reference_target(ref));
@@ -191,7 +183,7 @@ static int commit_name_dup(struct commit_name **out, struct commit_name *in)
        struct commit_name *name;
 
        name = git__malloc(sizeof(struct commit_name));
-       GITERR_CHECK_ALLOC(name);
+       GIT_ERROR_CHECK_ALLOC(name);
 
        memcpy(name, in,  sizeof(struct commit_name));
        name->tag = NULL;
@@ -201,7 +193,7 @@ static int commit_name_dup(struct commit_name **out, struct commit_name *in)
                return -1;
 
        name->path = git__strdup(in->path);
-       GITERR_CHECK_ALLOC(name->path);
+       GIT_ERROR_CHECK_ALLOC(name->path);
 
        *out = name;
        return 0;
@@ -224,7 +216,7 @@ static int get_name(const char *refname, void *payload)
                return 0;
 
        /* Accept only tags that match the pattern, if given */
-       if (data->opts->pattern && (!is_tag || p_fnmatch(data->opts->pattern,
+       if (data->opts->pattern && (!is_tag || wildmatch(data->opts->pattern,
                refname + strlen(GIT_REFS_TAGS_DIR), 0)))
                                return 0;
 
@@ -267,7 +259,7 @@ static int possible_tag_dup(struct possible_tag **out, struct possible_tag *in)
        int error;
 
        tag = git__malloc(sizeof(struct possible_tag));
-       GITERR_CHECK_ALLOC(tag);
+       GIT_ERROR_CHECK_ALLOC(tag);
 
        memcpy(tag, in, sizeof(struct possible_tag));
        tag->name = NULL;
@@ -331,18 +323,18 @@ static unsigned long finish_depth_computation(
        return seen_commits;
 }
 
-static int display_name(git_buf *buf, git_repository *repo, struct commit_name *n)
+static int display_name(git_str *buf, git_repository *repo, struct commit_name *n)
 {
        if (n->prio == 2 && !n->tag) {
                if (git_tag_lookup(&n->tag, repo, &n->sha1) < 0) {
-                       giterr_set(GITERR_TAG, "annotated tag '%s' not available", n->path);
+                       git_error_set(GIT_ERROR_TAG, "annotated tag '%s' not available", n->path);
                        return -1;
                }
        }
 
        if (n->tag && !n->name_checked) {
                if (!git_tag_name(n->tag)) {
-                       giterr_set(GITERR_TAG, "annotated tag '%s' has no embedded name", n->path);
+                       git_error_set(GIT_ERROR_TAG, "annotated tag '%s' has no embedded name", n->path);
                        return -1;
                }
 
@@ -355,9 +347,9 @@ static int display_name(git_buf *buf, git_repository *repo, struct commit_name *
        }
 
        if (n->tag)
-               git_buf_printf(buf, "%s", git_tag_name(n->tag));
+               git_str_printf(buf, "%s", git_tag_name(n->tag));
        else
-               git_buf_printf(buf, "%s", n->path);
+               git_str_printf(buf, "%s", n->path);
 
        return 0;
 }
@@ -366,7 +358,7 @@ static int find_unique_abbrev_size(
        int *out,
        git_repository *repo,
        const git_oid *oid_in,
-       int abbreviated_size)
+       unsigned int abbreviated_size)
 {
        size_t size = abbreviated_size;
        git_odb *odb;
@@ -392,16 +384,16 @@ static int find_unique_abbrev_size(
 
        /* If we didn't find any shorter prefix, we have to do the whole thing */
        *out = GIT_OID_HEXSZ;
-       
+
        return 0;
 }
 
 static int show_suffix(
-       git_buf *buf,
+       git_str *buf,
        int depth,
        git_repository *repo,
-       const git_oidid,
-       size_t abbrev_size)
+       const git_oid *id,
+       unsigned int abbrev_size)
 {
        int error, size = 0;
 
@@ -412,11 +404,11 @@ static int show_suffix(
 
        git_oid_fmt(hex_oid, id);
 
-       git_buf_printf(buf, "-%d-g", depth);
+       git_str_printf(buf, "-%d-g", depth);
 
-       git_buf_put(buf, hex_oid, size);
+       git_str_put(buf, hex_oid, size);
 
-       return git_buf_oom(buf) ? -1 : 0;
+       return git_str_oom(buf) ? -1 : 0;
 }
 
 #define MAX_CANDIDATES_TAGS FLAG_BITS - 1
@@ -425,7 +417,7 @@ static int describe_not_found(const git_oid *oid, const char *message_format) {
        char oid_str[GIT_OID_HEXSZ + 1];
        git_oid_tostr(oid_str, sizeof(oid_str), oid);
 
-       giterr_set(GITERR_DESCRIBE, message_format, oid_str);
+       git_error_set(GIT_ERROR_DESCRIBE, message_format, oid_str);
        return GIT_ENOTFOUND;
 }
 
@@ -504,7 +496,7 @@ static int describe(
                                unannotated_cnt++;
                        } else if (match_cnt < data->opts->max_candidates_tags) {
                                struct possible_tag *t = git__malloc(sizeof(struct commit_name));
-                               GITERR_CHECK_ALLOC(t);
+                               GIT_ERROR_CHECK_ALLOC(t);
                                if ((error = git_vector_insert(&all_matches, t)) < 0)
                                        goto cleanup;
 
@@ -563,14 +555,14 @@ static int describe(
                        goto cleanup;
                }
                if (unannotated_cnt) {
-                       error = describe_not_found(git_commit_id(commit), 
+                       error = describe_not_found(git_commit_id(commit),
                                "cannot describe - "
                                "no annotated tags can describe '%s'; "
                            "however, there were unannotated tags.");
                        goto cleanup;
                }
                else {
-                       error = describe_not_found(git_commit_id(commit), 
+                       error = describe_not_found(git_commit_id(commit),
                                "cannot describe - "
                                "no tags can describe '%s'.");
                        goto cleanup;
@@ -664,10 +656,11 @@ int git_describe_commit(
        int error = -1;
        git_describe_options normalized;
 
-       assert(committish);
+       GIT_ASSERT_ARG(result);
+       GIT_ASSERT_ARG(committish);
 
        data.result = git__calloc(1, sizeof(git_describe_result));
-       GITERR_CHECK_ALLOC(data.result);
+       GIT_ERROR_CHECK_ALLOC(data.result);
        data.result->repo = git_object_owner(committish);
 
        data.repo = git_object_owner(committish);
@@ -675,18 +668,18 @@ int git_describe_commit(
        if ((error = normalize_options(&normalized, opts)) < 0)
                return error;
 
-       GITERR_CHECK_VERSION(
+       GIT_ERROR_CHECK_VERSION(
                &normalized,
                GIT_DESCRIBE_OPTIONS_VERSION,
                "git_describe_options");
        data.opts = &normalized;
 
-       data.names = git_oidmap_alloc();
-       GITERR_CHECK_ALLOC(data.names);
+       if ((error = git_oidmap_new(&data.names)) < 0)
+               return error;
 
        /** TODO: contains to be implemented */
 
-       if ((error = git_object_peel((git_object **)(&commit), committish, GIT_OBJ_COMMIT)) < 0)
+       if ((error = git_object_peel((git_object **)(&commit), committish, GIT_OBJECT_COMMIT)) < 0)
                goto cleanup;
 
        if ((error = git_reference_foreach_name(
@@ -694,8 +687,8 @@ int git_describe_commit(
                        get_name, &data)) < 0)
                                goto cleanup;
 
-       if (git_oidmap_size(data.names) == 0 && !opts->show_commit_oid_as_fallback) {
-               giterr_set(GITERR_DESCRIBE, "cannot describe - "
+       if (git_oidmap_size(data.names) == 0 && !normalized.show_commit_oid_as_fallback) {
+               git_error_set(GIT_ERROR_DESCRIBE, "cannot describe - "
                        "no reference found, cannot describe anything.");
                error = -1;
                goto cleanup;
@@ -738,7 +731,7 @@ int git_describe_workdir(
        if ((error = git_reference_name_to_id(&current_id, repo, GIT_HEAD_FILE)) < 0)
                return error;
 
-       if ((error = git_object_lookup(&commit, repo, &current_id, GIT_OBJ_COMMIT)) < 0)
+       if ((error = git_object_lookup(&commit, repo, &current_id, GIT_OBJECT_COMMIT)) < 0)
                return error;
 
        /* The first step is to perform a describe of HEAD, so we can leverage this */
@@ -769,7 +762,7 @@ static int normalize_format_options(
        const git_describe_format_options *src)
 {
        if (!src) {
-               git_describe_init_format_options(dst, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION);
+               git_describe_format_options_init(dst, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION);
                return 0;
        }
 
@@ -777,23 +770,24 @@ static int normalize_format_options(
        return 0;
 }
 
-int git_describe_format(git_buf *out, const git_describe_result *result, const git_describe_format_options *given)
+static int git_describe__format(
+       git_str *out,
+       const git_describe_result *result,
+       const git_describe_format_options *given)
 {
        int error;
        git_repository *repo;
        struct commit_name *name;
        git_describe_format_options opts;
 
-       assert(out && result);
+       GIT_ASSERT_ARG(out);
+       GIT_ASSERT_ARG(result);
 
-       GITERR_CHECK_VERSION(given, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION, "git_describe_format_options");
+       GIT_ERROR_CHECK_VERSION(given, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION, "git_describe_format_options");
        normalize_format_options(&opts, given);
 
-       git_buf_sanitize(out);
-
-
        if (opts.always_use_long_format && opts.abbreviated_size == 0) {
-               giterr_set(GITERR_DESCRIBE, "cannot describe - "
+               git_error_set(GIT_ERROR_DESCRIBE, "cannot describe - "
                        "'always_use_long_format' is incompatible with a zero"
                        "'abbreviated_size'");
                return -1;
@@ -815,9 +809,9 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g
                }
 
                if (result->dirty && opts.dirty_suffix)
-                       git_buf_puts(out, opts.dirty_suffix);
+                       git_str_puts(out, opts.dirty_suffix);
 
-               return git_buf_oom(out) ? -1 : 0;
+               return git_str_oom(out) ? -1 : 0;
        }
 
        /* If we didn't find *any* tags, we fall back to the commit's id */
@@ -830,12 +824,12 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g
                        return -1;
 
                git_oid_fmt(hex_oid, &result->commit_id);
-               git_buf_put(out, hex_oid, size);
+               git_str_put(out, hex_oid, size);
 
                if (result->dirty && opts.dirty_suffix)
-                       git_buf_puts(out, opts.dirty_suffix);
+                       git_str_puts(out, opts.dirty_suffix);
 
-               return git_buf_oom(out) ? -1 : 0;
+               return git_str_oom(out) ? -1 : 0;
        }
 
        /* Lastly, if we found a matching tag, we show that */
@@ -851,10 +845,18 @@ int git_describe_format(git_buf *out, const git_describe_result *result, const g
        }
 
        if (result->dirty && opts.dirty_suffix) {
-               git_buf_puts(out, opts.dirty_suffix);
+               git_str_puts(out, opts.dirty_suffix);
        }
 
-       return git_buf_oom(out) ? -1 : 0;
+       return git_str_oom(out) ? -1 : 0;
+}
+
+int git_describe_format(
+       git_buf *out,
+       const git_describe_result *result,
+       const git_describe_format_options *given)
+{
+       GIT_BUF_WRAP_PRIVATE(out, git_describe__format, result, given);
 }
 
 void git_describe_result_free(git_describe_result *result)
@@ -878,16 +880,30 @@ void git_describe_result_free(git_describe_result *result)
        git__free(result);
 }
 
-int git_describe_init_options(git_describe_options *opts, unsigned int version)
+int git_describe_options_init(git_describe_options *opts, unsigned int version)
 {
        GIT_INIT_STRUCTURE_FROM_TEMPLATE(
                opts, version, git_describe_options, GIT_DESCRIBE_OPTIONS_INIT);
        return 0;
 }
 
-int git_describe_init_format_options(git_describe_format_options *opts, unsigned int version)
+#ifndef GIT_DEPRECATE_HARD
+int git_describe_init_options(git_describe_options *opts, unsigned int version)
+{
+       return git_describe_options_init(opts, version);
+}
+#endif
+
+int git_describe_format_options_init(git_describe_format_options *opts, unsigned int version)
 {
        GIT_INIT_STRUCTURE_FROM_TEMPLATE(
                opts, version, git_describe_format_options, GIT_DESCRIBE_FORMAT_OPTIONS_INIT);
        return 0;
 }
+
+#ifndef GIT_DEPRECATE_HARD
+int git_describe_init_format_options(git_describe_format_options *opts, unsigned int version)
+{
+       return git_describe_format_options_init(opts, version);
+}
+#endif