2006-02-21 06:50:01 +01:00
|
|
|
/*
|
|
|
|
* GIT - the stupid content tracker
|
|
|
|
*
|
|
|
|
* Copyright (c) Junio C Hamano, 2006
|
|
|
|
*/
|
|
|
|
#include "cache.h"
|
|
|
|
#include "quote.h"
|
2006-04-02 14:44:09 +02:00
|
|
|
#include "tree.h"
|
2009-01-18 13:00:12 +01:00
|
|
|
#include "exec_cmd.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);
|
|
|
|
|
|
|
|
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;
|
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);
|
2006-02-21 06:50:01 +01:00
|
|
|
}
|
|
|
|
|
2009-01-04 19:39:27 +01:00
|
|
|
static const char mktree_usage[] = "git mktree [-z]";
|
2006-02-21 06:50:01 +01:00
|
|
|
|
|
|
|
int main(int ac, char **av)
|
|
|
|
{
|
2008-10-09 21:12:12 +02:00
|
|
|
struct strbuf sb = STRBUF_INIT;
|
|
|
|
struct strbuf p_uq = STRBUF_INIT;
|
2006-02-21 06:50:01 +01:00
|
|
|
unsigned char sha1[20];
|
|
|
|
int line_termination = '\n';
|
|
|
|
|
2009-01-18 13:00:12 +01:00
|
|
|
git_extract_argv0_path(av[0]);
|
|
|
|
|
2006-02-21 06:50:01 +01:00
|
|
|
setup_git_directory();
|
|
|
|
|
|
|
|
while ((1 < ac) && av[1][0] == '-') {
|
|
|
|
char *arg = av[1];
|
|
|
|
if (!strcmp("-z", arg))
|
|
|
|
line_termination = 0;
|
|
|
|
else
|
|
|
|
usage(mktree_usage);
|
|
|
|
ac--;
|
|
|
|
av++;
|
|
|
|
}
|
|
|
|
|
2007-09-20 00:42:14 +02:00
|
|
|
while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
|
2006-02-21 06:50:01 +01:00
|
|
|
char *ptr, *ntr;
|
|
|
|
unsigned mode;
|
2007-02-26 20:55:59 +01:00
|
|
|
enum object_type type;
|
2006-02-21 06:50:01 +01:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
ptr = sb.buf;
|
|
|
|
/* Input is 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", sb.buf);
|
|
|
|
ptr = ntr + 1; /* type */
|
|
|
|
ntr = strchr(ptr, ' ');
|
2007-09-06 13:20:05 +02:00
|
|
|
if (!ntr || sb.buf + sb.len <= ntr + 40 ||
|
2006-02-21 06:50:01 +01:00
|
|
|
ntr[41] != '\t' ||
|
|
|
|
get_sha1_hex(ntr + 1, sha1))
|
|
|
|
die("input format error: %s", sb.buf);
|
2007-02-26 20:55:59 +01:00
|
|
|
type = sha1_object_info(sha1, NULL);
|
|
|
|
if (type < 0)
|
2006-02-21 06:50:01 +01:00
|
|
|
die("object %s unavailable", sha1_to_hex(sha1));
|
|
|
|
*ntr++ = 0; /* now at the beginning of SHA1 */
|
2007-02-26 20:55:59 +01:00
|
|
|
if (type != type_from_string(ptr))
|
|
|
|
die("object type %s mismatch (%s)", ptr, typename(type));
|
2006-02-21 06:50:01 +01:00
|
|
|
|
2007-09-20 00:42:14 +02:00
|
|
|
path = ntr + 41; /* at the beginning of name */
|
|
|
|
if (line_termination && path[0] == '"') {
|
|
|
|
strbuf_reset(&p_uq);
|
|
|
|
if (unquote_c_style(&p_uq, path, NULL)) {
|
|
|
|
die("invalid quoting");
|
|
|
|
}
|
|
|
|
path = p_uq.buf;
|
|
|
|
}
|
2006-02-21 06:50:01 +01:00
|
|
|
|
2007-09-20 00:42:14 +02:00
|
|
|
append_to_tree(mode, sha1, path);
|
2006-02-21 06:50:01 +01:00
|
|
|
}
|
2007-09-20 00:42:14 +02:00
|
|
|
strbuf_release(&p_uq);
|
2007-09-17 11:19:04 +02:00
|
|
|
strbuf_release(&sb);
|
2007-09-20 00:42:14 +02:00
|
|
|
|
2006-02-21 06:50:01 +01:00
|
|
|
write_tree(sha1);
|
|
|
|
puts(sha1_to_hex(sha1));
|
|
|
|
exit(0);
|
|
|
|
}
|