Skip to content

Commit 65a6a79

Browse files
peffgitster
authored andcommitted
hash-object: stop allowing unknown types
When passed the "--literally" option, hash-object will allow any arbitrary string for its "-t" type option. Such objects are only useful for testing or debugging, as they cannot be used in the normal way (e.g., you cannot fetch their contents!). Let's drop this feature, which will eventually let us simplify the object-writing code. This is technically backwards incompatible, but since such objects were never really functional, it seems unlikely that anybody will notice. We will retain the --literally flag, as it also instructs hash-object not to worry about other format issues (e.g., type-specific things that fsck would complain about). The documentation does not need to be updated, as it was always vague about which checks we're loosening (it uses only the phrase "any garbage"). The code change is a bit hard to verify from just the patch text. We can drop our local hash_literally() helper, but it was really just wrapping write_object_file_literally(). We now replace that with calling index_fd(), as we do for the non-literal code path, but dropping the INDEX_FORMAT_CHECK flag. This ends up being the same semantically as what the _literally() code path was doing (modulo handling unknown types, which is our goal). We'll be able to clean up these code paths a bit more in subsequent patches. The existing test is flipped to show that we now reject the unknown type. The additional "extra-long type" test is now redundant, as we bail early upon seeing a bogus type. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b5643b6 commit 65a6a79

File tree

2 files changed

+7
-33
lines changed

2 files changed

+7
-33
lines changed

builtin/hash-object.c

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@ enum {
2424
HASH_OBJECT_WRITE = (1 << 1),
2525
};
2626

27-
/*
28-
* This is to create corrupt objects for debugging and as such it
29-
* needs to bypass the data conversion performed by, and the type
30-
* limitation imposed by, index_fd() and its callees.
31-
*/
32-
static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags)
33-
{
34-
struct strbuf buf = STRBUF_INIT;
35-
int ret;
36-
37-
if (strbuf_read(&buf, fd, 4096) < 0)
38-
ret = -1;
39-
else
40-
ret = write_object_file_literally(buf.buf, buf.len, type, oid,
41-
(flags & HASH_OBJECT_WRITE) ? WRITE_OBJECT_FILE_PERSIST : 0);
42-
close(fd);
43-
strbuf_release(&buf);
44-
return ret;
45-
}
46-
4727
static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
4828
int literally)
4929
{
@@ -56,11 +36,12 @@ static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
5636
if (flags & HASH_OBJECT_CHECK)
5737
index_flags |= INDEX_FORMAT_CHECK;
5838

39+
if (literally)
40+
index_flags &= ~INDEX_FORMAT_CHECK;
41+
5942
if (fstat(fd, &st) < 0 ||
60-
(literally
61-
? hash_literally(&oid, fd, type, flags)
62-
: index_fd(the_repository->index, &oid, fd, &st,
63-
type_from_string(type), path, index_flags)))
43+
index_fd(the_repository->index, &oid, fd, &st,
44+
type_from_string(type), path, index_flags))
6445
die((flags & HASH_OBJECT_WRITE)
6546
? "Unable to add %s to database"
6647
: "Unable to hash %s", path);

t/t1007-hash-object.sh

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,8 @@ test_expect_success 'hash-object complains about truncated type name' '
248248
test_must_fail git hash-object -t bl --stdin </dev/null
249249
'
250250

251-
test_expect_success '--literally' '
252-
t=1234567890 &&
253-
echo example | git hash-object -t $t --literally --stdin
254-
'
255-
256-
test_expect_success '--literally with extra-long type' '
257-
t=12345678901234567890123456789012345678901234567890 &&
258-
t="$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t" &&
259-
echo example | git hash-object -t $t --literally --stdin
251+
test_expect_success '--literally complains about non-standard types' '
252+
test_must_fail git hash-object -t bogus --literally --stdin
260253
'
261254

262255
test_expect_success '--stdin outside of repository (uses SHA-1)' '

0 commit comments

Comments
 (0)