Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitignore based on unix file permissions

Tags:

git

gitignore

I am working on a C project which generates lots of binaries. I try to keep the source under source control. However, I would like the binaries to be ignored without having to add them to the gitignore file whenever I create a new one. This is a sandbox of sorts and so I create lots of new binaries as I try out new things. I could build all my binaries in a single directory or give them a suffix, but I dislike these reasons. Since GCC automatically sets the executable bit on the binaries it produces, can I use that flag to ignore them?

like image 873
azani Avatar asked Nov 03 '13 18:11

azani


People also ask

Does Git preserve directory permissions?

Git Tracks ONLY the Executable Bit of the Permissions for the User Who Owns the File.

Does Git track file permissions?

Yes, by default, git is configured to track the changes in file permission mode, too. Just to experiment with the idea, I created a dummy repo and "touched" an empty file. The initial default permission was 775.


1 Answers

I recommend against this. What happens if you need to use a script in your build process or at runtime, but it's accidentally gitignored because it has the executable bit? This could especially create confusion if other people contribute to your project without knowing about the gitignore. You're best off manually adding files (or using a bin directory) to solve this problem.

That being said, gitignore does not seem to support mode-based file ignores, but you can automate the process of manually adding files that are executable.

If your .gitignore file can be overwritten:

Run this to update your ignored files: find -type f -executable | sed 's#^.##' > .gitignore

If your .gitignore file should be kept intact:

  1. Setup a new ignore file (your existing .gitignore should still work): git config core.excludesfile .auto_gitignore
  2. Run this to create/update your ignored file list: find -type f -executable | sed 's#^.##' > .auto_gitignore
  3. Commit your .auto_gitignore if you want to share it with others, or add .auto_gitignore to .git/info/exclude if you don't. Note that if you do share it, your collaborators will also have to do step 1 individually.

Note: If you get an error because your version of find doesn't support -executable, replace it with -perm /111.

like image 144
Nick McCurdy Avatar answered Oct 27 '22 09:10

Nick McCurdy