Commit 5d2d21e536b83ca2cbf8c026b3149fdf776c3f58

Russell Belfer 2013-04-16T15:00:43

Consolidate packfile allocation further Rename git_packfile_check to git_packfile_alloc since it is now being used more in that capacity. Fix the various places that use it. Consolidate some repeated code in odb_pack.c related to the allocation of a new pack_backend.

diff --git a/src/indexer.c b/src/indexer.c
index 50a9d3a..6067719 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -62,7 +62,7 @@ static int open_pack(struct git_pack_file **out, const char *filename)
 {
 	struct git_pack_file *pack;
 
-	if (git_packfile_check(&pack, filename) < 0)
+	if (git_packfile_alloc(&pack, filename) < 0)
 		return -1;
 
 	if ((pack->mwf.fd = p_open(pack->pack_name, O_RDONLY)) < 0) {
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 773e149..eec7925 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -207,7 +207,7 @@ static int packfile_load__cb(void *_data, git_buf *path)
 			return 0;
 	}
 
-	error = git_packfile_check(&pack, path->ptr);
+	error = git_packfile_alloc(&pack, path->ptr);
 	if (error == GIT_ENOTFOUND)
 		/* ignore missing .pack file as git does */
 		return 0;
@@ -527,80 +527,75 @@ static void pack_backend__free(git_odb_backend *_backend)
 	git__free(backend);
 }
 
-int git_odb_backend_one_pack(git_odb_backend **backend_out, const char *idx)
+static int pack_backend__alloc(struct pack_backend **out, size_t initial_size)
 {
-	struct pack_backend *backend = NULL;
-	struct git_pack_file *packfile = NULL;
+	struct pack_backend *backend = git__calloc(1, sizeof(struct pack_backend));
+	GITERR_CHECK_ALLOC(backend);
 
-	if (git_packfile_check(&packfile, idx) < 0)
+	if (git_vector_init(&backend->packs, initial_size, packfile_sort__cb) < 0) {
+		git__free(backend);
 		return -1;
+	}
 
-	backend = git__calloc(1, sizeof(struct pack_backend));
-	GITERR_CHECK_ALLOC(backend);
 	backend->parent.version = GIT_ODB_BACKEND_VERSION;
 
-	if (git_vector_init(&backend->packs, 1, NULL) < 0)
-		goto on_error;
-
-	if (git_vector_insert(&backend->packs, packfile) < 0)
-		goto on_error;
-
 	backend->parent.read = &pack_backend__read;
 	backend->parent.read_prefix = &pack_backend__read_prefix;
 	backend->parent.read_header = &pack_backend__read_header;
 	backend->parent.exists = &pack_backend__exists;
 	backend->parent.refresh = &pack_backend__refresh;
 	backend->parent.foreach = &pack_backend__foreach;
+	backend->parent.writepack = &pack_backend__writepack;
 	backend->parent.free = &pack_backend__free;
 
-	*backend_out = (git_odb_backend *)backend;
-
+	*out = backend;
 	return 0;
-
-on_error:
-	git_vector_free(&backend->packs);
-	git__free(backend);
-	git__free(packfile);
-	return -1;
 }
 
-int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
+int git_odb_backend_one_pack(git_odb_backend **backend_out, const char *idx)
 {
 	struct pack_backend *backend = NULL;
-	git_buf path = GIT_BUF_INIT;
+	struct git_pack_file *packfile = NULL;
 
-	backend = git__calloc(1, sizeof(struct pack_backend));
-	GITERR_CHECK_ALLOC(backend);
-	backend->parent.version = GIT_ODB_BACKEND_VERSION;
+	if (pack_backend__alloc(&backend, 1) < 0)
+		return -1;
 
-	if (git_vector_init(&backend->packs, 8, packfile_sort__cb) < 0 ||
-		git_buf_joinpath(&path, objects_dir, "pack") < 0)
+	if (git_packfile_alloc(&packfile, idx) < 0 ||
+		git_vector_insert(&backend->packs, packfile) < 0)
 	{
-		git__free(backend);
+		pack_backend__free((git_odb_backend *)backend);
 		return -1;
 	}
 
-	if (git_path_isdir(git_buf_cstr(&path)) == true) {
-		int error;
+	*backend_out = (git_odb_backend *)backend;
+	return 0;
+}
+
+int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
+{
+	int error = 0;
+	struct pack_backend *backend = NULL;
+	git_buf path = GIT_BUF_INIT;
+
+	if (pack_backend__alloc(&backend, 8) < 0)
+		return -1;
 
+	if (!(error = git_buf_joinpath(&path, objects_dir, "pack")) &&
+		git_path_isdir(git_buf_cstr(&path)))
+	{
 		backend->pack_folder = git_buf_detach(&path);
+
 		error = pack_backend__refresh((git_odb_backend *)backend);
-		if (error < 0)
-			return error;
 	}
 
-	backend->parent.read = &pack_backend__read;
-	backend->parent.read_prefix = &pack_backend__read_prefix;
-	backend->parent.read_header = &pack_backend__read_header;
-	backend->parent.exists = &pack_backend__exists;
-	backend->parent.refresh = &pack_backend__refresh;
-	backend->parent.foreach = &pack_backend__foreach;
-	backend->parent.writepack = &pack_backend__writepack;
-	backend->parent.free = &pack_backend__free;
+	if (error < 0) {
+		pack_backend__free((git_odb_backend *)backend);
+		backend = NULL;
+	}
 
 	*backend_out = (git_odb_backend *)backend;
 
 	git_buf_free(&path);
 
-	return 0;
+	return error;
 }
diff --git a/src/pack.c b/src/pack.c
index 8e8a01a..33cdf76 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -885,7 +885,7 @@ cleanup:
 	return -1;
 }
 
-int git_packfile_check(struct git_pack_file **pack_out, const char *path)
+int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
 {
 	struct stat st;
 	struct git_pack_file *p;
diff --git a/src/pack.h b/src/pack.h
index b8014b1..aeeac9c 100644
--- a/src/pack.h
+++ b/src/pack.h
@@ -143,7 +143,8 @@ git_off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs,
 		git_off_t delta_obj_offset);
 
 void git_packfile_free(struct git_pack_file *p);
-int git_packfile_check(struct git_pack_file **pack_out, const char *path);
+int git_packfile_alloc(struct git_pack_file **pack_out, const char *path);
+
 int git_pack_entry_find(
 		struct git_pack_entry *e,
 		struct git_pack_file *p,