Skip to content

Commit 2c428e4

Browse files
committed
Merge branch 'ab/fix-commit-error-message-upon-unwritable-object-store'
"git commit" gave duplicated error message when the object store was unwritable, which has been corrected. * ab/fix-commit-error-message-upon-unwritable-object-store: commit: fix duplication regression in permission error output unwritable tests: assert exact error output
2 parents 6ffb5fc + 4ef91a2 commit 2c428e4

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

cache-tree.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,9 @@ static int update_one(struct cache_tree *it,
440440
} else if (dryrun) {
441441
hash_object_file(the_hash_algo, buffer.buf, buffer.len,
442442
tree_type, &it->oid);
443-
} else if (write_object_file(buffer.buf, buffer.len, tree_type,
444-
&it->oid)) {
443+
} else if (write_object_file_flags(buffer.buf, buffer.len, tree_type,
444+
&it->oid, flags & WRITE_TREE_SILENT
445+
? HASH_SILENT : 0)) {
445446
strbuf_release(&buffer);
446447
return -1;
447448
}

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ int ie_modified(struct index_state *, const struct cache_entry *, struct stat *,
887887
#define HASH_WRITE_OBJECT 1
888888
#define HASH_FORMAT_CHECK 2
889889
#define HASH_RENORMALIZE 4
890+
#define HASH_SILENT 8
890891
int index_fd(struct index_state *istate, struct object_id *oid, int fd, struct stat *st, enum object_type type, const char *path, unsigned flags);
891892
int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
892893

object-file.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,7 @@ static int create_tmpfile(struct strbuf *tmp, const char *filename)
18621862

18631863
static int write_loose_object(const struct object_id *oid, char *hdr,
18641864
int hdrlen, const void *buf, unsigned long len,
1865-
time_t mtime)
1865+
time_t mtime, unsigned flags)
18661866
{
18671867
int fd, ret;
18681868
unsigned char compressed[4096];
@@ -1876,7 +1876,9 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
18761876

18771877
fd = create_tmpfile(&tmp_file, filename.buf);
18781878
if (fd < 0) {
1879-
if (errno == EACCES)
1879+
if (flags & HASH_SILENT)
1880+
return -1;
1881+
else if (errno == EACCES)
18801882
return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
18811883
else
18821884
return error_errno(_("unable to create temporary file"));
@@ -1926,7 +1928,8 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
19261928
struct utimbuf utb;
19271929
utb.actime = mtime;
19281930
utb.modtime = mtime;
1929-
if (utime(tmp_file.buf, &utb) < 0)
1931+
if (utime(tmp_file.buf, &utb) < 0 &&
1932+
!(flags & HASH_SILENT))
19301933
warning_errno(_("failed utime() on %s"), tmp_file.buf);
19311934
}
19321935

@@ -1951,8 +1954,9 @@ static int freshen_packed_object(const struct object_id *oid)
19511954
return 1;
19521955
}
19531956

1954-
int write_object_file(const void *buf, unsigned long len, const char *type,
1955-
struct object_id *oid)
1957+
int write_object_file_flags(const void *buf, unsigned long len,
1958+
const char *type, struct object_id *oid,
1959+
unsigned flags)
19561960
{
19571961
char hdr[MAX_HEADER_LEN];
19581962
int hdrlen = sizeof(hdr);
@@ -1964,7 +1968,7 @@ int write_object_file(const void *buf, unsigned long len, const char *type,
19641968
&hdrlen);
19651969
if (freshen_packed_object(oid) || freshen_loose_object(oid))
19661970
return 0;
1967-
return write_loose_object(oid, hdr, hdrlen, buf, len, 0);
1971+
return write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags);
19681972
}
19691973

19701974
int hash_object_file_literally(const void *buf, unsigned long len,
@@ -1984,7 +1988,7 @@ int hash_object_file_literally(const void *buf, unsigned long len,
19841988
goto cleanup;
19851989
if (freshen_packed_object(oid) || freshen_loose_object(oid))
19861990
goto cleanup;
1987-
status = write_loose_object(oid, header, hdrlen, buf, len, 0);
1991+
status = write_loose_object(oid, header, hdrlen, buf, len, 0, 0);
19881992

19891993
cleanup:
19901994
free(header);
@@ -2006,7 +2010,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
20062010
if (!buf)
20072011
return error(_("cannot read object for %s"), oid_to_hex(oid));
20082012
hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1;
2009-
ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime);
2013+
ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0);
20102014
free(buf);
20112015

