Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds 2FA support to git-fork #615

Merged
merged 1 commit into from
Jan 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions bin/git-fork
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#!/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"

# validate user
echo "Enter your github username"
read user
[ "$user" = 'n' ] && abort "git username required"
[ -n "$user" ] || 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##*/}
Expand All @@ -31,28 +31,32 @@ 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"

echo "Add GitHub remote branch via SSH (you will be prompted to verify the server's credentials)? (y/n)"
read use_ssh
# Check if user has ssh configured with GitHub
ssh -o BatchMode=yes [email protected] exit >/dev/null 2>&1
ssh_exit_code=$?
if [[ "$ssh_exit_code" = 0 ]] || [[ "$ssh_exit_code" = 1 ]]; then
if [ -n "$use_ssh" ] && ssh -T [email protected] 2>&1 | grep -qi 'success'; then
remote_prefix="[email protected]:"
else
remote_prefix="https://github.com/"
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