Skip to content

Commit ebcfb37

Browse files
committed
write_idx_file: introduce a struct to hold idx customization options
Remove two globals, pack_idx_default version and pack_idx_off32_limit, and place them in a pack_idx_option structure. Allow callers to pass it to write_idx_file() as a parameter. Adjust all callers to the API change. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7218a21 commit ebcfb37

File tree

5 files changed

+48
-33
lines changed

5 files changed

+48
-33
lines changed

builtin/index-pack.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,12 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
880880

881881
static int git_index_pack_config(const char *k, const char *v, void *cb)
882882
{
883+
struct pack_idx_option *opts = cb;
884+
883885
if (!strcmp(k, "pack.indexversion")) {
884-
pack_idx_default_version = git_config_int(k, v);
885-
if (pack_idx_default_version > 2)
886-
die("bad pack.indexversion=%"PRIu32,
887-
pack_idx_default_version);
886+
opts->version = git_config_int(k, v);
887+
if (opts->version > 2)
888+
die("bad pack.indexversion=%"PRIu32, opts->version);
888889
return 0;
889890
}
890891
return git_default_config(k, v, cb);
@@ -898,14 +899,16 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
898899
const char *keep_name = NULL, *keep_msg = NULL;
899900
char *index_name_buf = NULL, *keep_name_buf = NULL;
900901
struct pack_idx_entry **idx_objects;
902+
struct pack_idx_option opts;
901903
unsigned char pack_sha1[20];
902904

903905
if (argc == 2 && !strcmp(argv[1], "-h"))
904906
usage(index_pack_usage);
905907

906908
read_replace_refs = 0;
907909

908-
git_config(git_index_pack_config, NULL);
910+
reset_pack_idx_option(&opts);
911+
git_config(git_index_pack_config, &opts);
909912
if (prefix && chdir(prefix))
910913
die("Cannot come back to cwd");
911914

@@ -944,12 +947,12 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
944947
index_name = argv[++i];
945948
} else if (!prefixcmp(arg, "--index-version=")) {
946949
char *c;
947-
pack_idx_default_version = strtoul(arg + 16, &c, 10);
948-
if (pack_idx_default_version > 2)
950+
opts.version = strtoul(arg + 16, &c, 10);
951+
if (opts.version > 2)
949952
die("bad %s", arg);
950953
if (*c == ',')
951-
pack_idx_off32_limit = strtoul(c+1, &c, 0);
952-
if (*c || pack_idx_off32_limit & 0x80000000)
954+
opts.off32_limit = strtoul(c+1, &c, 0);
955+
if (*c || opts.off32_limit & 0x80000000)
953956
die("bad %s", arg);
954957
} else
955958
usage(index_pack_usage);
@@ -1032,7 +1035,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
10321035
idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
10331036
for (i = 0; i < nr_objects; i++)
10341037
idx_objects[i] = &objects[i].idx;
1035-
curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1);
1038+
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);
10361039
free(idx_objects);
10371040

10381041
final(pack_name, curr_pack,

builtin/pack-objects.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static int local;
7070
static int incremental;
7171
static int ignore_packed_keep;
7272
static int allow_ofs_delta;
73+
static struct pack_idx_option pack_idx_opts;
7374
static const char *base_name;
7475
static int progress = 1;
7576
static int window = 10;
@@ -493,8 +494,8 @@ static void write_pack_file(void)
493494
const char *idx_tmp_name;
494495
char tmpname[PATH_MAX];
495496

496-
idx_tmp_name = write_idx_file(NULL, written_list,
497-
nr_written, sha1);
497+
idx_tmp_name = write_idx_file(NULL, written_list, nr_written,
498+
&pack_idx_opts, sha1);
498499

499500
snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
500501
base_name, sha1_to_hex(sha1));
@@ -1880,10 +1881,10 @@ static int git_pack_config(const char *k, const char *v, void *cb)
18801881
return 0;
18811882
}
18821883
if (!strcmp(k, "pack.indexversion")) {
1883-
pack_idx_default_version = git_config_int(k, v);
1884-
if (pack_idx_default_version > 2)
1884+
pack_idx_opts.version = git_config_int(k, v);
1885+
if (pack_idx_opts.version > 2)
18851886
die("bad pack.indexversion=%"PRIu32,
1886-
pack_idx_default_version);
1887+
pack_idx_opts.version);
18871888
return 0;
18881889
}
18891890
if (!strcmp(k, "pack.packsizelimit")) {
@@ -2130,6 +2131,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
21302131
rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
21312132
rp_ac = 2;
21322133

2134+
reset_pack_idx_option(&pack_idx_opts);
21332135
git_config(git_pack_config, NULL);
21342136
if (!pack_compression_seen && core_compression_seen)
21352137
pack_compression_level = core_compression_level;
@@ -2274,12 +2276,12 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
22742276
}
22752277
if (!prefixcmp(arg, "--index-version=")) {
22762278
char *c;
2277-
pack_idx_default_version = strtoul(arg + 16, &c, 10);
2278-
if (pack_idx_default_version > 2)
2279+
pack_idx_opts.version = strtoul(arg + 16, &c, 10);
2280+
if (pack_idx_opts.version > 2)
22792281
die("bad %s", arg);
22802282
if (*c == ',')
2281-
pack_idx_off32_limit = strtoul(c+1, &c, 0);
2282-
if (*c || pack_idx_off32_limit & 0x80000000)
2283+
pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
2284+
if (*c || pack_idx_opts.off32_limit & 0x80000000)
22832285
die("bad %s", arg);
22842286
continue;
22852287
}

