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

3

2

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

Andersen answered 26/12, 2013 at 16:16 Comment(5)
Why don't you add the file to the git template directory so that all new git repositories have the file by default?Finland
@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 machineAndersen
@AD7six: better than the template directory link to a template file as I ended up doingAndersen
That sounds like an unwritten answer (3k and you still "answer" by editing the question ?!?).Finland
@AD7six: nope - the unanswered question is how to include comments in the config file as jinit= `echo '* text=auto\n\n# comment`Andersen
U
1

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!

Unbidden answered 5/5, 2021 at 21:44 Comment(0)
A
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
[...]
Arlettearley answered 26/12, 2013 at 16:24 Comment(1)
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 :)Andersen
M
0

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.

Murchison answered 6/1 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.