20122016
return ret;

object-store.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,14 @@ int hash_object_file(const struct git_hash_algo *algo, const void *buf,
223223
unsigned long len, const char *type,
224224
struct object_id *oid);
225225

226-
int write_object_file(const void *buf, unsigned long len,
227-
const char *type, struct object_id *oid);
226+
int write_object_file_flags(const void *buf, unsigned long len,
227+
const char *type, struct object_id *oid,
228+
unsigned flags);
229+
static inline int write_object_file(const void *buf, unsigned long len,
230+
const char *type, struct object_id *oid)
231+
{
232+
return write_object_file_flags(buf, len, type, oid, 0);
233+
}
228234

229235
int hash_object_file_literally(const void *buf, unsigned long len,
230236
const char *type, struct object_id *oid,

t/t0004-unwritable.sh

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,66 @@ test_expect_success setup '
1919
test_expect_success POSIXPERM,SANITY 'write-tree should notice unwritable repository' '
2020
test_when_finished "chmod 775 .git/objects .git/objects/??" &&
2121
chmod a-w .git/objects .git/objects/?? &&
22-
test_must_fail git write-tree
22+
test_must_fail git write-tree 2>out.write-tree
23+
'
24+
25+
test_lazy_prereq WRITE_TREE_OUT 'test -e "$TRASH_DIRECTORY"/out.write-tree'
26+
test_expect_success WRITE_TREE_OUT 'write-tree output on unwritable repository' '
27+
cat >expect <<-\EOF &&
28+
error: insufficient permission for adding an object to repository database .git/objects
29+
fatal: git-write-tree: error building trees
30+
EOF
31+
test_cmp expect out.write-tree
2332
'
2433

2534
test_expect_success POSIXPERM,SANITY,!SANITIZE_LEAK 'commit should notice unwritable repository' '
2635
test_when_finished "chmod 775 .git/objects .git/objects/??" &&
2736
chmod a-w .git/objects .git/objects/?? &&
28-
test_must_fail git commit -m second
37+
test_must_fail git commit -m second 2>out.commit
38+
'
39+
40+
test_lazy_prereq COMMIT_OUT 'test -e "$TRASH_DIRECTORY"/out.commit'
41+
test_expect_success COMMIT_OUT 'commit output on unwritable repository' '
42+
cat >expect <<-\EOF &&
43+
error: insufficient permission for adding an object to repository database .git/objects
44+
error: Error building trees
45+
EOF
46+
test_cmp expect out.commit
2947
'
3048

3149
test_expect_success POSIXPERM,SANITY 'update-index should notice unwritable repository' '
3250
test_when_finished "chmod 775 .git/objects .git/objects/??" &&
3351
echo 6O >file &&
3452
chmod a-w .git/objects .git/objects/?? &&
35-
test_must_fail git update-index file
53+
test_must_fail git update-index file 2>out.update-index
54+
'
55+
56+
test_lazy_prereq UPDATE_INDEX_OUT 'test -e "$TRASH_DIRECTORY"/out.update-index'
57+
test_expect_success UPDATE_INDEX_OUT 'update-index output on unwritable repository' '
58+
cat >expect <<-\EOF &&
59+
error: insufficient permission for adding an object to repository database .git/objects
60+
error: file: failed to insert into database
61+
fatal: Unable to process path file
62+
EOF
63+
test_cmp expect out.update-index
3664
'
3765

3866
test_expect_success POSIXPERM,SANITY 'add should notice unwritable repository' '
3967
test_when_finished "chmod 775 .git/objects .git/objects/??" &&
4068
echo b >file &&
4169
chmod a-w .git/objects .git/objects/?? &&
42-
test_must_fail git add file
70+
test_must_fail git add file 2>out.add
71+
'
72+
73+
test_lazy_prereq ADD_OUT 'test -e "$TRASH_DIRECTORY"/out.add'
74+
test_expect_success ADD_OUT 'add output on unwritable repository' '
75+
cat >expect <<-\EOF &&
76+
error: insufficient permission for adding an object to repository database .git/objects
77+
error: file: failed to insert into database
78+
error: unable to index file '\''file'\''
79+
fatal: updating files failed
80+
EOF
81+
test_cmp expect out.add
4382
'
4483

4584
test_done

0 commit comments

Comments
 (0)