git alias for creating a .gitattributes on init - how do I add the new lines?

577 views Asked by At

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

3

There are 3 answers

1
gturri On

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
[...]
0
Kraigolas On

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!

0
Reed Dunkle On

Windows, Mac, Linux

  1. Make a folder somewhere, and add a .gitattributes file. This example uses git-atrributes/.gitattributes.
  2. In this .gitattributes file, add the attributes you want to template.
  3. In your .gitconfig, under the [init] section, add a key/value pair.
    • The key can be whatever you want. This example uses templateDir.
    • The value is the path/to/the/folder you created in Step 1.
    # .gitconfig
    
    [init]
      templateDir = C:/example/path/to/git-atrributes/
      # Windows, but forward slashes
    
  4. Make an [alias] key/value that runs git init, and then copies your template .gitattributes file to your new git repo. My alias key is called ini, but you can use whatever.
    # .gitconfig
    
    [alias]
      ini = "!git init; cp $(git config init.templateDir)/.gitattributes ./.gitattributes"
    
      # $(git config init.templateDir) <-- uses the key/value created in previous step
    
  5. To use it, I run git ini (instead of git init) to initialize new repos.
    mkdir my-new-repo
    cd my-new-repo
    git ini # Not `git init`
    

Note that there is native support for templates, but it currently excludes dotfiles:

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. (1/5/2024)

Credit: Answer by Kraigolas got me there.