Commit c968ce2c2c49ca7a559ecb8f7014f777c3a8a5f3

Carlos Martín Nieto 2014-05-12T02:01:05

pack: don't forget to cache the base object The base object is a good cache candidate, so we shouldn't forget to add it to the cache.

diff --git a/src/pack.c b/src/pack.c
index 235e8d3..d93ee25 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -668,7 +668,6 @@ int git_packfile_unpack(
 			error = packfile_unpack_compressed(obj, p, &w_curs, &curpos, elem->size, elem->type);
 			git_mwindow_close(&w_curs);
 			base_type = elem->type;
-			free_base = 1;
 		}
 		if (error < 0)
 			goto cleanup;
@@ -683,7 +682,7 @@ int git_packfile_unpack(
 	}
 
 	/*
-	 * Finding the object we want as the base element is
+	 * Finding the object we want a cached base element is
 	 * problematic, as we need to make sure we don't accidentally
 	 * give the caller the cached object, which it would then feel
 	 * free to free, so we need to copy the data.
@@ -701,6 +700,13 @@ int git_packfile_unpack(
 	while (elem_pos > 0 && !error) {
 		git_rawobj base, delta;
 
+		/*
+		 * We can now try to add the base to the cache, as
+		 * long as it's not already the cached one.
+		 */
+		if (!cached)
+			free_base = !!cache_add(&p->bases, obj, elem->base_key);
+
 		elem = &stack[elem_pos - 1];
 		curpos = elem->offset;
 		error = packfile_unpack_compressed(&delta, p, &w_curs, &curpos, elem->size, elem->type);
@@ -737,11 +743,6 @@ int git_packfile_unpack(
 		if (error < 0)
 			break;
 
-		/* only try to cache if we're not handing this buffer off to the caller */
-		if (elem_pos != 1 &&
-		    (error = cache_add(&p->bases, obj, elem->base_key)) < 0)
-			goto cleanup;
-
 		elem_pos--;
 	}