MCPcopy Index your code
hub / github.com/git/git / write_idx_file

Function write_idx_file

pack-write.c:57–180  ·  view source on GitHub ↗

* The *sha1 contains the pack content SHA1 hash. * The objects array passed in will be sorted by SHA1 on exit. */

Source from the content-addressed store, hash-verified

55 * The objects array passed in will be sorted by SHA1 on exit.
56 */
57const char *write_idx_file(struct repository *repo,
58 const char *index_name, struct pack_idx_entry **objects,
59 int nr_objects, const struct pack_idx_option *opts,
60 const unsigned char *sha1)
61{
62 struct hashfile *f;
63 struct pack_idx_entry **sorted_by_sha, **list, **last;
64 off_t last_obj_offset = 0;
65 int i, fd;
66 uint32_t index_version;
67
68 if (nr_objects) {
69 sorted_by_sha = objects;
70 list = sorted_by_sha;
71 last = sorted_by_sha + nr_objects;
72 for (i = 0; i < nr_objects; ++i) {
73 if (objects[i]->offset > last_obj_offset)
74 last_obj_offset = objects[i]->offset;
75 }
76 QSORT(sorted_by_sha, nr_objects, sha1_compare);
77 }
78 else
79 sorted_by_sha = list = last = NULL;
80
81 if (opts->flags & WRITE_IDX_VERIFY) {
82 assert(index_name);
83 f = hashfd_check(repo->hash_algo, index_name);
84 } else {
85 if (!index_name) {
86 struct strbuf tmp_file = STRBUF_INIT;
87 fd = odb_mkstemp(repo->objects, &tmp_file,
88 "pack/tmp_idx_XXXXXX");
89 index_name = strbuf_detach(&tmp_file, NULL);
90 } else {
91 unlink(index_name);
92 fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
93 }
94 f = hashfd(repo->hash_algo, fd, index_name);
95 }
96
97 /* if last object's offset is >= 2^31 we should use index V2 */
98 index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version;
99
100 /* index versions 2 and above need a header */
101 if (index_version >= 2) {
102 struct pack_idx_header hdr;
103 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
104 hdr.idx_version = htonl(index_version);
105 hashwrite(f, &hdr, sizeof(hdr));
106 }
107
108 /*
109 * Write the first-level table (the list is sorted,
110 * but we use a 256-entry lookup to be able to avoid
111 * having to do eight extra binary search iterations).
112 */
113 for (i = 0; i < 256; i++) {
114 struct pack_idx_entry **next = list;

Callers 3

stage_tmp_packfilesFunction · 0.85
cmd_index_packFunction · 0.85
create_indexFunction · 0.85

Calls 13

hashfd_checkFunction · 0.85
odb_mkstempFunction · 0.85
strbuf_detachFunction · 0.85
xopenFunction · 0.85
hashfdFunction · 0.85
need_large_offsetFunction · 0.85
hashwriteFunction · 0.85
hashwrite_be32Function · 0.85
oideqFunction · 0.85
oid_to_hexFunction · 0.85
hashwrite_be64Function · 0.85
finalize_hashfileFunction · 0.85

Tested by

no test coverage detected