Skip to content

Commit ba59a4a

Browse files
committed
Making get_delta_base() conform to the general error-handling pattern
This makes get_delta_base() return the error code as the return value and the delta base as an out-parameter.
1 parent f327372 commit ba59a4a

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

src/indexer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,11 @@ static int advance_delta_offset(git_indexer *idx, git_object_t type)
265265
if (type == GIT_OBJECT_REF_DELTA) {
266266
idx->off += GIT_OID_RAWSZ;
267267
} else {
268-
off64_t base_off = get_delta_base(idx->pack, &w, &idx->off, type, idx->entry_start);
268+
off64_t base_off;
269+
int error = get_delta_base(&base_off, idx->pack, &w, &idx->off, type, idx->entry_start);
269270
git_mwindow_close(&w);
270-
if (base_off < 0)
271-
return (int)base_off;
271+
if (error < 0)
272+
return error;
272273
}
273274

274275
return 0;

src/pack.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,11 @@ int git_packfile_resolve_header(
488488
size_t base_size;
489489
git_packfile_stream stream;
490490

491-
base_offset = get_delta_base(p, &w_curs, &curpos, type, offset);
491+
error = get_delta_base(&base_offset, p, &w_curs, &curpos, type, offset);
492492
git_mwindow_close(&w_curs);
493493

494-
if (base_offset < 0) {
495-
error = (int)base_offset;
494+
if (error < 0)
496495
return error;
497-
}
498496

499497
if ((error = git_packfile_stream_open(&stream, p, curpos)) < 0)
500498
return error;
@@ -515,13 +513,11 @@ int git_packfile_resolve_header(
515513
if (type != GIT_OBJECT_OFS_DELTA && type != GIT_OBJECT_REF_DELTA)
516514
break;
517515

518-
base_offset = get_delta_base(p, &w_curs, &curpos, type, base_offset);
516+
error = get_delta_base(&base_offset, p, &w_curs, &curpos, type, base_offset);
519517
git_mwindow_close(&w_curs);
520518

521-
if (base_offset < 0) {
522-
error = (int)base_offset;
519+
if (error < 0)
523520
return error;
524-
}
525521
}
526522
*type_p = type;
527523

@@ -595,13 +591,11 @@ static int pack_dependency_chain(git_dependency_chain *chain_out,
595591
if (type != GIT_OBJECT_OFS_DELTA && type != GIT_OBJECT_REF_DELTA)
596592
break;
597593

598-
base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
594+
error = get_delta_base(&base_offset, p, &w_curs, &curpos, type, obj_offset);
599595
git_mwindow_close(&w_curs);
600596

601-
if (base_offset < 0) { /* must actually be an error code */
602-
error = (int)base_offset;
597+
if (error < 0)
603598
goto on_error;
604-
}
605599

606600
/* we need to pass the pos *after* the delta-base bit */
607601
elem->offset = curpos;
@@ -886,18 +880,21 @@ static int packfile_unpack_compressed(
886880
* curpos is where the data starts, delta_obj_offset is the where the
887881
* header starts
888882
*/
889-
off64_t get_delta_base(
890-
struct git_pack_file *p,
891-
git_mwindow **w_curs,
892-
off64_t *curpos,
893-
git_object_t type,
894-
off64_t delta_obj_offset)
883+
int get_delta_base(
884+
off64_t *delta_base_out,
885+
struct git_pack_file *p,
886+
git_mwindow **w_curs,
887+
off64_t *curpos,
888+
git_object_t type,
889+
off64_t delta_obj_offset)
895890
{
896891
unsigned int left = 0;
897892
unsigned char *base_info;
898893
off64_t base_offset;
899894
git_oid unused;
900895

896+
assert(delta_base_out);
897+
901898
base_info = pack_window_open(p, w_curs, *curpos, &left);
902899
/* Assumption: the only reason this would fail is because the file is too small */
903900
if (base_info == NULL)
@@ -934,7 +931,8 @@ off64_t get_delta_base(
934931
git_oid_fromraw(&oid, base_info);
935932
if ((entry = git_oidmap_get(p->idx_cache, &oid)) != NULL) {
936933
*curpos += 20;
937-
return entry->offset;
934+
*delta_base_out = entry->offset;
935+
return 0;
938936
} else {
939937
/* If we're building an index, don't try to find the pack
940938
* entry; we just haven't seen it yet. We'll make
@@ -951,7 +949,8 @@ off64_t get_delta_base(
951949
} else
952950
return packfile_error("unknown object type");
953951

954-
return base_offset;
952+
*delta_base_out = base_offset;
953+
return 0;
955954
}
956955

957956
/***********************************************************

src/pack.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,12 @@ int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p,
143143
ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len);
144144
void git_packfile_stream_dispose(git_packfile_stream *obj);
145145

146-
off64_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs,
147-
off64_t *curpos, git_object_t type,
146+
int get_delta_base(
147+
off64_t *delta_base_out,
148+
struct git_pack_file *p,
149+
git_mwindow **w_curs,
150+
off64_t *curpos,
151+
git_object_t type,
148152
off64_t delta_obj_offset);
149153

150154
void git_packfile_close(struct git_pack_file *p, bool unlink_packfile);

0 commit comments

Comments
 (0)