Commit 29e92d385e0bb43d45ddea5f8173d78fcceac3a6

Russell Belfer 2013-09-10T16:53:09

Hook up filter initialize callback I knew I forgot something

diff --git a/include/git2/sys/filter.h b/include/git2/sys/filter.h
index ca1fbfc..dbb086b 100644
--- a/include/git2/sys/filter.h
+++ b/include/git2/sys/filter.h
@@ -139,7 +139,9 @@ typedef void (*git_filter_cleanup_fn)(
  */
 struct git_filter {
 	unsigned int           version;
+
 	const char            *attributes;
+
 	git_filter_init_fn     initialize;
 	git_filter_shutdown_fn shutdown;
 	git_filter_check_fn    check;
diff --git a/src/filter.c b/src/filter.c
index de0d490..0500146 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -39,6 +39,7 @@ typedef struct {
 	const char *filter_name;
 	git_filter *filter;
 	int priority;
+	int initialized;
 	size_t nattrs, nmatches;
 	char *attrdata;
 	const char *attrs[GIT_FLEX_ARRAY];
@@ -178,6 +179,23 @@ static git_filter_def *filter_find_by_name(size_t *pos, const char *name)
 	return fdef;
 }
 
+static int filter_initialize(git_filter_def *fdef)
+{
+	int error = 0;
+
+	if (!fdef->initialized &&
+		fdef->filter &&
+		fdef->filter->initialize &&
+		(error = fdef->filter->initialize(fdef->filter)) < 0)
+	{
+		git_filter_unregister(fdef->filter_name);
+		return error;
+	}
+
+	fdef->initialized = true;
+	return 0;
+}
+
 int git_filter_unregister(const char *name)
 {
 	size_t pos;
@@ -196,8 +214,10 @@ int git_filter_unregister(const char *name)
 
 	(void)git_vector_remove(&git__filter_registry, pos);
 
-	if (fdef->filter->shutdown)
+	if (fdef->initialized && fdef->filter && fdef->filter->shutdown) {
 		fdef->filter->shutdown(fdef->filter);
+		fdef->initialized = false;
+	}
 
 	git__free(fdef->attrdata);
 	git__free(fdef);
@@ -209,7 +229,14 @@ git_filter *git_filter_lookup(const char *name)
 {
 	size_t pos;
 	git_filter_def *fdef = filter_find_by_name(&pos, name);
-	return fdef ? fdef->filter : NULL;
+
+	if (!fdef)
+		return NULL;
+
+	if (!fdef->initialized && filter_initialize(fdef) < 0)
+		return NULL;
+
+	return fdef->filter;
 }
 
 static int filter_load_defaults(void)
@@ -341,6 +368,9 @@ int git_filter_list_load(
 				break;
 		}
 
+		if (!fdef->initialized && (error = filter_initialize(fdef)) < 0)
+			break;
+
 		if (fdef->filter->check)
 			error = fdef->filter->check(
 				fdef->filter, &payload, &src, values);