Skip to content

Commit b37f81b

Browse files
committed
Merge branch 'jh/note-trees-record-blobs'
"git notes -C <blob>" should not take an object that is not a blob. * jh/note-trees-record-blobs: notes: disallow reusing non-blob as a note object
2 parents c923f60 + ce8daa1 commit b37f81b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

builtin/notes.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
269269
die(_("Failed to resolve '%s' as a valid ref."), arg);
270270
if (!(buf = read_sha1_file(object, &type, &len)) || !len) {
271271
free(buf);
272-
die(_("Failed to read object '%s'."), arg);;
272+
die(_("Failed to read object '%s'."), arg);
273+
}
274+
if (type != OBJ_BLOB) {
275+
free(buf);
276+
die(_("Cannot read note data from non-blob object '%s'."), arg);
273277
}
274278
strbuf_add(&(msg->buf), buf, len);
275279
free(buf);

t/t3301-notes.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,33 @@ test_expect_success 'create note from non-existing note with "git notes add -C"
812812
test_must_fail git notes list HEAD
813813
'
814814

815+
test_expect_success 'create note from non-blob with "git notes add -C" fails' '
816+
commit=$(git rev-parse --verify HEAD) &&
817+
tree=$(git rev-parse --verify HEAD:) &&
818+
test_must_fail git notes add -C $commit &&
819+
test_must_fail git notes add -C $tree &&
820+
test_must_fail git notes list HEAD
821+
'
822+
823+
cat > expect << EOF
824+
commit 80d796defacd5db327b7a4e50099663902fbdc5c
825+
Author: A U Thor <[email protected]>
826+
Date: Thu Apr 7 15:20:13 2005 -0700
827+
828+
8th
829+
830+
Notes (other):
831+
This is a blob object
832+
EOF
833+
834+
test_expect_success 'create note from blob with "git notes add -C" reuses blob id' '
835+
blob=$(echo "This is a blob object" | git hash-object -w --stdin) &&
836+
git notes add -C $blob &&
837+
git log -1 > actual &&
838+
test_cmp expect actual &&
839+
test "$(git notes list HEAD)" = "$blob"
840+
'
841+
815842
cat > expect << EOF
816843
commit 016e982bad97eacdbda0fcbd7ce5b0ba87c81f1b
817844
Author: A U Thor <[email protected]>

0 commit comments

Comments
 (0)