1

I want to display my current branch status (like * meaning modified) when in a git repo over the terminal like the below. BUT, it is always displaying the following even when I switch to a git repo.

ᴾᴋᴹɴ iFeelNewsBot master wrong... ↪ 

When I open a new tab in my terminal it shows the proper text rendering for the current git branch seen further. How it should look

ᴾᴋᴹɴ iFeelNewsBot master * ↪ 

My custom bash profile code is below

# user name and color
USER_NAME='mr.universe';
TRAINER_TITLE='ᴾᴋᴹɴ'
USER_NAME_COLOR='\[\033[00m\]';
END_COLOR='\e[0m';

# \W = current working path
# \u = user
function parse_git_dirty {
  gitStatus=$(git status 2> /dev/null | tail -n1)
  clean="nothing to commit, working directory clean"
  if [[ -d "./.git" ]]
  then
    [[ $gitStatus != $clean ]] && echo "*" || echo "="
  elif [[ ! -d "./.git" ]]
  then
    echo "wrong...."
  else
    echo "STOP"
  fi
}

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}

export PS1="\[\033[40;02m\]$TRAINER_TITLE \W \[\033[36;02m\]\[\033[29;00m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2) $(parse_git_dirty) ↪ "
greg
  • 1,118
  • 1
  • 20
  • 40

2 Answers2

0

You're assuming every directory in a git repository has .git in it, but only the top directory of a repository has a .git directory in it. So the first if fails and elif checks if .git directory doesn't exist. Looks like the result is true and it prints 'wrong'.

0

You could check instead if you are inside a working tree of a git repo:

inside_git_repo="$(git -C $PWD rev-parse --is-inside-work-tree 2>/dev/null)"

if [ "$inside_git_repo" ]; then
  echo "inside git repo"
else
  echo "not in git repo"
fi

This is more reliable than looking for a .git folder.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • displaying `"inside git repo"` works, but when I swap to a new non git directory `"not in git repo"` is not displayed until I open a new tab or refresh my terminal – greg May 29 '17 at 19:18
  • By swapping to a new branch, do you mean `git checkout aNewBranch`? And what "refreshing a terminal" means? – VonC May 29 '17 at 19:19
  • my mistake I mean `cd` to a new non git directory. I mean close and reopen my terminal. – greg May 29 '17 at 19:22
  • If you `cd` to a non git directory, it stands to reason that the script displays "non in git repo", no? – VonC May 29 '17 at 19:26
  • it does not, instead it displays `"not in git repo"` until I open a new tab in my mac terminal or interestingly run this command `source ~/.bash_profile` – greg May 29 '17 at 19:31
  • @GregUniverse but if you have cd to a **non** git directory, it should display `not in git repo`. Maybe the bash_profile, when sourced, is sourced in a folder which is not the current one? Try and add (just for testing) a line with just `pwd`, to force the display of the current folder in which the bash_profile is sourced. – VonC May 29 '17 at 19:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145393/discussion-between-greg-universe-and-vonc). – greg May 29 '17 at 19:38