Skip to content

Commit aba1438

Browse files
vdyegitster
authored andcommitted
cat-file: add %(objectmode) atom
Add a formatting atom, used with the --batch-check/--batch-command options, that prints the octal representation of the object mode if a given revision includes that information, e.g. one that follows the format <tree-ish>:<path>. If the mode information does not exist, an empty string is printed instead. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9fd3803 commit aba1438

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

Documentation/git-cat-file.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ newline. The available atoms are:
307307
`objecttype`::
308308
The type of the object (the same as `cat-file -t` reports).
309309

310+
`objectmode`::
311+
If the specified object has mode information (such as a tree or
312+
index entry), the mode expressed as an octal integer. Otherwise,
313+
empty string.
314+
310315
`objectsize`::
311316
The size, in bytes, of the object (the same as `cat-file -s`
312317
reports).

builtin/cat-file.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ struct expand_data {
275275
struct object_id oid;
276276
enum object_type type;
277277
unsigned long size;
278+
unsigned short mode;
278279
off_t disk_size;
279280
const char *rest;
280281
struct object_id delta_base_oid;
@@ -306,6 +307,7 @@ struct expand_data {
306307
*/
307308
unsigned skip_object_info : 1;
308309
};
310+
#define EXPAND_DATA_INIT { .mode = S_IFINVALID }
309311

310312
static int is_atom(const char *atom, const char *s, int slen)
311313
{
@@ -345,6 +347,9 @@ static int expand_atom(struct strbuf *sb, const char *atom, int len,
345347
else
346348
strbuf_addstr(sb,
347349
oid_to_hex(&data->delta_base_oid));
350+
} else if (is_atom("objectmode", atom, len)) {
351+
if (!data->mark_query && !(S_IFINVALID == data->mode))
352+
strbuf_addf(sb, "%06o", data->mode);
348353
} else
349354
return 0;
350355
return 1;
@@ -613,6 +618,7 @@ static void batch_one_object(const char *obj_name,
613618
goto out;
614619
}
615620

621+
data->mode = ctx.mode;
616622
batch_object_write(obj_name, scratch, opt, data, NULL, 0);
617623

618624
out:
@@ -866,7 +872,7 @@ static int batch_objects(struct batch_options *opt)
866872
{
867873
struct strbuf input = STRBUF_INIT;
868874
struct strbuf output = STRBUF_INIT;
869-
struct expand_data data;
875+
struct expand_data data = EXPAND_DATA_INIT;
870876
int save_warning;
871877
int retval = 0;
872878

@@ -875,7 +881,6 @@ static int batch_objects(struct batch_options *opt)
875881
* object_info to be handed to oid_object_info_extended for each
876882
* object.
877883
*/
878-
memset(&data, 0, sizeof(data));
879884
data.mark_query = 1;
880885
expand_format(&output,
881886
opt->format ? opt->format : DEFAULT_FORMAT,

t/t1006-cat-file.sh

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ strlen () {
114114
run_tests () {
115115
type=$1
116116
object_name="$2"
117-
size=$3
118-
content=$4
119-
pretty_content=$5
120-
oid=${6:-"$object_name"}
117+
mode=$3
118+
size=$4
119+
content=$5
120+
pretty_content=$6
121+
oid=${7:-"$object_name"}
121122

122123
batch_output="$oid $type $size
123124
$content"
@@ -209,6 +210,12 @@ $content"
209210
test_cmp expect actual
210211
'
211212

213+
test_expect_success '--batch-check with %(objectmode)' '
214+
echo "$mode $oid" >expect &&
215+
echo $object_name | git cat-file --batch-check="%(objectmode) %(objectname)" >actual &&
216+
test_cmp expect actual
217+
'
218+
212219
test -z "$content" ||
213220
test_expect_success "--batch without type ($type)" '
214221
{
@@ -247,8 +254,7 @@ test_expect_success "setup" '
247254

248255
run_blob_tests () {
249256
oid=$1
250-
251-
run_tests 'blob' $oid $hello_size "$hello_content" "$hello_content"
257+
run_tests 'blob' $oid "" $hello_size "$hello_content" "$hello_content"
252258

253259
test_expect_success '--batch-command --buffer with flush for blob info' '
254260
echo "$oid blob $hello_size" >expect &&
@@ -286,12 +292,12 @@ tree_compat_size=$((2 * $(test_oid --hash=compat rawsz) + 13 + 24))
286292
tree_pretty_content="100644 blob $hello_oid hello${LF}100755 blob $hello_oid path with spaces${LF}"
287293
tree_compat_pretty_content="100644 blob $hello_compat_oid hello${LF}100755 blob $hello_compat_oid path with spaces${LF}"
288294

289-
run_tests 'tree' $tree_oid $tree_size "" "$tree_pretty_content"
290-
run_tests 'tree' $tree_compat_oid $tree_compat_size "" "$tree_compat_pretty_content"
291-
run_tests 'blob' "$tree_oid:hello" $hello_size "" "$hello_content" $hello_oid
292-
run_tests 'blob' "$tree_compat_oid:hello" $hello_size "" "$hello_content" $hello_compat_oid
293-
run_tests 'blob' "$tree_oid:path with spaces" $hello_size "" "$hello_content" $hello_oid
294-
run_tests 'blob' "$tree_compat_oid:path with spaces" $hello_size "" "$hello_content" $hello_compat_oid
295+
run_tests 'tree' $tree_oid "" $tree_size "" "$tree_pretty_content"
296+
run_tests 'tree' $tree_compat_oid "" $tree_compat_size "" "$tree_compat_pretty_content"
297+
run_tests 'blob' "$tree_oid:hello" "100644" $hello_size "" "$hello_content" $hello_oid
298+
run_tests 'blob' "$tree_compat_oid:hello" "100644" $hello_size "" "$hello_content" $hello_compat_oid
299+
run_tests 'blob' "$tree_oid:path with spaces" "100755" $hello_size "" "$hello_content" $hello_oid
300+
run_tests 'blob' "$tree_compat_oid:path with spaces" "100755" $hello_size "" "$hello_content" $hello_compat_oid
295301

296302
commit_message="Initial commit"
297303
commit_oid=$(echo_without_newline "$commit_message" | git commit-tree $tree_oid)
@@ -310,8 +316,8 @@ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
310316
311317
$commit_message"
312318

313-
run_tests 'commit' $commit_oid $commit_size "$commit_content" "$commit_content"
314-
run_tests 'commit' $commit_compat_oid $commit_compat_size "$commit_compat_content" "$commit_compat_content"
319+
run_tests 'commit' $commit_oid "" $commit_size "$commit_content" "$commit_content"
320+
run_tests 'commit' $commit_compat_oid "" $commit_compat_size "$commit_compat_content" "$commit_compat_content"
315321

316322
tag_header_without_oid="type blob
317323
tag hellotag
@@ -334,8 +340,8 @@ tag_size=$(strlen "$tag_content")
334340
tag_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $tag_oid)
335341
tag_compat_size=$(strlen "$tag_compat_content")
336342

337-
run_tests 'tag' $tag_oid $tag_size "$tag_content" "$tag_content"
338-
run_tests 'tag' $tag_compat_oid $tag_compat_size "$tag_compat_content" "$tag_compat_content"
343+
run_tests 'tag' $tag_oid "" $tag_size "$tag_content" "$tag_content"
344+
run_tests 'tag' $tag_compat_oid "" $tag_compat_size "$tag_compat_content" "$tag_compat_content"
339345

340346
test_expect_success "Reach a blob from a tag pointing to it" '
341347
echo_without_newline "$hello_content" >expect &&

0 commit comments

Comments
 (0)