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

Skip to content

Commit e10144a

Browse files
author
Edward Thomson
committed
odb: improved not found error messages
When looking up an abbreviated oid, show the actual (abbreviated) oid the caller passed instead of a full (but ambiguously truncated) oid.
1 parent 785d8c4 commit e10144a

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

src/odb.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ int git_odb_exists_prefix(
725725
git_oid_cpy(out, short_id);
726726
return 0;
727727
} else {
728-
return git_odb__error_notfound("no match for id prefix", short_id);
728+
return git_odb__error_notfound(
729+
"no match for id prefix", short_id, len);
729730
}
730731
}
731732

@@ -740,7 +741,7 @@ int git_odb_exists_prefix(
740741
error = odb_exists_prefix_1(out, db, &key, len, true);
741742

742743
if (error == GIT_ENOTFOUND)
743-
return git_odb__error_notfound("no match for id prefix", &key);
744+
return git_odb__error_notfound("no match for id prefix", &key, len);
744745

745746
return error;
746747
}
@@ -881,7 +882,7 @@ int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
881882
error = odb_read_1(out, db, id, true);
882883

883884
if (error == GIT_ENOTFOUND)
884-
return git_odb__error_notfound("no match for id", id);
885+
return git_odb__error_notfound("no match for id", id, GIT_OID_HEXSZ);
885886

886887
return error;
887888
}
@@ -967,7 +968,7 @@ int git_odb_read_prefix(
967968
error = read_prefix_1(out, db, &key, len, true);
968969

969970
if (error == GIT_ENOTFOUND)
970-
return git_odb__error_notfound("no match for prefix", &key);
971+
return git_odb__error_notfound("no match for prefix", &key, len);
971972

972973
return error;
973974
}
@@ -1223,12 +1224,14 @@ int git_odb_refresh(struct git_odb *db)
12231224
return 0;
12241225
}
12251226

