├── .gitmodules ├── .travis.yml ├── LICENSE ├── README.md ├── add-remote.plugin.zsh ├── add-remote.sh ├── git-add-remote ├── git-add-upstream └── tests ├── helper.sh ├── suite.clitest.md └── test /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "build"] 2 | path = build 3 | url = https://github.com/caarlos0/shell-travis-build.git 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: bash 2 | sudo: true 3 | os: 4 | - linux 5 | - osx 6 | install: 7 | - ./build/install.sh 8 | script: 9 | - ./build/build.sh 10 | - ./tests/test 11 | notifications: 12 | email: false 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2016 Carlos Alexandro Becker 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 | # git-add-remote [![Build Status](https://travis-ci.org/caarlos0/git-add-remote.svg?branch=master)](https://travis-ci.org/caarlos0/git-add-remote) 2 | 3 | > A shell plugin to easily add remotes to git repositories. 4 | 5 | # Why 6 | 7 | It is so damn boring to type or copy and paste the URLs changing only the 8 | fork name. 9 | 10 | # Usage 11 | 12 | Given that you are in a valid git managed folder, with a remote named `origin`, 13 | you can, for example, add your company's remote as upstream: 14 | 15 | ```console 16 | $ add-upstream company 17 | 18 | $ git remote -v 19 | origin git@github.com:caarlos0/random.git (fetch) 20 | origin git@github.com:caarlos0/random.git (push) 21 | upstream git@github.com:company/random.git (fetch) 22 | upstream git@github.com:company/random.git (push) 23 | ``` 24 | 25 | Or even add your coworker's remote to work in some branch together: 26 | 27 | ```console 28 | $ add-remote my-coworker 29 | 30 | $ git remote -v 31 | origin https://github.com/caarlos0/random.git (fetch) 32 | origin https://github.com/caarlos0/random.git (push) 33 | my-coworker https://github.com/my-coworker/random.git (fetch) 34 | my-coworker https://github.com/my-coworker/random.git (push) 35 | ``` 36 | 37 | Some people have hard aliases in github, so you can even set the remote's name: 38 | 39 | ```console 40 | $ add-remote _c0w0rk3r_0x12 newguy 41 | 42 | $ git remote -v 43 | origin git@github.com:caarlos0/random.git (fetch) 44 | origin git@github.com:caarlos0/random.git (push) 45 | newguy git@github.com:_c0w0rk3r_0x12/random.git (fetch) 46 | newguy git@github.com:_c0w0rk3r_0x12/random.git (push) 47 | ``` 48 | 49 | `newguy`'s tend to multiply, so I don't recommend naming it like that. 50 | 51 | # Installation 52 | 53 | It was innitialy made to be used in ZSH, so, if you use antibody, antigen, or 54 | whatever, just `bundle` it: 55 | 56 | ```console 57 | $ antibody bundle caarlos0/git-add-remote 58 | ``` 59 | 60 | If you use bash, you can clone this repo somewhere or just download the 61 | [add-remote.sh](/add-remote.sh) file and source the 62 | [add-remote.sh](/add-remote.sh) file in your `~/.profile` or wherever you like. 63 | 64 | ## Fig 65 | 66 | [Fig](https://fig.io) adds apps, shortcuts, and autocomplete to your existing terminal. 67 | 68 | 69 | Install `git-add-remote` in just one click. 70 | 71 | 72 | 73 | # `git alias`ing 74 | 75 | ```console 76 | $ git config --global alias.add-upstream '!zsh -ic \"add-upstream $*\"'' 77 | $ git config --global alias.add-remote '!zsh -ic \"add-remote $*\"'' 78 | $ git add-upstream org 79 | $ git add-remote user remote_name 80 | $ git add-remote user 81 | ``` 82 | 83 | > These examples are for zsh, but I'm pretty sure they also work on bash by 84 | replacing `zsh -ic` with `bash -ic`. 85 | -------------------------------------------------------------------------------- /add-remote.plugin.zsh: -------------------------------------------------------------------------------- 1 | add-remote.sh -------------------------------------------------------------------------------- /add-remote.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # gets the url for the given fork 3 | __addremote_url() { 4 | # shellcheck disable=SC2039 5 | local fork remote current 6 | fork="$1" 7 | if ! git config --get remote.origin.url > /dev/null 2>&1; then 8 | echo "A remote called 'origin' doesn't exist. Aborting." >&2 9 | return 1 10 | fi 11 | remote="$(git config --get remote.origin.url)" 12 | current="$(echo "$remote" | sed -e 's/.*github\.com.//' -e 's/\/.*//')" 13 | echo "$remote" | sed -e "s/$current/$fork/" 14 | } 15 | 16 | # adds a remote 17 | # shellcheck disable=SC2039 18 | add-remote() { 19 | # shellcheck disable=SC2039 20 | local fork="$1" name="$2" url 21 | test -z "$name" && name="$fork" 22 | url="$(__addremote_url "$fork")" || return 1 23 | git remote add "$name" "$url" 24 | } 25 | 26 | # adds an upstream remote 27 | # shellcheck disable=SC2039 28 | add-upstream() { 29 | add-remote "$1" "upstream" 30 | } 31 | -------------------------------------------------------------------------------- /git-add-remote: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # shellcheck disable=SC1090 3 | source "$(dirname "$0")/add-remote.sh" 4 | add-remote "$@" 5 | -------------------------------------------------------------------------------- /git-add-upstream: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # shellcheck disable=SC1090 3 | source "$(dirname "$0")/add-remote.sh" 4 | add-upstream "$@" 5 | -------------------------------------------------------------------------------- /tests/helper.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # setup a fake local repository with SSH protocol 3 | fakerepo_ssh() { 4 | cd "$(mktempdir)" || exit 5 | git init -q 6 | git remote add origin git@github.com:fake/fake.git 7 | pwd 8 | } 9 | 10 | # setup a fake local repository with HTTPS protocol 11 | fakerepo_https() { 12 | cd "$(mktempdir)" || exit 13 | git init -q 14 | git remote add origin https://github.com/fake/fake.git 15 | pwd 16 | } 17 | 18 | mktempdir() { 19 | mktemp -d 2>/dev/null || mktemp -d -t 'addremote' 20 | } 21 | -------------------------------------------------------------------------------- /tests/suite.clitest.md: -------------------------------------------------------------------------------- 1 | # add-remote test suite 2 | 3 | # setup 4 | 5 | ```console 6 | $ ROOT="$(pwd)" 7 | $ source add-remote.sh 8 | $ source tests/helper.sh 9 | $ 10 | ``` 11 | 12 | # add-upstream with ssh 13 | 14 | ```console 15 | $ cd "$(fakerepo_ssh)" 16 | $ add-upstream another-fake 17 | $ git config --get remote.upstream.url 18 | git@github.com:another-fake/fake.git 19 | $ 20 | ``` 21 | 22 | # add-upstream with https 23 | 24 | ```console 25 | $ cd "$(fakerepo_https)" 26 | $ add-upstream another-fake 27 | $ git config --get remote.upstream.url 28 | https://github.com/another-fake/fake.git 29 | $ 30 | ``` 31 | 32 | # add-remote with ssh and 2 args 33 | 34 | ```console 35 | $ cd "$(fakerepo_ssh)" 36 | $ add-remote fork becker 37 | $ git config --get remote.becker.url 38 | git@github.com:fork/fake.git 39 | $ 40 | ``` 41 | 42 | # add-remote with with https and 2 args 43 | 44 | ```console 45 | $ cd "$(fakerepo_https)" 46 | $ add-remote fork becker 47 | $ git config --get remote.becker.url 48 | https://github.com/fork/fake.git 49 | $ 50 | ``` 51 | 52 | # add-remote with ssh and 1 arg 53 | 54 | ```console 55 | $ cd "$(fakerepo_ssh)" 56 | $ add-remote caarlos0 57 | $ git config --get remote.caarlos0.url 58 | git@github.com:caarlos0/fake.git 59 | $ 60 | ``` 61 | 62 | # add-remote with https and 1 arg 63 | 64 | ```console 65 | $ cd "$(fakerepo_https)" 66 | $ add-remote caarlos0 67 | $ git config --get remote.caarlos0.url 68 | https://github.com/caarlos0/fake.git 69 | $ 70 | ``` 71 | 72 | # add-remote when folder is not git repository 73 | 74 | ```console 75 | $ cd "$(mktempdir)" 76 | $ add-remote whatever 77 | A remote called 'origin' doesn't exist. Aborting. 78 | $ 79 | ``` 80 | 81 | # add-upstream when folder is not git repository 82 | 83 | ```console 84 | $ cd "$(mktempdir)" 85 | $ add-upstream any 86 | A remote called 'origin' doesn't exist. Aborting. 87 | $ 88 | ``` 89 | 90 | # add-remote when origin does not exist with ssh 91 | 92 | ```console 93 | $ cd "$(fakerepo_ssh)" 94 | $ git remote remove origin 95 | $ add-remote ahoy 96 | A remote called 'origin' doesn't exist. Aborting. 97 | $ 98 | ``` 99 | 100 | # add-remote when origin does not exist with https 101 | 102 | ```console 103 | $ cd "$(fakerepo_https)" 104 | $ git remote remove origin 105 | $ add-remote ahoy 106 | A remote called 'origin' doesn't exist. Aborting. 107 | $ 108 | ``` 109 | 110 | # add-upstream when origin does not exist with ssh 111 | 112 | ```console 113 | $ cd "$(fakerepo_ssh)" 114 | $ git remote remove origin 115 | $ add-upstream nope 116 | A remote called 'origin' doesn't exist. Aborting. 117 | $ 118 | ``` 119 | 120 | # add-upstream when origin does not exist 121 | 122 | ```console 123 | $ cd "$(fakerepo_https)" 124 | $ git remote remove origin 125 | $ add-upstream nope 126 | A remote called 'origin' doesn't exist. Aborting. 127 | $ 128 | ``` 129 | -------------------------------------------------------------------------------- /tests/test: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | curl -s https://raw.githubusercontent.com/aureliojargas/clitest/master/clitest | 3 | bash -s -- ./tests/suite.clitest.md 4 | --------------------------------------------------------------------------------