]> asedeno.scripts.mit.edu Git - git.git/commitdiff
Add pretend_sha1_file() interface.
authorJunio C Hamano <junkio@cox.net>
Mon, 5 Feb 2007 05:42:38 +0000 (21:42 -0800)
committerJunio C Hamano <junkio@cox.net>
Mon, 5 Feb 2007 22:55:11 +0000 (14:55 -0800)
The new interface allows an application to temporarily hash a
small number of objects and pretend that they are available in
the object store without actually writing them.

Signed-off-by: Junio C Hamano <junkio@cox.net>
cache.h
sha1_file.c

diff --git a/cache.h b/cache.h
index 38a9bc02f6d765fb743d6070bbd145cdd673d43d..87142571b3a7abe7772386afebad73e69ac9540e 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -257,6 +257,7 @@ extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, uns
 extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);
 extern int hash_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *sha1);
 extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
+extern int pretend_sha1_file(void *, unsigned long, const char *, unsigned char *);
 
 extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);
 
index 1526a28095bdb161fd60554a994aefd7cebee27c..0d4bf80e74b16aa089acd06c24ad1b1a039e65b5 100644 (file)
@@ -1505,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)