1226-
int git_odb__error_notfound(const char *message, const git_oid *oid)
1227+
int git_odb__error_notfound(
1228+
const char *message, const git_oid *oid, size_t oid_len)
12271229
{
12281230
if (oid != NULL) {
12291231
char oid_str[GIT_OID_HEXSZ + 1];
1230-
git_oid_tostr(oid_str, sizeof(oid_str), oid);
1231-
giterr_set(GITERR_ODB, "Object not found - %s (%s)", message, oid_str);
1232+
git_oid_tostr(oid_str, oid_len, oid);
1233+
giterr_set(GITERR_ODB, "Object not found - %s (%.*s)",
1234+
message, oid_len, oid_str);
12321235
} else
12331236
giterr_set(GITERR_ODB, "Object not found - %s", message);
12341237

src/odb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ int git_odb__hashlink(git_oid *out, const char *path);
8282
/*
8383
* Generate a GIT_ENOTFOUND error for the ODB.
8484
*/
85-
int git_odb__error_notfound(const char *message, const git_oid *oid);
85+
int git_odb__error_notfound(
86+
const char *message, const git_oid *oid, size_t oid_len);
8687

8788
/*
8889
* Generate a GIT_EAMBIGUOUS error for the ODB.

src/odb_loose.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ static int locate_object_short_oid(
547547

548548
/* Check that directory exists */
549549
if (git_path_isdir(object_location->ptr) == false)
550-
return git_odb__error_notfound("no matching loose object for prefix", short_oid);
550+
return git_odb__error_notfound("no matching loose object for prefix",
551+
short_oid, len);
551552

552553
state.dir_len = git_buf_len(object_location);
553554
state.short_oid_len = len;
@@ -560,7 +561,8 @@ static int locate_object_short_oid(
560561
return error;
561562

562563
if (!state.found)
563-
return git_odb__error_notfound("no matching loose object for prefix", short_oid);
564+
return git_odb__error_notfound("no matching loose object for prefix",
565+
short_oid, len);
564566

565567
if (state.found > 1)
566568
return git_odb__error_ambiguous("multiple matches in loose objects");
@@ -613,9 +615,10 @@ static int loose_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_
613615
raw.len = 0;
614616
raw.type = GIT_OBJ_BAD;
615617

616-
if (locate_object(&object_path, (loose_backend *)backend, oid) < 0)
617-
error = git_odb__error_notfound("no matching loose object", oid);
618-
else if ((error = read_header_loose(&raw, &object_path)) == 0) {
618+
if (locate_object(&object_path, (loose_backend *)backend, oid) < 0) {
619+
error = git_odb__error_notfound("no matching loose object",
620+
oid, GIT_OID_HEXSZ);
621+
} else if ((error = read_header_loose(&raw, &object_path)) == 0) {
619622
*len_p = raw.len;
620623
*type_p = raw.type;
621624
}
@@ -633,9 +636,10 @@ static int loose_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p
633636

634637
assert(backend && oid);
635638

636-
if (locate_object(&object_path, (loose_backend *)backend, oid) < 0)
637-
error = git_odb__error_notfound("no matching loose object", oid);
638-
else if ((error = read_loose(&raw, &object_path)) == 0) {
639+
if (locate_object(&object_path, (loose_backend *)backend, oid) < 0) {
640+
error = git_odb__error_notfound("no matching loose object",
641+
oid, GIT_OID_HEXSZ);
642+
} else if ((error = read_loose(&raw, &object_path)) == 0) {
639643
*buffer_p = raw.data;
640644
*len_p = raw.len;
641645
*type_p = raw.type;

src/odb_pack.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backen
264264
if (!pack_entry_find_inner(e, backend, oid, last_found))
265265
return 0;
266266

267-
return git_odb__error_notfound("failed to find pack entry", oid);
267+
return git_odb__error_notfound(
268+
"failed to find pack entry", oid, GIT_OID_HEXSZ);
268269
}
269270

270271
static int pack_entry_find_prefix(
@@ -309,7 +310,8 @@ static int pack_entry_find_prefix(
309310
}
310311

311312
if (!found)
312-
return git_odb__error_notfound("no matching pack entry for prefix", short_oid);
313+
return git_odb__error_notfound("no matching pack entry for prefix",
314+
short_oid, len);
313315
else
314316
return 0;
315317
}
@@ -333,7 +335,7 @@ static int pack_backend__refresh(git_odb_backend *backend_)
333335
return 0;
334336

335337
if (p_stat(backend->pack_folder, &st) < 0 || !S_ISDIR(st.st_mode))
336-
return git_odb__error_notfound("failed to refresh packfiles", NULL);
338+
return git_odb__error_notfound("failed to refresh packfiles", NULL, 0);
337339

338340
git_buf_sets(&path, backend->pack_folder);
339341

src/pack.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ static int packfile_open(struct git_pack_file *p)
10181018
unsigned char *idx_sha1;
10191019

10201020
if (p->index_version == -1 && pack_index_open(p) < 0)
1021-
return git_odb__error_notfound("failed to open packfile", NULL);
1021+
return git_odb__error_notfound("failed to open packfile", NULL, 0);
10221022

10231023
/* if mwf opened by another thread, return now */
10241024
if (git_mutex_lock(&p->lock) < 0)
@@ -1099,7 +1099,7 @@ int git_packfile__name(char **out, const char *path)
10991099
path_len = strlen(path);
11001100

11011101
if (path_len < strlen(".idx"))
1102-
return git_odb__error_notfound("invalid packfile path", NULL);
1102+
return git_odb__error_notfound("invalid packfile path", NULL, 0);
11031103

11041104
if (git_buf_printf(&buf, "%.*s.pack", (int)(path_len - strlen(".idx")), path) < 0)
11051105
return -1;
@@ -1117,7 +1117,7 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
11171117
*pack_out = NULL;
11181118

11191119
if (path_len < strlen(".idx"))
1120-
return git_odb__error_notfound("invalid packfile path", NULL);
1120+
return git_odb__error_notfound("invalid packfile path", NULL, 0);
11211121

11221122
GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*p), path_len);
11231123
GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
@@ -1143,7 +1143,7 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
11431143

11441144
if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) {
11451145
git__free(p);
1146-
return git_odb__error_notfound("packfile not found", NULL);
1146+
return git_odb__error_notfound("packfile not found", NULL, 0);
11471147
}
11481148

11491149
/* ok, it looks sane as far as we can check without
@@ -1344,7 +1344,7 @@ static int pack_entry_find_offset(
13441344
}
13451345

13461346
if (!found)
1347-
return git_odb__error_notfound("failed to find offset for pack entry", short_oid);
1347+
return git_odb__error_notfound("failed to find offset for pack entry", short_oid, len);
13481348
if (found > 1)
13491349
return git_odb__error_ambiguous("found multiple offsets for pack entry");
13501350

0 commit comments

Comments
 (0)