Skip to content

Commit

Permalink
write_idx_file: introduce a struct to hold idx customization options
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Junio C Hamano committed Feb 28, 2011
1 parent 7218a21 commit ebcfb37
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 33 deletions.
23 changes: 13 additions & 10 deletions builtin/index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
struct pack_idx_option *opts = cb;

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

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

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))
die("Cannot come back to cwd");

Expand Down Expand Up @@ -944,12 +947,12 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
index_name = argv[++i];
} else if (!prefixcmp(arg, "--index-version=")) {
char *c;
pack_idx_default_version = strtoul(arg + 16, &c, 10);
if (pack_idx_default_version > 2)
opts.version = strtoul(arg + 16, &c, 10);
if (opts.version > 2)
die("bad %s", arg);
if (*c == ',')
pack_idx_off32_limit = strtoul(c+1, &c, 0);
if (*c || pack_idx_off32_limit & 0x80000000)
opts.off32_limit = strtoul(c+1, &c, 0);
if (*c || opts.off32_limit & 0x80000000)
die("bad %s", arg);
} else
usage(index_pack_usage);
Expand Down Expand Up @@ -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 *));
for (i = 0; i < nr_objects; i++)
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);

final(pack_name, curr_pack,
Expand Down
20 changes: 11 additions & 9 deletions builtin/pack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static int local;
static int incremental;
static int ignore_packed_keep;
static int allow_ofs_delta;
static struct pack_idx_option pack_idx_opts;
static const char *base_name;
static int progress = 1;
static int window = 10;
Expand Down Expand Up @@ -493,8 +494,8 @@ static void write_pack_file(void)
const char *idx_tmp_name;
char tmpname[PATH_MAX];

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

snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
base_name, sha1_to_hex(sha1));
Expand Down Expand Up @@ -1880,10 +1881,10 @@ static int git_pack_config(const char *k, const char *v, void *cb)
return 0;
}
if (!strcmp(k, "pack.indexversion")) {
pack_idx_default_version = git_config_int(k, v);
if (pack_idx_default_version > 2)
pack_idx_opts.version = git_config_int(k, v);
if (pack_idx_opts.version > 2)
die("bad pack.indexversion=%"PRIu32,
pack_idx_default_version);
pack_idx_opts.version);
return 0;
}
if (!strcmp(k, "pack.packsizelimit")) {
Expand Down Expand Up @@ -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_ac = 2;

reset_pack_idx_option(&pack_idx_opts);
git_config(git_pack_config, NULL);
if (!pack_compression_seen && core_compression_seen)
pack_compression_level = core_compression_level;
Expand Down Expand Up @@ -2274,12 +2276,12 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
}
if (!prefixcmp(arg, "--index-version=")) {
char *c;
pack_idx_default_version = strtoul(arg + 16, &c, 10);
if (pack_idx_default_version > 2)
pack_idx_opts.version = strtoul(arg + 16, &c, 10);
if (pack_idx_opts.version > 2)
die("bad %s", arg);
if (*c == ',')
pack_idx_off32_limit = strtoul(c+1, &c, 0);
if (*c || pack_idx_off32_limit & 0x80000000)
pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
if (*c || pack_idx_opts.off32_limit & 0x80000000)
die("bad %s", arg);
continue;
}
Expand Down
10 changes: 6 additions & 4 deletions fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ static unsigned int atom_cnt;
static struct atom_str **atom_table;

/* The .pack file being generated */
static struct pack_idx_option pack_idx_opts;
static unsigned int pack_id;
static struct sha1file *pack_file;
static struct packed_git *pack_data;
Expand Down Expand Up @@ -905,7 +906,7 @@ static const char *create_index(void)
if (c != last)
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);
return tmpfile;
}
Expand Down Expand Up @@ -3055,10 +3056,10 @@ static int git_pack_config(const char *k, const char *v, void *cb)
return 0;
}
if (!strcmp(k, "pack.indexversion")) {
pack_idx_default_version = git_config_int(k, v);
if (pack_idx_default_version > 2)
pack_idx_opts.version = git_config_int(k, v);
if (pack_idx_opts.version > 2)
die("bad pack.indexversion=%"PRIu32,
pack_idx_default_version);
pack_idx_opts.version);
return 0;
}
if (!strcmp(k, "pack.packsizelimit")) {
Expand Down Expand Up @@ -3116,6 +3117,7 @@ int main(int argc, const char **argv)
usage(fast_import_usage);

setup_git_directory();
reset_pack_idx_option(&pack_idx_opts);
git_config(git_pack_config, NULL);
if (!pack_compression_seen && core_compression_seen)
pack_compression_level = core_compression_level;
Expand Down
17 changes: 11 additions & 6 deletions pack-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
#include "pack.h"
#include "csum-file.h"

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

static int sha1_compare(const void *_a, const void *_b)
{
Expand All @@ -18,7 +22,8 @@ static int sha1_compare(const void *_a, const void *_b)
* will be sorted by SHA1 on exit.
*/
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 pack_idx_entry **sorted_by_sha, **list, **last;
Expand Down Expand Up @@ -55,7 +60,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
f = sha1fd(fd, index_name);

/* 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 */
if (index_version >= 2) {
Expand Down Expand Up @@ -115,7 +120,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
list = sorted_by_sha;
for (i = 0; i < nr_objects; i++) {
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++);
offset = htonl(offset);
sha1write(f, &offset, 4);
Expand All @@ -126,7 +131,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
while (nr_large_offset) {
struct pack_idx_entry *obj = *list++;
uint64_t offset = obj->offset;
if (offset > pack_idx_off32_limit) {
if (offset > opts->off32_limit) {
uint32_t split[2];
split[0] = htonl(offset >> 32);
split[1] = htonl(offset & 0xffffffff);
Expand Down
11 changes: 7 additions & 4 deletions pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ struct pack_header {
*/
#define PACK_IDX_SIGNATURE 0xff744f63 /* "\377tOc" */

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

extern void reset_pack_idx_option(struct pack_idx_option *);

/*
* Packed object index header
Expand All @@ -55,7 +58,7 @@ struct pack_idx_entry {
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 verify_pack_index(struct packed_git *);
extern int verify_pack(struct packed_git *);
Expand Down

0 comments on commit ebcfb37

Please sign in to comment.