Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a .zip from GitHub removes newlines from text files

Tags:

git

github

I have a small project on GitHub. The project includes a Readme.txt. Everything works fine in the repository and the newlines are still there, but when a user downloads the .zip file from the repo, the newlines disappear.

Example:

This is a line.
This is another line.
This is an indented line.

This line is far below.

becomes:

This is a line.This is another line. This is an intended line.This line is far below.

This behavior makes the Readme.txt pretty hard to read, especially if it has a lot of indentation.

Is there a way to fix this? Preferably other than changing the file type.

And for clarification, I'm aiming to do this without Git, with the "Download ZIP" button in the GitHub page.

like image 766
Anon25712398 Avatar asked Jun 27 '13 15:06

Anon25712398


2 Answers

As nulltoken explained, this is caused by the fact that GitHub runs git archive on a linux machine that will default to linux line endings. You can change this by explicitly setting line endings for the files in your repo. To achieve this, create a .gitattributes file with the following content in the root of your repo and commit it.

*.txt eol=crlf

All GitHub created zips of revisions that contain that file will now have CRLF line endings in all .txt files. You can expand that to all files by using * instead of *.txt, but I would advice against that because it will make linux users sad.

like image 157
Chronial Avatar answered Nov 17 '22 16:11

Chronial


Internally, the "Download Zip" feature from GitHub leverages git archive.

git archive actually performs a checkout of the pointed at commit, streaming the content to the tar or zip archiver.

The way the line endings are being dealt with, during the checkout process, eventually depends on the platform the command is being run on.

As GitHub servers are Linux based, the selected line ending for text files will be the Linux native one (i.e. LF).

So there's (currently) no way to interfere with this and text files inside your zip/tar downloads will be LF terminated.

However you may still

  • Use a tool like Unix2Dos to batch convert your text files
  • Send a mail to [email protected] and request for a change to their UI so that one could potentially select the expected line endings
like image 33
nulltoken Avatar answered Nov 17 '22 14:11

nulltoken