[bash] How to enable git-completion on PS1 variable (fix __git_ps1 error)

A nicer bash prompt I use includes a git completion script that indicates the branch of a repository I may be into. Like this:

git_completion

Green indicates “master” is clean and no modified files are present. Red indicates there is something new in that branch.

In order to configure that, I had to include some instructions on my .bashrc file. It looks like this:

# git completion – Linux
source /usr/share/git-core/contrib/completion/git-prompt.sh

# git completion – Mac OS
#source /usr/local/git/contrib/completion/git-prompt.sh

(…)

PS1='\[\e[01;30m\]\t`if [ $? = 0 ]; then echo "\[\e[32m\] ✔ "; else echo "\[\e[31m\] ✘ "; fi`\[\e[00;40;37m\]\u\[\e[01;37m\]:`[[ $(git status 2> /dev/null | head -n2 | tail -n1) != "# Changes to be committed:" ]] && echo "\[\e[31m\]" || echo "\[\e[33m\]"[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] || echo "\[\e[32m\]"`$(__git_ps1 "(%s)\[\e[00m\]")\[\e[01;34m\]\w\[\e[00m\]\$ '

Now, sometimes (in a new environment mostly), we may configure the .bashrc just the way we want it and then:

git_2

Boom! You see a big "__git_ps1: command not found"  message.

That is because you don’t have the git-prompt.sh file you included in the directory loaded at the top of you .bashrc file (the /usr/share/git-core/contrib/completion/git-prompt.sh)

So in order to get rid of that, we need to include it. The file is available in contrib/completion/git-prompt.bash from the git repository git://git.kernel.org/pub/scm/git/git.git

So after you clone it, you should simply copy the missing files to your system file system. In my case, I preferred to copy the whole “completion” dir to my file systems, as I did not have it. So:

$ sudo cp -r completion/ /usr/share/git-core/contrib/

NOTE: If you are using MacOS, the path should be /usr/local/git/contrib/. For more MacOS, please consider visiting this github page.

Now I have my git-completion.bash file in the correct place. Let’s see if that worked when running a new bash session:

git_3

Great! Worked like a charm.

Let me know if you have a different solution for it or if this did not solve your problem.

Thanks for reading.

Cheers!

Technically Speaking was used as a reference for this post.

Advertisements

Manage users on your system

This is a quick checklist on how to create a user from the command line:
1. useradd -m new_username (add a user called new_username with a home directory)
2. passwd new_username (set a password for new_username)
3. (optional) usermod -aG wheel new_username  (to include the user in the sudoers list)
4. su - new_username (to change to the new_username account)
NOTE: sudo usermod -a -G sudo username (to include user ‘username’ in the sudo group)

EDIT:

5. usermod -s /bin/bash NEW_USERNAME(to set the shell as bash. Change for other shells)

If you need to grant access to you system just for specific applications, you can tweak the visudo file. Using an admin user, issue:

  1. $ sudo visudocmnd_alias
  2. group the commands into aliases (line 18). List all commands you want to grant, separate by comma
  3. grant access to them (line 22). The ‘NOPASSWD’ will restrain to ask for a password for the minicom command. All the other commands ran with sudo will prompt for a password and will fail. To require password, do not include the ‘NOPASSWD’ tag.

Installing clang on RHEL/CentOS and Ubuntu

On Fedora 24:

Clang has a link problem and does not work well on F24:
https://whatofhow.wordpress.com/2016/07/04/clang-in-fedora-24/

On RHEL and CentOS:

1) Install EPEL repository (read here on how to install it)

2) Install clang package:
$ sudo yum install clang

That should be it!

On Ubuntu 16.04:

1) Install clang packages:
$ sudo apt-get install clang libclang-dev

2) Create a symlink to libclang:
$ cd /usr/lib/[arch]-linux-gnu
$ sudo ln -s libclang-[version].so libclang.so

Example:
$ cd /usr/lib/powerpc64le-linux-gnu
or…
$ cd /usr/lib/x86_64-linux-gnu 

and then…
$ sudo ln -s libclang-3.8.so libclang.so

Cheers!