2006-02-21 05:50:01 +00:00
|
|
|
/*
|
|
|
|
* GIT - the stupid content tracker
|
|
|
|
*
|
2009-05-10 17:28:18 +00:00
|
|
|
* Copyright (c) Junio C Hamano, 2006, 2009
|
2006-02-21 05:50:01 +00:00
|
|
|
*/
|
2009-05-10 17:28:18 +00:00
|
|
|
#include "builtin.h"
|
2023-02-24 00:09:24 +00:00
|
|
|
#include "alloc.h"
|
2023-03-21 06:25:54 +00:00
|
|
|
#include "gettext.h"
|
2023-02-24 00:09:27 +00:00
|
|
|
#include "hex.h"
|
2006-02-21 05:50:01 +00:00
|
|
|
#include "quote.h"
|
2023-05-16 06:34:06 +00:00
|
|
|
#include "strbuf.h"
|
2006-04-02 12:44:09 +00:00
|
|
|
#include "tree.h"
|
2009-05-10 17:31:56 +00:00
|
|
|
#include "parse-options.h"
|
2023-05-16 06:34:06 +00:00
|
|
|
#include "object-store-ll.h"
|
2006-02-21 05:50:01 +00:00
|
|
|
|
|
|
|
static struct treeent {
|
|
|
|
unsigned mode;
|
2018-03-12 02:27:41 +00:00
|
|
|
struct object_id oid;
|
2006-02-21 05:50:01 +00:00
|
|
|
int len;
|
|
|
|
char name[FLEX_ARRAY];
|
|
|
|
} **entries;
|
|
|
|
static int alloc, used;
|
|
|
|
|
2018-03-12 02:27:41 +00:00
|
|
|
static void append_to_tree(unsigned mode, struct object_id *oid, char *path)
|
2006-02-21 05:50:01 +00:00
|
|
|
{
|
|
|
|
struct treeent *ent;
|
2016-02-22 22:44:32 +00:00
|
|
|
size_t len = strlen(path);
|
2006-02-21 05:50:01 +00:00
|
|
|
if (strchr(path, '/'))
|
|
|
|
die("path %s contains slash", path);
|
|
|
|
|
2016-02-22 22:44:32 +00:00
|
|
|
FLEX_ALLOC_MEM(ent, name, path, len);
|
2006-02-21 05:50:01 +00:00
|
|
|
ent->mode = mode;
|
|
|
|
ent->len = len;
|
2018-03-12 02:27:41 +00:00
|
|
|
oidcpy(&ent->oid, oid);
|
2016-02-22 22:44:32 +00:00
|
|
|
|
|
|
|
ALLOC_GROW(entries, used + 1, alloc);
|
|
|
|
entries[used++] = ent;
|
2006-02-21 05:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-28 00:13:19 +00:00
|
|
|
static void write_tree(struct object_id *oid)
|
2006-02-21 05:50:01 +00:00
|
|
|
{
|
2007-09-06 11:20:08 +00:00
|
|
|
struct strbuf buf;
|
|
|
|
size_t size;
|
2006-02-21 05:50:01 +00:00
|
|
|
int i;
|
|
|
|
|
2016-09-29 15:27:31 +00:00
|
|
|
QSORT(entries, used, ent_compare);
|
2006-02-21 05:50:01 +00:00
|
|
|
for (size = i = 0; i < used; i++)
|
|
|
|
size += 32 + entries[i]->len;
|
|
|
|
|
2007-09-10 10:35:04 +00:00
|
|
|
strbuf_init(&buf, size);
|
2006-02-21 05:50:01 +00:00
|
|
|
for (i = 0; i < used; i++) {
|
|
|
|
struct treeent *ent = entries[i];
|
2007-09-06 11:20:08 +00:00
|
|
|
strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
|
2018-03-12 02:27:41 +00:00
|
|
|
strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
|
2006-02-21 05:50:01 +00:00
|
|
|
}
|
2007-09-06 11:20:08 +00:00
|
|
|
|
2022-02-04 23:48:26 +00:00
|
|
|
write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
|
2011-11-10 17:10:51 +00:00
|
|
|
strbuf_release(&buf);
|
2006-02-21 05:50:01 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 17:31:56 +00:00
|
|
|
static const char *mktree_usage[] = {
|
2022-01-31 22:07:48 +00:00
|
|
|
"git mktree [-z] [--missing] [--batch]",
|
2009-05-10 17:31:56 +00:00
|
|
|
NULL
|
|
|
|
};
|
2006-02-21 05:50:01 +00:00
|
|
|
|
2019-05-09 21:30:37 +00:00
|
|
|
static void mktree_line(char *buf, int nul_term_line, int allow_missing)
|
2009-05-10 17:41:22 +00:00
|
|
|
{
|
|
|
|
char *ptr, *ntr;
|
2018-03-12 02:27:41 +00:00
|
|
|
const char *p;
|
2009-05-10 17:41:22 +00:00
|
|
|
unsigned mode;
|
2009-05-14 22:49:10 +00:00
|
|
|
enum object_type mode_type; /* object type derived from mode */
|
|
|
|
enum object_type obj_type; /* object type derived from sha */
|
2022-06-21 13:59:39 +00:00
|
|
|
struct object_info oi = OBJECT_INFO_INIT;
|
2017-05-04 13:57:28 +00:00
|
|
|
char *path, *to_free = NULL;
|
2018-03-12 02:27:41 +00:00
|
|
|
struct object_id oid;
|
2009-05-10 17:41:22 +00:00
|
|
|
|
|
|
|
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, ' ');
|
2018-03-12 02:27:41 +00:00
|
|
|
if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
|
|
|
|
*p != '\t')
|
2009-05-10 17:41:22 +00:00
|
|
|
die("input format error: %s", buf);
|
2009-05-10 17:45:52 +00:00
|
|
|
|
|
|
|
/* It is perfectly normal if we do not have a commit from a submodule */
|
2009-05-10 18:30:03 +00:00
|
|
|
if (S_ISGITLINK(mode))
|
|
|
|
allow_missing = 1;
|
|
|
|
|
2009-05-10 17:45:52 +00:00
|
|
|
|
2009-05-10 17:41:22 +00:00
|
|
|
*ntr++ = 0; /* now at the beginning of SHA1 */
|
|
|
|
|
2018-10-15 00:01:51 +00:00
|
|
|
path = (char *)p + 1; /* at the beginning of name */
|
2016-01-13 23:55:12 +00:00
|
|
|
if (!nul_term_line && path[0] == '"') {
|
2009-05-10 17:41:22 +00:00
|
|
|
struct strbuf p_uq = STRBUF_INIT;
|
|
|
|
if (unquote_c_style(&p_uq, path, NULL))
|
|
|
|
die("invalid quoting");
|
2017-05-04 13:57:28 +00:00
|
|
|
path = to_free = strbuf_detach(&p_uq, NULL);
|
2009-05-10 17:41:22 +00:00
|
|
|
}
|
2009-05-14 22:49:10 +00: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)",
|
2018-02-14 18:59:24 +00:00
|
|
|
path, ptr, type_name(mode_type));
|
2009-05-14 22:49:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 13:59:39 +00:00
|
|
|
/* Check the type of object identified by oid without fetching objects */
|
|
|
|
oi.typep = &obj_type;
|
|
|
|
if (oid_object_info_extended(the_repository, &oid, &oi,
|
|
|
|
OBJECT_INFO_LOOKUP_REPLACE |
|
|
|
|
OBJECT_INFO_QUICK |
|
|
|
|
OBJECT_INFO_SKIP_FETCH_OBJECT) < 0)
|
|
|
|
obj_type = -1;
|
|
|
|
|
2009-05-14 22:49:10 +00:00
|
|
|
if (obj_type < 0) {
|
|
|
|
if (allow_missing) {
|
|
|
|
; /* no problem - missing objects are presumed to be of the right type */
|
|
|
|
} else {
|
2018-03-12 02:27:41 +00:00
|
|
|
die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid));
|
2009-05-14 22:49:10 +00:00
|
|
|
}
|
|
|
|
} 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)",
|
2018-03-12 02:27:41 +00:00
|
|
|
path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type));
|
2009-05-14 22:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 02:27:41 +00:00
|
|
|
append_to_tree(mode, &oid, path);
|
2017-05-04 13:57:28 +00:00
|
|
|
free(to_free);
|
2009-05-10 17:41:22 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 17:28:18 +00:00
|
|
|
int cmd_mktree(int ac, const char **av, const char *prefix)
|
2006-02-21 05:50:01 +00:00
|
|
|
{
|
2008-10-09 19:12:12 +00:00
|
|
|
struct strbuf sb = STRBUF_INIT;
|
2018-01-28 00:13:19 +00:00
|
|
|
struct object_id oid;
|
2016-01-13 23:55:12 +00:00
|
|
|
int nul_term_line = 0;
|
2009-05-10 18:30:03 +00:00
|
|
|
int allow_missing = 0;
|
2009-05-14 19:51:15 +00:00
|
|
|
int is_batch_mode = 0;
|
|
|
|
int got_eof = 0;
|
2016-01-13 23:55:12 +00:00
|
|
|
strbuf_getline_fn getline_fn;
|
2009-05-14 19:51:15 +00:00
|
|
|
|
2009-05-10 17:31:56 +00:00
|
|
|
const struct option option[] = {
|
2016-01-13 23:55:12 +00:00
|
|
|
OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
|
2012-08-20 12:32:25 +00:00
|
|
|
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 17:31:56 +00:00
|
|
|
OPT_END()
|
|
|
|
};
|
2006-02-21 05:50:01 +00:00
|
|
|
|
2009-05-23 18:53:12 +00:00
|
|
|
ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
|
2016-01-13 23:55:12 +00:00
|
|
|
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
|
2006-02-21 05:50:01 +00:00
|
|
|
|
2009-05-14 19:51:15 +00:00
|
|
|
while (!got_eof) {
|
|
|
|
while (1) {
|
2016-01-13 23:55:12 +00:00
|
|
|
if (getline_fn(&sb, stdin) == EOF) {
|
2009-05-14 19:51:15 +00:00
|
|
|
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)");
|
|
|
|
}
|
2019-05-09 21:30:37 +00:00
|
|
|
mktree_line(sb.buf, nul_term_line, allow_missing);
|
2009-05-14 19:51:15 +00:00
|
|
|
}
|
|
|
|
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 {
|
2018-01-28 00:13:19 +00:00
|
|
|
write_tree(&oid);
|
|
|
|
puts(oid_to_hex(&oid));
|
2009-05-14 19:51:15 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
used=0; /* reset tree entry buffer for re-use in batch mode */
|
|
|
|
}
|
2007-09-17 09:19:04 +00:00
|
|
|
strbuf_release(&sb);
|
2021-06-08 10:48:03 +00:00
|
|
|
return 0;
|
2006-02-21 05:50:01 +00:00
|
|
|
}
|