]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit: fix duplication regression in permission error output
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Tue, 12 Oct 2021 14:30:49 +0000 (16:30 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 12 Oct 2021 18:16:59 +0000 (11:16 -0700)
Fix a regression in the error output emitted when .git/objects can't
be written to. Before 9c4d6c0297b (cache-tree: Write updated
cache-tree after commit, 2014-07-13) we'd emit only one "insufficient
permission" error, now we'll do so again.

The cause is rather straightforward, we've got WRITE_TREE_SILENT for
the use-case of wanting to prepare an index silently, quieting any
permission etc. error output. Then when we attempt to update to
that (possibly broken) index we'll run into the same errors again.

But with 9c4d6c0297b the gap between the cache-tree API and the object
store wasn't closed in terms of asking write_object_file() to be
silent. I.e. post-9c4d6c0297b the first call is to prepare_index(),
and after that we'll call prepare_to_commit(). We only want verbose
error output from the latter.

So let's add and use that facility with a corresponding HASH_SILENT
flag, its only user is cache-tree.c's update_one(), which will set it
if its "WRITE_TREE_SILENT" flag is set.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache-tree.c
cache.h
object-file.c
object-store.h
t/t0004-unwritable.sh

index 90919f9e3454791e2132236a2319b5f51c9c1d41..f21bfc631c6be4d0eaf5859a5ba61887827035fb 100644 (file)
@@ -440,8 +440,9 @@ static int update_one(struct cache_tree *it,
        } else if (dryrun) {
                hash_object_file(the_hash_algo, buffer.buf, buffer.len,
                                 tree_type, &it->oid);
-       } else if (write_object_file(buffer.buf, buffer.len, tree_type,
-                                    &it->oid)) {
+       } else if (write_object_file_flags(buffer.buf, buffer.len, tree_type,
+                                          &it->oid, flags & WRITE_TREE_SILENT
+                                          ? HASH_SILENT : 0)) {
                strbuf_release(&buffer);
                return -1;
        }
diff --git a/cache.h b/cache.h
index bd4869beee4bb06412e50fe70f2b64fc255fecad..6d7db7bd0f67f9d397985eea2b7b3f6546a79ab1 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -887,6 +887,7 @@ int ie_modified(struct index_state *, const struct cache_entry *, struct stat *,
 #define HASH_WRITE_OBJECT 1
 #define HASH_FORMAT_CHECK 2
 #define HASH_RENORMALIZE  4
+#define HASH_SILENT 8
 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);
 int index_path(struct index_state *istate, struct object_id *oid, const char *path, struct stat *st, unsigned flags);
 
index a8be8994814933e9d1afaae77edb93ec9934a3ac..14825afd3c81363c12734999e03131f7867cd96a 100644 (file)
@@ -1915,7 +1915,7 @@ static int create_tmpfile(struct strbuf *tmp, const char *filename)
 
 static int write_loose_object(const struct object_id *oid, char *hdr,
                              int hdrlen, const void *buf, unsigned long len,
-                             time_t mtime)
+                             time_t mtime, unsigned flags)
 {
        int fd, ret;
        unsigned char compressed[4096];
@@ -1929,7 +1929,9 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
 
        fd = create_tmpfile(&tmp_file, filename.buf);
        if (fd < 0) {
-               if (errno == EACCES)
+               if (flags & HASH_SILENT)
+                       return -1;
+               else if (errno == EACCES)
                        return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
                else
                        return error_errno(_("unable to create temporary file"));
@@ -1979,7 +1981,8 @@ static int write_loose_object(const struct object_id *oid, char *hdr,
                struct utimbuf utb;
                utb.actime = mtime;
                utb.modtime = mtime;
-               if (utime(tmp_file.buf, &utb) < 0)
+               if (utime(tmp_file.buf, &utb) < 0 &&
+                   !(flags & HASH_SILENT))
                        warning_errno(_("failed utime() on %s"), tmp_file.buf);
        }
 
@@ -2004,8 +2007,9 @@ static int freshen_packed_object(const struct object_id *oid)
        return 1;
 }
 
-int write_object_file(const void *buf, unsigned long len, const char *type,
-                     struct object_id *oid)
+int write_object_file_flags(const void *buf, unsigned long len,
+                           const char *type, struct object_id *oid,
+                           unsigned flags)
 {
        char hdr[MAX_HEADER_LEN];
        int hdrlen = sizeof(hdr);
@@ -2017,7 +2021,7 @@ int write_object_file(const void *buf, unsigned long len, const char *type,
                                  &hdrlen);
        if (freshen_packed_object(oid) || freshen_loose_object(oid))
                return 0;
-       return write_loose_object(oid, hdr, hdrlen, buf, len, 0);
+       return write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags);
 }
 
 int hash_object_file_literally(const void *buf, unsigned long len,
@@ -2037,7 +2041,7 @@ int hash_object_file_literally(const void *buf, unsigned long len,
                goto cleanup;
        if (freshen_packed_object(oid) || freshen_loose_object(oid))
                goto cleanup;
-       status = write_loose_object(oid, header, hdrlen, buf, len, 0);
+       status = write_loose_object(oid, header, hdrlen, buf, len, 0, 0);
 
 cleanup:
        free(header);
@@ -2059,7 +2063,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
        if (!buf)
                return error(_("cannot read object for %s"), oid_to_hex(oid));
        hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1;
-       ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime);
+       ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0);
        free(buf);
 
        return ret;
index d24915ced1b2dd2aedb8300741d83372d9ea53a0..8bd4c788a7ad1a43e6c952970fbc22ba8e40a18a 100644 (file)
@@ -222,8 +222,14 @@ int hash_object_file(const struct git_hash_algo *algo, const void *buf,
                     unsigned long len, const char *type,
                     struct object_id *oid);
 
-int write_object_file(const void *buf, unsigned long len,
-                     const char *type, struct object_id *oid);
+int write_object_file_flags(const void *buf, unsigned long len,
+                           const char *type, struct object_id *oid,
+                           unsigned flags);
+static inline int write_object_file(const void *buf, unsigned long len,
+                                   const char *type, struct object_id *oid)
+{
+       return write_object_file_flags(buf, len, type, oid, 0);
+}
 
 int hash_object_file_literally(const void *buf, unsigned long len,
                               const char *type, struct object_id *oid,
index d26d18413cc6266e55bfea8e3f2d22a65e9c406c..3bdafbae0f003863b09d01c6ded07c8c1be14e03 100755 (executable)
@@ -37,7 +37,7 @@ test_expect_success POSIXPERM,SANITY 'commit should notice unwritable repository
 '
 
 test_lazy_prereq COMMIT_OUT 'test -e "$TRASH_DIRECTORY"/out.commit'
-test_expect_failure COMMIT_OUT 'commit output on unwritable repository' '
+test_expect_success COMMIT_OUT 'commit output on unwritable repository' '
        cat >expect <<-\EOF &&
        error: insufficient permission for adding an object to repository database .git/objects
        error: Error building trees