1

My latest iteration

jinit= !git init && printf "* text=auto\n*.java text\n*.jsp text\n*.css text\n*.html text\n*.js text\n*.xml text\n*.sql text\n*.MF text\n*.tld text\n*.md text\n\n# git files\n*.gitignore text\n*.gitattributes text\n\n# eclipse files\n*.classpath text\n*.project text\n*.prefs text\n*.properties text\n*.component text\n" >> .gitattributes && echo "bin/" >> .gitignore && git add .

This will actually try to execute the lines in gitattributes ! Changing \n for \r\n will crash the script and for \\\n will successfully run the script albeit the gitattributes will only contain

*

So how do I echo this with new lines ? Windows, mingwin, msysgit

EDIT : I also tried single quotes

EDIT 2014.03.13:

jinit= !git init && `echo '* text=auto\n\n*.java text\n*.jsp text\n\
*.css text\n*.html text\n*.js text\n*.xml text\n*.sql text\n*.MF text\n\
*.tld text\n*.md text\n\n*.gitignore text\n*.gitattributes text\n\n\
*.classpath text\n*.project text\n*.prefs text\n*.properties text\n\
*.component text\n' >> .gitattributes` && `echo "bin/" >> .gitignore` 

EDIT 2014.03.14: using \ to wrap the command - still works but a space is introduced before *.css, *.tld, *.classpath and *.component.

Can't seem to manage echoing comments. So if I add \n# git files\n\*.gitignore text\n (...) it EOFs - any workaround for that ?

Finally I used a different approach (see comments):

jinit= !git init\
    && `ln /c/Dropbox/_/_git/java_eclipse.gitattributes .gitattributes`\
    && `echo "bin/" >> .gitignore`\
    && git add .

but I leave this open for now, for the syntactic part

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • Why don't you add the file to the [git template directory](http://git-scm.com/docs/git-init) so that all new git repositories have the file by default? – AD7six Dec 26 '13 at 16:34
  • @AD7six: thanks for tip ! Didn't know about it - but I wanted to have different init aliases (and so gitignore/attrs) for different languages - and anyway there must be a way. I also thought of (hard) linking to a file instead of hardwiring the config in the script but I don;t believe I can call a command (plus it has the path to the file inside etc). Btw a script is once and for all while the template dir you must edit in every machine – Mr_and_Mrs_D Dec 26 '13 at 16:45
  • @AD7six: better than the template directory link to a template file as I ended up doing – Mr_and_Mrs_D Mar 14 '14 at 17:11
  • That sounds like an unwritten answer (3k and you still "answer" by editing the question ?!?). – AD7six Mar 15 '14 at 18:40
  • @AD7six: nope - the unanswered question is how to include comments in the config file as `jinit= \`echo '* text=auto\n\n# comment\`` – Mr_and_Mrs_D Mar 15 '14 at 19:01

2 Answers2

0

It works rather well on Linux.

To avoid having trouble with line endings, you could let echo add it:

echo "\* text=auto" >> .gitattributes
echo "\* .java text" >> .gitattributes
[...]
gturri
  • 13,807
  • 9
  • 40
  • 57
  • Yeah- I am on windows though and I want to make this work - calling echo many times in a row should do it but: 1. it will add LF instead of CRLF I hope to add with printf and 2. there must be a way, damn it :) – Mr_and_Mrs_D Dec 26 '13 at 16:31
0

There was a comment suggesting that you simply write a .gitattributes file into your git templates directory. This would not work because in the documentation, it specifically says:

Files and directories in the template directory whose name do not start with a dot will be copied to the $GIT_DIR after it is created.

However, we can actually use this to our advantage! You stated that

I wanted to have different init aliases (and so gitignore/attrs) for different languages

which is a fair point! However, I still believe your current approach is overly complicated. I would suggest adding files to your template directory with any name that begins with a dot which will allow you to identify it. For example, you might choose .gitattributes-english and .gitattributes-french.

Now, run the following commands:

git config --global alias.init-english "!git init;cp $(git config init.templatedir)/.gitattributes-english ./.gitattributes"

and

git config --global alias.init-french "!git init;cp $(git config init.templatedir)/.gitattributes-french ./.gitattributes"

Now, git init-english will initialize a repository, and copy the contents of .gitattributes-english from your template directory to .gitattributes in the new repository and will not add .gitattributes-french to the new repository. git init-french will instead use the .gitattributes-french and not .gitattributes-english.

I would argue that this also has the added benefit of allowing you to edit the specific .gitattributes files without having to update the alias!

You obviously do not have to use the init.templatedir I just find that it makes the most sense to use it as these are templates.

Note finally that this works on Windows!

Kraigolas
  • 5,121
  • 3
  • 12
  • 37