indexer: extract function reading stream objects The loop inside of `git_indexer_append` iterates over every object that is to be stored as part of the index. While the logic to retrieve every object from the packfile stream is rather involved, it currently just part of the loop, making it unnecessarily hard to follow. Move the logic into its own function `read_stream_object`, which unpacks a single object from the stream. Note that there is some subtletly here involving the special error `GIT_EBUFS`, which indicates to the indexer that no more data is currently available. So instead of returning an error and aborting the whole loop in that case, we do have to catch that value and return successfully to wait for more data to be read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
diff --git a/src/indexer.c b/src/indexer.c
index 91b9b52..c8fd9c2 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -525,6 +525,93 @@ static int append_to_pack(git_indexer *idx, const void *data, size_t size)
return write_at(idx, data, idx->pack->mwf.size, size);
}
+static int read_stream_object(git_indexer *idx, git_transfer_progress *stats)
+{
+ git_packfile_stream *stream = &idx->stream;
+ git_off_t entry_start = idx->off;
+ size_t entry_size;
+ git_otype type;
+ git_mwindow *w = NULL;
+ int error;
+
+ if (idx->pack->mwf.size <= idx->off + 20)
+ return GIT_EBUFS;
+
+ if (!idx->have_stream) {
+ error = git_packfile_unpack_header(&entry_size, &type, &idx->pack->mwf, &w, &idx->off);
+ if (error == GIT_EBUFS) {
+ idx->off = entry_start;
+ return error;
+ }
+ if (error < 0)
+ return error;
+
+ git_mwindow_close(&w);
+ idx->entry_start = entry_start;
+ git_hash_init(&idx->hash_ctx);
+
+ if (type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA) {
+ error = advance_delta_offset(idx, type);
+ if (error == GIT_EBUFS) {
+ idx->off = entry_start;
+ return error;
+ }
+ if (error < 0)
+ return error;
+
+ idx->have_delta = 1;
+ } else {
+ idx->have_delta = 0;
+
+ error = hash_header(&idx->hash_ctx, entry_size, type);
+ if (error < 0)
+ return error;
+ }
+
+ idx->have_stream = 1;
+
+ error = git_packfile_stream_open(stream, idx->pack, idx->off);
+ if (error < 0)
+ return error;
+ }
+
+ if (idx->have_delta) {
+ error = read_object_stream(idx, stream);
+ } else {
+ error = hash_object_stream(idx, stream);
+ }
+
+ idx->off = stream->curpos;
+ if (error == GIT_EBUFS)
+ return error;
+
+ /* We want to free the stream reasorces no matter what here */
+ idx->have_stream = 0;
+ git_packfile_stream_dispose(stream);
+
+ if (error < 0)
+ return error;
+
+ if (idx->have_delta) {
+ error = store_delta(idx);
+ } else {
+ error = store_object(idx);
+ }
+
+ if (error < 0)
+ return error;
+
+ if (!idx->have_delta) {
+ stats->indexed_objects++;
+ }
+ stats->received_objects++;
+
+ if ((error = do_progress_callback(idx, stats)) != 0)
+ return error;
+
+ return 0;
+}
+
int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_transfer_progress *stats)
{
int error = -1;
@@ -588,86 +675,12 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran
git_mwindow_free_all(mwf);
while (stats->indexed_objects < idx->nr_objects) {
- git_packfile_stream *stream = &idx->stream;
- git_off_t entry_start = idx->off;
- size_t entry_size;
- git_otype type;
- git_mwindow *w = NULL;
-
- if (idx->pack->mwf.size <= idx->off + 20)
- return 0;
-
- if (!idx->have_stream) {
- error = git_packfile_unpack_header(&entry_size, &type, mwf, &w, &idx->off);
- if (error == GIT_EBUFS) {
- idx->off = entry_start;
- return 0;
- }
- if (error < 0)
+ if ((error = read_stream_object(idx, stats)) != 0) {
+ if (error == GIT_EBUFS)
+ break;
+ else
goto on_error;
-
- git_mwindow_close(&w);
- idx->entry_start = entry_start;
- git_hash_init(&idx->hash_ctx);
-
- if (type == GIT_OBJ_REF_DELTA || type == GIT_OBJ_OFS_DELTA) {
- error = advance_delta_offset(idx, type);
- if (error == GIT_EBUFS) {
- idx->off = entry_start;
- return 0;
- }
- if (error < 0)
- goto on_error;
-
- idx->have_delta = 1;
- } else {
- idx->have_delta = 0;
-
- error = hash_header(&idx->hash_ctx, entry_size, type);
- if (error < 0)
- goto on_error;
- }
-
- idx->have_stream = 1;
-
- error = git_packfile_stream_open(stream, idx->pack, idx->off);
- if (error < 0)
- goto on_error;
- }
-
- if (idx->have_delta) {
- error = read_object_stream(idx, stream);
- } else {
- error = hash_object_stream(idx, stream);
}
-
- idx->off = stream->curpos;
- if (error == GIT_EBUFS)
- return 0;
-
- /* We want to free the stream reasorces no matter what here */
- idx->have_stream = 0;
- git_packfile_stream_dispose(stream);
-
- if (error < 0)
- goto on_error;
-
- if (idx->have_delta) {
- error = store_delta(idx);
- } else {
- error = store_object(idx);
- }
-
- if (error < 0)
- goto on_error;
-
- if (!idx->have_delta) {
- stats->indexed_objects++;
- }
- stats->received_objects++;
-
- if ((error = do_progress_callback(idx, stats)) != 0)
- goto on_error;
}
return 0;