Skip to content

Commit edae5f0

Browse files
committed
write-tree: properly detect failure to write tree objects
Tomasz Fortuna reported that "git commit" does not error out properly when it cannot write tree objects out. "git write-tree" shares the same issue, as the failure to notice the error is deep in the logic to write tree objects out recursively. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4f7ec79 commit edae5f0

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

cache-tree.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,11 @@ static int update_one(struct cache_tree *it,
341341

342342
if (dryrun)
343343
hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
344-
else
345-
write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
344+
else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
345+
strbuf_release(&buffer);
346+
return -1;
347+
}
348+
346349
strbuf_release(&buffer);
347350
it->entry_count = i;
348351
#if DEBUG

t/t0004-unwritable.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/sh
2+
3+
test_description='detect unwritable repository and fail correctly'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success setup '
8+
9+
>file &&
10+
git add file &&
11+
git commit -m initial &&
12+
echo >file &&
13+
git add file
14+
15+
'
16+
17+
test_expect_success 'write-tree should notice unwritable repository' '
18+
19+
(
20+
chmod a-w .git/objects
21+
test_must_fail git write-tree
22+
)
23+
status=$?
24+
chmod 775 .git/objects
25+
(exit $status)
26+
27+
'
28+
29+
test_expect_success 'commit should notice unwritable repository' '
30+
31+
(
32+
chmod a-w .git/objects
33+
test_must_fail git commit -m second
34+
)
35+
status=$?
36+
chmod 775 .git/objects
37+
(exit $status)
38+
39+
'
40+
41+
test_expect_success 'update-index should notice unwritable repository' '
42+
43+
(
44+
echo a >file &&
45+
chmod a-w .git/objects
46+
test_must_fail git update-index file
47+
)
48+
status=$?
49+
chmod 775 .git/objects
50+
(exit $status)
51+
52+
'
53+
54+
test_expect_success 'add should notice unwritable repository' '
55+
56+
(
57+
echo b >file &&
58+
chmod a-w .git/objects
59+
test_must_fail git add file
60+
)
61+
status=$?
62+
chmod 775 .git/objects
63+
(exit $status)
64+
65+
'
66+
67+
test_done

0 commit comments

Comments
 (0)