2006-02-21 06:50:01 +01:00
|
|
|
/*
|
|
|
|
* GIT - the stupid content tracker
|
|
|
|
*
|
2009-05-10 19:28:18 +02:00
|
|
|
* Copyright (c) Junio C Hamano, 2006, 2009
|
2006-02-21 06:50:01 +01:00
|
|
|
*/
|
2009-05-10 19:28:18 +02:00
|
|
|
#include "builtin.h"
|
2006-02-21 06:50:01 +01:00
|
|
|
#include "quote.h"
|
2006-04-02 14:44:09 +02:00
|
|
|
#include "tree.h"
|
2009-05-10 19:31:56 +02:00
|
|
|
#include "parse-options.h"
|
2006-02-21 06:50:01 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-03-03 23:32:00 +01:00
|
|
|
ALLOC_GROW(entries, used + 1, alloc);
|
2006-02-21 06:50:01 +01:00
|
|
|
ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
|
|
|
|
ent->mode = mode;
|
|
|
|
ent->len = len;
|
2006-08-23 08:49:00 +02:00
|
|
|
hashcpy(ent->sha1, sha1);
|
2006-02-21 06:50:01 +01:00
|
|
|
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)
|
|
|
|
{
|
2007-09-06 13:20:08 +02:00
|
|
|
struct strbuf buf;
|
|
|
|
size_t size;
|
2006-02-21 06:50:01 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
qsort(entries, used, sizeof(*entries), ent_compare);
|
|
|
|
for (size = i = 0; i < used; i++)
|
|
|
|
size += 32 + entries[i]->len;
|
|
|
|
|
2007-09-10 12:35:04 +02:00
|
|
|
strbuf_init(&buf, size);
|
2006-02-21 06:50:01 +01:00
|
|
|
for (i = 0; i < used; i++) {
|
|
|
|
struct treeent *ent = entries[i];
|
2007-09-06 13:20:08 +02:00
|
|
|
strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
|
|
|
|
strbuf_add(&buf, ent->sha1, 20);
|
2006-02-21 06:50:01 +01:00
|
|
|
}
|
2007-09-06 13:20:08 +02:00
|
|
|
|
|
|
|
write_sha1_file(buf.buf, buf.len, tree_type, sha1);
|
2011-11-10 18:10:51 +01:00
|
|
|
strbuf_release(&buf);
|
2006-02-21 06:50:01 +01:00
|
|
|
}
|
|
|
|
|
2009-05-10 19:31:56 +02:00
|
|
|
static const char *mktree_usage[] = {
|
2012-08-20 14:32:25 +02:00
|
|
|
N_("git mktree [-z] [--missing] [--batch]"),
|
2009-05-10 19:31:56 +02:00
|
|
|
NULL
|
|
|
|
};
|
2006-02-21 06:50:01 +01:00
|
|
|
|
2009-05-10 20:30:03 +02:00
|
|
|
static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing)
|
2009-05-10 19:41:22 +02:00
|
|
|
{
|
|
|
|
char *ptr, *ntr;
|
|
|
|
unsigned mode;
|
2009-05-15 00:49:10 +02:00
|
|
|
enum object_type mode_type; /* object type derived from mode */
|
|
|
|
enum object_type obj_type; /* object type derived from sha */
|
2009-05-10 19:41:22 +02:00
|
|
|
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);
|
2009-05-10 19:45:52 +02:00
|
|
|
|
|
|
|
/* It is perfectly normal if we do not have a commit from a submodule */
|
2009-05-10 20:30:03 +02:00
|
|
|
if (S_ISGITLINK(mode))
|
|
|
|
allow_missing = 1;
|
|
|
|
|
2009-05-10 19:45:52 +02:00
|
|
|
|
2009-05-10 19:41:22 +02:00
|
|
|
*ntr++ = 0; /* now at the beginning of SHA1 */
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2009-05-15 00:49:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Object type is redundantly derivable three ways.
|
|
|
|
* These should all agree.
|
|
|
|
*/
|
|
|
|
mode_type = object_type(mode);
|
|
|
|
if (mode_type != type_from_string(ptr)) {
|
|
|
|
die("entry '%s' object type (%s) doesn't match mode type (%s)",
|
|
|
|
path, ptr, typename(mode_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the type of object identified by sha1 */
|
|
|
|
obj_type = sha1_object_info(sha1, NULL);
|
|
|
|
if (obj_type < 0) {
|
|
|
|
if (allow_missing) {
|
|
|
|
; /* no problem - missing objects are presumed to be of the right type */
|
|
|
|
} else {
|
|
|
|
die("entry '%s' object %s is unavailable", path, sha1_to_hex(sha1));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (obj_type != mode_type) {
|
|
|
|
/*
|
|
|
|
* The object exists but is of the wrong type.
|
|
|
|
* This is a problem regardless of allow_missing
|
|
|
|
* because the new tree entry will never be correct.
|
|
|
|
*/
|
|
|
|
die("entry '%s' object %s is a %s but specified type was (%s)",
|
|
|
|
path, sha1_to_hex(sha1), typename(obj_type), typename(mode_type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:41:22 +02:00
|
|
|
append_to_tree(mode, sha1, path);
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:28:18 +02:00
|
|
|
int cmd_mktree(int ac, const char **av, const char *prefix)
|
2006-02-21 06:50:01 +01:00
|
|
|
{
|
2008-10-09 21:12:12 +02:00
|
|
|
struct strbuf sb = STRBUF_INIT;
|
2006-02-21 06:50:01 +01:00
|
|
|
unsigned char sha1[20];
|
|
|
|
int line_termination = '\n';
|
2009-05-10 20:30:03 +02:00
|
|
|
int allow_missing = 0;
|
2009-05-14 21:51:15 +02:00
|
|
|
int is_batch_mode = 0;
|
|
|
|
int got_eof = 0;
|
|
|
|
|
2009-05-10 19:31:56 +02:00
|
|
|
const struct option option[] = {
|
2012-08-20 14:32:25 +02:00
|
|
|
OPT_SET_INT('z', NULL, &line_termination, N_("input is NUL terminated"), '\0'),
|
|
|
|
OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
|
|
|
|
OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
|
2009-05-10 19:31:56 +02:00
|
|
|
OPT_END()
|
|
|
|
};
|
2006-02-21 06:50:01 +01:00
|
|
|
|
2009-05-23 20:53:12 +02:00
|
|
|
ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
|
2006-02-21 06:50:01 +01:00
|
|
|
|
2009-05-14 21:51:15 +02:00
|
|
|
while (!got_eof) {
|
|
|
|
while (1) {
|
|
|
|
if (strbuf_getline(&sb, stdin, line_termination) == EOF) {
|
|
|
|
got_eof = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sb.buf[0] == '\0') {
|
|
|
|
/* empty lines denote tree boundaries in batch mode */
|
|
|
|
if (is_batch_mode)
|
|
|
|
break;
|
|
|
|
die("input format error: (blank line only valid in batch mode)");
|
|
|
|
}
|
|
|
|
mktree_line(sb.buf, sb.len, line_termination, allow_missing);
|
|
|
|
}
|
|
|
|
if (is_batch_mode && got_eof && used < 1) {
|
|
|
|
/*
|
|
|
|
* Execution gets here if the last tree entry is terminated with a
|
|
|
|
* new-line. The final new-line has been made optional to be
|
|
|
|
* consistent with the original non-batch behaviour of mktree.
|
|
|
|
*/
|
|
|
|
; /* skip creating an empty tree */
|
|
|
|
} else {
|
|
|
|
write_tree(sha1);
|
|
|
|
puts(sha1_to_hex(sha1));
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
used=0; /* reset tree entry buffer for re-use in batch mode */
|
|
|
|
}
|
2007-09-17 11:19:04 +02:00
|
|
|
strbuf_release(&sb);
|
2006-02-21 06:50:01 +01:00
|
|
|
exit(0);
|
|
|
|
}
|