lf;
commit_msg ::= data;
- file_change ::= file_clr | file_del | file_obm | file_inm;
+ file_change ::= file_clr | file_del | file_rnm | 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_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
data;
all_packs[pack_id] = p;
}
-static void fixup_header_footer(void)
-{
- static const int buf_sz = 128 * 1024;
- int pack_fd = pack_data->pack_fd;
- SHA_CTX c;
- struct pack_header hdr;
- char *buf;
-
- if (lseek(pack_fd, 0, SEEK_SET) != 0)
- die("Failed seeking to start: %s", strerror(errno));
- if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
- die("Unable to reread header of %s", pack_data->pack_name);
- 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));
-
- SHA1_Init(&c);
- SHA1_Update(&c, &hdr, sizeof(hdr));
-
- buf = xmalloc(buf_sz);
- for (;;) {
- 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);
-
- SHA1_Final(pack_data->sha1, &c);
- write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
- close(pack_fd);
-}
-
static int oecmp (const void *a_, const void *b_)
{
struct object_entry *a = *((struct object_entry**)a_);
struct branch *b;
struct tag *t;
- fixup_header_footer();
+ fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
+ pack_data->pack_name, object_count);
+ close(pack_data->pack_fd);
idx_name = keep_pack(create_index());
/* Register the packfile with core git's machinary. */
return;
myoe = find_object(sha1);
- if (myoe) {
+ if (myoe && myoe->pack_id != MAX_PACK_ID) {
if (myoe->type != OBJ_TREE)
die("Not a tree: %s", sha1_to_hex(sha1));
t->delta_depth = 0;
|| le->pack_id != pack_id) {
lo.data = NULL;
lo.depth = 0;
+ lo.no_free = 0;
} else {
mktree(t, 0, &lo.len, &old_tree);
lo.data = old_tree.buffer;
struct tree_entry *root,
const char *p,
const unsigned char *sha1,
- const uint16_t mode)
+ const uint16_t mode,
+ struct tree_content *subtree)
{
struct tree_content *t = root->tree;
const char *slash1;
n = slash1 - p;
else
n = strlen(p);
+ if (!n)
+ die("Empty path component found in input");
+ if (!slash1 && !S_ISDIR(mode) && subtree)
+ die("Non-directories cannot have subtrees");
for (i = 0; i < t->entry_count; i++) {
e = t->entries[i];
if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
if (!slash1) {
- if (e->versions[1].mode == mode
+ if (!S_ISDIR(mode)
+ && e->versions[1].mode == mode
&& !hashcmp(e->versions[1].sha1, sha1))
return 0;
e->versions[1].mode = mode;
hashcpy(e->versions[1].sha1, sha1);
- if (e->tree) {
+ if (e->tree)
release_tree_content_recursive(e->tree);
- e->tree = NULL;
- }
+ e->tree = subtree;
hashclr(root->versions[1].sha1);
return 1;
}
}
if (!e->tree)
load_tree(e);
- if (tree_content_set(e, slash1 + 1, sha1, mode)) {
+ if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
hashclr(root->versions[1].sha1);
return 1;
}
if (slash1) {
e->tree = new_tree_content(8);
e->versions[1].mode = S_IFDIR;
- tree_content_set(e, slash1 + 1, sha1, mode);
+ tree_content_set(e, slash1 + 1, sha1, mode, subtree);
} else {
- e->tree = NULL;
+ e->tree = subtree;
e->versions[1].mode = mode;
hashcpy(e->versions[1].sha1, sha1);
}
return 1;
}
-static int tree_content_remove(struct tree_entry *root, const char *p)
+static int tree_content_remove(
+ struct tree_entry *root,
+ const char *p,
+ struct tree_entry *backup_leaf)
{
struct tree_content *t = root->tree;
const char *slash1;
goto del_entry;
if (!e->tree)
load_tree(e);
- if (tree_content_remove(e, slash1 + 1)) {
+ if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
for (n = 0; n < e->tree->entry_count; n++) {
if (e->tree->entries[n]->versions[1].mode) {
hashclr(root->versions[1].sha1);
return 1;
}
}
+ backup_leaf = NULL;
goto del_entry;
}
return 0;
return 0;
del_entry:
- if (e->tree) {
+ if (backup_leaf)
+ memcpy(backup_leaf, e, sizeof(*backup_leaf));
+ else if (e->tree)
release_tree_content_recursive(e->tree);
- e->tree = NULL;
- }
+ e->tree = NULL;
e->versions[1].mode = 0;
hashclr(e->versions[1].sha1);
hashclr(root->versions[1].sha1);
if (read_ref(b->name, old_sha1))
hashclr(old_sha1);
- lock = lock_any_ref_for_update(b->name, old_sha1);
+ lock = lock_any_ref_for_update(b->name, old_sha1, 0);
if (!lock)
return error("Unable to lock %s", b->name);
if (!force_update && !is_null_sha1(old_sha1)) {
typename(type), command_buf.buf);
}
- tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
+ tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
free(p_uq);
}
die("Garbage after path in: %s", command_buf.buf);
p = p_uq;
}
- tree_content_remove(&b->branch_tree, p);
+ tree_content_remove(&b->branch_tree, p, NULL);
free(p_uq);
}
+static void file_change_r(struct branch *b)
+{
+ const char *s, *d;
+ char *s_uq, *d_uq;
+ const char *endp;
+ struct tree_entry leaf;
+
+ s = command_buf.buf + 2;
+ s_uq = unquote_c_style(s, &endp);
+ if (s_uq) {
+ if (*endp != ' ')
+ die("Missing space after source: %s", command_buf.buf);
+ }
+ else {
+ endp = strchr(s, ' ');
+ if (!endp)
+ die("Missing space after source: %s", command_buf.buf);
+ s_uq = xmalloc(endp - s + 1);
+ memcpy(s_uq, s, endp - s);
+ s_uq[endp - s] = 0;
+ }
+ s = s_uq;
+
+ endp++;
+ if (!*endp)
+ die("Missing dest: %s", command_buf.buf);
+
+ d = endp;
+ d_uq = unquote_c_style(d, &endp);
+ if (d_uq) {
+ if (*endp)
+ die("Garbage after dest in: %s", command_buf.buf);
+ d = d_uq;
+ }
+
+ memset(&leaf, 0, sizeof(leaf));
+ tree_content_remove(&b->branch_tree, s, &leaf);
+ if (!leaf.versions[1].mode)
+ die("Path %s not in branch", s);
+ tree_content_set(&b->branch_tree, d,
+ leaf.versions[1].sha1,
+ leaf.versions[1].mode,
+ leaf.tree);
+
+ free(s_uq);
+ free(d_uq);
+}
+
static void file_change_deleteall(struct branch *b)
{
release_tree_content_recursive(b->branch_tree.tree);
load_tree(&b->branch_tree);
}
+static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
+{
+ if (!buf || size < 46)
+ die("Not a valid commit: %s", sha1_to_hex(b->sha1));
+ if (memcmp("tree ", buf, 5)
+ || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
+ die("The commit %s is corrupt", sha1_to_hex(b->sha1));
+ hashcpy(b->branch_tree.versions[0].sha1,
+ b->branch_tree.versions[1].sha1);
+}
+
+static void cmd_from_existing(struct branch *b)
+{
+ if (is_null_sha1(b->sha1)) {
+ hashclr(b->branch_tree.versions[0].sha1);
+ hashclr(b->branch_tree.versions[1].sha1);
+ } else {
+ unsigned long size;
+ char *buf;
+
+ buf = read_object_with_reference(b->sha1,
+ commit_type, &size, b->sha1);
+ cmd_from_commit(b, buf, size);
+ free(buf);
+ }
+}
+
static void cmd_from(struct branch *b)
{
const char *from;
} else if (*from == ':') {
uintmax_t idnum = strtoumax(from + 1, NULL, 10);
struct object_entry *oe = find_mark(idnum);
- unsigned long size;
- char *buf;
if (oe->type != OBJ_COMMIT)
die("Mark :%" PRIuMAX " not a commit", idnum);
hashcpy(b->sha1, oe->sha1);
- buf = gfi_unpack_entry(oe, &size);
- if (!buf || size < 46)
- die("Not a valid commit: %s", from);
- if (memcmp("tree ", buf, 5)
- || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
- die("The commit %s is corrupt", sha1_to_hex(b->sha1));
- free(buf);
- hashcpy(b->branch_tree.versions[0].sha1,
- b->branch_tree.versions[1].sha1);
- } else if (!get_sha1(from, b->sha1)) {
- if (is_null_sha1(b->sha1)) {
- hashclr(b->branch_tree.versions[0].sha1);
- hashclr(b->branch_tree.versions[1].sha1);
- } else {
+ if (oe->pack_id != MAX_PACK_ID) {
unsigned long size;
- char *buf;
-
- buf = read_object_with_reference(b->sha1,
- commit_type, &size, b->sha1);
- if (!buf || size < 46)
- die("Not a valid commit: %s", from);
- if (memcmp("tree ", buf, 5)
- || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
- die("The commit %s is corrupt", sha1_to_hex(b->sha1));
+ char *buf = gfi_unpack_entry(oe, &size);
+ cmd_from_commit(b, buf, size);
free(buf);
- hashcpy(b->branch_tree.versions[0].sha1,
- b->branch_tree.versions[1].sha1);
- }
- } else
+ } else
+ cmd_from_existing(b);
+ } else if (!get_sha1(from, b->sha1))
+ cmd_from_existing(b);
+ else
die("Invalid ref name or SHA1 expression: %s", from);
read_next_command();
file_change_m(b);
else if (!prefixcmp(command_buf.buf, "D "))
file_change_d(b);
+ else if (!prefixcmp(command_buf.buf, "R "))
+ file_change_r(b);
else if (!strcmp("deleteall", command_buf.buf))
file_change_deleteall(b);
else