@matheusbsbs / matheus-bagliano / commits / c4805393d7
Fork 0

autocrlf: Make it work also for un-normalized repositories

Previously, autocrlf would only work well for normalized repositories. Any text files that contained CRLF in the repository would cause problems, and would be modified when handled with core.autocrlf set. Change autocrlf to not do any conversions to files that in the repository already contain a CR. git with autocrlf set will never create such a file, or change a LF only file to contain CRs, so the (new) assumption is that if a file contains a CR, it is intentional, and autocrlf should not change that. The following sequence should now always be a NOP even with autocrlf set (assuming a clean working directory): git checkout <something> touch * git add -A . (will add nothing) git commit (nothing to commit) Previously this would break for any text file containing a CR. Some of you may have been folowing Eyvind's excellent thread about trying to make end-of-line translation in git a bit smoother. I decided to attack the problem from a different angle: Is it possible to make autocrlf behave non-destructively for all the previous problem cases? Stealing the problem from Eyvind's initial mail (paraphrased and summarized a bit): 1. Setting autocrlf globally is a pain since autocrlf does not work well with CRLF in the repo 2. Setting it in individual repos is hard since you do it "too late" (the clone will get it wrong) 3. If someone checks in a file with CRLF later, you get into problems again 4. If a repository once has contained CRLF, you can't tell autocrlf at which commit everything is sane again 5. autocrlf does needless work if you know that all your users want the same EOL style. I belive that this patch makes autocrlf a safe (and good) default setting for Windows, and this solves problems 1-4 (it solves 2 by being set by default, which is early enough for clone). I implemented it by looking for CR charactes in the index, and aborting any conversion attempt if this is found. Signed-off-by: Finn Arne Gangstad <finag@pvv.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Finn Arne Gangstad committed May 12, 2010 at 00:37 UTC c4805393d73425a5f467f10fa434fb99bfba17ac
2 files changed +101
convert.c
+49
@@ -120,6 +120,43 @@ static void check_safe_crlf(const char *path, int action,
120 }
121 }
122
123 +static int has_cr_in_index(const char *path)
124 +{
125 + int pos, len;
126 + unsigned long sz;
127 + enum object_type type;
128 + void *data;
129 + int has_cr;
130 + struct index_state *istate = &the_index;
131 +
132 + len = strlen(path);
133 + pos = index_name_pos(istate, path, len);
134 + if (pos < 0) {
135 + /*
136 + * We might be in the middle of a merge, in which
137 + * case we would read stage #2 (ours).
138 + */
139 + int i;
140 + for (i = -pos - 1;
141 + (pos < 0 && i < istate->cache_nr &&
142 + !strcmp(istate->cache[i]->name, path));
143 + i++)
144 + if (ce_stage(istate->cache[i]) == 2)
145 + pos = i;
146 + }
147 + if (pos < 0)
148 + return 0;
149 + data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
150 + if (!data || type != OBJ_BLOB) {
151 + free(data);
152 + return 0;
153 + }
154 +
155 + has_cr = memchr(data, '\r', sz) != NULL;
156 + free(data);
157 + return has_cr;
158 +}
159 +
160 static int crlf_to_git(const char *path, const char *src, size_t len,
161 struct strbuf *buf, int action, enum safe_crlf checksafe)
162 {
@@ -145,6 +182,13 @@ static int crlf_to_git(const char *path, const char *src, size_t len,
182 */
183 if (is_binary(len, &stats))
184 return 0;
185 +
186 + /*
187 + * If the file in the index has any CR in it, do not convert.
188 + * This is the new safer autocrlf handling.
189 + */
190 + if (has_cr_in_index(path))
191 + return 0;
192 }
193
194 check_safe_crlf(path, action, &stats, checksafe);
@@ -203,6 +247,11 @@ static int crlf_to_worktree(const char *path, const char *src, size_t len,
247 return 0;
248
249 if (action == CRLF_GUESS) {
250 + /* If we have any CR or CRLF line endings, we do not touch it */
251 + /* This is the new safer autocrlf-handling */
252 + if (stats.cr > 0 || stats.crlf > 0)
253 + return 0;
254 +
255 /* If we have any bare CR characters, we're not going to touch it */
256 if (stats.cr != stats.crlf)
257 return 0;
t/t0020-crlf.sh
+52
@@ -453,5 +453,57 @@ test_expect_success 'invalid .gitattributes (must not crash)' '
453 git diff
454
455 '
456 +# Some more tests here to add new autocrlf functionality.
457 +# We want to have a known state here, so start a bit from scratch
458 +
459 +test_expect_success 'setting up for new autocrlf tests' '
460 + git config core.autocrlf false &&
461 + git config core.safecrlf false &&
462 + rm -rf .????* * &&
463 + for w in I am all LF; do echo $w; done >alllf &&
464 + for w in Oh here is CRLFQ in text; do echo $w; done | q_to_cr >mixed &&
465 + for w in I am all CRLF; do echo $w; done | append_cr >allcrlf &&
466 + git add -A . &&
467 + git commit -m "alllf, allcrlf and mixed only" &&
468 + git tag -a -m "message" autocrlf-checkpoint
469 +'
470 +
471 +test_expect_success 'report no change after setting autocrlf' '
472 + git config core.autocrlf true &&
473 + touch * &&
474 + git diff --exit-code
475 +'
476 +
477 +test_expect_success 'files are clean after checkout' '
478 + rm * &&
479 + git checkout -f &&
480 + git diff --exit-code
481 +'
482 +
483 +cr_to_Q_no_NL () {
484 + tr '\015' Q | tr -d '\012'
485 +}
486 +
487 +test_expect_success 'LF only file gets CRLF with autocrlf' '
488 + test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
489 +'
490 +
491 +test_expect_success 'Mixed file is still mixed with autocrlf' '
492 + test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
493 +'
494 +
495 +test_expect_success 'CRLF only file has CRLF with autocrlf' '
496 + test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
497 +'
498 +
499 +test_expect_success 'New CRLF file gets LF in repo' '
500 + tr -d "\015" < alllf | append_cr > alllf2 &&
501 + git add alllf2 &&
502 + git commit -m "alllf2 added" &&
503 + git config core.autocrlf false &&
504 + rm * &&
505 + git checkout -f &&
506 + test_cmp alllf alllf2
507 +'
508
509 test_done