Subject: Re: [PATCH 01/24] dir.c: optionally compute sha-1 of a .gitignore file

From: Junio C Hamano <[email-masked]>
Date: Wed, 11 Feb 2015 13:23:04 -0800

Nguyễn Thái Ngọc Duy  <[email-masked]> writes:

> -int add_excludes_from_file_to_list(const char *fname,
> -				   const char *base,
> -				   int baselen,
> -				   struct exclude_list *el,
> -				   int check_index)
> +/*
> + * Given a file with name "fname", read it (either from disk, or from
> + * the index if "check_index" is non-zero), parse it and store the
> + * exclude rules in "el".
> + *
> + * If "ss" is not NULL, compute SHA-1 of the exclude file and fill
> + * stat data from disk (only valid if add_excludes returns zero). If
> + * ss_valid is non-zero, "ss" must contain good value as input.
> + */
> +static int add_excludes(const char *fname, const char *base, int baselen,
> +			struct exclude_list *el, int check_index,
> +			struct sha1_stat *sha1_stat)
> ...
> @@ -571,6 +588,21 @@ int add_excludes_from_file_to_list(const char *fname,
>  		}
>  		buf[size++] = '\n';
>  		close(fd);
> +		if (sha1_stat) {
> +			int pos;
> +			if (sha1_stat->valid &&
> +			    !match_stat_data(&sha1_stat->stat, &st))
> +				; /* no content change, ss->sha1 still good */
> +			else if (check_index &&
> +				 (pos = cache_name_pos(fname, strlen(fname))) >= 0 &&
> +				 !ce_stage(active_cache[pos]) &&
> +				 ce_uptodate(active_cache[pos]))
> +				hashcpy(sha1_stat->sha1, active_cache[pos]->sha1);
> +			else
> +				hash_sha1_file(buf, size, "blob", sha1_stat->sha1);

I do not think this would work well on DOS.

This helper function originally is meant to work *only* on the
checked out representation of the file and that is what is read by
read_in_full(), and that is the reason why it handles the case where
the contents of buf[] happens to be CRLF terminated in the function.

If you want to detect the content changes across working tree, index
and the tree objects by reusing hash_sha1_file(), however, you must
not feed the checked out (aka "smudged") representation to it.
You'd need to turn it into "cleaned" representation by doing the
equivalent of calling index_path().  Some helpers in the callchain
that originates from index_path() might directly be reusable for
your purpose.