Browse Source

builtin/mktree: convert to struct object_id

Convert this file to use struct object_id.  Modify one use of
get_sha1_hex into parse_oid_hex; this is safe since we get the data from
a strbuf.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
brian m. carlson 7 years ago committed by Junio C Hamano
parent
commit
83eb08020c
  1. 24
      builtin/mktree.c

24
builtin/mktree.c

@ -10,13 +10,13 @@


static struct treeent { static struct treeent {
unsigned mode; unsigned mode;
unsigned char sha1[20]; struct object_id oid;
int len; int len;
char name[FLEX_ARRAY]; char name[FLEX_ARRAY];
} **entries; } **entries;
static int alloc, used; static int alloc, used;


static void append_to_tree(unsigned mode, unsigned char *sha1, char *path) static void append_to_tree(unsigned mode, struct object_id *oid, char *path)
{ {
struct treeent *ent; struct treeent *ent;
size_t len = strlen(path); size_t len = strlen(path);
@ -26,7 +26,7 @@ static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
FLEX_ALLOC_MEM(ent, name, path, len); FLEX_ALLOC_MEM(ent, name, path, len);
ent->mode = mode; ent->mode = mode;
ent->len = len; ent->len = len;
hashcpy(ent->sha1, sha1); oidcpy(&ent->oid, oid);


ALLOC_GROW(entries, used + 1, alloc); ALLOC_GROW(entries, used + 1, alloc);
entries[used++] = ent; entries[used++] = ent;
@ -54,7 +54,7 @@ static void write_tree(struct object_id *oid)
for (i = 0; i < used; i++) { for (i = 0; i < used; i++) {
struct treeent *ent = entries[i]; struct treeent *ent = entries[i];
strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0'); strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
strbuf_add(&buf, ent->sha1, 20); strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
} }


write_object_file(buf.buf, buf.len, tree_type, oid); write_object_file(buf.buf, buf.len, tree_type, oid);
@ -69,11 +69,12 @@ static const char *mktree_usage[] = {
static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_missing) static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_missing)
{ {
char *ptr, *ntr; char *ptr, *ntr;
const char *p;
unsigned mode; unsigned mode;
enum object_type mode_type; /* object type derived from mode */ enum object_type mode_type; /* object type derived from mode */
enum object_type obj_type; /* object type derived from sha */ enum object_type obj_type; /* object type derived from sha */
char *path, *to_free = NULL; char *path, *to_free = NULL;
unsigned char sha1[20]; struct object_id oid;


ptr = buf; ptr = buf;
/* /*
@ -85,9 +86,8 @@ static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_miss
die("input format error: %s", buf); die("input format error: %s", buf);
ptr = ntr + 1; /* type */ ptr = ntr + 1; /* type */
ntr = strchr(ptr, ' '); ntr = strchr(ptr, ' ');
if (!ntr || buf + len <= ntr + 40 || if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
ntr[41] != '\t' || *p != '\t')
get_sha1_hex(ntr + 1, sha1))
die("input format error: %s", buf); die("input format error: %s", buf);


/* It is perfectly normal if we do not have a commit from a submodule */ /* It is perfectly normal if we do not have a commit from a submodule */
@ -116,12 +116,12 @@ static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_miss
} }


/* Check the type of object identified by sha1 */ /* Check the type of object identified by sha1 */
obj_type = sha1_object_info(sha1, NULL); obj_type = sha1_object_info(oid.hash, NULL);
if (obj_type < 0) { if (obj_type < 0) {
if (allow_missing) { if (allow_missing) {
; /* no problem - missing objects are presumed to be of the right type */ ; /* no problem - missing objects are presumed to be of the right type */
} else { } else {
die("entry '%s' object %s is unavailable", path, sha1_to_hex(sha1)); die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid));
} }
} else { } else {
if (obj_type != mode_type) { if (obj_type != mode_type) {
@ -131,11 +131,11 @@ static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_miss
* because the new tree entry will never be correct. * because the new tree entry will never be correct.
*/ */
die("entry '%s' object %s is a %s but specified type was (%s)", die("entry '%s' object %s is a %s but specified type was (%s)",
path, sha1_to_hex(sha1), type_name(obj_type), type_name(mode_type)); path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type));
} }
} }


append_to_tree(mode, sha1, path); append_to_tree(mode, &oid, path);
free(to_free); free(to_free);
} }



Loading…
Cancel
Save