1

I am working on Windows platform, with files in Mac ANSI encoding

when I edit a single line of a file, and then do a git diff, this is what I see:

diff --git a/class.php b/class.php
@@ -1 +1 @@
-<?php^Mclass Type {^M^M   private $x;// level^M ..etc
\ No newline at end of file
+<?php^Mclass Type {^M^M   private $x;// level^M ..etc
\ No newline at end of file

In other words, git sees my file as a single line, due to the mac line endings. I have looked at various posts on git hub and got tried a few solutions, but neither worked. I am kind of lost as to what is going on and how to commit my files properly.

Links I have tried:

Still same ^M in lines. git does not seem to handle \r on Windows quite well. Must I convert entire repo line endings to say \r\n? Is there not a solution to leave mac encoding as is?

More info:

My .gitattributes file:

# Set default behaviour, in case users don't have core.autocrlf set.
* text=auto

# Explicitly declare text files we want to always be normalized and converted
# to native line endings on checkout.
*.php text
*.inc text
*.js text
*.txt text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.gif binary

I ran this command: git config --global core.autocrlf true

Community
  • 1
  • 1
Dennis
  • 7,907
  • 11
  • 65
  • 115
  • MacOS no longer uses that encoding (which isn't really "ANSI", BTW). Why do you have files in an obsolete encoding? Converting them to Windows encoding would probably solve more problems than your git issues. – Keith Thompson Feb 12 '14 at 16:17
  • the main repo will be running on a Linux system. Do I convert line endings to Linux .. or still convert them to Windows? That will help me make the choice between using dos2unix or unix2dos... – Dennis Feb 12 '14 at 16:39
  • Are you *using* the files on Linux, on Windows, or both? `git` does have ways to deal with UNIX-style vs. Windows-style line endings, but I'm not familiar with the details. – Keith Thompson Feb 12 '14 at 16:42
  • I personally edit the files on Windows. Coworker edits files on his Mac. Web server that serves files runs on Linux. We got the "best" of all worlds. – Dennis Feb 12 '14 at 16:44
  • MacOS X uses UNIX-style line endings, though I suppose it might still tolerate the old CR endings of (IIRC) MacOS 9 and earlier. You should definitely abandon the old-style Mac endings. Beyond that, you need advice from someone who knows more about `git` Linux/Windows interoperability than I do. – Keith Thompson Feb 12 '14 at 16:59
  • 1
    I think the solution is probably to convert all line endings to Unix style (\n) and let git deal with the rest. Git has options to convert line endings to native system upon checkout, so it will work for me (Windows), and \n will work for Linux/OSX as-is. – Dennis Feb 12 '14 at 17:38
  • Are those really `\r`s in the repository? What tool is this friendly coworker using that it uses `\r` as a newline separator? – Edward Thomson Feb 12 '14 at 17:56
  • Yes, those are indeed '\r', as confirmed by unix `od -c` utility. Coworker uses PHP Storm on Windows and on Mac. '\r' probably got there from way before. The software being developed is 5-7 years old I think... So someone started it, and the tools he's using just continue it. – Dennis Feb 12 '14 at 19:35

1 Answers1

1

TL;DR version

#rewrite ALL your branches, ALL trees, ALL commits to use proper line endings
git filter-branch --tree-filter '~/fix-eol.sh' -d /dev/shm/repo-temp --tag-name-filter cat  --prune-empty -- --all

#git may complain after this procedure and have unclean directory.  Just do:
git reset --hard

#go over your repo and delete your tags and old tree references, 
#old branch pointers on both remote and local
git tag -d v1.1.0
git push . :refs/original/refs/tags/v1.1.0
etc...

#re-compress your objects..
git gc --aggressive

#some more clean up
git prune

#CAUTION:  this will rewrite your origin repo, 
#you (and everybody else) using it will have to run git clone 
#to re-download the entire repo.  This was a destructive operation afterall
#(normalizing all line endings in your entire repo)
git push --force

cat ~/fix-eol.sh

#!/bin/bash
# ~/fix-eol.sh
# convert all js,css,html, and txt files from DOS to UNIX line endings
find . -type f -regex ".*\.\(js\|css\|inc\|html\|txt\|php\)" | xargs perl -pi -e 's/\r\n|\n|\r/\n/g'

Long version:

I said the heck with it and now trying the 2nd solution described here:

http://blog.gyoshev.net/2013/08/normalizing-line-endings-in-git-repositories/

normalizing all my text and code containing files to contain \n unix-style line endings, using dos2unix command (dos2unix did not work for mac, ha haha, of course! It's not mac2unix)

so I used the converter here: Converting newline formatting from Mac to Windows

It rewrites entire repository, now with proper line endings, and I think that after push --force and people redownloading the repository, all will be okay.

Community
  • 1
  • 1
Dennis
  • 7,907
  • 11
  • 65
  • 115
  • this command left my repo in a weird state. I got this error: `tree filter branch error: Entry not uptodate. Cannot merge.` – Dennis Feb 13 '14 at 16:30
  • another helpful link: http://web.archive.org/web/20080930112610/http://log.emmanuelebassi.net/archives/2007/09/when-the-levee-breaks/ – Dennis Feb 13 '14 at 20:12
  • Indeed dos2unix is not mac2unix, but the mac2unix command is also available. Or you run dos2unix in mac mode by: dos2unix -c mac – Erwin Waterlander Feb 14 '14 at 10:00
  • and this: http://superuser.com/questions/283309/how-to-delete-the-git-reference-refs-original-refs-heads-master – Dennis Feb 14 '14 at 22:43