aboutsummaryrefslogtreecommitdiff
path: root/bin/git-fork
diff options
context:
space:
mode:
Diffstat (limited to 'bin/git-fork')
-rwxr-xr-xbin/git-fork64
1 files changed, 64 insertions, 0 deletions
diff --git a/bin/git-fork b/bin/git-fork
new file mode 100755
index 0000000..a03b7a9
--- /dev/null
+++ b/bin/git-fork
@@ -0,0 +1,64 @@
+#!/bin/bash
+# Copyright (C) 2015-2016 Miquel Sabaté Solà <mikisabate@gmail.com>
+#
+# 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 <http://www.gnu.org/licenses/>.
+
+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'
+
+# You can modify the defaults by specifying either "branch" or "remote/branch".
+if [ ! -z $1 ]; then
+ ifs=$IFS
+ IFS='/'
+
+ read -ra results <<< "$1"
+ if [ ${#results[@]} -eq 2 ]; then
+ upstream=${results[0]}
+ branch=${results[1]}
+ else
+ branch=${results[0]}
+ fi
+
+ IFS=$ifs
+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