git-svn: just use Digest::MD5 instead of requiring it
[gitweb.git] / read-cache.c
index 29cf9abe6455313d94d169cc5f16aac21cad3228..605b35239674c72a272e1c0fc1fb886c4b75be46 100644 (file)
@@ -344,16 +344,17 @@ int add_file_to_index(const char *path, int verbose)
        ce->ce_flags = htons(namelen);
        fill_stat_cache_info(ce, &st);
 
-       ce->ce_mode = create_ce_mode(st.st_mode);
-       if (!trust_executable_bit) {
+       if (trust_executable_bit)
+               ce->ce_mode = create_ce_mode(st.st_mode);
+       else {
                /* If there is an existing entry, pick the mode bits
                 * from it, otherwise assume unexecutable.
                 */
+               struct cache_entry *ent;
                int pos = cache_name_pos(path, namelen);
-               if (pos >= 0)
-                       ce->ce_mode = active_cache[pos]->ce_mode;
-               else if (S_ISREG(st.st_mode))
-                       ce->ce_mode = create_ce_mode(S_IFREG | 0666);
+
+               ent = (0 <= pos) ? active_cache[pos] : NULL;
+               ce->ce_mode = ce_mode_from_stat(ent, st.st_mode);
        }
 
        if (index_path(ce->sha1, path, &st, 1))
@@ -870,7 +871,7 @@ static int ce_write_flush(SHA_CTX *context, int fd)
        unsigned int buffered = write_buffer_len;
        if (buffered) {
                SHA1_Update(context, write_buffer, buffered);
-               if (write(fd, write_buffer, buffered) != buffered)
+               if (write_in_full(fd, write_buffer, buffered) != buffered)
                        return -1;
                write_buffer_len = 0;
        }
@@ -919,7 +920,7 @@ static int ce_flush(SHA_CTX *context, int fd)
 
        /* Flush first if not enough space for SHA1 signature */
        if (left + 20 > WRITE_BUFFER_SIZE) {
-               if (write(fd, write_buffer, left) != left)
+               if (write_in_full(fd, write_buffer, left) != left)
                        return -1;
                left = 0;
        }
@@ -927,7 +928,7 @@ static int ce_flush(SHA_CTX *context, int fd)
        /* Append the SHA1 signature at the end */
        SHA1_Final(write_buffer + left, context);
        left += 20;
-       return (write(fd, write_buffer, left) != left) ? -1 : 0;
+       return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
 }
 
 static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
@@ -1010,7 +1011,7 @@ int write_cache(int newfd, struct cache_entry **cache, int entries)
                if (data &&
                    !write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sz) &&
                    !ce_write(&c, newfd, data, sz))
-                       ;
+                       free(data);
                else {
                        free(data);
                        return -1;