Revision a0ad53c18100226cb1a138cb9b3bc3615170be8f authored by Torsten Bögershausen on 13 August 2016, 21:29:27 UTC, committed by Junio C Hamano on 14 August 2016, 20:45:52 UTC
When a non-reversible CRLF conversion is done in "git add",
a warning is printed on stderr (or Git dies, depending on checksafe)

The function commit_chk_wrnNNO() in t0027 was written to test this,
but did the wrong thing: Instead of looking at the warning
from "git add", it looked at the warning from "git commit".

This is racy because "git commit" may not have to do CRLF conversion
at all if it can use the sha1 value from the index (which depends on
whether "add" and "commit" run in a single second).

Correct t0027 and replace the commit for each and every file with a commit
of all files in one go.
The function commit_chk_wrnNNO() should be renamed in a separate commit.

Now that t0027 does the right thing, it detects a bug in covert.c:
This sequence should generate the warning `LF will be replaced by CRLF`,
but does not:

$ git init
$ git config core.autocrlf false
$ printf "Line\r\n" >file
$ git add file
$ git commit -m "commit with CRLF"
$ git config core.autocrlf true
$ printf "Line\n" >file
$ git add file

"git add" calls crlf_to_git() in convert.c, which calls check_safe_crlf().
When has_cr_in_index(path) is true, crlf_to_git() returns too early and
check_safe_crlf() is not called at all.

Factor out the code which determines if "git checkout" converts LF->CRLF
into will_convert_lf_to_crlf().

Update the logic around check_safe_crlf() and "simulate" the possible
LF->CRLF conversion at "git checkout" with help of will_convert_lf_to_crlf().
Thanks to Jeff King <peff@peff.net> for analyzing t0027.

Reported-By: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Torsten Bögershausen <tboegi@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 1335d76
Raw File
sha1-array.c
#include "cache.h"
#include "sha1-array.h"
#include "sha1-lookup.h"

void sha1_array_append(struct sha1_array *array, const unsigned char *sha1)
{
	ALLOC_GROW(array->sha1, array->nr + 1, array->alloc);
	hashcpy(array->sha1[array->nr++], sha1);
	array->sorted = 0;
}

static int void_hashcmp(const void *a, const void *b)
{
	return hashcmp(a, b);
}

static void sha1_array_sort(struct sha1_array *array)
{
	qsort(array->sha1, array->nr, sizeof(*array->sha1), void_hashcmp);
	array->sorted = 1;
}

static const unsigned char *sha1_access(size_t index, void *table)
{
	unsigned char (*array)[20] = table;
	return array[index];
}

int sha1_array_lookup(struct sha1_array *array, const unsigned char *sha1)
{
	if (!array->sorted)
		sha1_array_sort(array);
	return sha1_pos(sha1, array->sha1, array->nr, sha1_access);
}

void sha1_array_clear(struct sha1_array *array)
{
	free(array->sha1);
	array->sha1 = NULL;
	array->nr = 0;
	array->alloc = 0;
	array->sorted = 0;
}

void sha1_array_for_each_unique(struct sha1_array *array,
				for_each_sha1_fn fn,
				void *data)
{
	int i;

	if (!array->sorted)
		sha1_array_sort(array);

	for (i = 0; i < array->nr; i++) {
		if (i > 0 && !hashcmp(array->sha1[i], array->sha1[i-1]))
			continue;
		fn(array->sha1[i], data);
	}
}
back to top