Subject: [PATCH 1/2] Export crlf conversion function to libgit

From: Heiko Voigt <[email-masked]>
Date: Wed, 6 May 2009 17:09:05 +0200

This makes it possible for parsecvs to use the git heuristics to
convert textfiles into a normalized form

Signed-off-by: Heiko Voigt <[email-masked]>
---
There is another patch for parsecvs which depends on this change.

I first would like to know how you feel about exporting such a function?

 cache.h   |    1 +
 convert.c |   30 ++++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/cache.h b/cache.h
index f48e80b..42e9243 100644
--- a/cache.h
+++ b/cache.h
@@ -926,6 +926,7 @@ extern void trace_argv_printf(const char **argv, const char *format, ...);
 extern int convert_to_git(const char *path, const char *src, size_t len,
                           struct strbuf *dst, enum safe_crlf checksafe);
 extern int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst);
+extern int crlf_to_git_buf(size_t *p_len, char *p_blob);
 
 /* add */
 /*
diff --git a/convert.c b/convert.c
index 1816e97..0ced471 100644
--- a/convert.c
+++ b/convert.c
@@ -179,6 +179,36 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
 	return 1;
 }
 
+int crlf_to_git_buf(size_t *p_len, char *p_blob)
+{
+	struct strbuf nbuf;
+	strbuf_init(&nbuf, 0);
+	size_t len = *p_len;
+	char *blob = p_blob;
+
+	/* this is needed otherwise crlf_to_git will skip this file */
+	auto_crlf = 1;
+
+	/* filter blob with len and store in nbuf
+	 * if no conversion happens just return */
+	if (!crlf_to_git(NULL, blob, len, &nbuf, CRLF_GUESS))
+		return 0;
+
+	blob = strbuf_detach(&nbuf, &len);
+	/* crlf conversion should only make our buffer shorter */
+	if (len <= *p_len) {
+		*p_len  = len;
+		memcpy(p_blob, blob, len);
+		free(blob);
+	} else {
+		*p_len  = len;
+		free(blob);
+		return 0;
+	}
+
+	return 1;
+}
+
 static int crlf_to_worktree(const char *path, const char *src, size_t len,
                             struct strbuf *buf, int action)
 {
-- 
1.6.2.1.423.g442d