Browse Source

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>
maint
Junio C Hamano 14 years ago
parent
commit
ebcfb3791a
  1. 23
      builtin/index-pack.c
  2. 20
      builtin/pack-objects.c
  3. 10
      fast-import.c
  4. 17
      pack-write.c
  5. 11
      pack.h

23
builtin/index-pack.c

@ -880,11 +880,12 @@ static void final(const char *final_pack_name, const char *curr_pack_name,


static int git_index_pack_config(const char *k, const char *v, void *cb) static int git_index_pack_config(const char *k, const char *v, void *cb)
{ {
struct pack_idx_option *opts = cb;

if (!strcmp(k, "pack.indexversion")) { if (!strcmp(k, "pack.indexversion")) {
pack_idx_default_version = git_config_int(k, v); opts->version = git_config_int(k, v);
if (pack_idx_default_version > 2) if (opts->version > 2)
die("bad pack.indexversion=%"PRIu32, die("bad pack.indexversion=%"PRIu32, opts->version);
pack_idx_default_version);
return 0; return 0;
} }
return git_default_config(k, v, cb); return git_default_config(k, v, cb);
@ -898,6 +899,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
const char *keep_name = NULL, *keep_msg = NULL; const char *keep_name = NULL, *keep_msg = NULL;
char *index_name_buf = NULL, *keep_name_buf = NULL; char *index_name_buf = NULL, *keep_name_buf = NULL;
struct pack_idx_entry **idx_objects; struct pack_idx_entry **idx_objects;
struct pack_idx_option opts;
unsigned char pack_sha1[20]; unsigned char pack_sha1[20];


if (argc == 2 && !strcmp(argv[1], "-h")) if (argc == 2 && !strcmp(argv[1], "-h"))
@ -905,7 +907,8 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)


read_replace_refs = 0; read_replace_refs = 0;


git_config(git_index_pack_config, NULL); reset_pack_idx_option(&opts);
git_config(git_index_pack_config, &opts);
if (prefix && chdir(prefix)) if (prefix && chdir(prefix))
die("Cannot come back to cwd"); die("Cannot come back to cwd");


@ -944,12 +947,12 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
index_name = argv[++i]; index_name = argv[++i];
} else if (!prefixcmp(arg, "--index-version=")) { } else if (!prefixcmp(arg, "--index-version=")) {
char *c; char *c;
pack_idx_default_version = strtoul(arg + 16, &c, 10); opts.version = strtoul(arg + 16, &c, 10);
if (pack_idx_default_version > 2) if (opts.version > 2)
die("bad %s", arg); die("bad %s", arg);
if (*c == ',') if (*c == ',')
pack_idx_off32_limit = strtoul(c+1, &c, 0); opts.off32_limit = strtoul(c+1, &c, 0);
if (*c || pack_idx_off32_limit & 0x80000000) if (*c || opts.off32_limit & 0x80000000)
die("bad %s", arg); die("bad %s", arg);
} else } else
usage(index_pack_usage); usage(index_pack_usage);
@ -1032,7 +1035,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *)); idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
for (i = 0; i < nr_objects; i++) for (i = 0; i < nr_objects; i++)
idx_objects[i] = &objects[i].idx; idx_objects[i] = &objects[i].idx;
curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1); curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);
free(idx_objects); free(idx_objects);


final(pack_name, curr_pack, final(pack_name, curr_pack,

20
builtin/pack-objects.c

@ -70,6 +70,7 @@ static int local;
static int incremental; static int incremental;
static int ignore_packed_keep; static int ignore_packed_keep;
static int allow_ofs_delta; static int allow_ofs_delta;
static struct pack_idx_option pack_idx_opts;
static const char *base_name; static const char *base_name;
static int progress = 1; static int progress = 1;
static int window = 10; static int window = 10;
@ -493,8 +494,8 @@ static void write_pack_file(void)
const char *idx_tmp_name; const char *idx_tmp_name;
char tmpname[PATH_MAX]; char tmpname[PATH_MAX];


idx_tmp_name = write_idx_file(NULL, written_list, idx_tmp_name = write_idx_file(NULL, written_list, nr_written,
nr_written, sha1); &pack_idx_opts, sha1);


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


reset_pack_idx_option(&pack_idx_opts);
git_config(git_pack_config, NULL); git_config(git_pack_config, NULL);
if (!pack_compression_seen && core_compression_seen) if (!pack_compression_seen && core_compression_seen)
pack_compression_level = core_compression_level; pack_compression_level = core_compression_level;
@ -2274,12 +2276,12 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
} }
if (!prefixcmp(arg, "--index-version=")) { if (!prefixcmp(arg, "--index-version=")) {
char *c; char *c;
pack_idx_default_version = strtoul(arg + 16, &c, 10); pack_idx_opts.version = strtoul(arg + 16, &c, 10);
if (pack_idx_default_version > 2) if (pack_idx_opts.version > 2)
die("bad %s", arg); die("bad %s", arg);
if (*c == ',') if (*c == ',')
pack_idx_off32_limit = strtoul(c+1, &c, 0); pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
if (*c || pack_idx_off32_limit & 0x80000000) if (*c || pack_idx_opts.off32_limit & 0x80000000)
die("bad %s", arg); die("bad %s", arg);
continue; continue;
} }

