git-svn: reintroduce using a single get_log() to fetch
[gitweb.git] / sha1_file.c
index 1526a28095bdb161fd60554a994aefd7cebee27c..9a1dee051a20891698f44814824ba8aa3b83bc3a 100644 (file)
@@ -407,7 +407,6 @@ static unsigned int peak_pack_open_windows;
 static unsigned int pack_open_windows;
 static size_t peak_pack_mapped;
 static size_t pack_mapped;
-static size_t page_size;
 struct packed_git *packed_git;
 
 void pack_report()
@@ -416,7 +415,7 @@ void pack_report()
                "pack_report: getpagesize()            = %10" SZ_FMT "\n"
                "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n"
                "pack_report: core.packedGitLimit      = %10" SZ_FMT "\n",
-               page_size,
+               (size_t) getpagesize(),
                packed_git_window_size,
                packed_git_limit);
        fprintf(stderr,
@@ -662,10 +661,9 @@ unsigned char* use_pack(struct packed_git *p,
                                break;
                }
                if (!win) {
-                       if (!page_size)
-                               page_size = getpagesize();
+                       size_t window_align = packed_git_window_size / 2;
                        win = xcalloc(1, sizeof(*win));
-                       win->offset = (offset / page_size) * page_size;
+                       win->offset = (offset / window_align) * window_align;
                        win->len = p->pack_size - win->offset;
                        if (win->len > packed_git_window_size)
                                win->len = packed_git_window_size;
@@ -1505,10 +1503,69 @@ static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned lo
                return unpack_entry(e.p, e.offset, type, size);
 }
 
-void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
+/*
+ * This is meant to hold a *small* number of objects that you would
+ * want read_sha1_file() to be able to return, but yet you do not want
+ * to write them into the object store (e.g. a browse-only
+ * application).
+ */
+static struct cached_object {
+       unsigned char sha1[20];
+       const char *type;
+       void *buf;
+       unsigned long size;
+} *cached_objects;
+static int cached_object_nr, cached_object_alloc;
+
+static struct cached_object *find_cached_object(const unsigned char *sha1)
+{
+       int i;
+       struct cached_object *co = cached_objects;
+
+       for (i = 0; i < cached_object_nr; i++, co++) {
+               if (!hashcmp(co->sha1, sha1))
+                       return co;
+       }
+       return NULL;
+}
+
+int pretend_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1)
+{
+       struct cached_object *co;
+
+       hash_sha1_file(buf, len, type, sha1);
+       if (has_sha1_file(sha1) || find_cached_object(sha1))
+               return 0;
+       if (cached_object_alloc <= cached_object_nr) {
+               cached_object_alloc = alloc_nr(cached_object_alloc);
+               cached_objects = xrealloc(cached_objects,
+                                         sizeof(*cached_objects) *
+                                         cached_object_alloc);
+       }
+       co = &cached_objects[cached_object_nr++];
+       co->size = len;
+       co->type = strdup(type);
+       co->buf = xmalloc(len);
+       memcpy(co->buf, buf, len);
+       hashcpy(co->sha1, sha1);
+       return 0;
+}
+
+void *read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size)
 {
        unsigned long mapsize;
        void *map, *buf;
+       struct cached_object *co;
+
+       co = find_cached_object(sha1);
+       if (co) {
+               buf = xmalloc(co->size + 1);
+               memcpy(buf, co->buf, co->size);
+               ((char*)buf)[co->size] = 0;
+               strcpy(type, co->type);
+               *size = co->size;
+               return buf;
+       }
 
        buf = read_packed_sha1(sha1, type, size);
        if (buf)
@@ -2025,7 +2082,7 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con
 {
        unsigned long size = st->st_size;
        void *buf;
-       int ret;
+       int ret, re_allocated = 0;
 
        buf = "";
        if (size)
@@ -2034,10 +2091,30 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, con
 
        if (!type)
                type = blob_type;
+
+       /*
+        * Convert blobs to git internal format
+        */
+       if (!strcmp(type, blob_type)) {
+               unsigned long nsize = size;
+               char *nbuf = buf;
+               if (convert_to_git(NULL, &nbuf, &nsize)) {
+                       if (size)
+                               munmap(buf, size);
+                       size = nsize;
+                       buf = nbuf;
+                       re_allocated = 1;
+               }
+       }
+
        if (write_object)
                ret = write_sha1_file(buf, size, type, sha1);
        else
                ret = hash_sha1_file(buf, size, type, sha1);
+       if (re_allocated) {
+               free(buf);
+               return ret;
+       }
        if (size)
                munmap(buf, size);
        return ret;