Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/libgit2/config_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ static git_configmap _configmap_logallrefupdates[] = {
{GIT_CONFIGMAP_STRING, "always", GIT_LOGALLREFUPDATES_ALWAYS},
};

/*
* Generic map for integer values
*/
static git_configmap _configmap_int[] = {
static git_configmap _configmap_abbrev[] = {
{GIT_CONFIGMAP_INT32, NULL, 0},
{GIT_CONFIGMAP_FALSE, NULL, GIT_ABBREV_FALSE},
{GIT_CONFIGMAP_STRING, "auto", GIT_ABBREV_DEFAULT}
};

static struct map_data _configmaps[] = {
Expand All @@ -79,7 +78,7 @@ static struct map_data _configmaps[] = {
{"core.filemode", NULL, 0, GIT_FILEMODE_DEFAULT },
{"core.ignorestat", NULL, 0, GIT_IGNORESTAT_DEFAULT },
{"core.trustctime", NULL, 0, GIT_TRUSTCTIME_DEFAULT },
{"core.abbrev", _configmap_int, 1, GIT_ABBREV_DEFAULT },
{"core.abbrev", _configmap_abbrev, ARRAY_SIZE(_configmap_abbrev), GIT_ABBREV_DEFAULT },
{"core.precomposeunicode", NULL, 0, GIT_PRECOMPOSE_DEFAULT },
{"core.safecrlf", _configmap_safecrlf, ARRAY_SIZE(_configmap_safecrlf), GIT_SAFE_CRLF_DEFAULT},
{"core.logallrefupdates", _configmap_logallrefupdates, ARRAY_SIZE(_configmap_logallrefupdates), GIT_LOGALLREFUPDATES_DEFAULT},
Expand Down
7 changes: 2 additions & 5 deletions src/libgit2/diff_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "zstream.h"
#include "blob.h"
#include "delta.h"
#include "repository.h"
#include "git2/sys/diff.h"

typedef struct {
Expand Down Expand Up @@ -53,14 +54,10 @@ static int diff_print_info_init__common(
if (!pi->id_strlen) {
if (!repo)
pi->id_strlen = GIT_ABBREV_DEFAULT;
else if (git_repository__configmap_lookup(&pi->id_strlen, repo, GIT_CONFIGMAP_ABBREV) < 0)
else if (git_repository__abbrev_length(&pi->id_strlen, repo) < 0)
return -1;
}

if (pi->id_strlen > 0 &&
(size_t)pi->id_strlen > git_oid_hexsize(pi->oid_type))
pi->id_strlen = (int)git_oid_hexsize(pi->oid_type);

memset(&pi->line, 0, sizeof(pi->line));
pi->line.old_lineno = -1;
pi->line.new_lineno = -1;
Expand Down
11 changes: 6 additions & 5 deletions src/libgit2/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ static int git_object__short_id(git_str *out, const git_object *obj)
git_oid id;
git_odb *odb;
size_t oid_hexsize;
int len = GIT_ABBREV_DEFAULT, error;
int len, error;

GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(obj);
Expand All @@ -536,12 +536,13 @@ static int git_object__short_id(git_str *out, const git_object *obj)
git_oid_clear(&id, repo->oid_type);
oid_hexsize = git_oid_hexsize(repo->oid_type);

if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0)
if ((error = git_repository__abbrev_length(&len, repo)) < 0)
return error;

if (len < 0 || (size_t)len > oid_hexsize) {
git_error_set(GIT_ERROR_CONFIG, "invalid oid abbreviation setting: '%d'", len);
return -1;
if ((size_t)len == oid_hexsize) {
if ((error = git_oid_cpy(&id, &obj->cached.oid)) < 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we early return here?

return error;
}
}

if ((error = git_repository_odb(&odb, repo)) < 0)
Expand Down
24 changes: 24 additions & 0 deletions src/libgit2/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -4009,3 +4009,27 @@ int git_repository_commit_parents(git_commitarray *out, git_repository *repo)
git_reference_free(head_ref);
return error;
}

int git_repository__abbrev_length(int *out, git_repository *repo)
{
size_t oid_hexsize;
int len;
int error;

oid_hexsize = git_oid_hexsize(repo->oid_type);

if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0)
return error;

if (len < GIT_ABBREV_MINIMUM) {
git_error_set(GIT_ERROR_CONFIG, "invalid oid abbreviation setting: '%d'", len);
return -1;
}

if (len == GIT_ABBREV_FALSE || (size_t)len > oid_hexsize)
len = (int)oid_hexsize;

*out = len;

return error;
}
5 changes: 5 additions & 0 deletions src/libgit2/repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ typedef enum {
/* core.trustctime */
GIT_TRUSTCTIME_DEFAULT = GIT_CONFIGMAP_TRUE,
/* core.abbrev */
GIT_ABBREV_FALSE = GIT_OID_MAX_HEXSIZE,
GIT_ABBREV_MINIMUM = 4,
GIT_ABBREV_DEFAULT = 7,
/* core.precomposeunicode */
GIT_PRECOMPOSE_DEFAULT = GIT_CONFIGMAP_FALSE,
Expand Down Expand Up @@ -211,6 +213,9 @@ int git_repository__wrap_odb(
int git_repository__configmap_lookup(int *out, git_repository *repo, git_configmap_item item);
void git_repository__configmap_lookup_cache_clear(git_repository *repo);

/** Return the length that object names will be abbreviated to. */
int git_repository__abbrev_length(int *out, git_repository *repo);

int git_repository__item_path(git_str *out, const git_repository *repo, git_repository_item_t item);

GIT_INLINE(int) git_repository__ensure_not_bare(
Expand Down
55 changes: 52 additions & 3 deletions tests/libgit2/object/shortid.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ git_repository *_repo;

void test_object_shortid__initialize(void)
{
cl_git_pass(git_repository_open(&_repo, cl_fixture("duplicate.git")));
_repo = cl_git_sandbox_init("duplicate.git");
}

void test_object_shortid__cleanup(void)
{
git_repository_free(_repo);
_repo = NULL;
cl_git_sandbox_cleanup();
}

void test_object_shortid__select(void)
Expand Down Expand Up @@ -49,3 +48,53 @@ void test_object_shortid__select(void)

git_buf_dispose(&shorty);
}

void test_object_shortid__core_abbrev(void)
{
git_oid full;
git_object *obj;
git_buf shorty = {0};
git_config *cfg;

cl_git_pass(git_repository_config(&cfg, _repo));
git_oid__fromstr(&full, "ce013625030ba8dba906f756967f9e9ca394464a", GIT_OID_SHA1);
cl_git_pass(git_object_lookup(&obj, _repo, &full, GIT_OBJECT_ANY));

cl_git_pass(git_config_set_string(cfg, "core.abbrev", "auto"));
cl_git_pass(git_object_short_id(&shorty, obj));
cl_assert_equal_i(7, shorty.size);
cl_assert_equal_s("ce01362", shorty.ptr);

cl_git_pass(git_config_set_string(cfg, "core.abbrev", "off"));
cl_git_pass(git_object_short_id(&shorty, obj));
cl_assert_equal_i(40, shorty.size);
cl_assert_equal_s("ce013625030ba8dba906f756967f9e9ca394464a", shorty.ptr);

cl_git_pass(git_config_set_string(cfg, "core.abbrev", "false"));
cl_git_pass(git_object_short_id(&shorty, obj));
cl_assert_equal_i(40, shorty.size);
cl_assert_equal_s("ce013625030ba8dba906f756967f9e9ca394464a", shorty.ptr);

cl_git_pass(git_config_set_string(cfg, "core.abbrev", "99"));
cl_git_pass(git_object_short_id(&shorty, obj));
cl_assert_equal_i(40, shorty.size);
cl_assert_equal_s("ce013625030ba8dba906f756967f9e9ca394464a", shorty.ptr);

cl_git_pass(git_config_set_string(cfg, "core.abbrev", "4"));
cl_git_pass(git_object_short_id(&shorty, obj));
cl_assert_equal_i(4, shorty.size);
cl_assert_equal_s("ce01", shorty.ptr);

cl_git_pass(git_config_set_string(cfg, "core.abbrev", "0"));
cl_git_fail(git_object_short_id(&shorty, obj));
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "3"));
cl_git_fail(git_object_short_id(&shorty, obj));
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "invalid"));
cl_git_fail(git_object_short_id(&shorty, obj));
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "true"));
cl_git_fail(git_object_short_id(&shorty, obj));

git_object_free(obj);
git_buf_dispose(&shorty);
git_config_free(cfg);
}
Loading