Subject: Re: [PATCH 1/2] convert: add a helper to determine the correct EOL for a given path

From: Junio C Hamano <[email-masked]>
Date: Fri, 22 Jan 2016 10:47:58 -0800

Johannes Schindelin <[email-masked]> writes:

> Signed-off-by: Johannes Schindelin <[email-masked]>
> ---

This change somehow ringed a bell and reminded me of your recent
ls-files stuff.  Are there things that these topics can use from
each other?

These topics are similar in that they add many lines of code as
"helpers" that inspect data and guess what the existing code would
do to the data, without refactoring much existing code whose
behaviour these helpers are guessing/defining to use them, which is
somewhat disturbing, as the behaviour the actual code exhibits and
the guesses the helpers make can easily drift apart.

>  convert.c | 29 +++++++++++++++++++++++++++++
>  convert.h |  2 ++
>  2 files changed, 31 insertions(+)
>
> diff --git a/convert.c b/convert.c
> index 814e814..b458734 100644
> --- a/convert.c
> +++ b/convert.c
> @@ -758,6 +758,35 @@ static void convert_attrs(struct conv_attrs *ca, const char *path)
>  	}
>  }
>  
> +enum eol eol_for_path(const char *path, const char *src, size_t len)
> +{
> +	struct conv_attrs ca;
> +	struct text_stat stats;
> +
> +	if (!path) {
> +		memset(&ca, 0, sizeof(ca));
> +		ca.crlf_action = CRLF_AUTO;
> +		ca.eol_attr = EOL_UNSET;
> +	} else {
> +		convert_attrs(&ca, path);
> +		if (ca.eol_attr == EOL_UNSET)
> +			ca.eol_attr = output_eol(ca.crlf_action);
> +		if (ca.eol_attr != EOL_UNSET)
> +			return ca.eol_attr;
> +	}
> +	if (!len || (ca.crlf_action != CRLF_AUTO &&
> +				ca.crlf_action != CRLF_GUESS))
> +		return core_eol;
> +	ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
> +	gather_stats(src, len, &stats);
> +	if (ca.crlf_action == CRLF_GUESS && stats.cr > stats.crlf)
> +		return core_eol;
> +	else if (stats.crlf)
> +		return EOL_CRLF;
> +	else
> +		return EOL_LF;
> +}
> +
>  int would_convert_to_git_filter_fd(const char *path)
>  {
>  	struct conv_attrs ca;
> diff --git a/convert.h b/convert.h
> index d9d853c..1892867 100644
> --- a/convert.h
> +++ b/convert.h
> @@ -33,6 +33,8 @@ enum eol {
>  
>  extern enum eol core_eol;
>  
> +extern enum eol eol_for_path(const char *path, const char *src, size_t len);
> +
>  /* returns 1 if *dst was used */
>  extern int convert_to_git(const char *path, const char *src, size_t len,
>  			  struct strbuf *dst, enum safe_crlf checksafe);