@matheusbsbs / matheus-bagliano / commits / ebcfb3791a
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Feb 25, 2011 at 15:43 UTC ebcfb3791a53e0455bf8361046e3310993697a8e
5 files changed +48 -33
builtin/index-pack.c
+13 -10
@@ -880,11 +880,12 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
880
881 static int git_index_pack_config(const char *k, const char *v, void *cb)
882 {
883 + struct pack_idx_option *opts = cb;
884 +
885 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);
889 return 0;
890 }
891 return git_default_config(k, v, cb);
@@ -898,6 +899,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
899 const char *keep_name = NULL, *keep_msg = NULL;
900 char *index_name_buf = NULL, *keep_name_buf = NULL;
901 struct pack_idx_entry **idx_objects;
902 + struct pack_idx_option opts;
903 unsigned char pack_sha1[20];
904
905 if (argc == 2 && !strcmp(argv[1], "-h"))
@@ -905,7 +907,8 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
907
908 read_replace_refs = 0;
909
908 - git_config(git_index_pack_config, NULL);
910 + reset_pack_idx_option(&opts);
911 + git_config(git_index_pack_config, &opts);
912 if (prefix && chdir(prefix))
913 die("Cannot come back to cwd");
914
@@ -944,12 +947,12 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
947 index_name = argv[++i];
948 } else if (!prefixcmp(arg, "--index-version=")) {
949 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)
952 die("bad %s", arg);
953 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)
956 die("bad %s", arg);
957 } else
958 usage(index_pack_usage);
@@ -1032,7 +1035,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
1035 idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1036 for (i = 0; i < nr_objects; i++)
1037 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);
1039 free(idx_objects);
1040
1041 final(pack_name, curr_pack,
builtin/pack-objects.c
+11 -9
@@ -70,6 +70,7 @@ static int local;
70 static int incremental;
71 static int ignore_packed_keep;
72 static int allow_ofs_delta;
73 +static struct pack_idx_option pack_idx_opts;
74 static const char *base_name;
75 static int progress = 1;
76 static int window = 10;
@@ -493,8 +494,8 @@ static void write_pack_file(void)
494 const char *idx_tmp_name;
495 char tmpname[PATH_MAX];
496
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);
499
500 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
501 base_name, sha1_to_hex(sha1));
@@ -1880,10 +1881,10 @@ static int git_pack_config(const char *k, const char *v, void *cb)
1881 return 0;
1882 }
1883 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)
1886 die("bad pack.indexversion=%"PRIu32,
1886 - pack_idx_default_version);
1887 + pack_idx_opts.version);
1888 return 0;
1889 }
1890 if (!strcmp(k, "pack.packsizelimit")) {
@@ -2130,6 +2131,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
2131 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
2132 rp_ac = 2;
2133
2134 + reset_pack_idx_option(&pack_idx_opts);
2135 git_config(git_pack_config, NULL);
2136 if (!pack_compression_seen && core_compression_seen)
2137 pack_compression_level = core_compression_level;
@@ -2274,12 +2276,12 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
2276 }
2277 if (!prefixcmp(arg, "--index-version=")) {
2278 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)
2281 die("bad %s", arg);
2282 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)
2285 die("bad %s", arg);
2286 continue;
2287 }
fast-import.c
+6 -4
@@ -315,6 +315,7 @@ static unsigned int atom_cnt;
315 static struct atom_str **atom_table;
316
317 /* The .pack file being generated */
318 +static struct pack_idx_option pack_idx_opts;
319 static unsigned int pack_id;
320 static struct sha1file *pack_file;
321 static struct packed_git *pack_data;
@@ -905,7 +906,7 @@ static const char *create_index(void)
906 if (c != last)
907 die("internal consistency error creating the index");
908
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);
910 free(idx);
911 return tmpfile;
912 }
@@ -3055,10 +3056,10 @@ static int git_pack_config(const char *k, const char *v, void *cb)
3056 return 0;
3057 }
3058 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)
3061 die("bad pack.indexversion=%"PRIu32,
3061 - pack_idx_default_version);
3062 + pack_idx_opts.version);
3063 return 0;
3064 }
3065 if (!strcmp(k, "pack.packsizelimit")) {
@@ -3116,6 +3117,7 @@ int main(int argc, const char **argv)
3117 usage(fast_import_usage);
3118
3119 setup_git_directory();
3120 + reset_pack_idx_option(&pack_idx_opts);
3121 git_config(git_pack_config, NULL);
3122 if (!pack_compression_seen && core_compression_seen)
3123 pack_compression_level = core_compression_level;
pack-write.c
+11 -6
@@ -2,8 +2,12 @@
2 #include "pack.h"
3 #include "csum-file.h"
4
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 +}
11
12 static int sha1_compare(const void *_a, const void *_b)
13 {
@@ -18,7 +22,8 @@ static int sha1_compare(const void *_a, const void *_b)
22 * will be sorted by SHA1 on exit.
23 */
24 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)
27 {
28 struct sha1file *f;
29 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
60 f = sha1fd(fd, index_name);
61
62 /* 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;
64
65 /* index versions 2 and above need a header */
66 if (index_version >= 2) {
@@ -115,7 +120,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
120 list = sorted_by_sha;
121 for (i = 0; i < nr_objects; i++) {
122 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) ?
124 obj->offset : (0x80000000 | nr_large_offset++);
125 offset = htonl(offset);
126 sha1write(f, &offset, 4);
@@ -126,7 +131,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
131 while (nr_large_offset) {
132 struct pack_idx_entry *obj = *list++;
133 uint64_t offset = obj->offset;
129 - if (offset > pack_idx_off32_limit) {
134 + if (offset > opts->off32_limit) {
135 uint32_t split[2];
136 split[0] = htonl(offset >> 32);
137 split[1] = htonl(offset & 0xffffffff);
pack.h
+7 -4
@@ -34,9 +34,12 @@ struct pack_header {
34 */
35 #define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */
36
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 *);
43
44 /*
45 * Packed object index header
@@ -55,7 +58,7 @@ struct pack_idx_entry {
58 off_t offset;
59 };
60
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);
62 extern int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offset, off_t len, unsigned int nr);
63 extern int verify_pack_index(struct packed_git *);
64 extern int verify_pack(struct packed_git *);