Merge branch 'jc/mktree'

* jc/mktree:
  mktree: validate entry type in input
  mktree --batch: build more than one tree object
  mktree --missing: updated usage message and man page
  mktree --missing: allow missing objects
  t1010: add mktree test
  mktree: do not barf on a submodule commit
  builtin-mktree.c: use a helper function to handle one line of input
  mktree: use parse-options
  build-in git-mktree
This commit is contained in:
Junio C Hamano 2009-05-31 16:17:11 -07:00
commit 128b0c08ca
7 changed files with 280 additions and 135 deletions

View File

@ -8,12 +8,13 @@ git-mktree - Build a tree-object from ls-tree formatted text
SYNOPSIS
--------
'git mktree' [-z]
'git mktree' [-z] [--missing] [--batch]
DESCRIPTION
-----------
Reads standard input in non-recursive `ls-tree` output format,
and creates a tree object. The object name of the tree object
Reads standard input in non-recursive `ls-tree` output format, and creates
a tree object. The order of the tree entries is normalised by mktree so
pre-sorting the input is not required. The object name of the tree object
built is written to the standard output.
OPTIONS
@ -21,6 +22,18 @@ OPTIONS
-z::
Read the NUL-terminated `ls-tree -z` output instead.
--missing::
Allow missing objects. The default behaviour (without this option)
is to verify that each tree entry's sha1 identifies an existing
object. This option has no effect on the treatment of gitlink entries
(aka "submodules") which are always allowed to be missing.
--batch::
Allow building of more than one tree object before exiting. Each
tree is separated by as single blank line. The final new-line is
optional. Note - if the '-z' option is used, lines are terminated
with NUL.
Author
------
Written by Junio C Hamano <gitster@pobox.com>

View File

@ -336,7 +336,6 @@ PROGRAMS += git-index-pack$X
PROGRAMS += git-merge-index$X
PROGRAMS += git-merge-tree$X
PROGRAMS += git-mktag$X
PROGRAMS += git-mktree$X
PROGRAMS += git-pack-redundant$X
PROGRAMS += git-patch-id$X
PROGRAMS += git-shell$X
@ -590,6 +589,7 @@ BUILTIN_OBJS += builtin-merge-base.o
BUILTIN_OBJS += builtin-merge-file.o
BUILTIN_OBJS += builtin-merge-ours.o
BUILTIN_OBJS += builtin-merge-recursive.o
BUILTIN_OBJS += builtin-mktree.o
BUILTIN_OBJS += builtin-mv.o
BUILTIN_OBJS += builtin-name-rev.o
BUILTIN_OBJS += builtin-pack-objects.o

190
builtin-mktree.c Normal file
View File

@ -0,0 +1,190 @@
/*
* 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] [--missing] [--batch]",
NULL
};
static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing)
{
char *ptr, *ntr;
unsigned mode;
enum object_type mode_type; /* object type derived from mode */
enum object_type obj_type; /* object type derived from sha */
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;
*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);
}
/*
* 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));
}
}
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;
int is_batch_mode = 0;
int got_eof = 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_SET_INT( 0 , "batch", &is_batch_mode, "allow creation of more than one tree", 1),
OPT_END()
};
ac = parse_options(ac, av, option, mktree_usage, 0);
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 */
}
strbuf_release(&sb);
exit(0);
}

View File

@ -72,6 +72,7 @@ extern int cmd_merge_base(int argc, const char **argv, const char *prefix);
extern int cmd_merge_ours(int argc, const char **argv, const char *prefix);
extern int cmd_merge_file(int argc, const char **argv, const char *prefix);
extern int cmd_merge_recursive(int argc, const char **argv, const char *prefix);
extern int cmd_mktree(int argc, const char **argv, const char *prefix);
extern int cmd_mv(int argc, const char **argv, const char *prefix);
extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);

1
git.c
View File

@ -327,6 +327,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "merge-ours", cmd_merge_ours, RUN_SETUP },
{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
{ "mktree", cmd_mktree, RUN_SETUP },
{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
{ "name-rev", cmd_name_rev, RUN_SETUP },
{ "pack-objects", cmd_pack_objects, RUN_SETUP },

131
mktree.c
View File

@ -1,131 +0,0 @@
/*
* GIT - the stupid content tracker
*
* Copyright (c) Junio C Hamano, 2006
*/
#include "cache.h"
#include "quote.h"
#include "tree.h"
#include "exec_cmd.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]";
int main(int ac, char **av)
{
struct strbuf sb = STRBUF_INIT;
struct strbuf p_uq = STRBUF_INIT;
unsigned char sha1[20];
int line_termination = '\n';
git_extract_argv0_path(av[0]);
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++;
}
while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
char *ptr, *ntr;
unsigned mode;
enum object_type type;
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, ' ');
if (!ntr || sb.buf + sb.len <= ntr + 40 ||
ntr[41] != '\t' ||
get_sha1_hex(ntr + 1, sha1))
die("input format error: %s", sb.buf);
type = sha1_object_info(sha1, NULL);
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] == '"') {
strbuf_reset(&p_uq);
if (unquote_c_style(&p_uq, path, NULL)) {
die("invalid quoting");
}
path = p_uq.buf;
}
append_to_tree(mode, sha1, path);
}
strbuf_release(&p_uq);
strbuf_release(&sb);
write_tree(sha1);
puts(sha1_to_hex(sha1));
exit(0);
}

71
t/t1010-mktree.sh Executable file
View File

@ -0,0 +1,71 @@
#!/bin/sh
test_description='git mktree'
. ./test-lib.sh
test_expect_success setup '
for d in a a. a0
do
mkdir "$d" && echo "$d/one" >"$d/one" &&
git add "$d"
done &&
echo zero >one &&
git update-index --add --info-only one &&
git write-tree --missing-ok >tree.missing &&
git ls-tree $(cat tree.missing) >top.missing &&
git ls-tree -r $(cat tree.missing) >all.missing &&
echo one >one &&
git add one &&
git write-tree >tree &&
git ls-tree $(cat tree) >top &&
git ls-tree -r $(cat tree) >all &&
test_tick &&
git commit -q -m one &&
H=$(git rev-parse HEAD) &&
git update-index --add --cacheinfo 160000 $H sub &&
test_tick &&
git commit -q -m two &&
git rev-parse HEAD^{tree} >tree.withsub &&
git ls-tree HEAD >top.withsub &&
git ls-tree -r HEAD >all.withsub
'
test_expect_success 'ls-tree piped to mktree (1)' '
git mktree <top >actual &&
test_cmp tree actual
'
test_expect_success 'ls-tree piped to mktree (2)' '
git mktree <top.withsub >actual &&
test_cmp tree.withsub actual
'
test_expect_success 'ls-tree output in wrong order given to mktree (1)' '
perl -e "print reverse <>" <top |
git mktree >actual &&
test_cmp tree actual
'
test_expect_success 'ls-tree output in wrong order given to mktree (2)' '
perl -e "print reverse <>" <top.withsub |
git mktree >actual &&
test_cmp tree.withsub actual
'
test_expect_success 'allow missing object with --missing' '
git mktree --missing <top.missing >actual &&
test_cmp tree.missing actual
'
test_expect_failure 'mktree reads ls-tree -r output (1)' '
git mktree <all >actual &&
test_cmp tree actual
'
test_expect_failure 'mktree reads ls-tree -r output (2)' '
git mktree <all.withsub >actual &&
test_cmp tree.withsub actual
'
test_done