Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitignore loads other gitignores

Tags:

git

gitignore

Is it possible? The git update-index --assume-unchanged is no solution, ignored files have to be tracked. Creating submodules either.

E.g.

cat .customgitignore(1|2|3...)

i-do-not-need-this.extension

cat .gitignore

basic-stuff.extension
<load> .customgitignore1
<load> .customgitignore2
<load> .customgitignore3
 etc

Issue description for those interested.

I am creating private repo of configs. One branch = one config. Additional branch as workspace. I merge other branches-configs with workspace depending on requirements. Branches-configs each have their own .gitignore which should be applied only after merge. I am trying to omit conflicts in gitignore file.

like image 745
Marcin Rogacki Avatar asked Sep 11 '13 20:09

Marcin Rogacki


2 Answers

I am trying to omit conflicts in gitignore file

Then don't version the final .gitignore file itself: generate it on demand.

Keep your .gitignore files separates, but add a content filter driver (a smudge script), which will:

  • detect the content of each .gitignore.xxx files
  • concatenate their content to the actual .gitignore (which is not added to git, and remains a private file)

enter image description here

like image 71
VonC Avatar answered Oct 17 '22 18:10

VonC


As Git doesn't support this out of the box, you may implement one yourself with Git hooks, for example:

In any case, you will need to manage your ignores and includes in a custom file, not a .gitignore itself, as this one will be generated. So, let's say, you have a .gitignoreincludes file in a branch, and custom include files in other branches.

So, you may install a "post-merge" hook like this, for example:

#!/bin/bash
gitignoreWithIncludes=".gitignoreincludes"    
if [[ -f $gitignoreWithIncludes ]]; then
  echo ".gitignore" > .gitignore
  while read -r line
  do
      if [[ $line == include* ]]; then
        includeName=${line:8}
        while read -r includeLine
        do
          echo $includeLine >> .gitignore
        done < $includeName
      else
        echo $line >> .gitignore
      fi
  done < $gitignoreWithIncludes 
fi

This hook will launch on any successful merge, will look for a .gitignoreincludes file, and, if it exists, will generate a .gitignore file. .gitignore will contain all entries from .gitignoreincludes and all entries from all files referenced as include FILE_NAME. Having .gitignore ignoring itself allows it to be safely regenerated multiple times when doing merges.

This can be further improved by allowing include instructions in included files as well, or also a post-checkout hook can be implemented to regenerate the .gitignore when switching between branches, etc., etc..

like image 36
Timur Avatar answered Oct 17 '22 18:10

Timur