3

I stumbled upon some instructions in the book "Pro Git"

It suggests:

  1. Download this file : https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

    If you use the Bash shell, Git comes with a nice auto-completion script you can enable. Download it directly from the Git source code at https://github.com/git/git/blob/master/contrib/completion/git-completion.bash . Copy this file to your home directory, and add this to your .bashrc file:

    source ~/git-completion.bash

    Source https://github.com/progit/progit/blame/master/en/02-git-basics/01-chapter2.markdown#L1115

    (putting this line in .bashrc will not work in Mac OS X systems which I discussed here: Why doesn't Mac OS X source ~/.bashrc?)

  2. Put this file git-completion.bash ( in /opt/local/etc/bash_completion.d if you want bash completion (for the git command) to work on all user accounts. (As if bash reads all the scripts in /opt/local/etc/bash_completion.d)

    If you want to set up Git to automatically have Bash shell completion for all users, copy this script to the /opt/local/etc/bash_completion.d directory on Mac systems or to the /etc/bash_completion.d/ directory on Linux systems. This is a directory of scripts that Bash will automatically load to provide shell completions.

    Source : https://github.com/progit/progit/blame/master/en/02-git-basics/01-chapter2.markdown#L1119


I found out that Mac OS's bash DOES NOT read scripts inside /opt/local/etc/bash_completion.d. I put the file there (I created all the folders, because they didn't exist) : Terminal prompt showing that git-completion.bash exists and is in appropriate path

And git completion doesn't work on any account.

Does Mac OS X's bash read scripts for all users from /opt/local/etc/bash_completion.d ?

Is the "Pro Git" book wrong ? Can I report it on their Github page ?

Or am I wrong ?

3 Answers 3

1

Bash only reads scripts by default in your home directory, or if they are missing in /etc. See Bash's documentation. If using OSX's Terminal.app then by default it reads ~/.bash_profile.

This is true of all bash on Linux or other OSes.

To read from another directory e.g. /opt/local/etc/bash_completion.d you have to edit your start up files to source (i.e. include) the files from there

The instructions for the script do not mention /opt/local/... which is a non standard location on any Unix. (by non-standard it is allowed to be used by third party packages but not defined what should be in there) They say

  1. Copy this file to somewhere (e.g. ~/.git-completion.sh).
  2. Add the following line to your .bashrc/.zshrc:

    source ~/.git-completion.sh
    
  3. Consider changing your PS1 to also show the current branch, see git-prompt.sh for details.

The progit quote assumes you know bash. All it is saying is put the files from the first quote into a specific place if you want all users on the machine to use them and not in ~ where only the user installing it can see them. i.e. the point is multi user versus single user.

He also picks that path as nothing else uses it and you should not edit /etc files in OS X as Apple's OS upgrades could overwrite them so you need to choose another place. (I would have chosen something under /usr/local as there is where manually maintained scripts are meant to go).

~/.bashrc is the correct place to edit and add the source. See your other question and the bash manual for setting up ~/.bash_profile The bash suggested way is source .bashrc into .bash_profile. Note that on OS X using Terminal.app is not the one way of running shells so there can be sessions starting with .bashrc.

3
  • "If using OSX's Terminal.app then by default it reads ~/.bash_profile" It rather -> "After reading that file [/etc/profile], it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable." Source : gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files Commented Feb 4, 2014 at 15:26
  • -> "If you want to set up Git to automatically have Bash shell completion for all users, copy this script to the /opt/local/etc/bash_completion.d". Does this sentence sounds like "all users" were meant to source something? Maybe I have problem with understanding English (really), but I see here phrase "set up Git to automatically have Bash shell completion" which for me, mean that it will just work (without user sourcing what I have putted to this folder) Commented Feb 4, 2014 at 15:39
  • No that means for all uses rather than just one user and does not mean it just works
    – mmmmmm
    Commented Feb 4, 2014 at 19:57
0

The core bash completion files are not installed as part of OS X so there is nowhere to add your git completion for it to be picked up by the core.

I apologise but I'm not sure how I installed bash completion but I think I used MacPorts, you can also use Homebrew.

I do have the folder /usr/local/opt/bash-completion/etc/bash_completion.d which contains completion files for a number of command line tools. The bash_completion function certainly reads all the files in this directory.

So you first need to install bash-completion then the git completion file and all will be well.

6
  • Could you link to the bash completion scripts ? And confirm that This paragraph of "Pro Git" book is wrong, and should be notified (So other people don't spend time asking about this anymore) Commented Feb 4, 2014 at 9:59
  • It's wrong in that OS X doesn't come with completion built in. Rather than attempting to install bash completion by hand use either MacPorts or HomeBrew. Commented Feb 4, 2014 at 10:06
  • I understand that there are problems with Mac OS X, but I think that it is not ok to not point it out in book. Commented Feb 4, 2014 at 10:21
  • There are not problems with OSX here, it is meets all the relevant standards.
    – mmmmmm
    Commented Feb 4, 2014 at 11:07
  • If in /usr/local then it was not installed by macports
    – mmmmmm
    Commented Feb 4, 2014 at 11:26
0

Since none of the above answers got your check, why not move over to zsh which has a more uniform completion stack.

Yes the bash book needs to be corrected and yes, you have to do more work to get that working universally, but for most people having one account working seems good enough for bash.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .