├── gitio.plugin.zsh ├── preview.png ├── LICENSE ├── README.md └── gitio.zsh /gitio.plugin.zsh: -------------------------------------------------------------------------------- 1 | gitio.zsh -------------------------------------------------------------------------------- /preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/denysdovhan/gitio-zsh/HEAD/preview.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015, Denys Dovhan 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![gitio-zsh](./preview.png) 2 | 3 | # gitio ![zsh][zsh] [![MIT][mit-img]][mit-url] 4 | 5 | > A zsh plugin for generating a GitHub short URL using [git.io](https://git.io). 6 | 7 | Inspired by [tiny](https://github.com/fishery/tiny) utility by [Jorge Bucaran](https://github.com/bucaran). 8 | 9 | ## Install 10 | 11 | ### For antigen users 12 | 13 | Add the following snippet `~/.zshrc` after the line `antigen use oh-my-zsh`: 14 | 15 | ``` 16 | $ antigen bundle denysdovhan/gitio-zsh 17 | ``` 18 | 19 | ### For Zgen users 20 | 21 | Add the following line to your `~/.zshrc` where you're adding your other zsh plugins: 22 | 23 | ``` 24 | zgen load denysdovhan/gitio-zsh 25 | ``` 26 | 27 | ### Manually 28 | 29 | If you don't use any plugin managers, follow these instructions: 30 | 31 | 1. Clone this repository to your favorite path (e.g. `~/zsh-extensions/gitio-zsh`) 32 | 2. `source` the file in your `.zshrc` 33 | 3. Restart your `zsh` 34 | 35 | ```sh 36 | # Your .zshrc 37 | source $HOME/zsh-extensions/gitio-zsh/gitio.plugin.zsh 38 | ``` 39 | 40 | ## Usage 41 | 42 | In git repo: 43 | 44 | ```zsh 45 | gitio 46 | ``` 47 | 48 | In other directory: 49 | 50 | ```zsh 51 | gitio <[https://github.com/]username/repo> [cool-url] 52 | ``` 53 | 54 | ## License 55 | 56 | [MIT][mit-url] © [Denys Dovhan](http://denysdovhan.com) 57 | 58 | 59 | 60 | [zsh]: https://img.shields.io/badge/shell-zsh-brightgreen.svg?style=flat-square 61 | 62 | [mit-img]: https://img.shields.io/badge/License-MIT-707070.svg?style=flat-square 63 | [mit-url]: http://opensource.org/licenses/MIT 64 | -------------------------------------------------------------------------------- /gitio.zsh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env zsh 2 | 3 | 4 | # SYNOPSIS 5 | # gitio [[https://github.com/]username/repo] [vanity code] 6 | # 7 | # DESCRIPTION 8 | # A zsh plugin for generating a GitHub short URL. 9 | # 10 | # LINKS 11 | # github.com/blog/985-git-io-github-url-shortener 12 | # github.com/fishery/tiny 13 | function gitio() { 14 | 15 | # Show help message with colors 16 | function gitio-help() { 17 | printf "USAGE: gitio [[%s]%s/%s] [%s] \n" \ 18 | "https://github.com/" \ 19 | "${fg[cyan]}username${reset_color}" \ 20 | "${fg[magenta]}repo${reset_color}" \ 21 | "${fg[green]}vanity code${reset_color}" \ 22 | } 23 | 24 | # Get URL from git config 25 | if [[ "$#" -eq 0 && -s ".git/config" ]]; then 26 | local CONFIG_URL="$( 27 | awk '/url =/ { 28 | sub(/^.+github\.com[\/:]/, "", $3) 29 | sub(/\.git$/, "", $3) 30 | print $3 31 | exit 32 | }' .git/config 33 | )" 34 | fi 35 | 36 | # Show help message for -h and --help flags 37 | if [[ "$#" -eq 0 && -z "$CONFIG_URL" || "$1" == "-h" || "$1" == "--help" ]]; then 38 | gitio-help 39 | return 0 40 | fi 41 | 42 | # Check if URL contains http(s) 43 | if [[ -n "$CONFIG_URL" ]]; then 44 | local URL="https://github.com/$CONFIG_URL" 45 | elif [[ ! "$1" =~ ^(http|https)://github.com/ ]]; then 46 | local URL="https://github.com/$1" 47 | else 48 | local URL="$1" 49 | fi 50 | 51 | # Check if code is valid 52 | if [[ -n "$2" ]]; then 53 | local CODE="$(echo "$2" | tr "[:upper:]" "[:lower:]")" 54 | 55 | if [[ ! "$CODE" =~ ^[0-9a-z\-]+$ ]]; then 56 | printf "Error: %s code is invalid." "${fg_bold[red]}$2${reset_color}" 57 | return 1 58 | fi 59 | fi 60 | 61 | # Make request to git.io 62 | if [[ -n "$URL" && -n "$CODE" ]]; then 63 | local GITIO_URL="$( 64 | curl --include --silent https://git.io/ \ 65 | --form url="$URL" \ 66 | --form code="$CODE" \ 67 | )" 68 | elif [[ -n "$URL" ]]; then 69 | local GITIO_URL="$( 70 | curl --include --silent https://git.io/ \ 71 | --form url="$URL" \ 72 | )" 73 | else 74 | gitio-help 75 | return 1 76 | fi 77 | 78 | # Get shorten URL from response 79 | GITIO_URL="$( 80 | echo "$GITIO_URL" \ 81 | | grep "Location: http" \ 82 | | cut -c11- \ 83 | | awk '{print substr($0, 1, length($0) - 1)}' \ 84 | )" 85 | 86 | # Copy to clipboard if possible 87 | case "$(uname)" in 88 | ("Darwin") echo "$GITIO_URL" | pbcopy ;; 89 | ("Linux") echo "$GITIO_URL" | xclip -selection c ;; 90 | esac 91 | 92 | # Print shorten URL to terminal 93 | echo "${fg[cyan]}$GITIO_URL${reset_color}" 94 | 95 | # Open in browser if possible 96 | open "$GITIO_URL" 97 | } 98 | --------------------------------------------------------------------------------