I have a problem. I keep constantly committing (and pushing) to wrong branches and then keep searching for how to move those commits to the right place. So I decided that bash prompt should remind me, what branch I am currently on. I need a scm tool name (because I use several) and a branch.

Bash has some special environment variables, which are responsible for the bash prompt. Those are PS1, PS2, PS3, PS4 and PROMPT_COMMAND. We can set our prompt by changing the value of the PS1 environment variable, for example like that:

$ export PS1="My new prompt> "

But this only assigns the prompt statically. What we actually need is, assigning dynamically, based on what our current working directory is. For that the PROMPT_COMMAND variable is responsible. Its value is interpreted as a command to execute before the printing of each primary prompt ($PS1). PS1 variable is set in your ~/.bashrc, so what I did is added some code there:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
+PROMPT_COMMAND=scm_prompt
+
+scm_prompt(){
+  if [ -d ".git" ]; then
+    scm=" git(`git symbolic-ref --short HEAD`)"
+  elif [ -d ".hg" ]; then
+    scm=" hg(`hg branch`)"
+  else
+    scm=""
+  fi
+}
+
 if [ "$color_prompt" = yes ]; then
   PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
 else
-  PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
+  PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w${scm}\$ '
 fi
 unset color_prompt force_color_prompt

Now, if current directory contains .git or .hg directories, then I will see the current scm and branch I am on in bash prompt.

jt@ratchet:~/projects/dotfiles hg(default)$
comments powered by Disqus