fast-import.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ static unsigned int atom_cnt;
315315
static struct atom_str **atom_table;
316316

317317
/* The .pack file being generated */
318+
static struct pack_idx_option pack_idx_opts;
318319
static unsigned int pack_id;
319320
static struct sha1file *pack_file;
320321
static struct packed_git *pack_data;
@@ -905,7 +906,7 @@ static const char *create_index(void)
905906
if (c != last)
906907
die("internal consistency error creating the index");
907908

908-
tmpfile = write_idx_file(NULL, idx, object_count, pack_data->sha1);
909+
tmpfile = write_idx_file(NULL, idx, object_count, &pack_idx_opts, pack_data->sha1);
909910
free(idx);
910911
return tmpfile;
911912
}
@@ -3055,10 +3056,10 @@ static int git_pack_config(const char *k, const char *v, void *cb)
30553056
return 0;
30563057
}
30573058
if (!strcmp(k, "pack.indexversion")) {
3058-
pack_idx_default_version = git_config_int(k, v);
3059-
if (pack_idx_default_version > 2)
3059+
pack_idx_opts.version = git_config_int(k, v);
3060+
if (pack_idx_opts.version > 2)
30603061
die("bad pack.indexversion=%"PRIu32,
3061-
pack_idx_default_version);
3062+
pack_idx_opts.version);
30623063
return 0;
30633064
}
30643065
if (!strcmp(k, "pack.packsizelimit")) {
@@ -3116,6 +3117,7 @@ int main(int argc, const char **argv)
31163117
usage(fast_import_usage);
31173118

31183119
setup_git_directory();
3120+
reset_pack_idx_option(&pack_idx_opts);
31193121
git_config(git_pack_config, NULL);
31203122
if (!pack_compression_seen && core_compression_seen)
31213123
pack_compression_level = core_compression_level;

pack-write.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
#include "pack.h"
33
#include "csum-file.h"
44

5-
uint32_t pack_idx_default_version = 2;
6-
uint32_t pack_idx_off32_limit = 0x7fffffff;
5+
void reset_pack_idx_option(struct pack_idx_option *opts)
6+
{
7+
memset(opts, 0, sizeof(*opts));
8+
opts->version = 2;
9+
opts->off32_limit = 0x7fffffff;
10+
}
711

812
static int sha1_compare(const void *_a, const void *_b)
913
{
@@ -18,7 +22,8 @@ static int sha1_compare(const void *_a, const void *_b)
1822
* will be sorted by SHA1 on exit.
1923
*/
2024
const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
21-
int nr_objects, unsigned char *sha1)
25+
int nr_objects, const struct pack_idx_option *opts,
26+
unsigned char *sha1)
2227
{
2328
struct sha1file *f;
2429
struct pack_idx_entry **sorted_by_sha, **list, **last;
@@ -55,7 +60,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
5560
f = sha1fd(fd, index_name);
5661

5762
/* if last object's offset is >= 2^31 we should use index V2 */
58-
index_version = (last_obj_offset >> 31) ? 2 : pack_idx_default_version;
63+
index_version = (last_obj_offset >> 31) ? 2 : opts->version;
5964

6065
/* index versions 2 and above need a header */
6166
if (index_version >= 2) {
@@ -115,7 +120,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
115120
list = sorted_by_sha;
116121
for (i = 0; i < nr_objects; i++) {
117122
struct pack_idx_entry *obj = *list++;
118-
uint32_t offset = (obj->offset <= pack_idx_off32_limit) ?
123+
uint32_t offset = (obj->offset <= opts->off32_limit) ?
119124
obj->offset : (0x80000000 | nr_large_offset++);
120125
offset = htonl(offset);
121126
sha1write(f, &offset, 4);
@@ -126,7 +131,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
126131
while (nr_large_offset) {
127132
struct pack_idx_entry *obj = *list++;
128133
uint64_t offset = obj->offset;
129-
if (offset > pack_idx_off32_limit) {
134+
if (offset > opts->off32_limit) {
130135
uint32_t split[2];
131136
split[0] = htonl(offset >> 32);
132137
split[1] = htonl(offset & 0xffffffff);

pack.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ struct pack_header {
3434
*/
3535
#define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */
3636

37-
/* These may be overridden by command-line parameters */
38-
extern uint32_t pack_idx_default_version;
39-
extern uint32_t pack_idx_off32_limit;
37+
struct pack_idx_option {
38+
uint32_t version;
39+
uint32_t off32_limit;
40+
};
41+
42+
extern void reset_pack_idx_option(struct pack_idx_option *);
4043

4144
/*
4245
* Packed object index header
@@ -55,7 +58,7 @@ struct pack_idx_entry {
5558
off_t offset;
5659
};
5760

58-
extern const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects, int nr_objects, unsigned char *sha1);
61+
extern const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects, int nr_objects, const struct pack_idx_option *, unsigned char *sha1);
5962
extern int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offset, off_t len, unsigned int nr);
6063
extern int verify_pack_index(struct packed_git *);
6164
extern int verify_pack(struct packed_git *);

0 commit comments

Comments
 (0)