Skip to content

Commit ed83a28

Browse files
committed
cat-file: use GET_OID_ONLY_TO_DIE in --(textconv|filters)
Change the cat_one_file() logic that calls get_oid_with_context() under --textconv and --filters to use the GET_OID_ONLY_TO_DIE flag, thus improving the error messaging emitted when e.g. <path> is missing but <rev> is not. To service the "cat-file" use-case we need to introduce a new "GET_OID_REQUIRE_PATH" flag, otherwise it would exit early as soon as a valid "HEAD" was resolved, but in the "cat-file" case being changed we always need a valid revision and path. This arguably makes the "<bad rev>:<bad path>" and "<bad rev>:<good (in HEAD) path>" use cases worse, as we won't quote the <path> component at the user anymore, but let's just use the existing logic "git log" et al use for now. We can improve the messaging for those cases as a follow-up for all callers. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
1 parent 1980a0e commit ed83a28

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

builtin/cat-file.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,17 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
7373
struct object_info oi = OBJECT_INFO_INIT;
7474
struct strbuf sb = STRBUF_INIT;
7575
unsigned flags = OBJECT_INFO_LOOKUP_REPLACE;
76+
unsigned get_oid_flags = GET_OID_RECORD_PATH | GET_OID_ONLY_TO_DIE;
7677
const char *path = force_path;
78+
const int opt_cw = (opt == 'c' || opt == 'w');
79+
if (!path && opt_cw)
80+
get_oid_flags |= GET_OID_REQUIRE_PATH;
7781

7882
if (unknown_type)
7983
flags |= OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
8084

81-
if (get_oid_with_context(the_repository, obj_name,
82-
GET_OID_RECORD_PATH,
83-
&oid, &obj_context))
85+
if (get_oid_with_context(the_repository, obj_name, get_oid_flags, &oid,
86+
&obj_context))
8487
die("Not a valid object name %s", obj_name);
8588

8689
if (!path)
@@ -112,20 +115,13 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
112115
return !has_object_file(&oid);
113116

114117
case 'w':
115-
if (!path)
116-
die("git cat-file --filters %s: <object> must be "
117-
"<sha1:path>", obj_name);
118118

119119
if (filter_object(path, obj_context.mode,
120120
&oid, &buf, &size))
121121
return -1;
122122
break;
123123

124124
case 'c':
125-
if (!path)
126-
die("git cat-file --textconv %s: <object> must be <sha1:path>",
127-
obj_name);
128-
129125
if (textconv_object(the_repository, path, obj_context.mode,
130126
&oid, 1, &buf, &size))
131127
break;

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,7 @@ struct object_context {
13771377
#define GET_OID_FOLLOW_SYMLINKS 0100
13781378
#define GET_OID_RECORD_PATH 0200
13791379
#define GET_OID_ONLY_TO_DIE 04000
1380+
#define GET_OID_REQUIRE_PATH 010000
13801381

13811382
#define GET_OID_DISAMBIGUATORS \
13821383
(GET_OID_COMMIT | GET_OID_COMMITTISH | \

object-name.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,9 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
17991799
oc->mode = S_IFINVALID;
18001800
strbuf_init(&oc->symlink_path, 0);
18011801
ret = get_oid_1(repo, name, namelen, oid, flags);
1802+
if (!ret && flags & GET_OID_REQUIRE_PATH)
1803+
die(_("<object>:<path> required, only <object> '%s' given"),
1804+
name);
18021805
if (!ret)
18031806
return ret;
18041807
/*

t/t8007-cat-file-textconv.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ test_expect_success 'usage: <bad rev>' '
2929

3030
test_expect_success 'usage: <bad rev>:<bad path>' '
3131
cat >expect <<-\EOF &&
32-
fatal: Not a valid object name HEAD2:two.bin
32+
fatal: invalid object name '\''HEAD2'\''.
3333
EOF
3434
test_must_fail git cat-file --textconv HEAD2:two.bin 2>actual &&
3535
test_cmp expect actual
3636
'
3737

3838
test_expect_success 'usage: <rev>:<bad path>' '
3939
cat >expect <<-\EOF &&
40-
fatal: Not a valid object name HEAD:two.bin
40+
fatal: path '\''two.bin'\'' does not exist in '\''HEAD'\''
4141
EOF
4242
test_must_fail git cat-file --textconv HEAD:two.bin 2>actual &&
4343
test_cmp expect actual
@@ -46,7 +46,7 @@ test_expect_success 'usage: <rev>:<bad path>' '
4646

4747
test_expect_success 'usage: <rev> with no <path>' '
4848
cat >expect <<-\EOF &&
49-
fatal: git cat-file --textconv HEAD: <object> must be <sha1:path>
49+
fatal: <object>:<path> required, only <object> '\''HEAD'\'' given
5050
EOF
5151
test_must_fail git cat-file --textconv HEAD 2>actual &&
5252
test_cmp expect actual
@@ -55,7 +55,7 @@ test_expect_success 'usage: <rev> with no <path>' '
5555

5656
test_expect_success 'usage: <bad rev>:<good (in HEAD) path>' '
5757
cat >expect <<-\EOF &&
58-
fatal: Not a valid object name HEAD2:one.bin
58+
fatal: invalid object name '\''HEAD2'\''.
5959
EOF
6060
test_must_fail git cat-file --textconv HEAD2:one.bin 2>actual &&
6161
test_cmp expect actual

0 commit comments

Comments
 (0)