Category Archives: unix

How to see git/mercurial branch on your command line

Add this to your `~/.bashrc` file:


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

function hg_dirty() {
hg status --no-color 2> /dev/null \
| awk '$1 == "?" { unknown = 1 }
$1 != "?" { changed = 1 }
END {
if (changed) printf "!"
else if (unknown) printf "?"
}'
}

function hg_branch() {
hg branch 2> /dev/null | awk '{ printf " (" $1 ")" }'
hg bookmarks -a 2> /dev/null | awk '/\*/ { printf " (" $2 ")"}'
}

RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"

DEFAULT="[37;40m"
PINK="[35;40m"
RANGE="[33;40m"

PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$YELLOW\$(hg_branch)$NO_COLOR\$ "

How to configure Presto/Hive/HDFS on Mac

It is quite a pain to setup everything.
Here are some links which helped me significantly:

Tricks:

Use java 1.7 with newest hadoop/hdfs/hive 2.0.0

To create metastore – go to $HIVE_HOME/bin and run:

schematool -initSchema -dbType derby

Derby is java in-memory database. This option will not allow you to run simultaneously Hive metastore (required for Presto) and Hive itself and so consider using mysql for metastore.

Then install presto going through instructions on prestodb.io

So to use presto – you need to shutdown Hive CLI and start metastore service from same directory where your derby is being set with schematool. To start metastore:

hive --service metastore

To check which components of Hive/HDFS are running on machine, run:

jps

To start datanode:

hdfs datanode

Create 2 aliases in ~/.bashrc to start/stop hadoop/hdfs:

alias hstart="/usr/local/Cellar/hadoop/2.7.1/sbin/start-dfs.sh;/usr/local/Cellar/hadoop/2.7.1/sbin/start-yarn.sh"
alias hstop="/usr/local/Cellar/hadoop/2.7.1/sbin/stop-yarn.sh;/usr/local/Cellar/hadoop/2.7.1/sbin/stop-dfs.sh"

Slate – windows manager for Mac

I have discovered Slate – windows manager for Mac which is doing exceptionally great job in improving my productivity.

Begin with reading of this article from Tristan Hume on how to start working with Slate:

http://thume.ca/howto/2012/11/19/using-slate/

And here is slate by itself with full tutorial:

https://github.com/jigish/slate

Here is part of config which explains the idea:


config windowHintsShowIcons true
config windowHintsIgnoreHiddenWindows false
config windowHintsSpread true
config windowHintsDuration 5

bind esc:cmd hint

alias mon-laptop      1680x1050
alias mon-monitor     2560x1600

alias right-top move screenOriginX+screenSizeX/2;screenOriginY                   screenSizeX/2;screenSizeY/2 ${mon-monitor}
alias right-bottom move screenOriginX+screenSizeX/2;screenOriginY+screenSizeY/2     screenSizeX/2;screenSizeY/2 ${mon-monitor}
alias left-top move screenOriginX;screenOriginY                                 screenSizeX/2;screenSizeY/2 ${mon-monitor}
alias left-bottom move screenOriginX;screenOriginY+screenSizeY/2                   screenSizeX/2;screenSizeY/2 ${mon-monitor}
alias full-screen move screenOriginX;screenOriginY                                 screenSizeX;screenSizeY ${mon-laptop}

bind pad9:cmd ${right-top}
bind pad3:cmd ${right-bottom}
bind pad7:cmd ${left-top}
bind pad1:cmd ${left-bottom}
bind pad5:cmd ${full-screen}

Here is my full config:
https://gist.github.com/mgalushka/d79c68464f191ba8e11a

I use next combinations to manage windows:

cmd+number on pad panel – I use it to move current active window to corresponding position: to corners/half the screen up and down.

For window hints I use cmd+esc

Find process_id to be killed

Very often I need to kill some background job in unix.

To do this, I need to find its process_id to be passed to


kill -9 process_id

to be killed properly.

Here is quick way to combine finding process id for specific job:


ps aux | grep [my-fancy-filter-to-find-a task] |\
         awk '{print $2}'

This will just print process_id for my task to be killed.

Caution! Please, use this with care as if your grep return not the process_id  you are expecting – you may get to a trouble.

Watch git/mercurial branch in command prompt

Sometimes this is crucial to not make a mistake committing in wrong branch.

To help mitigating this type of errors, just enable previewing in prompt the current branch you are on.

Following code works equally for git/mercurial branches, you need to put this into your ~/.bashrc file.

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

function hg_branch() {
      hg branch 2> /dev/null |
           awk '{ printf "\033[37;0m\033[35;40m" $1 }'
      hg bookmarks 2> /dev/null |
           awk '/\*/ { printf " (" $2 ")"}'
}

PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$YELLOW\$(hg_branch)$NO_COLOR\$ "

For mercurial it also display current bookmark you are on.

This is how it look on my command prompt now:

Command prompt with git/mercurial branch