tags:

views:

407

answers:

4

Our website has one section which often changes to match the user. (examples are greatly simplified).

user:Mike
last visit:Yesterday

The method used to update the content [which I cannot now change] is that php searches the html webpage for the default content, and replaces it with new content before serving it to the user.

$stats = "user:Mike
last visit:Yesterday";
$defaults = "user:Guest
last visit:None";
$code = implode($stats, explode($defaults, $code));

All was fine till we started backing up the site with GIT [a glutton for punishment, we are].

It seems that GIT changes the newline character in the html page, and therefore php can no longer find the original text.

I do not understand how newlines are stored or changed, but occasionally I get an error from GIT during the submit, saying it must change the newline, and offering 'unlock' or 'continue'.

This is followed by the site not working, till I copy/paste the default text from an externally stored copy into that page - and don't do a commit.

I am aware that I can use regex to make the search/replace but the page sees enough usage for me to avoid unneeded expressions.

My local machine runs Windows. The server runs Unix.

+1  A: 

It seems it's converting Windows newlines (\r\n, or 0x0D0A) to Unix newlines (\n, or 0x0A). If your HTML contains Windows line endings, searching for a Unix-style line ending in the middle of a string won't work. You can explicitly state which one you want like this:

$stats = "user:Mike\r\nlast visit:Yesterday";

Or:

$stats = "user:Mike\nlast visit:Yesterday";
Samir Talwar
The actual content is very long and complex, and I can't replace the spaces [which the human designers require] with \r\n.If there is a way to have \r\n as well as the new line that shows up in the editor, that would be fine - can that be done? For the record, before switching to GIT it was no problem even though my machine is a Windows box.
SamGoody
Is there a difference between the 'continue' and 'unlock' options when GIT throws the newline error?
SamGoody
I'm not a Git user, so I couldn't say. It seems Dav has the solution though.
Samir Talwar
+5  A: 

http://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

crlf

This attribute controls the line-ending convention.

Set Setting the crlf attribute on a path is meant to mark the path as a "text" file. core.autocrlf conversion takes place without guessing the content type by inspection.

Unset Unsetting the crlf attribute on a path tells git not to attempt any end-of-line conversion upon checkin or checkout.

Tell git not to convert CRLFs by specifying crlf as Unset for the files in question and the issue should go away.

Amber
Please help me understand how to do this?
SamGoody
http://progit.org/book/ch7-2.html should give you a slightly more user-friendly overview of how gitattributes files work. But specifically, what you'd want to do is go into the directory with the files that shouldn't be CRLF-converted, and create a file there named `.gitattributes` with the line `*.html -crlf` as its contents. This will stop git from converting CRLFs for any .html files within that directory.
Amber
+1  A: 

Check if you don't have core.autocrlf set to "input" (e.g. using "git config --get core.autocrlf").

This configuration variable makes Git convert MS Windows end-of-line convention "\r\n" (CRLF) to UNIX convention "\n" (LF). See git-config manpage for details; the crlf gitattribute that Dav mentioned in answer is to have more fine-tuned selection.

Jakub Narębski
A: 

During install, mysGit gives the option of leaving line-breaks as they are.

This solved the problem once I upgraded.

SamGoody