├── LICENSE ├── README.md ├── _cd-gitroot ├── cd-gitroot └── cd-gitroot.plugin.zsh /LICENSE: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | # 3 | # Copyright (c) 2013 Hideaki Miyake 4 | # 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | # 12 | # The above copyright notice and this permission notice shall be included in 13 | # all copies or substantial portions of the Software. 14 | # 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | # THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cd-gitroot 2 | 3 | ## Synopsis 4 | zsh plugin to change directory to git repository root directory 5 | 6 | Inspired by [id:hitode909 blog post](http://hitode909.hatenablog.com/entry/20100211/1265879271). 7 | 8 | ## How to set up 9 | 10 | ### Manually install 11 | 12 | Put cd-gitroot and _cd-gitroot files somewhere in your $fpath and add the following line to your .zshrc: 13 | 14 | ``` 15 | autoload -Uz cd-gitroot 16 | ``` 17 | 18 | #### For example 19 | 20 | ``` 21 | # download all files 22 | % cd /path/to/dir 23 | % git clone https://github.com/mollifier/cd-gitroot.git 24 | ``` 25 | 26 | And add the following lines to your .zshrc: 27 | 28 | ``` 29 | fpath=(/path/to/dir/cd-gitroot(N-/) $fpath) 30 | 31 | autoload -Uz cd-gitroot 32 | ``` 33 | 34 | ### Installing using Antigen 35 | If you use [Antigen](https://github.com/zsh-users/antigen), add the following line to your .zshrc: 36 | 37 | ``` 38 | antigen bundle mollifier/cd-gitroot 39 | ``` 40 | ### Using Oh-my-zsh: 41 | If you use [Oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh): 42 | 43 | 1. Clone this repository in oh-my-zsh's plugins directory: 44 | 45 | git clone https://github.com/mollifier/cd-gitroot.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/cd-gitroot 46 | you 47 | 2. Activate the plugin in `~/.zshrc`: 48 | 49 | plugins=( [plugins...] cd-gitroot) 50 | 51 | 3. Source `~/.zshrc` to take changes into account: 52 | 53 | source ~/.zshrc 54 | ### Installing using Zgen 55 | If you use [zgen](https://github.com/tarjoilija/zgen), add the following to your `.zshrc` 56 | ``` 57 | zgen load mollifier/cd-gitroot 58 | ``` 59 | to your `.zshrc` with your other `zgen load` commands. 60 | 61 | You can set alias to this function. 62 | e.g. 63 | 64 | ``` 65 | alias cdu='cd-gitroot' 66 | ``` 67 | 68 | ## Usage 69 | 70 | ``` 71 | cd-gitroot [PATH] 72 | ``` 73 | 74 | If PATH isn't specified, change directory to current git repository root directory. 75 | else change directory to PATH instead of it. 76 | PATH is treated relative path in git root directory. 77 | 78 | ## Options 79 | \-h display help and exit 80 | 81 | -------------------------------------------------------------------------------- /_cd-gitroot: -------------------------------------------------------------------------------- 1 | #compdef cd-gitroot 2 | # ------------------------------------------------------------------------------ 3 | # Copyright (c) 2013 Hideaki Miyake 4 | # Licensed under the MIT License (MIT) 5 | # ------------------------------------------------------------------------------ 6 | # Description 7 | # ----------- 8 | # 9 | # Completion script for cd-gitroot (https://github.com/mollifier/cd-gitroot) 10 | # 11 | # ------------------------------------------------------------------------------ 12 | # Authors 13 | # ------- 14 | # 15 | # * Hideaki Miyake (https://github.com/mollifier) 16 | # 17 | # ------------------------------------------------------------------------------ 18 | 19 | declare -a opts args 20 | args=( 21 | '-h[display this help and exit]' 22 | '1:path:_cd-gitroot_path' 23 | ) 24 | local curcontext=$curcontext state line ret=1 25 | declare -A opt_args 26 | 27 | # show directories in git top level directory 28 | (( $+functions[_cd-gitroot_path] )) || 29 | _cd-gitroot_path() { 30 | if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) != 'true' ]]; then 31 | return 1 32 | fi 33 | 34 | local root_path=$(git rev-parse --show-toplevel) 35 | _path_files -W "$root_path" -/ 36 | } 37 | 38 | _arguments -C $opts \ 39 | $args && ret=0 40 | 41 | return $ret 42 | 43 | # Local Variables: 44 | # mode: Shell-Script 45 | # sh-indentation: 2 46 | # indent-tabs-mode: nil 47 | # sh-basic-offset: 2 48 | # End: 49 | # vim: ft=zsh sw=2 ts=2 et 50 | 51 | -------------------------------------------------------------------------------- /cd-gitroot: -------------------------------------------------------------------------------- 1 | # cd-gitroot - zsh plugin to cd to git repository root directory 2 | # 3 | # Copyright (c) 2013 Hideaki Miyake 4 | # Licensed under the MIT License (MIT) 5 | # 6 | # Author : Hideaki Miyake (https://github.com/mollifier) 7 | # URL : https://github.com/mollifier/cd-gitroot 8 | # 9 | # How to set up 10 | # Put cd-gitroot and _cd-gitroot files somewhere in your $fpath 11 | # and add this line to your .zshrc: 12 | # 13 | # autoload -Uz cd-gitroot 14 | # 15 | # Usage: 16 | # cd-gitroot [PATH] 17 | # 18 | 19 | function cdgitroot_print_usage() 20 | { 21 | cat << EOF 22 | Usage: cd-gitroot [PATH] 23 | Change directory to current git repository root directory. 24 | If PATH is specified, change directory to PATH instead of it. 25 | PATH is treated relative path in git root directory. 26 | 27 | -h display this help and exit 28 | EOF 29 | } 30 | 31 | function cdgitroot_print_error() 32 | { 33 | echo "cd-gitroot: $@" 1>&2 34 | echo "Try \`-h' option for more information." 1>&2 35 | } 36 | 37 | function cdgitroot() { 38 | 39 | local option OPTARG OPTIND 40 | while getopts ':h' option; do 41 | case $option in 42 | h) 43 | cdgitroot_print_usage 44 | return 0 45 | ;; 46 | :) 47 | cdgitroot_print_error "option requires an argument -- $OPTARG" 48 | return 1 49 | ;; 50 | *) 51 | cdgitroot_print_error "invalid option -- $OPTARG" 52 | return 1 53 | ;; 54 | esac 55 | done 56 | shift $(expr $OPTIND - 1) 57 | 58 | if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) != 'true' ]]; then 59 | cdgitroot_print_error "Not in a git repository" 60 | return 1 61 | fi 62 | 63 | local relative_path="$1" 64 | local root_path=$(git rev-parse --show-toplevel) 65 | 66 | if [ -n "$relative_path" ]; then 67 | cd "$root_path/$relative_path" 68 | else 69 | cd "$root_path" 70 | fi 71 | } 72 | 73 | cdgitroot "$@" 74 | 75 | # vim:set ft=zsh: 76 | -------------------------------------------------------------------------------- /cd-gitroot.plugin.zsh: -------------------------------------------------------------------------------- 1 | # init script for antigen 2 | autoload -Uz cd-gitroot 3 | --------------------------------------------------------------------------------