#!/bin/bash # Copyright (C) 2015-2020 Miquel Sabaté Solà # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . set -e # This script adds the `fork` subcommand to git. This subcommand helps to keep # a forked project updated with upstream. So, in a forked repository you would # do: # # $ git fork # # The previous command will fetch the upstream remote and merge the master # branch. You can change the remote and the branch to be picked like this: # # $ git fork branch # $ git fork remote branch # # Finally, if the given remote does not exist, the user will be prompted to # provide the URL to the remote, so it can be created. # Configurable values: the name of the branch to be picked and the name of the # upstream remote. branch='master' upstream='upstream' if [ ! -z "$1" ]; then upstream="$1" fi if [ ! -z "$2" ]; then branch="$2" fi # Check whether the given upstream remote really exists. If it doesn't it will # ask the user for a remote URL. if [[ $(git remote show | xargs -0 | grep -Fxq "$upstream") -eq 1 ]]; then echo "The given upstream '$upstream' remote does not exist" echo -n "Provide one: " read -ra url git remote add $upstream $url fi git fetch $upstream && git merge $upstream/$branch