git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 4/4] convert: make it harder to screw up adding a conversion attribute

From
Junio C Hamano <gitster@pobox.com>
Date
May 9, 2011, 22:05 UTC
Message-ID
<1304978701-19310-5-git-send-email-gitster@pobox.com>
In-Reply-To
<1304978701-19310-1-git-send-email-gitster@pobox.com>

The current internal API requires the callers of setup_convert_check() to supply the git_attr_check structures (hence they need to know how many to allocate), but they grab the same set of attributes for given path.

Define a new convert_attrs() API that fills a higher level information that the callers (convert_to_git and convert_to_working_tree) really want, and move the common code to interact with the attributes system to it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 convert.c |   79 +++++++++++++++++++++++++++++-------------------------------
 1 files changed, 38 insertions(+), 41 deletions(-)
diff --git a/convert.c b/convert.c
index a05820b..efc7e07 100644
--- a/convert.c
+++ b/convert.c
@@ -703,12 +703,19 @@ static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol e
 	return text_attr;
 }
 
+struct conv_attrs {
+	struct convert_driver *drv;
+	enum crlf_action crlf_action;
+	enum eol eol_attr;
+	int ident;
+};
+
 static const char *conv_attr_name[] = {
 	"crlf", "ident", "filter", "eol", "text",
 };
 #define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
 
-static void setup_convert_check(struct git_attr_check *check)
+static void convert_attrs(struct conv_attrs *ca, const char *path)
 {
 	int i;
 	static struct git_attr_check ccheck[NUM_CONV_ATTRS];
@@ -719,70 +726,60 @@ static void setup_convert_check(struct git_attr_check *check)
 		user_convert_tail = &user_convert;
 		git_config(read_convert_config, NULL);
 	}
-	for (i = 0; i < NUM_CONV_ATTRS; i++)
-		check[i].attr = ccheck[i].attr;
+
+	if (!git_checkattr(path, NUM_CONV_ATTRS, ccheck)) {
+		ca->crlf_action = git_path_check_crlf(path, ccheck + 4);
+		if (ca->crlf_action == CRLF_GUESS)
+			ca->crlf_action = git_path_check_crlf(path, ccheck + 0);
+		ca->ident = git_path_check_ident(path, ccheck + 1);
+		ca->drv = git_path_check_convert(path, ccheck + 2);
+		ca->eol_attr = git_path_check_eol(path, ccheck + 3);
+	} else {
+		ca->drv = NULL;
+		ca->crlf_action = CRLF_GUESS;
+		ca->eol_attr = EOL_UNSET;
+		ca->ident = 0;
+	}
 }
 
 int convert_to_git(const char *path, const char *src, size_t len,
                    struct strbuf *dst, enum safe_crlf checksafe)
 {
-	struct git_attr_check check[NUM_CONV_ATTRS];
-	enum crlf_action crlf_action = CRLF_GUESS;
-	enum eol eol_attr = EOL_UNSET;
-	int ident = 0, ret = 0;
+	int ret = 0;
 	const char *filter = NULL;
+	struct conv_attrs ca;
 
-	setup_convert_check(check);
-	if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
-		struct convert_driver *drv;
-		crlf_action = git_path_check_crlf(path, check + 4);
-		if (crlf_action == CRLF_GUESS)
-			crlf_action = git_path_check_crlf(path, check + 0);
-		ident = git_path_check_ident(path, check + 1);
-		drv = git_path_check_convert(path, check + 2);
-		eol_attr = git_path_check_eol(path, check + 3);
-		if (drv && drv->clean)
-			filter = drv->clean;
-	}
+	convert_attrs(&ca, path);
+	if (ca.drv)
+		filter = ca.drv->clean;
 
 	ret |= apply_filter(path, src, len, dst, filter);
 	if (ret) {
 		src = dst->buf;
 		len = dst->len;
 	}
-	crlf_action = input_crlf_action(crlf_action, eol_attr);
-	ret |= crlf_to_git(path, src, len, dst, crlf_action, checksafe);
+	ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
+	ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
 	if (ret) {
 		src = dst->buf;
 		len = dst->len;
 	}
-	return ret | ident_to_git(path, src, len, dst, ident);
+	return ret | ident_to_git(path, src, len, dst, ca.ident);
 }
 
 static int convert_to_working_tree_internal(const char *path, const char *src,
 					    size_t len, struct strbuf *dst,
 					    int normalizing)
 {
-	struct git_attr_check check[NUM_CONV_ATTRS];
-	enum crlf_action crlf_action = CRLF_GUESS;
-	enum eol eol_attr = EOL_UNSET;
-	int ident = 0, ret = 0;
+	int ret = 0;
 	const char *filter = NULL;
+	struct conv_attrs ca;
 
-	setup_convert_check(check);
-	if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
-		struct convert_driver *drv;
-		crlf_action = git_path_check_crlf(path, check + 4);
-		if (crlf_action == CRLF_GUESS)
-			crlf_action = git_path_check_crlf(path, check + 0);
-		ident = git_path_check_ident(path, check + 1);
-		drv = git_path_check_convert(path, check + 2);
-		eol_attr = git_path_check_eol(path, check + 3);
-		if (drv && drv->smudge)
-			filter = drv->smudge;
-	}
+	convert_attrs(&ca, path);
+	if (ca.drv)
+		filter = ca.drv->smudge;
 
-	ret |= ident_to_worktree(path, src, len, dst, ident);
+	ret |= ident_to_worktree(path, src, len, dst, ca.ident);
 	if (ret) {
 		src = dst->buf;
 		len = dst->len;
@@ -792,8 +789,8 @@ static int convert_to_working_tree_internal(const char *path, const char *src,
 	 * is a smudge filter.  The filter might expect CRLFs.
 	 */
 	if (filter || !normalizing) {
-		crlf_action = input_crlf_action(crlf_action, eol_attr);
-		ret |= crlf_to_worktree(path, src, len, dst, crlf_action);
+		ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
+		ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
 		if (ret) {
 			src = dst->buf;
 			len = dst->len;
-- 
1.7.5.1.288.g93ebc
Previous: Junio C HamanoNext: Nguyen Thai Ngoc Duy
Message 11 of 18 in “git add a-Big-file”
  1. 0/3 git add a-Big-fileJunio C Hamano, May 8, 2011
  2. 1/3 index_fd(): turn write_object and format_check arguments into one flagJunio C Hamano, May 8, 2011
  3. 2/3 index_fd(): split into two helper functionsJunio C Hamano, May 8, 2011
  4. 3/3 Bigfile: teach "git add" to send a large file straight to a packJunio C Hamano, May 8, 2011
  5. Nguyen Thai Ngoc DuyMay 8, 2011
  6. Junio C HamanoMay 8, 2011
  7. 0/4 convert.c clean-upJunio C Hamano, May 9, 2011
  8. 1/4 convert: rename the "eol" global variable to "core_eol"Junio C Hamano, May 9, 2011
  9. 2/4 convert: give saner names to crlf/eol variables, types and functionsJunio C Hamano, May 9, 2011
  10. 3/4 convert: make it safer to add conversion attributesJunio C Hamano, May 9, 2011
  11. 4/4 convert: make it harder to screw up adding a conversion attributeJunio C Hamano, May 9, 2011
  12. Nguyen Thai Ngoc DuyMay 10, 2011
  13. Junio C HamanoMay 10, 2011
  14. Shawn PearceMay 9, 2011
  15. Junio C HamanoMay 9, 2011
  16. Junio C HamanoMay 9, 2011
  17. Ævar Arnfjörð BjarmasonMay 29, 2011
  18. Junio C HamanoMay 29, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.