diff options
Diffstat (limited to 'fast-import.c')
-rw-r--r-- | fast-import.c | 530 |
1 files changed, 226 insertions, 304 deletions
diff --git a/fast-import.c b/fast-import.c index 35119c7a5d..b8b65a801c 100644 --- a/fast-import.c +++ b/fast-import.c @@ -1,159 +1,6 @@ -/* -(See Documentation/git-fast-import.txt for maintained documentation.) -Format of STDIN stream: - - stream ::= cmd*; - - cmd ::= new_blob - | new_commit - | new_tag - | reset_branch - | checkpoint - | progress - ; - - new_blob ::= 'blob' lf - mark? - file_content; - file_content ::= data; - - new_commit ::= 'commit' sp ref_str lf - mark? - ('author' (sp name)? sp '<' email '>' sp when lf)? - 'committer' (sp name)? sp '<' email '>' sp when lf - commit_msg - ('from' sp commit-ish lf)? - ('merge' sp commit-ish lf)* - (file_change | ls)* - lf?; - commit_msg ::= data; - - ls ::= 'ls' sp '"' quoted(path) '"' lf; - - file_change ::= file_clr - | file_del - | file_rnm - | file_cpy - | file_obm - | file_inm; - file_clr ::= 'deleteall' lf; - file_del ::= 'D' sp path_str lf; - file_rnm ::= 'R' sp path_str sp path_str lf; - file_cpy ::= 'C' sp path_str sp path_str lf; - file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf; - file_inm ::= 'M' sp mode sp 'inline' sp path_str lf - data; - note_obm ::= 'N' sp (hexsha1 | idnum) sp commit-ish lf; - note_inm ::= 'N' sp 'inline' sp commit-ish lf - data; - - new_tag ::= 'tag' sp tag_str lf - 'from' sp commit-ish lf - ('tagger' (sp name)? sp '<' email '>' sp when lf)? - tag_msg; - tag_msg ::= data; - - reset_branch ::= 'reset' sp ref_str lf - ('from' sp commit-ish lf)? - lf?; - - checkpoint ::= 'checkpoint' lf - lf?; - - progress ::= 'progress' sp not_lf* lf - lf?; - - # note: the first idnum in a stream should be 1 and subsequent - # idnums should not have gaps between values as this will cause - # the stream parser to reserve space for the gapped values. An - # idnum can be updated in the future to a new object by issuing - # a new mark directive with the old idnum. - # - mark ::= 'mark' sp idnum lf; - data ::= (delimited_data | exact_data) - lf?; - - # note: delim may be any string but must not contain lf. - # data_line may contain any data but must not be exactly - # delim. - delimited_data ::= 'data' sp '<<' delim lf - (data_line lf)* - delim lf; - - # note: declen indicates the length of binary_data in bytes. - # declen does not include the lf preceding the binary data. - # - exact_data ::= 'data' sp declen lf - binary_data; - - # note: quoted strings are C-style quoting supporting \c for - # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn - # is the signed byte value in octal. Note that the only - # characters which must actually be escaped to protect the - # stream formatting is: \, " and LF. Otherwise these values - # are UTF8. - # - commit-ish ::= (ref_str | hexsha1 | sha1exp_str | idnum); - ref_str ::= ref; - sha1exp_str ::= sha1exp; - tag_str ::= tag; - path_str ::= path | '"' quoted(path) '"' ; - mode ::= '100644' | '644' - | '100755' | '755' - | '120000' - ; - - declen ::= # unsigned 32 bit value, ascii base10 notation; - bigint ::= # unsigned integer value, ascii base10 notation; - binary_data ::= # file content, not interpreted; - - when ::= raw_when | rfc2822_when; - raw_when ::= ts sp tz; - rfc2822_when ::= # Valid RFC 2822 date and time; - - sp ::= # ASCII space character; - lf ::= # ASCII newline (LF) character; - - # note: a colon (':') must precede the numerical value assigned to - # an idnum. This is to distinguish it from a ref or tag name as - # GIT does not permit ':' in ref or tag strings. - # - idnum ::= ':' bigint; - path ::= # GIT style file path, e.g. "a/b/c"; - ref ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT"; - tag ::= # GIT tag name, e.g. "FIREFOX_1_5"; - sha1exp ::= # Any valid GIT SHA1 expression; - hexsha1 ::= # SHA1 in hexadecimal format; - - # note: name and email are UTF8 strings, however name must not - # contain '<' or lf and email must not contain any of the - # following: '<', '>', lf. - # - name ::= # valid GIT author/committer name; - email ::= # valid GIT author/committer email; - ts ::= # time since the epoch in seconds, ascii base10 notation; - tz ::= # GIT style timezone; - - # note: comments, get-mark, ls-tree, and cat-blob requests may - # appear anywhere in the input, except within a data command. Any - # form of the data command always escapes the related input from - # comment processing. - # - # In case it is not clear, the '#' that starts the comment - # must be the first character on that line (an lf - # preceded it). - # - - get_mark ::= 'get-mark' sp idnum lf; - cat_blob ::= 'cat-blob' sp (hexsha1 | idnum) lf; - ls_tree ::= 'ls' sp (hexsha1 | idnum) sp path_str lf; - - comment ::= '#' not_lf* lf; - not_lf ::= # Any byte that is not ASCII newline (LF); -*/ - #include "builtin.h" #include "cache.h" +#include "repository.h" #include "config.h" #include "lockfile.h" #include "object.h" @@ -168,6 +15,9 @@ Format of STDIN stream: #include "dir.h" #include "run-command.h" #include "packfile.h" +#include "object-store.h" +#include "mem-pool.h" +#include "commit-reach.h" #define PACK_ID_BITS 16 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1) @@ -179,6 +29,13 @@ Format of STDIN stream: */ #define NO_DELTA S_ISUID +/* + * The amount of additional space required in order to write an object into the + * current pack. This is the hash lengths at the end of the pack, plus the + * length of one object ID. + */ +#define PACK_SIZE_THRESHOLD (the_hash_algo->rawsz * 3) + struct object_entry { struct pack_idx_entry idx; struct object_entry *next; @@ -209,13 +66,6 @@ struct last_object { unsigned no_swap : 1; }; -struct mem_pool { - struct mem_pool *next_pool; - char *next_free; - char *end; - uintmax_t space[FLEX_ARRAY]; /* more */ -}; - struct atom_str { struct atom_str *next_atom; unsigned short str_len; @@ -304,9 +154,8 @@ static int global_argc; static const char **global_argv; /* Memory pools */ -static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool); -static size_t total_allocd; -static struct mem_pool *mem_pool; +static struct mem_pool fi_mem_pool = {NULL, 2*1024*1024 - + sizeof(struct mp_block), 0 }; /* Atom management */ static unsigned int atom_table_sz = 4451; @@ -341,6 +190,7 @@ static unsigned int tree_entry_alloc = 1000; static void *avail_tree_entry; static unsigned int avail_tree_table_sz = 100; static struct avail_tree_content **avail_tree_table; +static size_t tree_entry_allocd; static struct strbuf old_tree = STRBUF_INIT; static struct strbuf new_tree = STRBUF_INIT; @@ -577,7 +427,7 @@ static struct object_entry *find_object(struct object_id *oid) unsigned int h = oid->hash[0] << 8 | oid->hash[1]; struct object_entry *e; for (e = object_table[h]; e; e = e->next) - if (!oidcmp(oid, &e->idx.oid)) + if (oideq(oid, &e->idx.oid)) return e; return NULL; } @@ -588,7 +438,7 @@ static struct object_entry *insert_object(struct object_id *oid) struct object_entry *e = object_table[h]; while (e) { - if (!oidcmp(oid, &e->idx.oid)) + if (oideq(oid, &e->idx.oid)) return e; e = e->next; } @@ -635,49 +485,10 @@ static unsigned int hc_str(const char *s, size_t len) return r; } -static void *pool_alloc(size_t len) -{ - struct mem_pool *p; - void *r; - - /* round up to a 'uintmax_t' alignment */ - if (len & (sizeof(uintmax_t) - 1)) - len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1)); - - for (p = mem_pool; p; p = p->next_pool) - if ((p->end - p->next_free >= len)) - break; - - if (!p) { - if (len >= (mem_pool_alloc/2)) { - total_allocd += len; - return xmalloc(len); - } - total_allocd += sizeof(struct mem_pool) + mem_pool_alloc; - p = xmalloc(st_add(sizeof(struct mem_pool), mem_pool_alloc)); - p->next_pool = mem_pool; - p->next_free = (char *) p->space; - p->end = p->next_free + mem_pool_alloc; - mem_pool = p; - } - - r = p->next_free; - p->next_free += len; - return r; -} - -static void *pool_calloc(size_t count, size_t size) -{ - size_t len = count * size; - void *r = pool_alloc(len); - memset(r, 0, len); - return r; -} - static char *pool_strdup(const char *s) { size_t len = strlen(s) + 1; - char *r = pool_alloc(len); + char *r = mem_pool_alloc(&fi_mem_pool, len); memcpy(r, s, len); return r; } @@ -686,7 +497,7 @@ static void insert_mark(uintmax_t idnum, struct object_entry *oe) { struct mark_set *s = marks; while ((idnum >> s->shift) >= 1024) { - s = pool_calloc(1, sizeof(struct mark_set)); + s = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set)); s->shift = marks->shift + 10; s->data.sets[0] = marks; marks = s; @@ -695,7 +506,7 @@ static void insert_mark(uintmax_t idnum, struct object_entry *oe) uintmax_t i = idnum >> s->shift; idnum -= i << s->shift; if (!s->data.sets[i]) { - s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set)); + s->data.sets[i] = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set)); s->data.sets[i]->shift = s->shift - 10; } s = s->data.sets[i]; @@ -733,7 +544,7 @@ static struct atom_str *to_atom(const char *s, unsigned short len) if (c->str_len == len && !strncmp(s, c->str_dat, len)) return c; - c = pool_alloc(sizeof(struct atom_str) + len + 1); + c = mem_pool_alloc(&fi_mem_pool, sizeof(struct atom_str) + len + 1); c->str_len = len; memcpy(c->str_dat, s, len); c->str_dat[len] = 0; @@ -764,7 +575,7 @@ static struct branch *new_branch(const char *name) if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL)) die("Branch name doesn't conform to GIT standards: %s", name); - b = pool_calloc(1, sizeof(struct branch)); + b = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct branch)); b->name = pool_strdup(name); b->table_next_branch = branch_table[hc]; b->branch_tree.versions[0].mode = S_IFDIR; @@ -800,7 +611,7 @@ static struct tree_content *new_tree_content(unsigned int cnt) avail_tree_table[hc] = f->next_avail; } else { cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt; - f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt); + f = mem_pool_alloc(&fi_mem_pool, sizeof(*t) + sizeof(t->entries[0]) * cnt); f->entry_capacity = cnt; } @@ -834,7 +645,7 @@ static struct tree_content *grow_tree_content( struct tree_content *r = new_tree_content(t->entry_count + amt); r->entry_count = t->entry_count; r->delta_depth = t->delta_depth; - memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0])); + COPY_ARRAY(r->entries, t->entries, t->entry_count); release_tree_content(t); return r; } @@ -845,7 +656,7 @@ static struct tree_entry *new_tree_entry(void) if (!avail_tree_entry) { unsigned int n = tree_entry_alloc; - total_allocd += n * sizeof(struct tree_entry); + tree_entry_allocd += n * sizeof(struct tree_entry); ALLOC_ARRAY(e, n); avail_tree_entry = e; while (n-- > 1) { @@ -939,7 +750,8 @@ 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_idx_opts, pack_data->sha1); + tmpfile = write_idx_file(NULL, idx, object_count, &pack_idx_opts, + pack_data->hash); free(idx); return tmpfile; } @@ -950,7 +762,7 @@ static char *keep_pack(const char *curr_index_name) struct strbuf name = STRBUF_INIT; int keep_fd; - odb_pack_name(&name, pack_data->sha1, "keep"); + odb_pack_name(&name, pack_data->hash, "keep"); keep_fd = odb_pack_keep(name.buf); if (keep_fd < 0) die_errno("cannot create keep file"); @@ -958,11 +770,11 @@ static char *keep_pack(const char *curr_index_name) if (close(keep_fd)) die_errno("failed to write keep file"); - odb_pack_name(&name, pack_data->sha1, "pack"); + odb_pack_name(&name, pack_data->hash, "pack"); if (finalize_object_file(pack_data->pack_name, name.buf)) die("cannot store pack file"); - odb_pack_name(&name, pack_data->sha1, "idx"); + odb_pack_name(&name, pack_data->hash, "idx"); if (finalize_object_file(curr_index_name, name.buf)) die("cannot store index file"); free((void *)curr_index_name); @@ -976,7 +788,7 @@ static void unkeep_all_packs(void) for (k = 0; k < pack_id; k++) { struct packed_git *p = all_packs[k]; - odb_pack_name(&name, p->sha1, "keep"); + odb_pack_name(&name, p->hash, "keep"); unlink_or_warn(name.buf); } strbuf_release(&name); @@ -1017,10 +829,10 @@ static void end_packfile(void) struct tag *t; close_pack_windows(pack_data); - hashclose(pack_file, cur_pack_oid.hash, 0); - fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1, - pack_data->pack_name, object_count, - cur_pack_oid.hash, pack_size); + finalize_hashfile(pack_file, cur_pack_oid.hash, 0); + fixup_pack_header_footer(pack_data->pack_fd, pack_data->hash, + pack_data->pack_name, object_count, + cur_pack_oid.hash, pack_size); if (object_count <= unpack_limit) { if (!loosen_small_pack(pack_data)) { @@ -1037,7 +849,7 @@ static void end_packfile(void) if (!new_p) die("core git rejected index %s", idx_name); all_packs[pack_id] = new_p; - install_packed_git(new_p); + install_packed_git(the_repository, new_p); free(idx_name); /* Print the boundary */ @@ -1111,7 +923,8 @@ static int store_object( if (e->idx.offset) { duplicate_count_by_type[type]++; return 1; - } else if (find_sha1_pack(oid.hash, packed_git)) { + } else if (find_sha1_pack(oid.hash, + get_all_packs(the_repository))) { e->type = type; e->pack_id = MAX_PACK_ID; e->idx.offset = 1; /* just not zero! */ @@ -1119,7 +932,7 @@ static int store_object( return 1; } - if (last && last->data.buf && last->depth < max_depth + if (last && last->data.len && last->data.buf && last->depth < max_depth && dat->len > the_hash_algo->rawsz) { delta_count_attempts_by_type[type]++; @@ -1144,8 +957,9 @@ static int store_object( git_deflate_end(&s); /* Determine if we should auto-checkpoint. */ - if ((max_packsize && (pack_size + 60 + s.total_out) > max_packsize) - || (pack_size + 60 + s.total_out) < pack_size) { + if ((max_packsize + && (pack_size + PACK_SIZE_THRESHOLD + s.total_out) > max_packsize) + || (pack_size + PACK_SIZE_THRESHOLD + s.total_out) < pack_size) { /* This new object needs to *not* have the current pack_id. */ e->pack_id = pack_id + 1; @@ -1240,8 +1054,9 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark) int status = Z_OK; /* Determine if we should auto-checkpoint. */ - if ((max_packsize && (pack_size + 60 + len) > max_packsize) - || (pack_size + 60 + len) < pack_size) + if ((max_packsize + && (pack_size + PACK_SIZE_THRESHOLD + len) > max_packsize) + || (pack_size + PACK_SIZE_THRESHOLD + len) < pack_size) cycle_packfile(); hashfile_checkpoint(pack_file, &checkpoint); @@ -1308,7 +1123,8 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark) duplicate_count_by_type[OBJ_BLOB]++; truncate_pack(&checkpoint); - } else if (find_sha1_pack(oid.hash, packed_git)) { + } else if (find_sha1_pack(oid.hash, + get_all_packs(the_repository))) { e->type = OBJ_BLOB; e->pack_id = MAX_PACK_ID; e->idx.offset = 1; /* just not zero! */ @@ -1373,7 +1189,7 @@ static void *gfi_unpack_entry( */ p->pack_size = pack_size + the_hash_algo->rawsz; } - return unpack_entry(p, oe->idx.offset, &type, sizep); + return unpack_entry(the_repository, p, oe->idx.offset, &type, sizep); } static const char *get_mode(const char *str, uint16_t *modep) @@ -1413,7 +1229,7 @@ static void load_tree(struct tree_entry *root) die("Can't load tree %s", oid_to_hex(oid)); } else { enum object_type type; - buf = read_sha1_file(oid->hash, &type, &size); + buf = read_object_file(oid, &type, &size); if (!buf || type != OBJ_TREE) die("Can't load tree %s", oid_to_hex(oid)); } @@ -1435,7 +1251,7 @@ static void load_tree(struct tree_entry *root) c += e->name->str_len + 1; hashcpy(e->versions[0].oid.hash, (unsigned char *)c); hashcpy(e->versions[1].oid.hash, (unsigned char *)c); - c += GIT_SHA1_RAWSZ; + c += the_hash_algo->rawsz; } free(buf); } @@ -1482,7 +1298,7 @@ static void mktree(struct tree_content *t, int v, struct strbuf *b) strbuf_addf(b, "%o %s%c", (unsigned int)(e->versions[v].mode & ~NO_DELTA), e->name->str_dat, '\0'); - strbuf_add(b, e->versions[v].oid.hash, GIT_SHA1_RAWSZ); + strbuf_add(b, e->versions[v].oid.hash, the_hash_algo->rawsz); } } @@ -1575,7 +1391,7 @@ static int tree_content_set( if (!*slash1) { if (!S_ISDIR(mode) && e->versions[1].mode == mode - && !oidcmp(&e->versions[1].oid, oid)) + && oideq(&e->versions[1].oid, oid)) return 0; e->versions[1].mode = mode; oidcpy(&e->versions[1].oid, oid); @@ -1766,8 +1582,10 @@ static int update_branch(struct branch *b) if (!force_update && !is_null_oid(&old_oid)) { struct commit *old_cmit, *new_cmit; - old_cmit = lookup_commit_reference_gently(&old_oid, 0); - new_cmit = lookup_commit_reference_gently(&b->oid, 0); + old_cmit = lookup_commit_reference_gently(the_repository, + &old_oid, 0); + new_cmit = lookup_commit_reference_gently(the_repository, + &b->oid, 0); if (!old_cmit || !new_cmit) return error("Branch %s is missing commits.", b->name); @@ -1859,7 +1677,7 @@ static void dump_marks_helper(FILE *f, static void dump_marks(void) { - static struct lock_file mark_lock; + struct lock_file mark_lock = LOCK_INIT; FILE *f; if (!export_marks_file || (import_marks_file && !import_marks_file_done)) @@ -1920,7 +1738,8 @@ static void read_marks(void) die("corrupt mark line: %s", line); e = find_object(&oid); if (!e) { - enum object_type type = sha1_object_info(oid.hash, NULL); + enum object_type type = oid_object_info(the_repository, + &oid, NULL); if (type < 0) die("object not found: %s", oid_to_hex(&oid)); e = insert_object(&oid); @@ -1946,14 +1765,11 @@ static int read_next_command(void) } for (;;) { - const char *p; - if (unread_command_buf) { unread_command_buf = 0; } else { struct recent_command *rc; - strbuf_detach(&command_buf, NULL); stdin_eof = strbuf_getline_lf(&command_buf, stdin); if (stdin_eof) return EOF; @@ -1974,20 +1790,12 @@ static int read_next_command(void) free(rc->buf); } - rc->buf = command_buf.buf; + rc->buf = xstrdup(command_buf.buf); rc->prev = cmd_tail; rc->next = cmd_hist.prev; rc->prev->next = rc; cmd_tail = rc; } - if (skip_prefix(command_buf.buf, "get-mark ", &p)) { - parse_get_mark(p); - continue; - } - if (skip_prefix(command_buf.buf, "cat-blob ", &p)) { - parse_cat_blob(p); - continue; - } if (command_buf.buf[0] == '#') continue; return 0; @@ -2012,6 +1820,13 @@ static void parse_mark(void) next_mark = 0; } +static void parse_original_identifier(void) +{ + const char *v; + if (skip_prefix(command_buf.buf, "original-oid ", &v)) + read_next_command(); +} + static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res) { const char *data; @@ -2024,7 +1839,6 @@ static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res) char *term = xstrdup(data); size_t term_len = command_buf.len - (data - command_buf.buf); - strbuf_detach(&command_buf, NULL); for (;;) { if (strbuf_getline_lf(&command_buf, stdin) == EOF) die("EOF in data (terminator '%s' not found)", term); @@ -2154,6 +1968,7 @@ static void parse_new_blob(void) { read_next_command(); parse_mark(); + parse_original_identifier(); parse_and_store_blob(&last_blob, NULL, next_mark); } @@ -2236,7 +2051,9 @@ static uintmax_t do_change_note_fanout( unsigned int i, tmp_hex_oid_len, tmp_fullpath_len; uintmax_t num_notes = 0; struct object_id oid; - char realpath[60]; + /* hex oid + '/' between each pair of hex digits + NUL */ + char realpath[GIT_MAX_HEXSZ + ((GIT_MAX_HEXSZ / 2) - 1) + 1]; + const unsigned hexsz = the_hash_algo->hexsz; if (!root->tree) load_tree(root); @@ -2256,7 +2073,7 @@ static uintmax_t do_change_note_fanout( * of 2 chars. */ if (!e->versions[1].mode || - tmp_hex_oid_len > GIT_SHA1_HEXSZ || + tmp_hex_oid_len > hexsz || e->name->str_len % 2) continue; @@ -2270,7 +2087,7 @@ static uintmax_t do_change_note_fanout( tmp_fullpath_len += e->name->str_len; fullpath[tmp_fullpath_len] = '\0'; - if (tmp_hex_oid_len == GIT_SHA1_HEXSZ && !get_oid_hex(hex_oid, &oid)) { + if (tmp_hex_oid_len == hexsz && !get_oid_hex(hex_oid, &oid)) { /* This is a note entry */ if (fanout == 0xff) { /* Counting mode, no rename */ @@ -2444,13 +2261,21 @@ static void file_change_m(const char *p, struct branch *b) strbuf_addstr(&uq, p); p = uq.buf; } - read_next_command(); - parse_and_store_blob(&last_blob, &oid, 0); + while (read_next_command() != EOF) { + const char *v; + if (skip_prefix(command_buf.buf, "cat-blob ", &v)) + parse_cat_blob(v); + else { + parse_and_store_blob(&last_blob, &oid, 0); + break; + } + } } else { enum object_type expected = S_ISDIR(mode) ? OBJ_TREE: OBJ_BLOB; enum object_type type = oe ? oe->type : - sha1_object_info(oid.hash, NULL); + oid_object_info(the_repository, &oid, + NULL); if (type < 0) die("%s not found: %s", S_ISDIR(mode) ? "Tree" : "Blob", @@ -2540,7 +2365,7 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa struct object_entry *oe; struct branch *s; struct object_id oid, commit_oid; - char path[60]; + char path[GIT_MAX_RAWSZ * 3]; uint16_t inline_data = 0; unsigned char new_fanout; @@ -2590,9 +2415,11 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa oidcpy(&commit_oid, &commit_oe->idx.oid); } else if (!get_oid(p, &commit_oid)) { unsigned long size; - char *buf = read_object_with_reference(commit_oid.hash, - commit_type, &size, commit_oid.hash); - if (!buf || size < 46) + char *buf = read_object_with_reference(the_repository, + &commit_oid, + commit_type, &size, + &commit_oid); + if (!buf || size < the_hash_algo->hexsz + 6) die("Not a valid commit: %s", p); free(buf); } else @@ -2610,7 +2437,8 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa die("Not a blob (actually a %s): %s", type_name(oe->type), command_buf.buf); } else if (!is_null_oid(&oid)) { - enum object_type type = sha1_object_info(oid.hash, NULL); + enum object_type type = oid_object_info(the_repository, &oid, + NULL); if (type < 0) die("Blob not found: %s", command_buf.buf); if (type != OBJ_BLOB) @@ -2642,7 +2470,7 @@ static void file_change_deleteall(struct branch *b) static void parse_from_commit(struct branch *b, char *buf, unsigned long size) { - if (!buf || size < GIT_SHA1_HEXSZ + 6) + if (!buf || size < the_hash_algo->hexsz + 6) die("Not a valid commit: %s", oid_to_hex(&b->oid)); if (memcmp("tree ", buf, 5) || get_oid_hex(buf + 5, &b->branch_tree.versions[1].oid)) @@ -2660,26 +2488,22 @@ static void parse_from_existing(struct branch *b) unsigned long size; char *buf; - buf = read_object_with_reference(b->oid.hash, - commit_type, &size, - b->oid.hash); + buf = read_object_with_reference(the_repository, + &b->oid, commit_type, &size, + &b->oid); parse_from_commit(b, buf, size); free(buf); } } -static int parse_from(struct branch *b) +static int parse_objectish(struct branch *b, const char *objectish) { - const char *from; struct branch *s; struct object_id oid; - if (!skip_prefix(command_buf.buf, "from ", &from)) - return 0; - oidcpy(&oid, &b->branch_tree.versions[1].oid); - s = lookup_branch(from); + s = lookup_branch(objectish); if (b == s) die("Can't create a branch from itself: %s", b->name); else if (s) { @@ -2687,12 +2511,12 @@ static int parse_from(struct branch *b) oidcpy(&b->oid, &s->oid); oidcpy(&b->branch_tree.versions[0].oid, t); oidcpy(&b->branch_tree.versions[1].oid, t); - } else if (*from == ':') { - uintmax_t idnum = parse_mark_ref_eol(from); + } else if (*objectish == ':') { + uintmax_t idnum = parse_mark_ref_eol(objectish); struct object_entry *oe = find_mark(idnum); if (oe->type != OBJ_COMMIT) die("Mark :%" PRIuMAX " not a commit", idnum); - if (oidcmp(&b->oid, &oe->idx.oid)) { + if (!oideq(&b->oid, &oe->idx.oid)) { oidcpy(&b->oid, &oe->idx.oid); if (oe->pack_id != MAX_PACK_ID) { unsigned long size; @@ -2702,15 +2526,15 @@ static int parse_from(struct branch *b) } else parse_from_existing(b); } - } else if (!get_oid(from, &b->oid)) { + } else if (!get_oid(objectish, &b->oid)) { parse_from_existing(b); if (is_null_oid(&b->oid)) b->delete = 1; } else - die("Invalid ref name or SHA1 expression: %s", from); + die("Invalid ref name or SHA1 expression: %s", objectish); - if (b->branch_tree.tree && oidcmp(&oid, &b->branch_tree.versions[1].oid)) { + if (b->branch_tree.tree && !oideq(&oid, &b->branch_tree.versions[1].oid)) { release_tree_content_recursive(b->branch_tree.tree); b->branch_tree.tree = NULL; } @@ -2719,6 +2543,26 @@ static int parse_from(struct branch *b) return 1; } +static int parse_from(struct branch *b) +{ + const char *from; + + if (!skip_prefix(command_buf.buf, "from ", &from)) + return 0; + + return parse_objectish(b, from); +} + +static int parse_objectish_with_prefix(struct branch *b, const char *prefix) +{ + const char *base; + + if (!skip_prefix(command_buf.buf, prefix, &base)) + return 0; + + return parse_objectish(b, base); +} + static struct hash_list *parse_merge(unsigned int *count) { struct hash_list *list = NULL, **tail = &list, *n; @@ -2739,9 +2583,11 @@ static struct hash_list *parse_merge(unsigned int *count) oidcpy(&n->oid, &oe->idx.oid); } else if (!get_oid(from, &n->oid)) { unsigned long size; - char *buf = read_object_with_reference(n->oid.hash, - commit_type, &size, n->oid.hash); - if (!buf || size < 46) + char *buf = read_object_with_reference(the_repository, + &n->oid, + commit_type, + &size, &n->oid); + if (!buf || size < the_hash_algo->hexsz + 6) die("Not a valid commit: %s", from); free(buf); } else @@ -2763,6 +2609,7 @@ static void parse_new_commit(const char *arg) struct branch *b; char *author = NULL; char *committer = NULL; + char *encoding = NULL; struct hash_list *merge_list = NULL; unsigned int merge_count; unsigned char prev_fanout, new_fanout; @@ -2774,6 +2621,7 @@ static void parse_new_commit(const char *arg) read_next_command(); parse_mark(); + parse_original_identifier(); if (skip_prefix(command_buf.buf, "author ", &v)) { author = parse_ident(v); read_next_command(); @@ -2784,6 +2632,10 @@ static void parse_new_commit(const char *arg) } if (!committer) die("Expected committer but didn't get one"); + if (skip_prefix(command_buf.buf, "encoding ", &v)) { + encoding = xstrdup(v); + read_next_command(); + } parse_data(&msg, 0, NULL); read_next_command(); parse_from(b); @@ -2813,6 +2665,8 @@ static void parse_new_commit(const char *arg) file_change_deleteall(b); else if (skip_prefix(command_buf.buf, "ls ", &v)) parse_ls(v, b); + else if (skip_prefix(command_buf.buf, "cat-blob ", &v)) + parse_cat_blob(v); else { unread_command_buf = 1; break; @@ -2845,12 +2699,17 @@ static void parse_new_commit(const char *arg) } strbuf_addf(&new_data, "author %s\n" - "committer %s\n" - "\n", + "committer %s\n", author ? author : committer, committer); + if (encoding) + strbuf_addf(&new_data, + "encoding %s\n", + encoding); + strbuf_addch(&new_data, '\n'); strbuf_addbuf(&new_data, &msg); free(author); free(committer); + free(encoding); if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark)) b->pack_id = pack_id; @@ -2869,7 +2728,7 @@ static void parse_new_tag(const char *arg) enum object_type type; const char *v; - t = pool_alloc(sizeof(struct tag)); + t = mem_pool_alloc(&fi_mem_pool, sizeof(struct tag)); memset(t, 0, sizeof(struct tag)); t->name = pool_strdup(arg); if (last_tag) @@ -2878,6 +2737,7 @@ static void parse_new_tag(const char *arg) first_tag = t; last_tag = t; read_next_command(); + parse_mark(); /* from ... */ if (!skip_prefix(command_buf.buf, "from ", &from)) @@ -2897,7 +2757,7 @@ static void parse_new_tag(const char *arg) } else if (!get_oid(from, &oid)) { struct object_entry *oe = find_object(&oid); if (!oe) { - type = sha1_object_info(oid.hash, NULL); + type = oid_object_info(the_repository, &oid, NULL); if (type < 0) die("Not a valid object: %s", from); } else @@ -2906,6 +2766,9 @@ static void parse_new_tag(const char *arg) die("Invalid ref name or SHA1 expression: %s", from); read_next_command(); + /* original-oid ... */ + parse_original_identifier(); + /* tagger ... */ if (skip_prefix(command_buf.buf, "tagger ", &v)) { tagger = parse_ident(v); @@ -2931,7 +2794,7 @@ static void parse_new_tag(const char *arg) strbuf_addbuf(&new_data, &msg); free(tagger); - if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, 0)) + if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, next_mark)) t->pack_id = MAX_PACK_ID; else t->pack_id = pack_id; @@ -2940,6 +2803,7 @@ static void parse_new_tag(const char *arg) static void parse_reset_branch(const char *arg) { struct branch *b; + const char *tag_name; b = lookup_branch(arg); if (b) { @@ -2955,6 +2819,32 @@ static void parse_reset_branch(const char *arg) b = new_branch(arg); read_next_command(); parse_from(b); + if (b->delete && skip_prefix(b->name, "refs/tags/", &tag_name)) { + /* + * Elsewhere, we call dump_branches() before dump_tags(), + * and dump_branches() will handle ref deletions first, so + * in order to make sure the deletion actually takes effect, + * we need to remove the tag from our list of tags to update. + * + * NEEDSWORK: replace list of tags with hashmap for faster + * deletion? + */ + struct tag *t, *prev = NULL; + for (t = first_tag; t; t = t->next_tag) { + if (!strcmp(t->name, tag_name)) + break; + prev = t; + } + if (t) { + if (prev) + prev->next_tag = t->next_tag; + else + first_tag = t->next_tag; + if (!t->next_tag) + last_tag = prev; + /* There is no mem_pool_free(t) function to call. */ + } + } if (command_buf.len > 0) unread_command_buf = 1; } @@ -2973,7 +2863,7 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid) char *buf; if (!oe || oe->pack_id == MAX_PACK_ID) { - buf = read_sha1_file(oid->hash, &type, &size); + buf = read_object_file(oid, &type, &size); } else { type = oe->type; buf = gfi_unpack_entry(oe, &size); @@ -2996,8 +2886,8 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid) die("Object %s is a %s but a blob was expected.", oid_to_hex(oid), type_name(type)); strbuf_reset(&line); - strbuf_addf(&line, "%s %s %lu\n", oid_to_hex(oid), - type_name(type), size); + strbuf_addf(&line, "%s %s %"PRIuMAX"\n", oid_to_hex(oid), + type_name(type), (uintmax_t)size); cat_blob_write(line.buf, line.len); strbuf_release(&line); cat_blob_write(buf, size); @@ -3024,7 +2914,7 @@ static void parse_get_mark(const char *p) die("Unknown mark: %s", command_buf.buf); xsnprintf(output, sizeof(output), "%s\n", oid_to_hex(&oe->idx.oid)); - cat_blob_write(output, GIT_SHA1_HEXSZ + 1); + cat_blob_write(output, the_hash_algo->hexsz + 1); } static void parse_cat_blob(const char *p) @@ -3054,8 +2944,11 @@ static struct object_entry *dereference(struct object_entry *oe, { unsigned long size; char *buf = NULL; + const unsigned hexsz = the_hash_algo->hexsz; + if (!oe) { - enum object_type type = sha1_object_info(oid->hash, NULL); + enum object_type type = oid_object_info(the_repository, oid, + NULL); if (type < 0) die("object not found: %s", oid_to_hex(oid)); /* cache it! */ @@ -3078,7 +2971,7 @@ static struct object_entry *dereference(struct object_entry *oe, buf = gfi_unpack_entry(oe, &size); } else { enum object_type unused; - buf = read_sha1_file(oid->hash, &unused, &size); + buf = read_object_file(oid, &unused, &size); } if (!buf) die("Can't load object %s", oid_to_hex(oid)); @@ -3086,12 +2979,12 @@ static struct object_entry *dereference(struct object_entry *oe, /* Peel one layer. */ switch (oe->type) { case OBJ_TAG: - if (size < GIT_SHA1_HEXSZ + strlen("object ") || + if (size < hexsz + strlen("object ") || get_oid_hex(buf + strlen("object "), oid)) die("Invalid SHA1 in tag: %s", command_buf.buf); break; case OBJ_COMMIT: - if (size < GIT_SHA1_HEXSZ + strlen("tree ") || + if (size < hexsz + strlen("tree ") || get_oid_hex(buf + strlen("tree "), oid)) die("Invalid SHA1 in commit: %s", command_buf.buf); } @@ -3123,7 +3016,7 @@ static struct object_entry *parse_treeish_dataref(const char **p) return e; } -static void print_ls(int mode, const unsigned char *sha1, const char *path) +static void print_ls(int mode, const unsigned char *hash, const char *path) { static struct strbuf line = STRBUF_INIT; @@ -3143,7 +3036,7 @@ static void print_ls(int mode, const unsigned char *sha1, const char *path) /* mode SP type SP object_name TAB path LF */ strbuf_reset(&line); strbuf_addf(&line, "%06o %s %s\t", - mode & ~NO_DELTA, type, sha1_to_hex(sha1)); + mode & ~NO_DELTA, type, hash_to_hex(hash)); quote_c_style(path, &line, NULL, 0); strbuf_addch(&line, '\n'); } @@ -3218,6 +3111,28 @@ static void parse_progress(void) skip_optional_lf(); } +static void parse_alias(void) +{ + struct object_entry *e; + struct branch b; + + skip_optional_lf(); + read_next_command(); + + /* mark ... */ + parse_mark(); + if (!next_mark) + die(_("Expected 'mark' command, got %s"), command_buf.buf); + + /* to ... */ + memset(&b, 0, sizeof(b)); + if (!parse_objectish_with_prefix(&b, "to ")) + die(_("Expected 'to' command, got %s"), command_buf.buf); + e = find_object(&b.oid); + assert(e); + insert_mark(next_mark, e); +} + static char* make_fast_import_path(const char *path) { if (!relative_marks_paths || is_absolute_path(path)) @@ -3355,6 +3270,8 @@ static int parse_one_feature(const char *feature, int from_stream) } else if (skip_prefix(feature, "export-marks=", &arg)) { check_unsafe_feature(feature, from_stream); option_export_marks(arg); + } else if (!strcmp(feature, "alias")) { + ; /* Don't die - this feature is supported */ } else if (!strcmp(feature, "get-mark")) { ; /* Don't die - this feature is supported */ } else if (!strcmp(feature, "cat-blob")) { @@ -3478,7 +3395,7 @@ int cmd_main(int argc, const char **argv) atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*)); branch_table = xcalloc(branch_table_sz, sizeof(struct branch*)); avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*)); - marks = pool_calloc(1, sizeof(struct mark_set)); + marks = mem_pool_calloc(&fi_mem_pool, 1, sizeof(struct mark_set)); /* * We don't parse most options until after we've seen the set of @@ -3497,12 +3414,11 @@ int cmd_main(int argc, const char **argv) global_argc = argc; global_argv = argv; - rc_free = pool_alloc(cmd_save * sizeof(*rc_free)); + rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free)); for (i = 0; i < (cmd_save - 1); i++) rc_free[i].next = &rc_free[i + 1]; rc_free[cmd_save - 1].next = NULL; - prepare_packed_git(); start_packfile(); set_die_routine(die_nicely); set_checkpoint_signal(); @@ -3510,18 +3426,24 @@ int cmd_main(int argc, const char **argv) const char *v; if (!strcmp("blob", command_buf.buf)) parse_new_blob(); - else if (skip_prefix(command_buf.buf, "ls ", &v)) - parse_ls(v, NULL); else if (skip_prefix(command_buf.buf, "commit ", &v)) parse_new_commit(v); else if (skip_prefix(command_buf.buf, "tag ", &v)) parse_new_tag(v); else if (skip_prefix(command_buf.buf, "reset ", &v)) parse_reset_branch(v); + else if (skip_prefix(command_buf.buf, "ls ", &v)) + parse_ls(v, NULL); + else if (skip_prefix(command_buf.buf, "cat-blob ", &v)) + parse_cat_blob(v); + else if (skip_prefix(command_buf.buf, "get-mark ", &v)) + parse_get_mark(v); else if (!strcmp("checkpoint", command_buf.buf)) parse_checkpoint(); else if (!strcmp("done", command_buf.buf)) break; + else if (!strcmp("alias", command_buf.buf)) + parse_alias(); else if (starts_with(command_buf.buf, "progress ")) parse_progress(); else if (skip_prefix(command_buf.buf, "feature ", &v)) @@ -3572,8 +3494,8 @@ int cmd_main(int argc, const char **argv) fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count); fprintf(stderr, " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count); fprintf(stderr, " atoms: %10u\n", atom_cnt); - fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024); - fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)(total_allocd/1024)); + fprintf(stderr, "Memory total: %10" PRIuMAX " KiB\n", (tree_entry_allocd + fi_mem_pool.pool_alloc + alloc_count*sizeof(struct object_entry))/1024); + fprintf(stderr, " pools: %10lu KiB\n", (unsigned long)((tree_entry_allocd + fi_mem_pool.pool_alloc) /1024)); fprintf(stderr, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024); fprintf(stderr, "---------------------------------------------------------------------\n"); pack_report(); |