| new_commit
| new_tag
| reset_branch
+ | checkpoint
;
new_blob ::= 'blob' lf
new_commit ::= 'commit' sp ref_str lf
mark?
- ('author' sp name '<' email '>' ts tz lf)?
- 'committer' sp name '<' email '>' ts tz lf
+ ('author' sp name '<' email '>' when lf)?
+ 'committer' sp name '<' email '>' when lf
commit_msg
('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
lf;
commit_msg ::= data;
- file_change ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf
- | 'D' sp path_str lf
- ;
- mode ::= '644' | '755';
+ file_change ::= file_clr | file_del | file_obm | file_inm;
+ file_clr ::= 'deleteall' lf;
+ file_del ::= 'D' 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;
new_tag ::= 'tag' sp tag_str lf
'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
- 'tagger' sp name '<' email '>' ts tz lf
+ 'tagger' sp name '<' email '>' when lf
tag_msg;
tag_msg ::= data;
# 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 preceeding or trailing the
- # binary data.
+ # declen does not include the lf preceeding the binary data.
#
- data ::= 'data' sp declen lf
- binary_data
- lf;
+ 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
# stream formatting is: \, " and LF. Otherwise these values
# are UTF8.
#
- ref_str ::= ref | '"' quoted(ref) '"' ;
- sha1exp_str ::= sha1exp | '"' quoted(sha1exp) '"' ;
- tag_str ::= tag | '"' quoted(tag) '"' ;
+ 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;
#include "object.h"
#include "blob.h"
#include "tree.h"
+#include "commit.h"
#include "delta.h"
#include "pack.h"
#include "refs.h"
#include "strbuf.h"
#include "quote.h"
+#define PACK_ID_BITS 16
+#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
+
+#ifndef PRIuMAX
+#define PRIuMAX "llu"
+#endif
+
struct object_entry
{
struct object_entry *next;
- unsigned long offset;
+ uint32_t offset;
unsigned type : TYPE_BITS;
- unsigned pack_id : 16;
+ unsigned pack_id : PACK_ID_BITS;
unsigned char sha1[20];
};
struct mark_set
{
- int shift;
union {
struct object_entry *marked[1024];
struct mark_set *sets[1024];
} data;
+ unsigned int shift;
};
struct last_object
{
void *data;
unsigned long len;
- unsigned long offset;
+ uint32_t offset;
unsigned int depth;
unsigned no_free:1;
};
struct atom_str
{
struct atom_str *next_atom;
- int str_len;
+ unsigned short str_len;
char str_dat[FLEX_ARRAY]; /* more */
};
struct atom_str* name;
struct tree_entry_ms
{
- unsigned int mode;
+ uint16_t mode;
unsigned char sha1[20];
} versions[2];
};
struct branch *table_next_branch;
struct branch *active_next_branch;
const char *name;
- unsigned long last_commit;
struct tree_entry branch_tree;
+ uintmax_t last_commit;
unsigned int pack_id;
unsigned char sha1[20];
};
unsigned char sha1[20];
};
+typedef enum {
+ WHENSPEC_RAW = 1,
+ WHENSPEC_RFC2822,
+ WHENSPEC_NOW,
+} whenspec_type;
+
/* Configured limits on output */
static unsigned long max_depth = 10;
static unsigned long max_packsize = (1LL << 32) - 1;
-static uintmax_t max_objects = -1;
+static int force_update;
/* Stats and misc. counters */
static uintmax_t alloc_count;
static unsigned long object_count;
static unsigned long branch_count;
static unsigned long branch_load_count;
+static int failure;
+static FILE *pack_edges;
/* Memory pools */
static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
static struct tag *last_tag;
/* Input stream parsing */
+static whenspec_type whenspec = WHENSPEC_RAW;
static struct strbuf command_buf;
static uintmax_t next_mark;
static struct dbuf new_data;
-static FILE* branch_log;
static void alloc_objects(unsigned int cnt)
alloc_count += cnt;
}
-static struct object_entry* new_object(unsigned char *sha1)
+static struct object_entry *new_object(unsigned char *sha1)
{
struct object_entry *e;
return e;
}
-static struct object_entry* find_object(unsigned char *sha1)
+static struct object_entry *find_object(unsigned char *sha1)
{
unsigned int h = sha1[0] << 8 | sha1[1];
struct object_entry *e;
return NULL;
}
-static struct object_entry* insert_object(unsigned char *sha1)
+static struct object_entry *insert_object(unsigned char *sha1)
{
unsigned int h = sha1[0] << 8 | sha1[1];
struct object_entry *e = object_table[h];
return r;
}
-static void* pool_alloc(size_t len)
+static void *pool_alloc(size_t len)
{
struct mem_pool *p;
void *r;
return r;
}
-static void* pool_calloc(size_t count, size_t size)
+static void *pool_calloc(size_t count, size_t size)
{
size_t len = count * size;
void *r = pool_alloc(len);
return r;
}
-static char* pool_strdup(const char *s)
+static char *pool_strdup(const char *s)
{
char *r = pool_alloc(strlen(s) + 1);
strcpy(r, s);
s->data.marked[idnum] = oe;
}
-static struct object_entry* find_mark(uintmax_t idnum)
+static struct object_entry *find_mark(uintmax_t idnum)
{
uintmax_t orig_idnum = idnum;
struct mark_set *s = marks;
oe = s->data.marked[idnum];
}
if (!oe)
- die("mark :%ju not declared", orig_idnum);
+ die("mark :%" PRIuMAX " not declared", orig_idnum);
return oe;
}
-static struct atom_str* to_atom(const char *s, size_t len)
+static struct atom_str *to_atom(const char *s, unsigned short len)
{
unsigned int hc = hc_str(s, len) % atom_table_sz;
struct atom_str *c;
return c;
}
-static struct branch* lookup_branch(const char *name)
+static struct branch *lookup_branch(const char *name)
{
unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
struct branch *b;
return NULL;
}
-static struct branch* new_branch(const char *name)
+static struct branch *new_branch(const char *name)
{
unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
struct branch* b = lookup_branch(name);
b->table_next_branch = branch_table[hc];
b->branch_tree.versions[0].mode = S_IFDIR;
b->branch_tree.versions[1].mode = S_IFDIR;
+ b->pack_id = MAX_PACK_ID;
branch_table[hc] = b;
branch_count++;
return b;
return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
}
-static struct tree_content* new_tree_content(unsigned int cnt)
+static struct tree_content *new_tree_content(unsigned int cnt)
{
struct avail_tree_content *f, *l = NULL;
struct tree_content *t;
release_tree_content(t);
}
-static struct tree_content* grow_tree_content(
+static struct tree_content *grow_tree_content(
struct tree_content *t,
int amt)
{
return r;
}
-static struct tree_entry* new_tree_entry()
+static struct tree_entry *new_tree_entry(void)
{
struct tree_entry *e;
avail_tree_entry = e;
}
-static void start_packfile()
+static void start_packfile(void)
{
static char tmpfile[PATH_MAX];
struct packed_git *p;
all_packs[pack_id] = p;
}
-static void fixup_header_footer()
+static void fixup_header_footer(void)
{
+ static const int buf_sz = 128 * 1024;
int pack_fd = pack_data->pack_fd;
SHA_CTX c;
- char hdr[8];
- unsigned long cnt;
+ struct pack_header hdr;
char *buf;
if (lseek(pack_fd, 0, SEEK_SET) != 0)
die("Failed seeking to start: %s", strerror(errno));
-
- SHA1_Init(&c);
- if (read_in_full(pack_fd, hdr, 8) != 8)
+ if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
die("Unable to reread header of %s", pack_data->pack_name);
- SHA1_Update(&c, hdr, 8);
+ if (lseek(pack_fd, 0, SEEK_SET) != 0)
+ die("Failed seeking to start: %s", strerror(errno));
+ hdr.hdr_entries = htonl(object_count);
+ write_or_die(pack_fd, &hdr, sizeof(hdr));
- cnt = htonl(object_count);
- SHA1_Update(&c, &cnt, 4);
- write_or_die(pack_fd, &cnt, 4);
+ SHA1_Init(&c);
+ SHA1_Update(&c, &hdr, sizeof(hdr));
- buf = xmalloc(128 * 1024);
+ buf = xmalloc(buf_sz);
for (;;) {
- size_t n = xread(pack_fd, buf, 128 * 1024);
- if (n <= 0)
+ size_t n = xread(pack_fd, buf, buf_sz);
+ if (!n)
break;
+ if (n < 0)
+ die("Failed to checksum %s", pack_data->pack_name);
SHA1_Update(&c, buf, n);
}
free(buf);
return hashcmp(a->sha1, b->sha1);
}
-static char* create_index()
+static char *create_index(void)
{
static char tmpfile[PATH_MAX];
SHA_CTX ctx;
struct sha1file *f;
struct object_entry **idx, **c, **last, *e;
struct object_entry_pool *o;
- unsigned int array[256];
+ uint32_t array[256];
int i, idx_fd;
/* Build the sorted table of object IDs. */
sha1write(f, array, 256 * sizeof(int));
SHA1_Init(&ctx);
for (c = idx; c != last; c++) {
- unsigned int offset = htonl((*c)->offset);
+ uint32_t offset = htonl((*c)->offset);
sha1write(f, &offset, 4);
sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
SHA1_Update(&ctx, (*c)->sha1, 20);
return tmpfile;
}
-static char* keep_pack(char *curr_index_name)
+static char *keep_pack(char *curr_index_name)
{
static char name[PATH_MAX];
static char *keep_msg = "fast-import";
return name;
}
-static void unkeep_all_packs()
+static void unkeep_all_packs(void)
{
static char name[PATH_MAX];
int k;
}
}
-static void end_packfile()
+static void end_packfile(void)
{
struct packed_git *old_p = pack_data, *new_p;
install_packed_git(new_p);
/* Print the boundary */
- fprintf(stdout, "%s:", new_p->pack_name);
- for (i = 0; i < branch_table_sz; i++) {
- for (b = branch_table[i]; b; b = b->table_next_branch) {
- if (b->pack_id == pack_id)
- fprintf(stdout, " %s", sha1_to_hex(b->sha1));
+ if (pack_edges) {
+ fprintf(pack_edges, "%s:", new_p->pack_name);
+ for (i = 0; i < branch_table_sz; i++) {
+ for (b = branch_table[i]; b; b = b->table_next_branch) {
+ if (b->pack_id == pack_id)
+ fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
+ }
}
+ for (t = first_tag; t; t = t->next_tag) {
+ if (t->pack_id == pack_id)
+ fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
+ }
+ fputc('\n', pack_edges);
+ fflush(pack_edges);
}
- for (t = first_tag; t; t = t->next_tag) {
- if (t->pack_id == pack_id)
- fprintf(stdout, " %s", sha1_to_hex(t->sha1));
- }
- fputc('\n', stdout);
pack_id++;
}
last_blob.depth = 0;
}
-static void checkpoint()
+static void cycle_packfile(void)
{
end_packfile();
start_packfile();
SHA_CTX c;
z_stream s;
- hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
+ hdrlen = sprintf((char*)hdr,"%s %lu", type_names[type],
+ (unsigned long)datlen) + 1;
SHA1_Init(&c);
SHA1_Update(&c, hdr, hdrlen);
SHA1_Update(&c, dat, datlen);
deflateEnd(&s);
/* Determine if we should auto-checkpoint. */
- if ((object_count + 1) > max_objects
- || (object_count + 1) < object_count
- || (pack_size + 60 + s.total_out) > max_packsize
+ if ((pack_size + 60 + s.total_out) > max_packsize
|| (pack_size + 60 + s.total_out) < pack_size) {
/* This new object needs to *not* have the current pack_id. */
e->pack_id = pack_id + 1;
- checkpoint();
+ cycle_packfile();
/* We cannot carry a delta into the new pack. */
if (delta) {
pack_size += s.total_out;
free(out);
- if (delta)
- free(delta);
+ free(delta);
if (last) {
- if (last->data && !last->no_free)
+ if (!last->no_free)
free(last->data);
last->data = dat;
last->offset = e->offset;
return unpack_entry(p, oe->offset, type, sizep);
}
-static const char *get_mode(const char *str, unsigned int *modep)
+static const char *get_mode(const char *str, uint16_t *modep)
{
unsigned char c;
- unsigned int mode = 0;
+ uint16_t mode = 0;
while ((c = *str++) != ' ') {
if (c < '0' || c > '7')
if (!c)
die("Corrupt mode in %s", sha1_to_hex(sha1));
e->versions[0].mode = e->versions[1].mode;
- e->name = to_atom(c, strlen(c));
+ e->name = to_atom(c, (unsigned short)strlen(c));
c += e->name->str_len + 1;
hashcpy(e->versions[0].sha1, (unsigned char*)c);
hashcpy(e->versions[1].sha1, (unsigned char*)c);
struct tree_entry *e = t->entries[i];
if (!e->versions[v].mode)
continue;
- c += sprintf(c, "%o", e->versions[v].mode);
+ c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
*c++ = ' ';
strcpy(c, e->name->str_dat);
c += e->name->str_len + 1;
struct tree_entry *root,
const char *p,
const unsigned char *sha1,
- const unsigned int mode)
+ const uint16_t mode)
{
struct tree_content *t = root->tree;
const char *slash1;
if (t->entry_count == t->entry_capacity)
root->tree = t = grow_tree_content(t, 8);
e = new_tree_entry();
- e->name = to_atom(p, n);
+ e->name = to_atom(p, (unsigned short)n);
e->versions[0].mode = 0;
hashclr(e->versions[0].sha1);
t->entries[t->entry_count++] = e;
return 1;
}
-static void dump_branches()
+static int update_branch(struct branch *b)
{
static const char *msg = "fast-import";
+ struct ref_lock *lock;
+ unsigned char old_sha1[20];
+
+ if (read_ref(b->name, old_sha1))
+ hashclr(old_sha1);
+ lock = lock_any_ref_for_update(b->name, old_sha1);
+ if (!lock)
+ return error("Unable to lock %s", b->name);
+ if (!force_update && !is_null_sha1(old_sha1)) {
+ struct commit *old_cmit, *new_cmit;
+
+ old_cmit = lookup_commit_reference_gently(old_sha1, 0);
+ new_cmit = lookup_commit_reference_gently(b->sha1, 0);
+ if (!old_cmit || !new_cmit) {
+ unlock_ref(lock);
+ return error("Branch %s is missing commits.", b->name);
+ }
+
+ if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
+ unlock_ref(lock);
+ warn("Not updating %s"
+ " (new tip %s does not contain %s)",
+ b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
+ return -1;
+ }
+ }
+ if (write_ref_sha1(lock, b->sha1, msg) < 0)
+ return error("Unable to update %s", b->name);
+ return 0;
+}
+
+static void dump_branches(void)
+{
unsigned int i;
struct branch *b;
- struct ref_lock *lock;
for (i = 0; i < branch_table_sz; i++) {
- for (b = branch_table[i]; b; b = b->table_next_branch) {
- lock = lock_any_ref_for_update(b->name, NULL);
- if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
- die("Can't write %s", b->name);
- }
+ for (b = branch_table[i]; b; b = b->table_next_branch)
+ failure |= update_branch(b);
}
}
-static void dump_tags()
+static void dump_tags(void)
{
static const char *msg = "fast-import";
struct tag *t;
struct ref_lock *lock;
- char path[PATH_MAX];
+ char ref_name[PATH_MAX];
for (t = first_tag; t; t = t->next_tag) {
- sprintf(path, "refs/tags/%s", t->name);
- lock = lock_any_ref_for_update(path, NULL);
+ sprintf(ref_name, "tags/%s", t->name);
+ lock = lock_ref_sha1(ref_name, NULL);
if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
- die("Can't write %s", path);
+ failure |= error("Unable to update %s", ref_name);
}
}
} else {
for (k = 0; k < 1024; k++) {
if (m->data.marked[k])
- fprintf(f, ":%ju %s\n", base + k,
+ fprintf(f, ":%" PRIuMAX " %s\n", base + k,
sha1_to_hex(m->data.marked[k]->sha1));
}
}
}
-static void dump_marks()
+static void dump_marks(void)
{
if (mark_file)
{
FILE *f = fopen(mark_file, "w");
- dump_marks_helper(f, 0, marks);
- fclose(f);
+ if (f) {
+ dump_marks_helper(f, 0, marks);
+ fclose(f);
+ } else
+ failure |= error("Unable to write marks file %s: %s",
+ mark_file, strerror(errno));
}
}
-static void read_next_command()
+static void read_next_command(void)
{
read_line(&command_buf, stdin, '\n');
}
-static void cmd_mark()
+static void cmd_mark(void)
{
if (!strncmp("mark :", command_buf.buf, 6)) {
next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
next_mark = 0;
}
-static void* cmd_data (size_t *size)
+static void *cmd_data (size_t *size)
{
- size_t n = 0;
- void *buffer;
size_t length;
+ char *buffer;
if (strncmp("data ", command_buf.buf, 5))
die("Expected 'data n' command, found: %s", command_buf.buf);
- length = strtoul(command_buf.buf + 5, NULL, 10);
- buffer = xmalloc(length);
-
- while (n < length) {
- size_t s = fread((char*)buffer + n, 1, length - n, stdin);
- if (!s && feof(stdin))
- die("EOF in data (%lu bytes remaining)", length - n);
- n += s;
+ if (!strncmp("<<", command_buf.buf + 5, 2)) {
+ char *term = xstrdup(command_buf.buf + 5 + 2);
+ size_t sz = 8192, term_len = command_buf.len - 5 - 2;
+ length = 0;
+ buffer = xmalloc(sz);
+ for (;;) {
+ read_next_command();
+ if (command_buf.eof)
+ die("EOF in data (terminator '%s' not found)", term);
+ if (term_len == command_buf.len
+ && !strcmp(term, command_buf.buf))
+ break;
+ if (sz < (length + command_buf.len)) {
+ sz = sz * 3 / 2 + 16;
+ if (sz < (length + command_buf.len))
+ sz = length + command_buf.len;
+ buffer = xrealloc(buffer, sz);
+ }
+ memcpy(buffer + length,
+ command_buf.buf,
+ command_buf.len - 1);
+ length += command_buf.len - 1;
+ buffer[length++] = '\n';
+ }
+ free(term);
+ }
+ else {
+ size_t n = 0;
+ length = strtoul(command_buf.buf + 5, NULL, 10);
+ buffer = xmalloc(length);
+ while (n < length) {
+ size_t s = fread(buffer + n, 1, length - n, stdin);
+ if (!s && feof(stdin))
+ die("EOF in data (%lu bytes remaining)",
+ (unsigned long)(length - n));
+ n += s;
+ }
}
if (fgetc(stdin) != '\n')
return buffer;
}
-static void cmd_new_blob()
+static int validate_raw_date(const char *src, char *result, int maxlen)
+{
+ const char *orig_src = src;
+ char *endp, sign;
+
+ strtoul(src, &endp, 10);
+ if (endp == src || *endp != ' ')
+ return -1;
+
+ src = endp + 1;
+ if (*src != '-' && *src != '+')
+ return -1;
+ sign = *src;
+
+ strtoul(src + 1, &endp, 10);
+ if (endp == src || *endp || (endp - orig_src) >= maxlen)
+ return -1;
+
+ strcpy(result, orig_src);
+ return 0;
+}
+
+static char *parse_ident(const char *buf)
+{
+ const char *gt;
+ size_t name_len;
+ char *ident;
+
+ gt = strrchr(buf, '>');
+ if (!gt)
+ die("Missing > in ident string: %s", buf);
+ gt++;
+ if (*gt != ' ')
+ die("Missing space after > in ident string: %s", buf);
+ gt++;
+ name_len = gt - buf;
+ ident = xmalloc(name_len + 24);
+ strncpy(ident, buf, name_len);
+
+ switch (whenspec) {
+ case WHENSPEC_RAW:
+ if (validate_raw_date(gt, ident + name_len, 24) < 0)
+ die("Invalid raw date \"%s\" in ident: %s", gt, buf);
+ break;
+ case WHENSPEC_RFC2822:
+ if (parse_date(gt, ident + name_len, 24) < 0)
+ die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
+ break;
+ case WHENSPEC_NOW:
+ if (strcmp("now", gt))
+ die("Date in ident must be 'now': %s", buf);
+ datestamp(ident + name_len, 24);
+ break;
+ }
+
+ return ident;
+}
+
+static void cmd_new_blob(void)
{
size_t l;
void *d;
free(d);
}
-static void unload_one_branch()
+static void unload_one_branch(void)
{
while (cur_active_branches
&& cur_active_branches >= max_active_branches) {
const char *p = command_buf.buf + 2;
char *p_uq;
const char *endp;
- struct object_entry *oe;
+ struct object_entry *oe = oe;
unsigned char sha1[20];
- unsigned int mode;
+ uint16_t mode, inline_data = 0;
char type[20];
p = get_mode(p, &mode);
oe = find_mark(strtoumax(p + 1, &x, 10));
hashcpy(sha1, oe->sha1);
p = x;
+ } else if (!strncmp("inline", p, 6)) {
+ inline_data = 1;
+ p += 6;
} else {
if (get_sha1_hex(p, sha1))
die("Invalid SHA1: %s", command_buf.buf);
p = p_uq;
}
- if (oe) {
+ if (inline_data) {
+ size_t l;
+ void *d;
+ if (!p_uq)
+ p = p_uq = xstrdup(p);
+ read_next_command();
+ d = cmd_data(&l);
+ if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
+ free(d);
+ } else if (oe) {
if (oe->type != OBJ_BLOB)
die("Not a blob (actually a %s): %s",
command_buf.buf, type_names[oe->type]);
}
tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
-
- if (p_uq)
- free(p_uq);
+ free(p_uq);
}
static void file_change_d(struct branch *b)
p = p_uq;
}
tree_content_remove(&b->branch_tree, p);
- if (p_uq)
- free(p_uq);
+ free(p_uq);
+}
+
+static void file_change_deleteall(struct branch *b)
+{
+ release_tree_content_recursive(b->branch_tree.tree);
+ hashclr(b->branch_tree.versions[0].sha1);
+ hashclr(b->branch_tree.versions[1].sha1);
+ load_tree(&b->branch_tree);
}
static void cmd_from(struct branch *b)
{
- const char *from, *endp;
- char *str_uq;
+ const char *from;
struct branch *s;
if (strncmp("from ", command_buf.buf, 5))
return;
- if (b->last_commit)
- die("Can't reinitailize branch %s", b->name);
-
- from = strchr(command_buf.buf, ' ') + 1;
- str_uq = unquote_c_style(from, &endp);
- if (str_uq) {
- if (*endp)
- die("Garbage after string in: %s", command_buf.buf);
- from = str_uq;
+ if (b->branch_tree.tree) {
+ release_tree_content_recursive(b->branch_tree.tree);
+ b->branch_tree.tree = NULL;
}
+ from = strchr(command_buf.buf, ' ') + 1;
s = lookup_branch(from);
if (b == s)
die("Can't create a branch from itself: %s", b->name);
unsigned long size;
char *buf;
if (oe->type != OBJ_COMMIT)
- die("Mark :%ju not a commit", idnum);
+ die("Mark :%" PRIuMAX " not a commit", idnum);
hashcpy(b->sha1, oe->sha1);
buf = gfi_unpack_entry(oe, &size);
if (!buf || size < 46)
read_next_command();
}
-static struct hash_list* cmd_merge(unsigned int *count)
+static struct hash_list *cmd_merge(unsigned int *count)
{
- struct hash_list *list = NULL, *n, *e;
- const char *from, *endp;
- char *str_uq;
+ struct hash_list *list = NULL, *n, *e = e;
+ const char *from;
struct branch *s;
*count = 0;
while (!strncmp("merge ", command_buf.buf, 6)) {
from = strchr(command_buf.buf, ' ') + 1;
- str_uq = unquote_c_style(from, &endp);
- if (str_uq) {
- if (*endp)
- die("Garbage after string in: %s", command_buf.buf);
- from = str_uq;
- }
-
n = xmalloc(sizeof(*n));
s = lookup_branch(from);
if (s)
uintmax_t idnum = strtoumax(from + 1, NULL, 10);
struct object_entry *oe = find_mark(idnum);
if (oe->type != OBJ_COMMIT)
- die("Mark :%ju not a commit", idnum);
+ die("Mark :%" PRIuMAX " not a commit", idnum);
hashcpy(n->sha1, oe->sha1);
} else if (get_sha1(from, n->sha1))
die("Invalid ref name or SHA1 expression: %s", from);
else
list = n;
e = n;
- *count++;
+ (*count)++;
read_next_command();
}
return list;
}
-static void cmd_new_commit()
+static void cmd_new_commit(void)
{
struct branch *b;
void *msg;
size_t msglen;
- char *str_uq;
- const char *endp;
char *sp;
char *author = NULL;
char *committer = NULL;
/* Obtain the branch name from the rest of our command */
sp = strchr(command_buf.buf, ' ') + 1;
- str_uq = unquote_c_style(sp, &endp);
- if (str_uq) {
- if (*endp)
- die("Garbage after ref in: %s", command_buf.buf);
- sp = str_uq;
- }
b = lookup_branch(sp);
if (!b)
b = new_branch(sp);
- if (str_uq)
- free(str_uq);
read_next_command();
cmd_mark();
if (!strncmp("author ", command_buf.buf, 7)) {
- author = strdup(command_buf.buf);
+ author = parse_ident(command_buf.buf + 7);
read_next_command();
}
if (!strncmp("committer ", command_buf.buf, 10)) {
- committer = strdup(command_buf.buf);
+ committer = parse_ident(command_buf.buf + 10);
read_next_command();
}
if (!committer)
file_change_m(b);
else if (!strncmp("D ", command_buf.buf, 2))
file_change_d(b);
+ else if (!strcmp("deleteall", command_buf.buf))
+ file_change_deleteall(b);
else
die("Unsupported file_change: %s", command_buf.buf);
read_next_command();
store_tree(&b->branch_tree);
hashcpy(b->branch_tree.versions[0].sha1,
b->branch_tree.versions[1].sha1);
- size_dbuf(&new_data, 97 + msglen
+ size_dbuf(&new_data, 114 + msglen
+ merge_count * 49
+ (author
? strlen(author) + strlen(committer)
free(merge_list);
merge_list = next;
}
- if (author)
- sp += sprintf(sp, "%s\n", author);
- else
- sp += sprintf(sp, "author %s\n", committer + 10);
- sp += sprintf(sp, "%s\n\n", committer);
+ sp += sprintf(sp, "author %s\n", author ? author : committer);
+ sp += sprintf(sp, "committer %s\n", committer);
+ *sp++ = '\n';
memcpy(sp, msg, msglen);
sp += msglen;
- if (author)
- free(author);
+ free(author);
free(committer);
free(msg);
- store_object(OBJ_COMMIT,
+ if (!store_object(OBJ_COMMIT,
new_data.buffer, sp - (char*)new_data.buffer,
- NULL, b->sha1, next_mark);
+ NULL, b->sha1, next_mark))
+ b->pack_id = pack_id;
b->last_commit = object_count_by_type[OBJ_COMMIT];
- b->pack_id = pack_id;
-
- if (branch_log) {
- int need_dq = quote_c_style(b->name, NULL, NULL, 0);
- fprintf(branch_log, "commit ");
- if (need_dq) {
- fputc('"', branch_log);
- quote_c_style(b->name, NULL, branch_log, 0);
- fputc('"', branch_log);
- } else
- fprintf(branch_log, "%s", b->name);
- fprintf(branch_log," :%ju %s\n",next_mark,sha1_to_hex(b->sha1));
- }
}
-static void cmd_new_tag()
+static void cmd_new_tag(void)
{
- char *str_uq;
- const char *endp;
char *sp;
const char *from;
char *tagger;
/* Obtain the new tag name from the rest of our command */
sp = strchr(command_buf.buf, ' ') + 1;
- str_uq = unquote_c_style(sp, &endp);
- if (str_uq) {
- if (*endp)
- die("Garbage after tag name in: %s", command_buf.buf);
- sp = str_uq;
- }
t = pool_alloc(sizeof(struct tag));
t->next_tag = NULL;
t->name = pool_strdup(sp);
else
first_tag = t;
last_tag = t;
- if (str_uq)
- free(str_uq);
read_next_command();
/* from ... */
if (strncmp("from ", command_buf.buf, 5))
die("Expected from command, got %s", command_buf.buf);
-
from = strchr(command_buf.buf, ' ') + 1;
- str_uq = unquote_c_style(from, &endp);
- if (str_uq) {
- if (*endp)
- die("Garbage after string in: %s", command_buf.buf);
- from = str_uq;
- }
-
s = lookup_branch(from);
if (s) {
hashcpy(sha1, s->sha1);
} else if (*from == ':') {
+ struct object_entry *oe;
from_mark = strtoumax(from + 1, NULL, 10);
- struct object_entry *oe = find_mark(from_mark);
+ oe = find_mark(from_mark);
if (oe->type != OBJ_COMMIT)
- die("Mark :%ju not a commit", from_mark);
+ die("Mark :%" PRIuMAX " not a commit", from_mark);
hashcpy(sha1, oe->sha1);
} else if (!get_sha1(from, sha1)) {
unsigned long size;
free(buf);
} else
die("Invalid ref name or SHA1 expression: %s", from);
-
- if (str_uq)
- free(str_uq);
read_next_command();
/* tagger ... */
if (strncmp("tagger ", command_buf.buf, 7))
die("Expected tagger command, got %s", command_buf.buf);
- tagger = strdup(command_buf.buf);
+ tagger = parse_ident(command_buf.buf + 7);
/* tag payload/message */
read_next_command();
sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
sp += sprintf(sp, "tag %s\n", t->name);
- sp += sprintf(sp, "%s\n\n", tagger);
+ sp += sprintf(sp, "tagger %s\n", tagger);
+ *sp++ = '\n';
memcpy(sp, msg, msglen);
sp += msglen;
free(tagger);
free(msg);
- store_object(OBJ_TAG, new_data.buffer, sp - (char*)new_data.buffer,
- NULL, t->sha1, 0);
- t->pack_id = pack_id;
-
- if (branch_log) {
- int need_dq = quote_c_style(t->name, NULL, NULL, 0);
- fprintf(branch_log, "tag ");
- if (need_dq) {
- fputc('"', branch_log);
- quote_c_style(t->name, NULL, branch_log, 0);
- fputc('"', branch_log);
- } else
- fprintf(branch_log, "%s", t->name);
- fprintf(branch_log," :%ju %s\n",from_mark,sha1_to_hex(t->sha1));
- }
+ if (store_object(OBJ_TAG, new_data.buffer,
+ sp - (char*)new_data.buffer,
+ NULL, t->sha1, 0))
+ t->pack_id = MAX_PACK_ID;
+ else
+ t->pack_id = pack_id;
}
-static void cmd_reset_branch()
+static void cmd_reset_branch(void)
{
struct branch *b;
- char *str_uq;
- const char *endp;
char *sp;
/* Obtain the branch name from the rest of our command */
sp = strchr(command_buf.buf, ' ') + 1;
- str_uq = unquote_c_style(sp, &endp);
- if (str_uq) {
- if (*endp)
- die("Garbage after ref in: %s", command_buf.buf);
- sp = str_uq;
- }
b = lookup_branch(sp);
if (b) {
- b->last_commit = 0;
+ hashclr(b->sha1);
+ hashclr(b->branch_tree.versions[0].sha1);
+ hashclr(b->branch_tree.versions[1].sha1);
if (b->branch_tree.tree) {
release_tree_content_recursive(b->branch_tree.tree);
b->branch_tree.tree = NULL;
}
else
b = new_branch(sp);
- if (str_uq)
- free(str_uq);
read_next_command();
cmd_from(b);
}
-static void cmd_checkpoint()
+static void cmd_checkpoint(void)
{
- if (object_count)
- checkpoint();
+ if (object_count) {
+ cycle_packfile();
+ dump_branches();
+ dump_tags();
+ dump_marks();
+ }
read_next_command();
}
static const char fast_import_usage[] =
-"git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
+"git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
int main(int argc, const char **argv)
{
- int i;
- uintmax_t est_obj_cnt = object_entry_alloc;
- uintmax_t total_count, duplicate_count;
+ int i, show_stats = 1;
- setup_ident();
git_config(git_default_config);
for (i = 1; i < argc; i++) {
if (*a != '-' || !strcmp(a, "--"))
break;
- else if (!strncmp(a, "--objects=", 10))
- est_obj_cnt = strtoumax(a + 10, NULL, 0);
- else if (!strncmp(a, "--max-objects-per-pack=", 23))
- max_objects = strtoumax(a + 23, NULL, 0);
+ else if (!strncmp(a, "--date-format=", 14)) {
+ const char *fmt = a + 14;
+ if (!strcmp(fmt, "raw"))
+ whenspec = WHENSPEC_RAW;
+ else if (!strcmp(fmt, "rfc2822"))
+ whenspec = WHENSPEC_RFC2822;
+ else if (!strcmp(fmt, "now"))
+ whenspec = WHENSPEC_NOW;
+ else
+ die("unknown --date-format argument %s", fmt);
+ }
else if (!strncmp(a, "--max-pack-size=", 16))
max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
else if (!strncmp(a, "--depth=", 8))
max_active_branches = strtoul(a + 18, NULL, 0);
else if (!strncmp(a, "--export-marks=", 15))
mark_file = a + 15;
- else if (!strncmp(a, "--branch-log=", 13)) {
- branch_log = fopen(a + 13, "w");
- if (!branch_log)
- die("Can't create %s: %s", a + 13, strerror(errno));
- }
+ else if (!strncmp(a, "--export-pack-edges=", 20)) {
+ if (pack_edges)
+ fclose(pack_edges);
+ pack_edges = fopen(a + 20, "a");
+ if (!pack_edges)
+ die("Cannot open %s: %s", a + 20, strerror(errno));
+ } else if (!strcmp(a, "--force"))
+ force_update = 1;
+ else if (!strcmp(a, "--quiet"))
+ show_stats = 0;
+ else if (!strcmp(a, "--stats"))
+ show_stats = 1;
else
die("unknown option %s", a);
}
if (i != argc)
usage(fast_import_usage);
- alloc_objects(est_obj_cnt);
+ alloc_objects(object_entry_alloc);
strbuf_init(&command_buf);
atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
dump_tags();
unkeep_all_packs();
dump_marks();
- if (branch_log)
- fclose(branch_log);
-
- total_count = 0;
- for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
- total_count += object_count_by_type[i];
- duplicate_count = 0;
- for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
- duplicate_count += duplicate_count_by_type[i];
-
- fprintf(stderr, "%s statistics:\n", argv[0]);
- fprintf(stderr, "---------------------------------------------------------------------\n");
- fprintf(stderr, "Alloc'd objects: %10ju (%10ju overflow )\n", alloc_count, alloc_count - est_obj_cnt);
- fprintf(stderr, "Total objects: %10ju (%10ju duplicates )\n", total_count, duplicate_count);
- fprintf(stderr, " blobs : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
- fprintf(stderr, " trees : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
- fprintf(stderr, " commits: %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
- fprintf(stderr, " tags : %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
- fprintf(stderr, "Total branches: %10lu (%10lu loads )\n", branch_count, branch_load_count);
- fprintf(stderr, " marks: %10ju (%10ju unique )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
- fprintf(stderr, " atoms: %10u\n", atom_cnt);
- fprintf(stderr, "Memory total: %10ju KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
- fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
- fprintf(stderr, " objects: %10ju KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
- fprintf(stderr, "---------------------------------------------------------------------\n");
- pack_report();
- fprintf(stderr, "---------------------------------------------------------------------\n");
- fprintf(stderr, "\n");
- return 0;
+ if (pack_edges)
+ fclose(pack_edges);
+
+ if (show_stats) {
+ uintmax_t total_count = 0, duplicate_count = 0;
+ for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
+ total_count += object_count_by_type[i];
+ for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
+ duplicate_count += duplicate_count_by_type[i];
+
+ fprintf(stderr, "%s statistics:\n", argv[0]);
+ fprintf(stderr, "---------------------------------------------------------------------\n");
+ fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
+ fprintf(stderr, "Total objects: %10" PRIuMAX " (%10" PRIuMAX " duplicates )\n", total_count, duplicate_count);
+ fprintf(stderr, " blobs : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
+ fprintf(stderr, " trees : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
+ fprintf(stderr, " commits: %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
+ fprintf(stderr, " tags : %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
+ 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, " objects: %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
+ fprintf(stderr, "---------------------------------------------------------------------\n");
+ pack_report();
+ fprintf(stderr, "---------------------------------------------------------------------\n");
+ fprintf(stderr, "\n");
+ }
+
+ return failure ? 1 : 0;
}