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

Re: [PATCH v11 06/10] convert: add 'working-tree-encoding' attribute

From
Lars Schneider <larsxschneider@gmail.com>
Date
Apr 1, 2018, 13:24 UTC
Message-ID
<0FEBEFB2-46D6-4688-AF07-654B56FFF9D8@gmail.com>
In-Reply-To
<20180318072435.GA24190@tor.lan>
Show 20 quoted lines
> On 18 Mar 2018, at 08:24, Torsten Bögershausen <tboegi@web.de> wrote:
> 
> Some comments inline
> 
> On Fri, Mar 09, 2018 at 06:35:32PM +0100, lars.schneider@autodesk.com wrote:
>> From: Lars Schneider <larsxschneider@gmail.com>
>> 
>> Git recognizes files encoded with ASCII or one of its supersets (e.g.
>> UTF-8 or ISO-8859-1) as text files. All other encodings are usually
>> interpreted as binary and consequently built-in Git text processing
>> tools (e.g. 'git diff') as well as most Git web front ends do not
>> visualize the content.
>> 
>> Add an attribute to tell Git what encoding the user has defined for a
>> given file. If the content is added to the index, then Git converts the
> 
> Minor comment:
> "Git converts the content"
> Everywhere else (?) "encodes or reencodes" is used.
> "Git reencodes the content" may be more consistent.
OK, will change.
Show 21 quoted lines
>> 
>> +static const char *default_encoding = "UTF-8";
>> +
>> +static int encode_to_git(const char *path, const char *src, size_t src_len,
>> +			 struct strbuf *buf, const char *enc, int conv_flags)
>> +{
>> +	char *dst;
>> +	int dst_len;
>> +	int die_on_error = conv_flags & CONV_WRITE_OBJECT;
>> +
>> +	/*
>> +	 * No encoding is specified or there is nothing to encode.
>> +	 * Tell the caller that the content was not modified.
>> +	 */
>> +	if (!enc || (src && !src_len))
>> +		return 0;
> 
> (This may have been discussed before.
> As we checked (enc != NULL) I think we can add here:)
> 	if (is_encoding_utf8(enc))
> 		return 0;

This should be covered in git_path_check_encoding(), introduced in v12:

        /* Don't encode to the default encoding */
	if (same_encoding(value, default_encoding))
		return NULL;

In that function the encoding of a certain file is read from the .gitattributes. If the encoding matches the compile-time defined default encoding (= UTF-8), then the encoding is set to NULL.

Show 56 quoted lines
>> 
>> +
>> +static int encode_to_worktree(const char *path, const char *src, size_t src_len,
>> +			      struct strbuf *buf, const char *enc)
>> +{
>> +	char *dst;
>> +	int dst_len;
>> +
>> +	/*
>> +	 * No encoding is specified or there is nothing to encode.
>> +	 * Tell the caller that the content was not modified.
>> +	 */
>> +	if (!enc || (src && !src_len))
>> +		return 0;
> 
> Same as above:
> 	if (is_encoding_utf8(enc))
> 		return 0;
> 
>> +
>> +	dst = reencode_string_len(src, src_len, enc, default_encoding,
>> +				  &dst_len);
>> +	if (!dst) {
>> +		error("failed to encode '%s' from %s to %s",
>> +			path, default_encoding, enc);
>> +		return 0;
>> +	}
>> +
>> +	strbuf_attach(buf, dst, dst_len, dst_len + 1);
>> +	return 1;
>> +}
>> +
>> static int crlf_to_git(const struct index_state *istate,
>> 		       const char *path, const char *src, size_t len,
>> 		       struct strbuf *buf,
>> @@ -978,6 +1051,25 @@ static int ident_to_worktree(const char *path, const char *src, size_t len,
>> 	return 1;
>> }
>> 
>> +static const char *git_path_check_encoding(struct attr_check_item *check)
>> +{
>> +	const char *value = check->value;
>> +
>> +	if (ATTR_UNSET(value) || !strlen(value))
>> +		return NULL;
>> +
> 
> 
>> +	if (ATTR_TRUE(value) || ATTR_FALSE(value)) {
>> +		error(_("working-tree-encoding attribute requires a value"));
>> +		return NULL;
>> +	}
> 
> TRUE or false are values, but just wrong ones.
> If this test is removed, the user will see "failed to encode "TRUE" to "UTF-8",
> which should give enough information to fix it.

I see your point. However, I would like to stop the processing right there for these invalid values. How about

  error(_("true/false are no valid working-tree-encodings"));

I think that is the most straight forward/helpful error message for the enduser (I consider the term "boolean" but dismissed it as potentially confusing to folks not familiar with the term).

OK with you?
Show 8 quoted lines
> 
>> +
>> +	/* Don't encode to the default encoding */
>> +	if (!strcasecmp(value, default_encoding))
>> +		return NULL;
> Same as above ?:
> 	if (is_encoding_utf8(value))
> 		return 0;
Yes, that was fixed in v12 as mentioned above :-)
- Lars
Previous: Torsten BögershausenNext: Torsten Bögershausen
Message 16 of 25 in “convert: add support for different encodings”
  1. 00/10 convert: add support for different encodingslars.schneider@autodesk.com, Mar 9, 2018
  2. 02/10 strbuf: add xstrdup_toupper()lars.schneider@autodesk.com, Mar 9, 2018
  3. 08/10 convert: advise canonical UTF encoding nameslars.schneider@autodesk.com, Mar 9, 2018
  4. Junio C HamanoMar 9, 2018
  5. Lars SchneiderMar 15, 2018
  6. 03/10 strbuf: add a case insensitive starts_with()lars.schneider@autodesk.com, Mar 9, 2018
  7. 09/10 convert: add tracing for 'working-tree-encoding' attributelars.schneider@autodesk.com, Mar 9, 2018
  8. 07/10 convert: check for detectable errors in UTF encodingslars.schneider@autodesk.com, Mar 9, 2018
  9. Junio C HamanoMar 9, 2018
  10. Lars SchneiderMar 9, 2018
  11. Junio C HamanoMar 9, 2018
  12. 06/10 convert: add 'working-tree-encoding' attributelars.schneider@autodesk.com, Mar 9, 2018
  13. Junio C HamanoMar 9, 2018
  14. Lars SchneiderMar 15, 2018
  15. Torsten BögershausenMar 18, 2018
  16. Lars SchneiderApr 1, 2018
  17. Torsten BögershausenApr 5, 2018
  18. Lars SchneiderApr 15, 2018
  19. 10/10 convert: add round trip check based on 'core.checkRoundtripEncoding'lars.schneider@autodesk.com, Mar 9, 2018
  20. Eric SunshineMar 9, 2018
  21. Junio C HamanoMar 9, 2018
  22. Eric SunshineMar 9, 2018
  23. 01/10 strbuf: remove unnecessary NUL assignment in xstrdup_tolower()lars.schneider@autodesk.com, Mar 9, 2018
  24. 05/10 utf8: add function to detect a missing UTF-16/32 BOMlars.schneider@autodesk.com, Mar 9, 2018
  25. 04/10 utf8: add function to detect prohibited UTF-16/32 BOMlars.schneider@autodesk.com, Mar 9, 2018

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.