Commit f5a0e734bc440ca6c89d15a894fea0ea3bf38f36

nulltoken 2012-11-17T06:46:42

tests: introduce cl_git_remove_placeholders()

diff --git a/tests-clar/clar_helpers.c b/tests-clar/clar_helpers.c
index 3d09bd4..b718d43 100644
--- a/tests-clar/clar_helpers.c
+++ b/tests-clar/clar_helpers.c
@@ -271,3 +271,52 @@ const char* cl_git_path_url(const char *path)
 	git_buf_free(&path_buf);
 	return url;
 }
+
+typedef struct {
+	const char *filename;
+	size_t filename_len;
+} remove_data;
+
+static int remove_placeholders_recurs(void *_data, git_buf *path)
+{
+	remove_data *data = (remove_data *)_data;
+	size_t pathlen;
+
+	if (git_path_isdir(path->ptr) == true)
+		return git_path_direach(path, remove_placeholders_recurs, data);
+
+	pathlen = path->size;
+
+	if (pathlen < data->filename_len)
+		return 0;
+
+	/* if path ends in '/'+filename (or equals filename) */
+	if (!strcmp(data->filename, path->ptr + pathlen - data->filename_len) &&
+		(pathlen == data->filename_len ||
+		 path->ptr[pathlen - data->filename_len - 1] == '/'))
+		return p_unlink(path->ptr);
+
+	return 0;
+}
+
+int cl_git_remove_placeholders(const char *directory_path, const char *filename)
+{
+	int error;
+	remove_data data;
+	git_buf buffer = GIT_BUF_INIT;
+
+	if (git_path_isdir(directory_path) == false)
+		return -1;
+
+	if (git_buf_sets(&buffer, directory_path) < 0)
+		return -1;
+
+	data.filename = filename;
+	data.filename_len = strlen(filename);
+
+	error = remove_placeholders_recurs(&data, &buffer);
+
+	git_buf_free(&buffer);
+
+	return error;
+}
diff --git a/tests-clar/clar_libgit2.h b/tests-clar/clar_libgit2.h
index fd20c12..91a5426 100644
--- a/tests-clar/clar_libgit2.h
+++ b/tests-clar/clar_libgit2.h
@@ -61,4 +61,7 @@ void cl_git_sandbox_cleanup(void);
 const char* cl_git_fixture_url(const char *fixturename);
 const char* cl_git_path_url(const char *path);
 
+/* Test repository cleaner */
+int cl_git_remove_placeholders(const char *directory_path, const char *filename);
+
 #endif