* 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 */
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(
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;
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);
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));
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;
name->path = NULL;
- if (in->tag && git_object_dup((git_object **) &name->tag, (git_object *) in->tag) < 0)
+ if (in->tag && git_tag_dup(&name->tag, in->tag) < 0)
return -1;
name->path = git__strdup(in->path);
- GITERR_CHECK_ALLOC(name->path);
+ GIT_ERROR_CHECK_ALLOC(name->path);
*out = name;
return 0;
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;
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;
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;
}
}
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;
}
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;
/* 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_oid* id,
- size_t abbrev_size)
+ const git_oid *id,
+ unsigned int abbrev_size)
{
int error, size = 0;
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
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;
}
if (!data->opts->max_candidates_tags) {
error = describe_not_found(
git_commit_id(commit),
- "Cannot describe - no tag exactly matches '%s'");
+ "cannot describe - no tag exactly matches '%s'");
goto cleanup;
}
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;
goto cleanup;
}
if (unannotated_cnt) {
- error = describe_not_found(git_commit_id(commit),
- "Cannot describe - "
- "No annotated tags can describe '%s'."
- "However, there were unannotated tags.");
+ 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),
- "Cannot describe - "
- "No tags can describe '%s'.");
+ error = describe_not_found(git_commit_id(commit),
+ "cannot describe - "
+ "no tags can describe '%s'.");
goto cleanup;
}
}
best = (struct possible_tag *)git_vector_get(&all_matches, 0);
if (gave_up_on) {
- git_pqueue_insert(&list, gave_up_on);
+ if ((error = git_pqueue_insert(&list, gave_up_on)) < 0)
+ goto cleanup;
seen_commits--;
}
if ((error = finish_depth_computation(
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);
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(
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 - "
- "No reference found, cannot describe anything.");
+ 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;
}
if ((error = git_reference_name_to_id(¤t_id, repo, GIT_HEAD_FILE)) < 0)
return error;
- if ((error = git_object_lookup(&commit, repo, ¤t_id, GIT_OBJ_COMMIT)) < 0)
+ if ((error = git_object_lookup(&commit, repo, ¤t_id, GIT_OBJECT_COMMIT)) < 0)
return error;
/* The first step is to perform a describe of HEAD, so we can leverage this */
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;
}
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;
}
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 */
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 */
}
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)
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