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
Torsten Bögershausen <tboegi@web.de>
Date
Mar 18, 2018, 07:24 UTC
Message-ID
<20180318072435.GA24190@tor.lan>
In-Reply-To
<20180309173536.62012-7-lars.schneider@autodesk.com>
Some comments inline
On Fri, Mar 09, 2018 at 06:35:32PM +0100, lars.schneider@autodesk.com wrote:
Show 10 quoted lines
> 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.

[No comments on the .gitattributes]
Show 32 quoted lines
>  
> diff --git a/convert.c b/convert.c
> index b976eb968c..aa59ecfe49 100644
> --- a/convert.c
> +++ b/convert.c
> @@ -7,6 +7,7 @@
>  #include "sigchain.h"
>  #include "pkt-line.h"
>  #include "sub-process.h"
> +#include "utf8.h"
>  
>  /*
>   * convert.c - convert a file when checking it out and checking it in.
> @@ -265,6 +266,78 @@ static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
>  
>  }
>  
> +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;
Show 44 quoted lines
> +
> +	/*
> +	 * Looks like we got called from "would_convert_to_git()".
> +	 * This means Git wants to know if it would encode (= modify!)
> +	 * the content. Let's answer with "yes", since an encoding was
> +	 * specified.
> +	 */
> +	if (!buf && !src)
> +		return 1;
> +
> +	dst = reencode_string_len(src, src_len, default_encoding, enc,
> +				  &dst_len);
> +	if (!dst) {
> +		/*
> +		 * We could add the blob "as-is" to Git. However, on checkout
> +		 * we would try to reencode to the original encoding. This
> +		 * would fail and we would leave the user with a messed-up
> +		 * working tree. Let's try to avoid this by screaming loud.
> +		 */
> +		const char* msg = _("failed to encode '%s' from %s to %s");
> +		if (die_on_error)
> +			die(msg, path, enc, default_encoding);
> +		else {
> +			error(msg, path, enc, default_encoding);
> +			return 0;
> +		}
> +	}
> +
> +	strbuf_attach(buf, dst, dst_len, dst_len + 1);
> +	return 1;
> +}
> +
> +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;
Show 27 quoted lines
> +
> +	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.

> +
> +	/* Don't encode to the default encoding */
> +	if (!strcasecmp(value, default_encoding))
> +		return NULL;
 Same as above ?:
	if (is_encoding_utf8(value))
		return 0;
Previous: Lars SchneiderNext: Lars Schneider
Message 15 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.