commit-graph: extract write_commit_graph_file()
The write_commit_graph() method is too complex, so we are extracting helper functions one by one. Extract write_commit_graph_file() that takes all of the information in the context struct and writes the data to a commit-graph file. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Derrick Stolee committed
Jun 12, 2019 at 06:29 UTC
238def57fe7ba219c9e50094512fff1068ac5413
1 file changed
+80
-75
commit-graph.c
+80
-75
@@ -1015,21 +1015,91 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1015
stop_progress(&ctx->progress);
1016
}
1017
1018
-int write_commit_graph(const char *obj_dir,
1019
- struct string_list *pack_indexes,
1020
- struct string_list *commit_hex,
1021
- unsigned int flags)
1018
+static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1019
{
1023
- struct write_commit_graph_context *ctx;
1020
+ uint32_t i;
1021
struct hashfile *f;
1025
- uint32_t i, count_distinct = 0;
1026
- char *graph_name = NULL;
1022
struct lock_file lk = LOCK_INIT;
1023
uint32_t chunk_ids[5];
1024
uint64_t chunk_offsets[5];
1030
- int num_chunks;
1025
const unsigned hashsz = the_hash_algo->rawsz;
1026
struct strbuf progress_title = STRBUF_INIT;
1027
+ int num_chunks = ctx->num_extra_edges ? 4 : 3;
1028
+
1029
+ ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
1030
+ if (safe_create_leading_directories(ctx->graph_name)) {
1031
+ UNLEAK(ctx->graph_name);
1032
+ error(_("unable to create leading directories of %s"),
1033
+ ctx->graph_name);
1034
+ return -1;
1035
+ }
1036
+
1037
+ hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1038
+ f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1039
+
1040
+ hashwrite_be32(f, GRAPH_SIGNATURE);
1041
+
1042
+ hashwrite_u8(f, GRAPH_VERSION);
1043
+ hashwrite_u8(f, oid_version());
1044
+ hashwrite_u8(f, num_chunks);
1045
+ hashwrite_u8(f, 0); /* unused padding byte */
1046
+
1047
+ chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1048
+ chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1049
+ chunk_ids[2] = GRAPH_CHUNKID_DATA;
1050
+ if (ctx->num_extra_edges)
1051
+ chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES;
1052
+ else
1053
+ chunk_ids[3] = 0;
1054
+ chunk_ids[4] = 0;
1055
+
1056
+ chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1057
+ chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1058
+ chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1059
+ chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1060
+ chunk_offsets[4] = chunk_offsets[3] + 4 * ctx->num_extra_edges;
1061
+
1062
+ for (i = 0; i <= num_chunks; i++) {
1063
+ uint32_t chunk_write[3];
1064
+
1065
+ chunk_write[0] = htonl(chunk_ids[i]);
1066
+ chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1067
+ chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1068
+ hashwrite(f, chunk_write, 12);
1069
+ }
1070
+
1071
+ if (ctx->report_progress) {
1072
+ strbuf_addf(&progress_title,
1073
+ Q_("Writing out commit graph in %d pass",
1074
+ "Writing out commit graph in %d passes",
1075
+ num_chunks),
1076
+ num_chunks);
1077
+ ctx->progress = start_delayed_progress(
1078
+ progress_title.buf,
1079
+ num_chunks * ctx->commits.nr);
1080
+ }
1081
+ write_graph_chunk_fanout(f, ctx);
1082
+ write_graph_chunk_oids(f, hashsz, ctx);
1083
+ write_graph_chunk_data(f, hashsz, ctx);
1084
+ if (ctx->num_extra_edges)
1085
+ write_graph_chunk_extra_edges(f, ctx);
1086
+ stop_progress(&ctx->progress);
1087
+ strbuf_release(&progress_title);
1088
+
1089
+ close_commit_graph(ctx->r);
1090
+ finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1091
+ commit_lock_file(&lk);
1092
+
1093
+ return 0;
1094
+}
1095
+
1096
+int write_commit_graph(const char *obj_dir,
1097
+ struct string_list *pack_indexes,
1098
+ struct string_list *commit_hex,
1099
+ unsigned int flags)
1100
+{
1101
+ struct write_commit_graph_context *ctx;
1102
+ uint32_t i, count_distinct = 0;
1103
int res = 0;
1104
1105
if (!commit_graph_compatible(the_repository))
@@ -1096,75 +1166,10 @@ int write_commit_graph(const char *obj_dir,
1166
1167
compute_generation_numbers(ctx);
1168
1099
- num_chunks = ctx->num_extra_edges ? 4 : 3;
1100
-
1101
- ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
1102
- if (safe_create_leading_directories(ctx->graph_name)) {
1103
- UNLEAK(ctx->graph_name);
1104
- error(_("unable to create leading directories of %s"),
1105
- ctx->graph_name);
1106
- res = -1;
1107
- goto cleanup;
1108
- }
1109
-
1110
- hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1111
- f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1112
-
1113
- hashwrite_be32(f, GRAPH_SIGNATURE);
1114
-
1115
- hashwrite_u8(f, GRAPH_VERSION);
1116
- hashwrite_u8(f, oid_version());
1117
- hashwrite_u8(f, num_chunks);
1118
- hashwrite_u8(f, 0); /* unused padding byte */
1119
-
1120
- chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1121
- chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1122
- chunk_ids[2] = GRAPH_CHUNKID_DATA;
1123
- if (ctx->num_extra_edges)
1124
- chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES;
1125
- else
1126
- chunk_ids[3] = 0;
1127
- chunk_ids[4] = 0;
1128
-
1129
- chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1130
- chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1131
- chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1132
- chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1133
- chunk_offsets[4] = chunk_offsets[3] + 4 * ctx->num_extra_edges;
1134
-
1135
- for (i = 0; i <= num_chunks; i++) {
1136
- uint32_t chunk_write[3];
1137
-
1138
- chunk_write[0] = htonl(chunk_ids[i]);
1139
- chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1140
- chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1141
- hashwrite(f, chunk_write, 12);
1142
- }
1143
-
1144
- if (ctx->report_progress) {
1145
- strbuf_addf(&progress_title,
1146
- Q_("Writing out commit graph in %d pass",
1147
- "Writing out commit graph in %d passes",
1148
- num_chunks),
1149
- num_chunks);
1150
- ctx->progress = start_delayed_progress(
1151
- progress_title.buf,
1152
- num_chunks * ctx->commits.nr);
1153
- }
1154
- write_graph_chunk_fanout(f, ctx);
1155
- write_graph_chunk_oids(f, hashsz, ctx);
1156
- write_graph_chunk_data(f, hashsz, ctx);
1157
- if (ctx->num_extra_edges)
1158
- write_graph_chunk_extra_edges(f, ctx);
1159
- stop_progress(&ctx->progress);
1160
- strbuf_release(&progress_title);
1161
-
1162
- close_commit_graph(ctx->r);
1163
- finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1164
- commit_lock_file(&lk);
1169
+ res = write_commit_graph_file(ctx);
1170
1171
cleanup:
1167
- free(graph_name);
1172
+ free(ctx->graph_name);
1173
free(ctx->commits.list);
1174
free(ctx->oids.list);
1175
free(ctx);