提交 b66fde9a 编写于 作者: J Junio C Hamano

find_unique_abbrev(): redefine semantics

The function returned NULL when no object that matches the name
was found, but that made the callers more complicated, as nobody
used that NULL return as an indication that no object with such
a name exists.  They (at least the careful ones) instead took
the full 40-hexdigit and used in such a case, and the careless
ones segfaulted.

With this "git rev-parse --short 5555555555555555555555555555555555555555"
would stop segfaulting.

This is based on Jeff King's rewrite to my RFC patch, but "missing"
logic swapped to "exists".  The final logic reads:

    For existing objects, make sure the abbreviated string uniquely
    identifies it.  Otherwise, make sure the abbreviated string is
    long enough so that it would not name any existing object.
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 48411d22
......@@ -192,26 +192,25 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
const char *find_unique_abbrev(const unsigned char *sha1, int len)
{
int status, is_null;
int status, exists;
static char hex[41];
is_null = is_null_sha1(sha1);
exists = has_sha1_file(sha1);
memcpy(hex, sha1_to_hex(sha1), 40);
if (len == 40 || !len)
return hex;
while (len < 40) {
unsigned char sha1_ret[20];
status = get_short_sha1(hex, len, sha1_ret, 1);
if (!status ||
(is_null && status != SHORT_NAME_AMBIGUOUS)) {
if (exists
? !status
: status == SHORT_NAME_NOT_FOUND) {
hex[len] = 0;
return hex;
}
if (status != SHORT_NAME_AMBIGUOUS)
return NULL;
len++;
}
return NULL;
return hex;
}
static int ambiguous_path(const char *path, int len)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册