From 09afc86a8f83bcf9cf55903c16217f15d3673726 Mon Sep 17 00:00:00 2001 From: Andrew Griffiths Date: Mon, 16 Jan 2017 12:36:40 +0000 Subject: [PATCH] Adds 2FA support to git-fork --- bin/git-fork | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/bin/git-fork b/bin/git-fork index cd39fdd91..09c61dc96 100755 --- a/bin/git-fork +++ b/bin/git-fork @@ -1,14 +1,12 @@ #!/usr/bin/env bash abort() { - echo $@ + echo "$@" exit 1 } url="$1" - -test -z "$url" && url=`git remote get-url origin 2> /dev/null` && origin=true - +test -z "$url" && url=$(git remote get-url origin 2> /dev/null) && origin=true # validate repo url test -z "$url" && abort "github repo needs to be specified as an argument" @@ -16,6 +14,8 @@ test -z "$url" && abort "github repo needs to be specified as an argument" echo "Enter your github username" read user [ "$user" = 'n' ] && abort "git username required" +echo "Enter github two-factor authentication code (leave blank if not set up)" +read MFA_CODE # extract owner + project from repo url project=${url##*/} @@ -31,13 +31,16 @@ fi [ -z "$project" -o -z "$owner" ] && abort "github repo needs to be specified as an argument" # create fork -curl -X POST -u "$user" "https://api.github.com/repos/$owner/$project/forks" +curl -qs \ + -X POST \ + -u "$user" \ + -H "X-GitHub-OTP: $MFA_CODE" \ + "https://api.github.com/repos/$owner/$project/forks" + [ $? = 0 ] || abort "fork failed" # Check if user has ssh configured with GitHub -ssh -o BatchMode=yes git@github.com exit >/dev/null 2>&1 -ssh_exit_code=$? -if [[ "$ssh_exit_code" = 0 ]] || [[ "$ssh_exit_code" = 1 ]]; then +if ssh -T git@github.com 2>&1 | grep -qi 'success'; then remote_prefix="git@github.com:" else remote_prefix="https://github.com/" @@ -45,14 +48,13 @@ fi if [ "$origin" = true ]; then git remote rename origin upstream - git remote add origin "$remote_prefix$user/$project" + git remote add origin "${remote_prefix}${user}/${project}.git" git fetch origin else # clone forked repo into current dir - git clone "$remote_prefix$user/$project" "$project" - + git clone "${remote_prefix}${user}/${project}.git" "$project" # add reference to origin fork so can merge in upstream changes cd "$project" - git remote add upstream "$remote_prefix$owner/$project" + git remote add upstream "${remote_prefix}${owner}/${project}.git" git fetch upstream fi