Merge branch 'ab/sha1dc'
[gitweb.git] / sha1_file.c
index bf6b64ec8f5053d6e705fb887ea1969ef4493fe2..5862386cd0c2b2020890905bd06cd3a9d89c123b 100644 (file)
@@ -7,6 +7,7 @@
  * creation etc.
  */
 #include "cache.h"
+#include "config.h"
 #include "string-list.h"
 #include "lockfile.h"
 #include "delta.h"
@@ -610,8 +611,7 @@ char *compute_alternate_path(const char *path, struct strbuf *err)
 
 out:
        if (seen_error) {
-               free(ref_git);
-               ref_git = NULL;
+               FREE_AND_NULL(ref_git);
        }
 
        return ref_git;
@@ -3494,18 +3494,10 @@ int has_sha1_pack(const unsigned char *sha1)
 
 int has_sha1_file_with_flags(const unsigned char *sha1, int flags)
 {
-       struct pack_entry e;
-
        if (!startup_info->have_repository)
                return 0;
-       if (find_pack_entry(sha1, &e))
-               return 1;
-       if (has_loose_object(sha1))
-               return 1;
-       if (flags & HAS_SHA1_QUICK)
-               return 0;
-       reprepare_packed_git();
-       return find_pack_entry(sha1, &e);
+       return sha1_object_info_extended(sha1, NULL,
+                                        flags | OBJECT_INFO_SKIP_CACHED) >= 0;
 }
 
 int has_object_file(const struct object_id *oid)
@@ -3561,7 +3553,7 @@ static int index_mem(unsigned char *sha1, void *buf, size_t size,
         */
        if ((type == OBJ_BLOB) && path) {
                struct strbuf nbuf = STRBUF_INIT;
-               if (convert_to_git(path, buf, size, &nbuf,
+               if (convert_to_git(&the_index, path, buf, size, &nbuf,
                                   write_object ? safe_crlf : SAFE_CRLF_FALSE)) {
                        buf = strbuf_detach(&nbuf, &size);
                        re_allocated = 1;
@@ -3595,7 +3587,7 @@ static int index_stream_convert_blob(unsigned char *sha1, int fd,
        assert(path);
        assert(would_convert_to_git_filter_fd(path));
 
-       convert_to_git_filter_fd(path, fd, &sbuf,
+       convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
                                 write_object ? safe_crlf : SAFE_CRLF_FALSE);
 
        if (write_object)
@@ -3683,7 +3675,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st,
        else if (!S_ISREG(st->st_mode))
                ret = index_pipe(sha1, fd, type, path, flags);
        else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
-                (path && would_convert_to_git(path)))
+                (path && would_convert_to_git(&the_index, path)))
                ret = index_core(sha1, fd, xsize_t(st->st_size), type, path,
                                 flags);
        else
@@ -3750,22 +3742,32 @@ void assert_sha1_type(const unsigned char *sha1, enum object_type expect)
                    typename(expect));
 }
 
-static int for_each_file_in_obj_subdir(int subdir_nr,
-                                      struct strbuf *path,
-                                      each_loose_object_fn obj_cb,
-                                      each_loose_cruft_fn cruft_cb,
-                                      each_loose_subdir_fn subdir_cb,
-                                      void *data)
+int for_each_file_in_obj_subdir(unsigned int subdir_nr,
+                               struct strbuf *path,
+                               each_loose_object_fn obj_cb,
+                               each_loose_cruft_fn cruft_cb,
+                               each_loose_subdir_fn subdir_cb,
+                               void *data)
 {
-       size_t baselen = path->len;
-       DIR *dir = opendir(path->buf);
+       size_t origlen, baselen;
+       DIR *dir;
        struct dirent *de;
        int r = 0;
 
+       if (subdir_nr > 0xff)
+               BUG("invalid loose object subdirectory: %x", subdir_nr);
+
+       origlen = path->len;
+       strbuf_complete(path, '/');
+       strbuf_addf(path, "%02x", subdir_nr);
+       baselen = path->len;
+
+       dir = opendir(path->buf);
        if (!dir) {
-               if (errno == ENOENT)
-                       return 0;
-               return error_errno("unable to open %s", path->buf);
+               if (errno != ENOENT)
+                       r = error_errno("unable to open %s", path->buf);
+               strbuf_setlen(path, origlen);
+               return r;
        }
 
        while ((de = readdir(dir))) {
@@ -3803,6 +3805,8 @@ static int for_each_file_in_obj_subdir(int subdir_nr,
        if (!r && subdir_cb)
                r = subdir_cb(subdir_nr, path->buf, data);
 
+       strbuf_setlen(path, origlen);
+
        return r;
 }
 
@@ -3812,15 +3816,12 @@ int for_each_loose_file_in_objdir_buf(struct strbuf *path,
                            each_loose_subdir_fn subdir_cb,
                            void *data)
 {
-       size_t baselen = path->len;
        int r = 0;
        int i;
 
        for (i = 0; i < 256; i++) {
-               strbuf_addf(path, "/%02x", i);
                r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
                                                subdir_cb, data);
-               strbuf_setlen(path, baselen);
                if (r)
                        break;
        }