git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Mirochnik, Oleg V" <oleg.v.mirochnik@intel.com>
To: "git@vger.kernel.org" <git@vger.kernel.org>
Subject: Git apply can convert LF file into CRLF when .gitattributes file is a part of a patch
Date: Wed, 8 Sep 2021 00:53:42 +0000	[thread overview]
Message-ID: <SJ0PR11MB49429B90AA71A06BD4A3BD15C6D49@SJ0PR11MB4942.namprd11.prod.outlook.com> (raw)

Hi,

Originally the issue was reported in https://github.com/git-for-windows/git/issues/3409
But I was asked to report it in the mail list as the issue is not a Windows related.

As you can see below "git apply --binary" converts files into CRLF mode if .gitattributes file modification is included into a patch file.
IMO behavior should be consistent - "git apply --binary" should not convert files into CRLF mode because of unrelated change in .gitattributes file.

Steps:
$ cat doit
git --version --build-options
git config --global core.autocrlf true

rm -rf tst
mkdir tst
cd tst
mkdir repo.git
cd repo.git
git init --bare
cd ..
git clone repo.git 1
cd 1
mkdir cr lf
echo '* -text' > lf/.gitattributes
for d in *; do for b in {1..5}; do echo foo >> $d/foo; done; done
git add *
git commit -m init
git push
cd ../

git clone repo.git/ 2
cd 2
for a in `find * -type f`; do echo `od -c $a | grep -q '\\r' && echo CR || echo LF` $a; done
for a in `find * -type f`; do echo '# dummy' >> $a; done
git commit -a -m dummy
echo ""
echo +++ After commit and before diff
for a in `find * -type f`; do echo `od -c $a | grep -q '\\r' && echo CR || echo LF` $a; done
git diff --binary HEAD~ -- cr lf     > ../patch.with
git diff --binary HEAD~ -- cr lf/foo > ../patch.without
git diff --binary HEAD~ --    lf     > ../patch.lf-only
cd ..

echo ""
echo +++ Patches
for a in patch*; do echo `od -c $a | grep -q '\\r' && echo CR || echo LF` $a; done

git clone repo.git 3
cd 3

echo ""
echo +++ patch w/o .gitattributes
git reset --hard
echo +++ After reset and before patch
for a in `find * -type f`; do echo `od -c $a | grep -q '\\r' && echo CR || echo LF` $a; done
git apply --binary ../patch.without
echo +++ After patch
for a in `find * -type f`; do echo `od -c $a | grep -q '\\r' && echo CR || echo LF` $a; done

echo ""
echo +++ patch with .gitattributes
git reset --hard
echo +++ After reset and before patch
for a in `find * -type f`; do echo `od -c $a | grep -q '\\r' && echo CR || echo LF` $a; done
git apply --binary ../patch.with
echo +++ After patch
for a in `find * -type f`; do echo `od -c $a | grep -q '\\r' && echo CR || echo LF` $a; done


echo ""
echo +++ patch no CR
git reset --hard
echo +++ After reset and before patch
for a in `find * -type f`; do echo `od -c $a | grep -q '\\r' && echo CR || echo LF` $a; done
git apply --binary ../patch.lf-only
echo +++ After patch
for a in `find * -type f`; do echo `od -c $a | grep -q '\\r' && echo CR || echo LF` $a; done

git config --global core.autocrlf false

$ sh doit
git version 2.29.2
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
Initialized empty Git repository in /tmp/issue3409/tst/repo.git/
Cloning into '1'...
warning: You appear to have cloned an empty repository.
done.
warning: LF will be replaced by CRLF in cr/foo.
The file will have its original line endings in your working directory
[master (root-commit) fc793f1] init
 3 files changed, 11 insertions(+)
 create mode 100644 cr/foo
 create mode 100644 lf/.gitattributes
 create mode 100644 lf/foo
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 24 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 378 bytes | 378.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
To /tmp/issue3409/tst/repo.git
 * [new branch]      master -> master
Cloning into '2'...
done.
CR cr/foo
LF lf/foo
LF lf/.gitattributes
warning: LF will be replaced by CRLF in cr/foo.
The file will have its original line endings in your working directory
[master 718534d] dummy
 3 files changed, 3 insertions(+)

+++ After commit and before diff
CR cr/foo
LF lf/foo
LF lf/.gitattributes

+++ Patches
CR patch.lf-only
CR patch.with
CR patch.without
Cloning into '3'...
done.

+++ patch w/o .gitattributes
HEAD is now at fc793f1 init
+++ After reset and before patch
CR cr/foo
LF lf/foo
LF lf/.gitattributes
+++ After patch
CR cr/foo
LF lf/foo
LF lf/.gitattributes

+++ patch with .gitattributes
HEAD is now at fc793f1 init
+++ After reset and before patch
CR cr/foo
LF lf/foo
LF lf/.gitattributes
+++ After patch
CR cr/foo
CR lf/foo
CR lf/.gitattributes

+++ patch no CR
HEAD is now at fc793f1 init
+++ After reset and before patch
CR cr/foo
LF lf/foo
LF lf/.gitattributes
+++ After patch
CR cr/foo
LF lf/foo
LF lf/.gitattributes

             reply	other threads:[~2021-09-08  0:53 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-08  0:53 Mirochnik, Oleg V [this message]
2021-09-11  6:43 ` Git apply can convert LF file into CRLF when .gitattributes file is a part of a patch Torsten =?unknown-8bit?Q?B=C3=B6gershausen?=

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=SJ0PR11MB49429B90AA71A06BD4A3BD15C6D49@SJ0PR11MB4942.namprd11.prod.outlook.com \
    --to=oleg.v.mirochnik@intel.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).