10
fast-import.c

@ -315,6 +315,7 @@ static unsigned int atom_cnt;
static struct atom_str **atom_table; static struct atom_str **atom_table;


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


tmpfile = write_idx_file(NULL, idx, object_count, pack_data->sha1); tmpfile = write_idx_file(NULL, idx, object_count, &pack_idx_opts, pack_data->sha1);
free(idx); free(idx);
return tmpfile; return tmpfile;
} }
@ -3055,10 +3056,10 @@ static int git_pack_config(const char *k, const char *v, void *cb)
return 0; return 0;
} }
if (!strcmp(k, "pack.indexversion")) { if (!strcmp(k, "pack.indexversion")) {
pack_idx_default_version = git_config_int(k, v); pack_idx_opts.version = git_config_int(k, v);
if (pack_idx_default_version > 2) if (pack_idx_opts.version > 2)
die("bad pack.indexversion=%"PRIu32, die("bad pack.indexversion=%"PRIu32,
pack_idx_default_version); pack_idx_opts.version);
return 0; return 0;
} }
if (!strcmp(k, "pack.packsizelimit")) { if (!strcmp(k, "pack.packsizelimit")) {
@ -3116,6 +3117,7 @@ int main(int argc, const char **argv)
usage(fast_import_usage); usage(fast_import_usage);


setup_git_directory(); setup_git_directory();
reset_pack_idx_option(&pack_idx_opts);
git_config(git_pack_config, NULL); git_config(git_pack_config, NULL);
if (!pack_compression_seen && core_compression_seen) if (!pack_compression_seen && core_compression_seen)
pack_compression_level = core_compression_level; pack_compression_level = core_compression_level;

17
pack-write.c

@ -2,8 +2,12 @@
#include "pack.h" #include "pack.h"
#include "csum-file.h" #include "csum-file.h"


uint32_t pack_idx_default_version = 2; void reset_pack_idx_option(struct pack_idx_option *opts)
uint32_t pack_idx_off32_limit = 0x7fffffff; {
memset(opts, 0, sizeof(*opts));
opts->version = 2;
opts->off32_limit = 0x7fffffff;
}


static int sha1_compare(const void *_a, const void *_b) static int sha1_compare(const void *_a, const void *_b)
{ {
@ -18,7 +22,8 @@ static int sha1_compare(const void *_a, const void *_b)
* will be sorted by SHA1 on exit. * will be sorted by SHA1 on exit.
*/ */
const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects, const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
int nr_objects, unsigned char *sha1) int nr_objects, const struct pack_idx_option *opts,
unsigned char *sha1)
{ {
struct sha1file *f; struct sha1file *f;
struct pack_idx_entry **sorted_by_sha, **list, **last; 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
f = sha1fd(fd, index_name); f = sha1fd(fd, index_name);


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


/* index versions 2 and above need a header */ /* index versions 2 and above need a header */
if (index_version >= 2) { if (index_version >= 2) {
@ -115,7 +120,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
list = sorted_by_sha; list = sorted_by_sha;
for (i = 0; i < nr_objects; i++) { for (i = 0; i < nr_objects; i++) {
struct pack_idx_entry *obj = *list++; struct pack_idx_entry *obj = *list++;
uint32_t offset = (obj->offset <= pack_idx_off32_limit) ? uint32_t offset = (obj->offset <= opts->off32_limit) ?
obj->offset : (0x80000000 | nr_large_offset++); obj->offset : (0x80000000 | nr_large_offset++);
offset = htonl(offset); offset = htonl(offset);
sha1write(f, &offset, 4); sha1write(f, &offset, 4);
@ -126,7 +131,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
while (nr_large_offset) { while (nr_large_offset) {
struct pack_idx_entry *obj = *list++; struct pack_idx_entry *obj = *list++;
uint64_t offset = obj->offset; uint64_t offset = obj->offset;
if (offset > pack_idx_off32_limit) { if (offset > opts->off32_limit) {
uint32_t split[2]; uint32_t split[2];
split[0] = htonl(offset >> 32); split[0] = htonl(offset >> 32);
split[1] = htonl(offset & 0xffffffff); split[1] = htonl(offset & 0xffffffff);

11
pack.h

@ -34,9 +34,12 @@ struct pack_header {
*/ */
#define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */ #define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */


/* These may be overridden by command-line parameters */ struct pack_idx_option {
extern uint32_t pack_idx_default_version; uint32_t version;
extern uint32_t pack_idx_off32_limit; uint32_t off32_limit;
};

extern void reset_pack_idx_option(struct pack_idx_option *);


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


extern const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects, int nr_objects, unsigned char *sha1); 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);
extern int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offset, off_t len, unsigned int nr); extern int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off_t offset, off_t len, unsigned int nr);
extern int verify_pack_index(struct packed_git *); extern int verify_pack_index(struct packed_git *);
extern int verify_pack(struct packed_git *); extern int verify_pack(struct packed_git *);

Loading…
Cancel
Save