diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt index bd95a6c10a7d0b..69b50d2042ff2f 100644 --- a/Documentation/git-cat-file.txt +++ b/Documentation/git-cat-file.txt @@ -292,6 +292,11 @@ newline. The available atoms are: `objecttype`:: The type of the object (the same as `cat-file -t` reports). +`objectmode`:: + If the specified object has mode information (such as a tree or + index entry), the mode expressed as an octal integer. Otherwise, + empty string. + `objectsize`:: The size, in bytes, of the object (the same as `cat-file -s` reports). @@ -407,6 +412,11 @@ Note also that multiple copies of an object may be present in the object database; in this case, it is undefined which copy's size or delta base will be reported. +Submodules are handled specially in `git cat-file`, as the objects +corresponding to the recorded OIDs are not expected to be present in the +current repository. For that reason, submodules are reported as having +type `submodule` and mode 1600000 and all other fields are zeroed out. + GIT --- Part of the linkgit:git[1] suite diff --git a/builtin/cat-file.c b/builtin/cat-file.c index bbf851138ec408..c59ad682d1f6d9 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -128,7 +128,9 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, switch (opt) { case 't': oi.type_name = &sb; - if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0) + if (obj_context.mode == S_IFGITLINK) + strbuf_addstr(&sb, "submodule"); + else if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0) die("git cat-file: could not get object info"); if (sb.len) { printf("%s\n", sb.buf); @@ -272,6 +274,7 @@ struct expand_data { struct object_id oid; enum object_type type; unsigned long size; + unsigned short mode; off_t disk_size; const char *rest; struct object_id delta_base_oid; @@ -303,6 +306,7 @@ struct expand_data { */ unsigned skip_object_info : 1; }; +#define EXPAND_DATA_INIT { .mode = S_IFINVALID } static int is_atom(const char *atom, const char *s, int slen) { @@ -317,17 +321,26 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len, if (!data->mark_query) strbuf_addstr(sb, oid_to_hex(&data->oid)); } else if (is_atom("objecttype", atom, len)) { - if (data->mark_query) + if (data->mode == S_IFGITLINK) { + if (!data->mark_query) + strbuf_addstr(sb, "submodule"); + } else if (data->mark_query) data->info.typep = &data->type; else strbuf_addstr(sb, type_name(data->type)); } else if (is_atom("objectsize", atom, len)) { - if (data->mark_query) + if (data->mode == S_IFGITLINK) { + if (!data->mark_query) + strbuf_addstr(sb, "0"); + } else if (data->mark_query) data->info.sizep = &data->size; else strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size); } else if (is_atom("objectsize:disk", atom, len)) { - if (data->mark_query) + if (data->mode == S_IFGITLINK) { + if (!data->mark_query) + strbuf_addstr(sb, "0"); + } else if (data->mark_query) data->info.disk_sizep = &data->disk_size; else strbuf_addf(sb, "%"PRIuMAX, (uintmax_t)data->disk_size); @@ -342,6 +355,9 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len, else strbuf_addstr(sb, oid_to_hex(&data->delta_base_oid)); + } else if (is_atom("objectmode", atom, len)) { + if (!data->mark_query && !(S_IFINVALID == data->mode)) + strbuf_addf(sb, "%06o", data->mode); } else die("unknown format element: %.*s", len, atom); } @@ -443,7 +459,8 @@ static void print_default_format(struct strbuf *scratch, struct expand_data *dat struct batch_options *opt) { strbuf_addf(scratch, "%s %s %"PRIuMAX"%c", oid_to_hex(&data->oid), - type_name(data->type), + data->mode == S_IFGITLINK ? + "submodule" : type_name(data->type), (uintmax_t)data->size, opt->output_delim); } @@ -465,7 +482,15 @@ static void batch_object_write(const char *obj_name, if (use_mailmap) data->info.typep = &data->type; - if (pack) + if (data->mode == S_IFGITLINK) { + data->type = OBJ_BAD; /* `type_name()` does not know submodules */ + data->size = 0; + data->disk_size = 0; + data->rest = NULL; + oidcpy(&data->delta_base_oid, null_oid()); + memset(&data->info, 0, sizeof(data->info)); + ret = 0; /* no info to look up */ + } else if (pack) ret = packed_object_info(the_repository, pack, offset, &data->info); else @@ -562,6 +587,7 @@ static void batch_one_object(const char *obj_name, return; } + data->mode = ctx.mode; batch_object_write(obj_name, scratch, opt, data, NULL, 0); } @@ -766,7 +792,7 @@ static int batch_objects(struct batch_options *opt) { struct strbuf input = STRBUF_INIT; struct strbuf output = STRBUF_INIT; - struct expand_data data; + struct expand_data data = EXPAND_DATA_INIT; int save_warning; int retval = 0; @@ -775,7 +801,6 @@ static int batch_objects(struct batch_options *opt) * object_info to be handed to oid_object_info_extended for each * object. */ - memset(&data, 0, sizeof(data)); data.mark_query = 1; expand_format(&output, opt->format ? opt->format : DEFAULT_FORMAT, diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index e0c6482797e120..3368b663ef3fda 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -112,65 +112,67 @@ strlen () { run_tests () { type=$1 - sha1=$2 - size=$3 - content=$4 - pretty_content=$5 - - batch_output="$sha1 $type $size + object_name=$2 + oid=$(git rev-parse --verify $object_name) + mode=$3 + size=$4 + content=$5 + pretty_content=$6 + + batch_output="$oid $type $size $content" test_expect_success "$type exists" ' - git cat-file -e $sha1 + git cat-file -e $object_name ' test_expect_success "Type of $type is correct" ' echo $type >expect && - git cat-file -t $sha1 >actual && + git cat-file -t $object_name >actual && test_cmp expect actual ' test_expect_success "Size of $type is correct" ' echo $size >expect && - git cat-file -s $sha1 >actual && + git cat-file -s $object_name >actual && test_cmp expect actual ' test_expect_success "Type of $type is correct using --allow-unknown-type" ' echo $type >expect && - git cat-file -t --allow-unknown-type $sha1 >actual && + git cat-file -t --allow-unknown-type $object_name >actual && test_cmp expect actual ' test_expect_success "Size of $type is correct using --allow-unknown-type" ' echo $size >expect && - git cat-file -s --allow-unknown-type $sha1 >actual && + git cat-file -s --allow-unknown-type $object_name >actual && test_cmp expect actual ' test -z "$content" || test_expect_success "Content of $type is correct" ' echo_without_newline "$content" >expect && - git cat-file $type $sha1 >actual && + git cat-file $type $object_name >actual && test_cmp expect actual ' test_expect_success "Pretty content of $type is correct" ' echo_without_newline "$pretty_content" >expect && - git cat-file -p $sha1 >actual && + git cat-file -p $object_name >actual && test_cmp expect actual ' test -z "$content" || test_expect_success "--batch output of $type is correct" ' echo "$batch_output" >expect && - echo $sha1 | git cat-file --batch >actual && + echo $object_name | git cat-file --batch >actual && test_cmp expect actual ' test_expect_success "--batch-check output of $type is correct" ' - echo "$sha1 $type $size" >expect && - echo_without_newline $sha1 | git cat-file --batch-check >actual && + echo "$oid $type $size" >expect && + echo_without_newline $object_name | git cat-file --batch-check >actual && test_cmp expect actual ' @@ -179,44 +181,50 @@ $content" test -z "$content" || test_expect_success "--batch-command $opt output of $type content is correct" ' echo "$batch_output" >expect && - test_write_lines "contents $sha1" | git cat-file --batch-command $opt >actual && + test_write_lines "contents $object_name" | git cat-file --batch-command $opt >actual && test_cmp expect actual ' test_expect_success "--batch-command $opt output of $type info is correct" ' - echo "$sha1 $type $size" >expect && - test_write_lines "info $sha1" | + echo "$oid $type $size" >expect && + test_write_lines "info $object_name" | git cat-file --batch-command $opt >actual && test_cmp expect actual ' done test_expect_success "custom --batch-check format" ' - echo "$type $sha1" >expect && - echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual && + echo "$type $oid" >expect && + echo $object_name | git cat-file --batch-check="%(objecttype) %(objectname)" >actual && test_cmp expect actual ' test_expect_success "custom --batch-command format" ' - echo "$type $sha1" >expect && - echo "info $sha1" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual && + echo "$type $oid" >expect && + echo "info $object_name" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual && test_cmp expect actual ' test_expect_success '--batch-check with %(rest)' ' echo "$type this is some extra content" >expect && - echo "$sha1 this is some extra content" | + echo "$object_name this is some extra content" | git cat-file --batch-check="%(objecttype) %(rest)" >actual && test_cmp expect actual ' + test_expect_success '--batch-check with %(objectmode)' ' + echo "$mode $oid" >expect && + echo $object_name | git cat-file --batch-check="%(objectmode) %(objectname)" >actual && + test_cmp expect actual + ' + test -z "$content" || test_expect_success "--batch without type ($type)" ' { echo "$size" && echo "$content" } >expect && - echo $sha1 | git cat-file --batch="%(objectsize)" >actual && + echo $object_name | git cat-file --batch="%(objectsize)" >actual && test_cmp expect actual ' @@ -226,7 +234,7 @@ $content" echo "$type" && echo "$content" } >expect && - echo $sha1 | git cat-file --batch="%(objecttype)" >actual && + echo $object_name | git cat-file --batch="%(objecttype)" >actual && test_cmp expect actual ' } @@ -240,7 +248,7 @@ test_expect_success "setup" ' git update-index --add hello ' -run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content" +run_tests 'blob' $hello_sha1 "" $hello_size "$hello_content" "$hello_content" test_expect_success '--batch-command --buffer with flush for blob info' ' echo "$hello_sha1 blob $hello_size" >expect && @@ -270,7 +278,8 @@ tree_sha1=$(git write-tree) tree_size=$(($(test_oid rawsz) + 13)) tree_pretty_content="100644 blob $hello_sha1 hello${LF}" -run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content" +run_tests 'tree' $tree_sha1 "" $tree_size "" "$tree_pretty_content" +run_tests 'blob' "$tree_sha1:hello" "100644" $hello_size "" "$hello_content" commit_message="Initial commit" commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1) @@ -281,7 +290,7 @@ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE $commit_message" -run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" +run_tests 'commit' $commit_sha1 "" $commit_size "$commit_content" "$commit_content" tag_header_without_timestamp="object $hello_sha1 type blob @@ -295,7 +304,7 @@ $tag_description" tag_sha1=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w) tag_size=$(strlen "$tag_content") -run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" +run_tests 'tag' $tag_sha1 "" $tag_size "$tag_content" "$tag_content" test_expect_success "Reach a blob from a tag pointing to it" ' echo_without_newline "$hello_content" >expect && @@ -1169,6 +1178,16 @@ test_expect_success 'cat-file --batch-check respects replace objects' ' test_cmp expect actual ' +test_expect_success 'batch-command with a submodule' ' + printf "160000 commit %0.*d\tsub\n" $(test_oid hexsz) 17 >tree-with-sub && + tree=$(git mktree actual <<-EOF && + $tree:sub + EOF + printf "%0.*d submodule 0\n" $(test_oid hexsz) 17 >expect && + test_cmp expect actual +' + # Pull the entry for object with oid "$1" out of the output of # "cat-file --batch", including its object content (which requires # parsing and reading a set amount of bytes, hence perl).