1
0
mirror of https://github.com/git/git.git synced 2024-09-28 16:13:01 +02:00
git/builtin-mktree.c
Junio C Hamano 1c64e79a62 mktree --missing: allow missing objects
We need to allow input lines that point at objects that we do not
have when dealing with submodule entries anyway.  This adds an explicit
option to allow missing objects of other types, to be consistent with
the use of --info-only option to the update-index command and --missing-ok
option to the write-tree command.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
2009-05-10 17:31:01 -07:00

142 lines
3.4 KiB
C

/*
* GIT - the stupid content tracker
*
* Copyright (c) Junio C Hamano, 2006, 2009
*/
#include "builtin.h"
#include "quote.h"
#include "tree.h"
#include "parse-options.h"
static struct treeent {
unsigned mode;
unsigned char sha1[20];
int len;
char name[FLEX_ARRAY];
} **entries;
static int alloc, used;
static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
{
struct treeent *ent;
int len = strlen(path);
if (strchr(path, '/'))
die("path %s contains slash", path);
if (alloc <= used) {
alloc = alloc_nr(used);
entries = xrealloc(entries, sizeof(*entries) * alloc);
}
ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
ent->mode = mode;
ent->len = len;
hashcpy(ent->sha1, sha1);
memcpy(ent->name, path, len+1);
}
static int ent_compare(const void *a_, const void *b_)
{
struct treeent *a = *(struct treeent **)a_;
struct treeent *b = *(struct treeent **)b_;
return base_name_compare(a->name, a->len, a->mode,
b->name, b->len, b->mode);
}
static void write_tree(unsigned char *sha1)
{
struct strbuf buf;
size_t size;
int i;
qsort(entries, used, sizeof(*entries), ent_compare);
for (size = i = 0; i < used; i++)
size += 32 + entries[i]->len;
strbuf_init(&buf, size);
for (i = 0; i < used; i++) {
struct treeent *ent = entries[i];
strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
strbuf_add(&buf, ent->sha1, 20);
}
write_sha1_file(buf.buf, buf.len, tree_type, sha1);
}
static const char *mktree_usage[] = {
"git mktree [-z]",
NULL
};
static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing)
{
char *ptr, *ntr;
unsigned mode;
enum object_type type;
char *path;
unsigned char sha1[20];
ptr = buf;
/*
* Read non-recursive ls-tree output format:
* mode SP type SP sha1 TAB name
*/
mode = strtoul(ptr, &ntr, 8);
if (ptr == ntr || !ntr || *ntr != ' ')
die("input format error: %s", buf);
ptr = ntr + 1; /* type */
ntr = strchr(ptr, ' ');
if (!ntr || buf + len <= ntr + 40 ||
ntr[41] != '\t' ||
get_sha1_hex(ntr + 1, sha1))
die("input format error: %s", buf);
/* It is perfectly normal if we do not have a commit from a submodule */
if (S_ISGITLINK(mode))
allow_missing = 1;
if (!allow_missing)
type = sha1_object_info(sha1, NULL);
else
type = object_type(mode);
if (type < 0)
die("object %s unavailable", sha1_to_hex(sha1));
*ntr++ = 0; /* now at the beginning of SHA1 */
if (type != type_from_string(ptr))
die("object type %s mismatch (%s)", ptr, typename(type));
path = ntr + 41; /* at the beginning of name */
if (line_termination && path[0] == '"') {
struct strbuf p_uq = STRBUF_INIT;
if (unquote_c_style(&p_uq, path, NULL))
die("invalid quoting");
path = strbuf_detach(&p_uq, NULL);
}
append_to_tree(mode, sha1, path);
}
int cmd_mktree(int ac, const char **av, const char *prefix)
{
struct strbuf sb = STRBUF_INIT;
unsigned char sha1[20];
int line_termination = '\n';
int allow_missing = 0;
const struct option option[] = {
OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
OPT_SET_INT( 0 , "missing", &allow_missing, "allow missing objects", 1),
OPT_END()
};
ac = parse_options(ac, av, option, mktree_usage, 0);
while (strbuf_getline(&sb, stdin, line_termination) != EOF)
mktree_line(sb.buf, sb.len, line_termination, allow_missing);
strbuf_release(&sb);
write_tree(sha1);
puts(sha1_to_hex(sha1));
exit(0);
}