Skip to content

Commit

Permalink
nth_packed_object_oid(): use customary integer return
Browse files Browse the repository at this point in the history
Our nth_packed_object_sha1() function returns NULL for error. So when we
wrapped it with nth_packed_object_oid(), we kept the same semantics. But
it's a bit funny, because the caller actually passes in an out
parameter, and the pointer we return is just that same struct they
passed to us (or NULL).

It's not too terrible, but it does make the interface a little
non-idiomatic. Let's switch to our usual "0 for success, negative for
error" return value. Most callers either don't check it, or are
trivially converted. The one that requires the biggest change is
actually improved, as we can ditch an extra aliased pointer variable.

Since we are changing the interface in a subtle way that the compiler
wouldn't catch, let's also change the name to catch any topics in
flight. We can drop the 'o' and make it nth_packed_object_id(). That's
slightly shorter, but also less redundant since the 'o' stands for
"object" already.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
peff authored and gitster committed Feb 24, 2020
1 parent 51ebf55 commit 0763671
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
4 changes: 2 additions & 2 deletions builtin/pack-objects.c
Expand Up @@ -3053,7 +3053,7 @@ static void add_objects_in_unpacked_packs(void)
in_pack.alloc);

for (i = 0; i < p->num_objects; i++) {
nth_packed_object_oid(&oid, p, i);
nth_packed_object_id(&oid, p, i);
o = lookup_unknown_object(&oid);
if (!(o->flags & OBJECT_ADDED))
mark_in_pack_object(o, p, &in_pack);
Expand Down Expand Up @@ -3157,7 +3157,7 @@ static void loosen_unused_packed_objects(void)
die(_("cannot open pack index"));

for (i = 0; i < p->num_objects; i++) {
nth_packed_object_oid(&oid, p, i);
nth_packed_object_id(&oid, p, i);
if (!packlist_find(&to_pack, &oid) &&
!has_sha1_pack_kept_or_nonlocal(&oid) &&
!loosened_object_can_be_discarded(&oid, p->mtime))
Expand Down
2 changes: 1 addition & 1 deletion midx.c
Expand Up @@ -534,7 +534,7 @@ static void fill_pack_entry(uint32_t pack_int_id,
uint32_t cur_object,
struct pack_midx_entry *entry)
{
if (!nth_packed_object_oid(&entry->oid, p, cur_object))
if (nth_packed_object_id(&entry->oid, p, cur_object) < 0)
die(_("failed to locate object %d in packfile"), cur_object);

entry->pack_int_id = pack_int_id;
Expand Down
4 changes: 2 additions & 2 deletions pack-bitmap.c
Expand Up @@ -658,7 +658,7 @@ static void show_objects_for_type(
offset += ewah_bit_ctz64(word >> offset);

entry = &bitmap_git->pack->revindex[pos + offset];
nth_packed_object_oid(&oid, bitmap_git->pack, entry->nr);
nth_packed_object_id(&oid, bitmap_git->pack, entry->nr);

if (bitmap_git->hashes)
hash = get_be32(bitmap_git->hashes + entry->nr);
Expand Down Expand Up @@ -1136,7 +1136,7 @@ int rebuild_existing_bitmaps(struct bitmap_index *bitmap_git,
struct object_entry *oe;

entry = &bitmap_git->pack->revindex[i];
nth_packed_object_oid(&oid, bitmap_git->pack, entry->nr);
nth_packed_object_id(&oid, bitmap_git->pack, entry->nr);
oe = packlist_find(mapping, &oid);

if (oe)
Expand Down
18 changes: 9 additions & 9 deletions packfile.c
Expand Up @@ -1261,7 +1261,7 @@ static int retry_bad_packed_offset(struct repository *r,
revidx = find_pack_revindex(p, obj_offset);
if (!revidx)
return OBJ_BAD;
nth_packed_object_oid(&oid, p, revidx->nr);
nth_packed_object_id(&oid, p, revidx->nr);
mark_bad_packed_object(p, oid.hash);
type = oid_object_info(r, &oid, NULL);
if (type <= OBJ_NONE)
Expand Down Expand Up @@ -1693,7 +1693,7 @@ void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset,
off_t len = revidx[1].offset - obj_offset;
if (check_pack_crc(p, &w_curs, obj_offset, len, revidx->nr)) {
struct object_id oid;
nth_packed_object_oid(&oid, p, revidx->nr);
nth_packed_object_id(&oid, p, revidx->nr);
error("bad packed object CRC for %s",
oid_to_hex(&oid));
mark_bad_packed_object(p, oid.hash);
Expand Down Expand Up @@ -1782,7 +1782,7 @@ void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset,
struct object_id base_oid;
revidx = find_pack_revindex(p, obj_offset);
if (revidx) {
nth_packed_object_oid(&base_oid, p, revidx->nr);
nth_packed_object_id(&base_oid, p, revidx->nr);
error("failed to read delta base object %s"
" at offset %"PRIuMAX" from %s",
oid_to_hex(&base_oid), (uintmax_t)obj_offset,
Expand Down Expand Up @@ -1890,15 +1890,15 @@ const unsigned char *nth_packed_object_sha1(struct packed_git *p,
}
}

const struct object_id *nth_packed_object_oid(struct object_id *oid,
struct packed_git *p,
uint32_t n)
int nth_packed_object_id(struct object_id *oid,
struct packed_git *p,
uint32_t n)
{
const unsigned char *hash = nth_packed_object_sha1(p, n);
if (!hash)
return NULL;
return -1;
hashcpy(oid->hash, hash);
return oid;
return 0;
}

void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
Expand Down Expand Up @@ -2077,7 +2077,7 @@ int for_each_object_in_pack(struct packed_git *p,
else
pos = i;

if (!nth_packed_object_oid(&oid, p, pos))
if (nth_packed_object_id(&oid, p, pos) < 0)
return error("unable to get sha1 of object %u in %s",
pos, p->pack_name);

Expand Down
5 changes: 2 additions & 3 deletions packfile.h
Expand Up @@ -129,10 +129,9 @@ int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32
const unsigned char *nth_packed_object_sha1(struct packed_git *, uint32_t n);
/*
* Like nth_packed_object_sha1, but write the data into the object specified by
* the the first argument. Returns the first argument on success, and NULL on
* error.
* the the first argument. Returns 0 on success, negative otherwise.
*/
const struct object_id *nth_packed_object_oid(struct object_id *, struct packed_git *, uint32_t n);
int nth_packed_object_id(struct object_id *, struct packed_git *, uint32_t n);

/*
* Return the offset of the nth object within the specified packfile.
Expand Down
13 changes: 6 additions & 7 deletions sha1-name.c
Expand Up @@ -155,7 +155,6 @@ static void unique_in_pack(struct packed_git *p,
struct disambiguate_state *ds)
{
uint32_t num, i, first = 0;
const struct object_id *current = NULL;

if (p->multi_pack_index)
return;
Expand All @@ -173,10 +172,10 @@ static void unique_in_pack(struct packed_git *p,
*/
for (i = first; i < num && !ds->ambiguous; i++) {
struct object_id oid;
current = nth_packed_object_oid(&oid, p, i);
if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash))
nth_packed_object_id(&oid, p, i);
if (!match_sha(ds->len, ds->bin_pfx.hash, oid.hash))
break;
update_candidates(ds, current);
update_candidates(ds, &oid);
}
}

Expand Down Expand Up @@ -643,14 +642,14 @@ static void find_abbrev_len_for_pack(struct packed_git *p,
*/
mad->init_len = 0;
if (!match) {
if (nth_packed_object_oid(&oid, p, first))
if (!nth_packed_object_id(&oid, p, first))
extend_abbrev_len(&oid, mad);
} else if (first < num - 1) {
if (nth_packed_object_oid(&oid, p, first + 1))
if (!nth_packed_object_id(&oid, p, first + 1))
extend_abbrev_len(&oid, mad);
}
if (first > 0) {
if (nth_packed_object_oid(&oid, p, first - 1))
if (!nth_packed_object_id(&oid, p, first - 1))
extend_abbrev_len(&oid, mad);
}
mad->init_len = mad->cur_len;
Expand Down

0 comments on commit 0763671

Please sign in to comment.