1

My distro is RHEL so I have a DIR_COLORS folder and an LS_COLORS variable. I know how to modify the colors. I also know how to check if a folder is a git repo. What I don't know is how to have ls change the color of the directory if that folder is a repo (default is blue, the repo folder would be purple or something).

Here's my attempt at bad sudo code

for d in ls
    if git_repo($d)
        LS_COLORS = git_colors
    else
        LS_COLORS = normal
    print ls[$d]

I know that probably doesn't make "legal" sense but hopefully you get the gist. I know I can manually do it by echo'ing it out in a single column and manually changing the text color (in theory) but I want it to look like a normal ls with multi columns.

If there's a better way I'll take it. I also want to throw this into my bashrc into a function so I can alias ls to that function

4

1 Answer 1

1

Git prompts

Rather than roll your own here I'd recommend using what's provided by the Git project out of the box.

On a CentOS 7.x box if you yum install git everything is already included with Git to do both commandline completion + the git prompt.

$ rpm -ql git|grep completion
/etc/bash_completion.d
/etc/bash_completion.d/git
/usr/share/doc/git-1.8.3.1/contrib/completion
/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.bash
/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.tcsh
/usr/share/doc/git-1.8.3.1/contrib/completion/git-completion.zsh
/usr/share/doc/git-1.8.3.1/contrib/completion/git-prompt.sh
/usr/share/git-core/contrib/completion
/usr/share/git-core/contrib/completion/git-completion.tcsh
/usr/share/git-core/contrib/completion/git-prompt.sh

Here's the steps to do this manually, they'd need to be added your ~/.bash_profile to make them permanent.

$ . /usr/share/git-core/contrib/completion/git-prompt.sh
$ mkdir somedir && cd somedir
$ git init
$ PS1="$GREEN\t$RED-$BLUE\u$YELLOW\w\[\033[m\]$MAGENTA\$(__git_ps1)$WHITE \$ "

Doing the above will set your prompt so that it goes from this:

[root@centos7 somedir]#

-to-

13:18:42-root/home/vagrant/somedir (master) $

NOTE: it's now displaying the branch that I'm currently on in this repo, in my prompt.

Where's the colors?

To get this colored you need to define the colors so that the variable PS1 for the prompt's color variables have corresponding escape codes for their respective colors:

RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
MAGENTA="\[\033[0;35m\]"
WHITE="\[\033[0;37m\]"

Now reset the PS1 again to pick these up:

13:24:26-root/home/vagrant/somedir (master) $ PS1="$GREEN\t$RED-$BLUE\u$YELLOW\w\[\033[m\]$MAGENTA\$(__git_ps1)$WHITE \$ "
13:24:35-root/home/vagrant/somedir (master) $

And it'll look like this now:

     ss#1

And what about changing the color of ls?

If is probably something I'd discourage your from because you need to then run a git status against every directory in each ls command to determine if it's a Git repo or not.

Imagine this:

$ ls somedir
dir1
dir2
dir3
dir4
dir5
dir6
dir7

would result in 7 git status commands to determine which directory is a Git repo and which is not, so that you can decide whether to color it as purple or not.

1
  • This is definitly useful for if I'm inside a repo. However my question is specific to if I'm in a parent directory where I have folders that some are cloned repos and some arent. From what I could tell you don't neccessarily need to do a git status against each one. What I've done in the past is cd, do if [ -d .git ] , and then you know if it's a repo
    – Excal
    Commented Jul 13, 2018 at 19:32

You must log in to answer this question.

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