Code

Merge branch 'master' of git://repo.or.cz/git-gui
[git.git] / sha1_file.c
index eec4f418bcd07db32bbbdb116c0a3b2aa00be438..0d4bf80e74b16aa089acd06c24ad1b1a039e65b5 100644 (file)
@@ -552,7 +552,11 @@ void unuse_pack(struct pack_window **w_cursor)
        }
 }
 
-static int open_packed_git(struct packed_git *p)
+/*
+ * Do not call this directly as this leaks p->pack_fd on error return;
+ * call open_packed_git() instead.
+ */
+static int open_packed_git_1(struct packed_git *p)
 {
        struct stat st;
        struct pack_header hdr;
@@ -608,6 +612,17 @@ static int open_packed_git(struct packed_git *p)
        return 0;
 }
 
+static int open_packed_git(struct packed_git *p)
+{
+       if (!open_packed_git_1(p))
+               return 0;
+       if (p->pack_fd != -1) {
+               close(p->pack_fd);
+               p->pack_fd = -1;
+       }
+       return -1;
+}
+
 static int in_window(struct pack_window *win, unsigned long offset)
 {
        /* We must promise at least 20 bytes (one hash) after the
@@ -1148,7 +1163,7 @@ static unsigned long unpack_object_header(struct packed_git *p,
 
        /* use_pack() assures us we have [base, base + 20) available
         * as a range that we can look at at.  (Its actually the hash
-        * size that is assurred.)  With our object header encoding
+        * size that is assured.)  With our object header encoding
         * the maximum deflated object size is 2^137, which is just
         * insane, so we know won't exceed what we have been given.
         */
@@ -1408,6 +1423,18 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e, cons
                }
                offset = find_pack_entry_one(sha1, p);
                if (offset) {
+                       /*
+                        * We are about to tell the caller where they can
+                        * locate the requested object.  We better make
+                        * sure the packfile is still here and can be
+                        * accessed before supplying that answer, as
+                        * it may have been deleted since the index
+                        * was loaded!
+                        */
+                       if (p->pack_fd == -1 && open_packed_git(p)) {
+                               error("packfile %s cannot be accessed", p->pack_name);
+                               continue;
+                       }
                        e->offset = offset;
                        e->p = p;
                        hashcpy(e->sha1, sha1);
@@ -1478,10 +1505,67 @@ static void *read_packed_sha1(const unsigned char *sha1, char *type, unsigned lo
                return unpack_entry(e.p, e.offset, type, 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);
+       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)