Commit 46a2b8e8 by Carlos Martín Nieto

Merge pull request #2592 from libgit2/cmn/describe

Implement git-describe
parents 324154a4 a3b9270d
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "git2/commit.h" #include "git2/commit.h"
#include "git2/common.h" #include "git2/common.h"
#include "git2/config.h" #include "git2/config.h"
#include "git2/describe.h"
#include "git2/diff.h" #include "git2/diff.h"
#include "git2/errors.h" #include "git2/errors.h"
#include "git2/filter.h" #include "git2/filter.h"
......
...@@ -83,6 +83,8 @@ GIT_BEGIN_DECL ...@@ -83,6 +83,8 @@ GIT_BEGIN_DECL
*/ */
#define GIT_OID_HEX_ZERO "0000000000000000000000000000000000000000" #define GIT_OID_HEX_ZERO "0000000000000000000000000000000000000000"
#define FLAG_BITS 27
/** /**
* Return the version of the libgit2 library * Return the version of the libgit2 library
* being currently used. * being currently used.
......
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_git_describe_h__
#define INCLUDE_git_describe_h__
#include "common.h"
#include "types.h"
#include "buffer.h"
/**
* @file git2/describe.h
* @brief Git describing routines
* @defgroup git_describe Git describing routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
/**
* Reference lookup strategy
*
* These behave like the --tags and --all optios to git-describe,
* namely they say to look for any reference in either refs/tags/ or
* refs/ respectively.
*/
typedef enum {
GIT_DESCRIBE_DEFAULT,
GIT_DESCRIBE_TAGS,
GIT_DESCRIBE_ALL,
} git_describe_strategy_t;
/**
* Describe options structure
*
* Initialize with `GIT_DESCRIBE_OPTIONS_INIT` macro to correctly set
* the `version` field. E.g.
*
* git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
*/
typedef struct git_describe_options {
unsigned int version;
unsigned int max_candidates_tags; /** default: 10 */
unsigned int describe_strategy; /** default: GIT_DESCRIBE_DEFAULT */
const char *pattern;
/**
* When calculating the distance from the matching tag or
* reference, only walk down the first-parent ancestry.
*/
int only_follow_first_parent;
/**
* If no matching tag or reference is found, the describe
* operation would normally fail. If this option is set, it
* will instead fall back to showing the full id of the
* commit.
*/
int show_commit_oid_as_fallback;
} git_describe_options;
#define GIT_DESCRIBE_DEFAULT_MAX_CANDIDATES_TAGS 10
#define GIT_DESCRIBE_DEFAULT_ABBREVIATED_SIZE 7
#define GIT_DESCRIBE_OPTIONS_VERSION 1
#define GIT_DESCRIBE_OPTIONS_INIT { \
GIT_DESCRIBE_OPTIONS_VERSION, \
GIT_DESCRIBE_DEFAULT_MAX_CANDIDATES_TAGS, \
}
GIT_EXTERN(int) git_describe_init_options(git_describe_options *opts, unsigned int version);
/**
* Options for formatting the describe string
*/
typedef struct {
unsigned int version;
/**
* Size of the abbreviated commit id to use. This value is the
* lower bound for the length of the abbreviated string. The
* default is 7.
*/
unsigned int abbreviated_size;
/**
* Set to use the long format even when a shorter name could be used.
*/
int always_use_long_format;
/**
* If the workdir is dirty and this is set, this string will
* be appended to the description string.
*/
char *dirty_suffix;
} git_describe_format_options;
#define GIT_DESCRIBE_FORMAT_OPTIONS_VERSION 1
#define GIT_DESCRIBE_FORMAT_OPTIONS_INIT { \
GIT_DESCRIBE_FORMAT_OPTIONS_VERSION, \
GIT_DESCRIBE_DEFAULT_ABBREVIATED_SIZE, \
}
GIT_EXTERN(int) git_describe_init_format_options(git_describe_format_options *opts, unsigned int version);
typedef struct git_describe_result git_describe_result;
/**
* Describe a commit
*
* Perform the describe operation on the given committish object.
*
* @param result pointer to store the result. You must free this once
* you're done with it.
* @param committish a committish to describe
* @param opts the lookup options
*/
GIT_EXTERN(int) git_describe_commit(
git_describe_result **result,
git_object *committish,
git_describe_options *opts);
/**
* Describe a commit
*
* Perform the describe operation on the current commit and the
* worktree. After peforming describe on HEAD, a status is run and the
* description is considered to be dirty if there are.
*
* @param result pointer to store the result. You must free this once
* you're done with it.
* @param repo the repository in which to perform the describe
* @param opts the lookup options
*/
GIT_EXTERN(int) git_describe_workdir(
git_describe_result **out,
git_repository *repo,
git_describe_options *opts);
/**
* Print the describe result to a buffer
*
* @param result the result from `git_describe_commit()` or
* `git_describe_workdir()`.
* @param opt the formatting options
*/
GIT_EXTERN(int) git_describe_format(
git_buf *out,
const git_describe_result *result,
const git_describe_format_options *opts);
/**
* Free the describe result.
*/
GIT_EXTERN(void) git_describe_result_free(git_describe_result *result);
/** @} */
GIT_END_DECL
#endif
...@@ -89,6 +89,7 @@ typedef enum { ...@@ -89,6 +89,7 @@ typedef enum {
GITERR_REVERT, GITERR_REVERT,
GITERR_CALLBACK, GITERR_CALLBACK,
GITERR_CHERRYPICK, GITERR_CHERRYPICK,
GITERR_DESCRIBE,
} git_error_t; } git_error_t;
/** /**
......
...@@ -25,7 +25,7 @@ typedef struct git_commit_list_node { ...@@ -25,7 +25,7 @@ typedef struct git_commit_list_node {
uninteresting:1, uninteresting:1,
topo_delay:1, topo_delay:1,
parsed:1, parsed:1,
flags : 4; flags : FLAG_BITS;
unsigned short in_degree; unsigned short in_degree;
unsigned short out_degree; unsigned short out_degree;
......
This diff is collapsed. Click to expand it.
...@@ -32,4 +32,20 @@ GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid) ...@@ -32,4 +32,20 @@ GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid)
#define git_oidmap_alloc() kh_init(oid) #define git_oidmap_alloc() kh_init(oid)
#define git_oidmap_free(h) kh_destroy(oid,h), h = NULL #define git_oidmap_free(h) kh_destroy(oid,h), h = NULL
#define git_oidmap_lookup_index(h, k) kh_get(oid, h, k)
#define git_oidmap_valid_index(h, idx) (idx != kh_end(h))
#define git_oidmap_value_at(h, idx) kh_val(h, idx)
#define git_oidmap_insert(h, key, val, rval) do { \
khiter_t __pos = kh_put(oid, h, key, &rval); \
if (rval >= 0) { \
if (rval == 0) kh_key(h, __pos) = key; \
kh_val(h, __pos) = val; \
} } while (0)
#define git_oidmap_foreach_value kh_foreach_value
#define git_oidmap_size(h) kh_size(h)
#endif #endif
#include "clar_libgit2.h"
#include "describe_helpers.h"
void test_describe_describe__can_describe_against_a_bare_repo(void)
{
git_repository *repo;
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
assert_describe("hard_tag", "HEAD", repo, &opts, &fmt_opts);
opts.show_commit_oid_as_fallback = 1;
assert_describe("be3563a*", "HEAD^", repo, &opts, &fmt_opts);
git_repository_free(repo);
}
static int delete_cb(git_reference *ref, void *payload)
{
GIT_UNUSED(payload);
cl_git_pass(git_reference_delete(ref));
git_reference_free(ref);
return 0;
}
void test_describe_describe__cannot_describe_against_a_repo_with_no_ref(void)
{
git_repository *repo;
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_buf buf = GIT_BUF_INIT;
git_object *object;
git_describe_result *result = NULL;
repo = cl_git_sandbox_init("testrepo.git");
cl_git_pass(git_revparse_single(&object, repo, "HEAD"));
cl_git_pass(git_reference_foreach(repo, delete_cb, NULL));
cl_git_fail(git_describe_commit(&result, object, &opts));
git_describe_result_free(result);
git_object_free(object);
git_buf_free(&buf);
cl_git_sandbox_cleanup();
}
#include "describe_helpers.h"
void assert_describe(
const char *expected_output,
const char *revparse_spec,
git_repository *repo,
git_describe_options *opts,
git_describe_format_options *fmt_opts)
{
git_object *object;
git_buf label = GIT_BUF_INIT;
git_describe_result *result;
cl_git_pass(git_revparse_single(&object, repo, revparse_spec));
cl_git_pass(git_describe_commit(&result, object, opts));
cl_git_pass(git_describe_format(&label, result, fmt_opts));
cl_git_pass(p_fnmatch(expected_output, git_buf_cstr(&label), 0));
git_describe_result_free(result);
git_object_free(object);
git_buf_free(&label);
}
void assert_describe_workdir(
const char *expected_output,
git_repository *repo,
git_describe_options *opts,
git_describe_format_options *fmt_opts)
{
git_buf label = GIT_BUF_INIT;
git_describe_result *result;
cl_git_pass(git_describe_workdir(&result, repo, opts));
cl_git_pass(git_describe_format(&label, result, fmt_opts));
cl_git_pass(p_fnmatch(expected_output, git_buf_cstr(&label), 0));
git_describe_result_free(result);
git_buf_free(&label);
}
#include "clar_libgit2.h"
#include "buffer.h"
extern void assert_describe(
const char *expected_output,
const char *revparse_spec,
git_repository *repo,
git_describe_options *opts,
git_describe_format_options *fmt_opts);
extern void assert_describe_workdir(
const char *expected_output,
git_repository *repo,
git_describe_options *opts,
git_describe_format_options *fmt_opts);
#include "clar_libgit2.h"
#include "describe_helpers.h"
#include "repository.h"
// Ported from https://github.com/git/git/blob/adfc1857bdb090786fd9d22c1acec39371c76048/t/t6120-describe.sh
static git_repository *repo;
void test_describe_t6120__initialize(void)
{
repo = cl_git_sandbox_init("describe");
}
void test_describe_t6120__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_describe_t6120__default(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
assert_describe("A-*", "HEAD", repo, &opts, &fmt_opts);
assert_describe("A-*", "HEAD^", repo, &opts, &fmt_opts);
assert_describe("R-*", "HEAD^^", repo, &opts, &fmt_opts);
assert_describe("A-*", "HEAD^^2", repo, &opts, &fmt_opts);
assert_describe("B", "HEAD^^2^", repo, &opts, &fmt_opts);
assert_describe("R-*", "HEAD^^^", repo, &opts, &fmt_opts);
}
void test_describe_t6120__tags(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
opts.describe_strategy = GIT_DESCRIBE_TAGS;
assert_describe("c-*", "HEAD", repo, &opts, &fmt_opts);
assert_describe("c-*", "HEAD^", repo, &opts, &fmt_opts);
assert_describe("e-*", "HEAD^^", repo, &opts, &fmt_opts);
assert_describe("c-*", "HEAD^^2", repo, &opts, &fmt_opts);
assert_describe("B", "HEAD^^2^", repo, &opts, &fmt_opts);
assert_describe("e", "HEAD^^^", repo, &opts, &fmt_opts);
}
void test_describe_t6120__all(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
opts.describe_strategy = GIT_DESCRIBE_ALL;
assert_describe("heads/master", "HEAD", repo, &opts, &fmt_opts);
assert_describe("tags/c-*", "HEAD^", repo, &opts, &fmt_opts);
assert_describe("tags/e", "HEAD^^^", repo, &opts, &fmt_opts);
}
void test_describe_t6120__longformat(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
fmt_opts.always_use_long_format = 1;
assert_describe("B-0-*", "HEAD^^2^", repo, &opts, &fmt_opts);
assert_describe("A-3-*", "HEAD^^2", repo, &opts, &fmt_opts);
}
void test_describe_t6120__firstparent(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
opts.describe_strategy = GIT_DESCRIBE_TAGS;
assert_describe("c-7-*", "HEAD", repo, &opts, &fmt_opts);
opts.only_follow_first_parent = 1;
assert_describe("e-3-*", "HEAD", repo, &opts, &fmt_opts);
}
void test_describe_t6120__workdir(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
assert_describe_workdir("A-*[0-9a-f]", repo, &opts, &fmt_opts);
cl_git_mkfile("describe/file", "something different");
fmt_opts.dirty_suffix = "-dirty";
assert_describe_workdir("A-*[0-9a-f]-dirty", repo, &opts, &fmt_opts);
fmt_opts.dirty_suffix = ".mod";
assert_describe_workdir("A-*[0-9a-f].mod", repo, &opts, &fmt_opts);
}
static void commit_and_tag(
git_time_t *time,
const char *commit_msg,
const char *tag_name)
{
git_index *index;
git_oid commit_id;
git_reference *ref;
cl_git_pass(git_repository_index__weakptr(&index, repo));
cl_git_append2file("describe/file", "\n");
git_index_add_bypath(index, "describe/file");
git_index_write(index);
*time += 10;
cl_repo_commit_from_index(&commit_id, repo, NULL, *time, commit_msg);
if (tag_name == NULL)
return;
cl_git_pass(git_reference_create(&ref, repo, tag_name, &commit_id, 0, NULL, NULL));
git_reference_free(ref);
}
void test_describe_t6120__pattern(void)
{
git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT;
git_describe_format_options fmt_opts = GIT_DESCRIBE_FORMAT_OPTIONS_INIT;
git_oid tag_id;
git_object *head;
git_signature *tagger;
git_time_t time;
/* set-up matching pattern tests */
cl_git_pass(git_revparse_single(&head, repo, "HEAD"));
time = 1380553019;
cl_git_pass(git_signature_new(&tagger, "tagger", "tagger@libgit2.org", time, 0));
cl_git_pass(git_tag_create(&tag_id, repo, "test-annotated", head, tagger, "test-annotated", 0));
git_signature_free(tagger);
git_object_free(head);
commit_and_tag(&time, "one more", "refs/tags/test1-lightweight");
commit_and_tag(&time, "yet another", "refs/tags/test2-lightweight");
commit_and_tag(&time, "even more", NULL);
/* Exercize */
opts.pattern = "test-*";
assert_describe("test-annotated-*", "HEAD", repo, &opts, &fmt_opts);
opts.describe_strategy = GIT_DESCRIBE_TAGS;
opts.pattern = "test1-*";
assert_describe("test1-lightweight-*", "HEAD", repo, &opts, &fmt_opts);
opts.pattern = "test2-*";
assert_describe("test2-lightweight-*", "HEAD", repo, &opts, &fmt_opts);
fmt_opts.always_use_long_format = 1;
assert_describe("test2-lightweight-*", "HEAD^", repo, &opts, &fmt_opts);
}
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly
0000000000000000000000000000000000000000 108b485d8268ea595df8ffea74f0f4b186577d32 nulltoken <emeric.fermas@gmail.com> 1380209394 +0200 commit (initial): initial
108b485d8268ea595df8ffea74f0f4b186577d32 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f nulltoken <emeric.fermas@gmail.com> 1380209404 +0200 commit: second
4d6558b8fa764baeb0f19c1e857df91e0eda5a0f b240c0fb88c5a629e00ebc1275fa1f33e364a705 nulltoken <emeric.fermas@gmail.com> 1380209414 +0200 commit: third
b240c0fb88c5a629e00ebc1275fa1f33e364a705 81f4b1aac643e6983fab370eae8aefccecbf3a4c nulltoken <emeric.fermas@gmail.com> 1380209425 +0200 commit: A
81f4b1aac643e6983fab370eae8aefccecbf3a4c 6126a5f9c57ebc81e64370ec3095184ad92dab1c nulltoken <emeric.fermas@gmail.com> 1380209445 +0200 commit: c
6126a5f9c57ebc81e64370ec3095184ad92dab1c 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f nulltoken <emeric.fermas@gmail.com> 1380209455 +0200 reset: moving to 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f
4d6558b8fa764baeb0f19c1e857df91e0eda5a0f 31fc9136820b507e938a9c6b88bf2c567a9f6f4b nulltoken <emeric.fermas@gmail.com> 1380209465 +0200 commit: B
31fc9136820b507e938a9c6b88bf2c567a9f6f4b ce1c4f8b6120122e23d4442925d98c56c41917d8 nulltoken <emeric.fermas@gmail.com> 1380209486 +0200 merge c: Merge made by the 'recursive' strategy.
ce1c4f8b6120122e23d4442925d98c56c41917d8 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f nulltoken <emeric.fermas@gmail.com> 1380209486 +0200 reset: moving to 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f
4d6558b8fa764baeb0f19c1e857df91e0eda5a0f 6a12b56088706aa6c39ccd23b7c7ce60f3a0b9a1 nulltoken <emeric.fermas@gmail.com> 1380209496 +0200 commit: D
6a12b56088706aa6c39ccd23b7c7ce60f3a0b9a1 1e016431ec7b22dd3e23f3e6f5f68f358f9227cf nulltoken <emeric.fermas@gmail.com> 1380209527 +0200 commit: another
1e016431ec7b22dd3e23f3e6f5f68f358f9227cf a9eb02af13df030159e39f70330d5c8a47655691 nulltoken <emeric.fermas@gmail.com> 1380209547 +0200 commit: yet another
a9eb02af13df030159e39f70330d5c8a47655691 949b98e208015bfc0e2f573debc34ae2f97a7f0e nulltoken <emeric.fermas@gmail.com> 1380209557 +0200 merge ce1c4f8b6120122e23d4442925d98c56c41917d8: Merge made by the 'recursive' strategy.
949b98e208015bfc0e2f573debc34ae2f97a7f0e a6095f816e81f64651595d488badc42399837d6a nulltoken <emeric.fermas@gmail.com> 1380209567 +0200 commit: x
0000000000000000000000000000000000000000 108b485d8268ea595df8ffea74f0f4b186577d32 nulltoken <emeric.fermas@gmail.com> 1380209394 +0200 commit (initial): initial
108b485d8268ea595df8ffea74f0f4b186577d32 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f nulltoken <emeric.fermas@gmail.com> 1380209404 +0200 commit: second
4d6558b8fa764baeb0f19c1e857df91e0eda5a0f b240c0fb88c5a629e00ebc1275fa1f33e364a705 nulltoken <emeric.fermas@gmail.com> 1380209414 +0200 commit: third
b240c0fb88c5a629e00ebc1275fa1f33e364a705 81f4b1aac643e6983fab370eae8aefccecbf3a4c nulltoken <emeric.fermas@gmail.com> 1380209425 +0200 commit: A
81f4b1aac643e6983fab370eae8aefccecbf3a4c 6126a5f9c57ebc81e64370ec3095184ad92dab1c nulltoken <emeric.fermas@gmail.com> 1380209445 +0200 commit: c
6126a5f9c57ebc81e64370ec3095184ad92dab1c 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f nulltoken <emeric.fermas@gmail.com> 1380209455 +0200 reset: moving to 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f
4d6558b8fa764baeb0f19c1e857df91e0eda5a0f 31fc9136820b507e938a9c6b88bf2c567a9f6f4b nulltoken <emeric.fermas@gmail.com> 1380209465 +0200 commit: B
31fc9136820b507e938a9c6b88bf2c567a9f6f4b ce1c4f8b6120122e23d4442925d98c56c41917d8 nulltoken <emeric.fermas@gmail.com> 1380209486 +0200 merge c: Merge made by the 'recursive' strategy.
ce1c4f8b6120122e23d4442925d98c56c41917d8 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f nulltoken <emeric.fermas@gmail.com> 1380209486 +0200 reset: moving to 4d6558b8fa764baeb0f19c1e857df91e0eda5a0f
4d6558b8fa764baeb0f19c1e857df91e0eda5a0f 6a12b56088706aa6c39ccd23b7c7ce60f3a0b9a1 nulltoken <emeric.fermas@gmail.com> 1380209496 +0200 commit: D
6a12b56088706aa6c39ccd23b7c7ce60f3a0b9a1 1e016431ec7b22dd3e23f3e6f5f68f358f9227cf nulltoken <emeric.fermas@gmail.com> 1380209527 +0200 commit: another
1e016431ec7b22dd3e23f3e6f5f68f358f9227cf a9eb02af13df030159e39f70330d5c8a47655691 nulltoken <emeric.fermas@gmail.com> 1380209547 +0200 commit: yet another
a9eb02af13df030159e39f70330d5c8a47655691 949b98e208015bfc0e2f573debc34ae2f97a7f0e nulltoken <emeric.fermas@gmail.com> 1380209557 +0200 merge ce1c4f8b6120122e23d4442925d98c56c41917d8: Merge made by the 'recursive' strategy.
949b98e208015bfc0e2f573debc34ae2f97a7f0e a6095f816e81f64651595d488badc42399837d6a nulltoken <emeric.fermas@gmail.com> 1380209567 +0200 commit: x
a6095f816e81f64651595d488badc42399837d6a
aaddd4f14847e0e323924ec262c2343249a84f8b
52912fbab0715dec53d43053966e78ad213ba359
10bd08b099ecb79184c60183f5c94ca915f427ad
680166b6cd31f76354fee2572618e6b0142d05e6
6126a5f9c57ebc81e64370ec3095184ad92dab1c
1e016431ec7b22dd3e23f3e6f5f68f358f9227cf
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment