8000 Merge pull request #6944 from mathworks/pr_6724 · libgit2/libgit2@0bb7d45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bb7d45

Browse files
authored
Merge pull request #6944 from mathworks/pr_6724
object: git_object_short_id fails with core.abbrev string values
2 parents 8ad1eb9 + d363cc8 commit 0bb7d45

File tree

6 files changed

+93
-18
lines changed

6 files changed

+93
-18
lines changed

src/libgit2/config_cache.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ static git_configmap _configmap_logallrefupdates[] = {
6464
{GIT_CONFIGMAP_STRING, "always", GIT_LOGALLREFUPDATES_ALWAYS},
6565
};
6666

67-
/*
68-
* Generic map for integer values
69-
*/
70-
static git_configmap _configmap_int[] = {
67+
static git_configmap _configmap_abbrev[] = {
7168
{GIT_CONFIGMAP_INT32, NULL, 0},
69+
{GIT_CONFIGMAP_FALSE, NULL, GIT_ABBREV_FALSE},
70+
{GIT_CONFIGMAP_STRING, "auto", GIT_ABBREV_DEFAULT}
7271
};
7372

7473
static struct map_data _configmaps[] = {
@@ -79,7 +78,7 @@ static struct map_data _configmaps[] = {
7978
{"core.filemode", NULL, 0, GIT_FILEMODE_DEFAULT },
8079
{"core.ignorestat", NULL, 0, GIT_IGNORESTAT_DEFAULT },
8180
{"core.trustctime", NULL, 0, GIT_TRUSTCTIME_DEFAULT },
82-
{"core.abbrev", _configmap_int, 1, GIT_ABBREV_DEFAULT },
81+
{"core.abbrev", _configmap_abbrev, ARRAY_SIZE(_configmap_abbrev), GIT_ABBREV_DEFAULT },
8382
{"core.precomposeunicode", NULL, 0, GIT_PRECOMPOSE_DEFAULT },
8483
{"core.safecrlf", _configmap_safecrlf, ARRAY_SIZE(_configmap_safecrlf), GIT_SAFE_CRLF_DEFAULT},
8584
{"core.logallrefupdates", _configmap_logallrefupdates, ARRAY_SIZE(_configmap_logallrefupdates), GIT_LOGALLREFUPDATES_DEFAULT},

src/libgit2/diff_print.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "zstream.h"
1616
#include "blob.h"
1717
#include "delta.h"
18+
#include "repository.h"
1819
#include "git2/sys/diff.h"
1920

2021
typedef struct {
@@ -53,14 +54,10 @@ static int diff_print_info_init__common(
5354
if (!pi->id_strlen) {
5455
if (!repo)
5556
pi->id_strlen = GIT_ABBREV_DEFAULT;
56-
else if (git_repository__configmap_lookup(&pi->id_strlen, repo, GIT_CONFIGMAP_ABBREV) < 0)
57+
else if (git_repository__abbrev_length(&pi->id_strlen, repo) < 0)
5758
return -1;
5859
}
5960

60-
if (pi->id_strlen > 0 &&
61-
(size_t)pi->id_strlen > git_oid_hexsize(pi->oid_type))
62-
pi->id_strlen = (int)git_oid_hexsize(pi->oid_type);
63-
6461
memset(&pi->line, 0, sizeof(pi->line));
6562
pi->line.old_lineno = -1;
6663
pi->line.new_lineno = -1;

src/libgit2/object.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ static int git_object__short_id(git_str *out, const git_object *obj)
526526
git_oid id;
527527
git_odb *odb;
528528
size_t oid_hexsize;
529-
int len = GIT_ABBREV_DEFAULT, error;
529+
int len, error;
530530

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

539-
if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0)
539+
if ((error = git_repository__abbrev_length(&len, repo)) < 0)
540540
return error;
541541

542-
if (len < 0 || (size_t)len > oid_hexsize) {
543-
git_error_set(GIT_ERROR_CONFIG, "invalid oid abbreviation setting: '%d'", len);
544-
return -1;
542+
if ((size_t)len == oid_hexsize) {
543+
if ((error = git_oid_cpy(&id, &obj->cached.oid)) < 0) {
544+
return error;
545+
}
545546
}
546547

547548
if ((error = git_repository_odb(&odb, repo)) < 0)

src/libgit2/repository.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4009,3 +4009,27 @@ int git_repository_commit_parents(git_commitarray *out, git_repository *repo)
40094009
git_reference_free(head_ref);
40104010
return error;
40114011
}
4012+
4013+
int git_repository__abbrev_length(int *out, git_repository *repo)
4014+
{
4015+
size_t oid_hexsize;
4016+
int len;
4017+
int error;
4018+
4019+
oid_hexsize = git_oid_hexsize(repo->oid_type);
4020+
4021+
if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0)
4022+
return error;
4023+
4024+
if (len < GIT_ABBREV_MINIMUM) {
4025+
git_error_set(GIT_ERROR_CONFIG, "invalid oid abbreviation setting: '%d'", len);
4026+
return -1;
4027+
}
4028+
4029+
if (len == GIT_ABBREV_FALSE || (size_t)len > oid_hexsize)
4030+
len = (int)oid_hexsize;
4031+
4032+
*out = len;
4033+
4034+
return error;
4035+
}

src/libgit2/repository.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ typedef enum {
102102
/* core.trustctime */
103103
GIT_TRUSTCTIME_DEFAULT = GIT_CONFIGMAP_TRUE,
104104
/* core.abbrev */
105+
GIT_ABBREV_FALSE = GIT_OID_MAX_HEXSIZE,
106+
GIT_ABBREV_MINIMUM = 4,
105107
GIT_ABBREV_DEFAULT = 7,
106108
/* core.precomposeunicode */
107109
GIT_PRECOMPOSE_DEFAULT = GIT_CONFIGMAP_FALSE,
@@ -211,6 +213,9 @@ int git_repository__wrap_odb(
211213
int git_repository__configmap_lookup(int *out, git_repository *repo, git_configmap_item item);
212214
void git_repository__configmap_lookup_cache_clear(git_repository *repo);
213215

216+
/** Return the length that object names will be abbreviated to. */
217+
int git_repository__abbrev_length(int *out, git_repository *repo);
218+
214219
int git_repository__item_path(git_str *out, const git_repository *repo, git_repository_item_t item);
215220

216221
GIT_INLINE(int) git_repository__ensure_not_bare(

tests/libgit2/object/shortid.c

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ git_repository *_repo;
44

55
void test_object_shortid__initialize(void)
66
{
7-
cl_git_pass(git_repository_open(&_repo, cl_fixture("duplicate.git")));
7+
_repo = cl_git_sandbox_init("duplicate.git");
88
}
99

1010
void test_object_shortid__cleanup(void)
1111
{
12-
git_repository_free(_repo);
13-
_repo = NULL;
12+
cl_git_sandbox_cleanup();
1413
}
1514

1615
void test_object_shortid__select(void)
@@ -49,3 +48,53 @@ void test_object_shortid__select(void)
4948

5049
git_buf_dispose(&shorty);
5150
}
51+
52+
void test_object_shortid__core_abbrev(void)
53+
{
54+
git_oid full;
55+
git_object *obj;
56+
git_buf shorty = {0};
57+
git_config *cfg;
58+
59+
cl_git_pass(git_repository_config(&cfg, _repo));
60+
git_oid__fromstr(&full, "ce013625030ba8dba906f756967f9e9ca394464a", GIT_OID_SHA1);
61+
cl_git_pass(git_object_lookup(&obj, _repo, &full, GIT_OBJECT_ANY));
62+
63+
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "auto"));
64+
cl_git_pass(git_object_short_id(&shorty, obj));
65+
cl_assert_equal_i(7, shorty.size);
66+
cl_assert_equal_s("ce01362", shorty.ptr);
67+
68+
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "off"));
69+
cl_git_pass(git_object_short_id(&shorty, obj));
70+
cl_assert_equal_i(40, shorty.size);
71+
cl_assert_equal_s("ce013625030ba8dba906f756967f9e9ca394464a", shorty.ptr);
72+
73+
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "false"));
74+
cl_git_pass(git_object_short_id(&shorty, obj));
75+
cl_assert_equal_i(40, shorty.size);
76+
cl_assert_equal_s("ce013625030ba8dba906f756967f9e9ca394464a", shorty.ptr);
77+
78+
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "99"));
79+
cl_git_pass(git_object_short_id(&shorty, obj));
80+
cl_assert_equal_i(40, shorty.size);
81+
cl_assert_equal_s("ce013625030ba8dba906f756967f9e9ca394464a", shorty.ptr);
82+
83+
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "4"));
84+
cl_git_pass(git_object_short_id(&shorty, obj));
85+
cl_assert_equal_i(4, shorty.size);
86+
cl_assert_equal_s("ce01", shorty.ptr);
87+
88+
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "0"));
89+
cl_git_fail(git_object_short_id(&shorty, obj));
90+
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "3"));
91+
cl_git_fail(git_object_short_id(&shorty, obj));
92+
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "invalid"));
93+
cl_git_fail(git_object_short_id(&shorty, obj));
94+
cl_git_pass(git_config_set_string(cfg, "core.abbrev", "true"));
95+
cl_git_fail(git_object_short_id(&shorty, obj));
96+
97+
git_object_free(obj);
98+
git_buf_dispose(&shorty);
99+
git_config_free(cfg);
100+
}

0 commit comments

Comments
 (0)
0