copy to mercurial library from: https://gist.github.com/yuitowest/1426715