1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#!/usr/bin/env bash
abort() {
echo "$@"
exit 1
}
url="$1"
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 -r user
[ -n "$user" ] || abort "git username required"
# personal access token
# config name is github-personal-access-token '_' is not allowed in git config
github_personal_access_token=$(git config --default "$GITHUB_TOKEN" git-extras.github-personal-access-token)
test -z "$github_personal_access_token" && abort "GITHUB_TOKEN, or git config git-extras.github-personal-access-token required"
# extract owner + project from repo url
project=${url##*/}
owner=${url%/"$project"}
project=${project%.git}
if [[ $owner == git@* ]]; then
owner=${owner##*:}
else
owner=${owner##*/}
fi
# validate
[[ -z "$project" || -z "$owner" ]] && abort "github repo needs to be specified as an argument"
# create fork
if ! curl -qsf \
-X POST \
-u "$user:$github_personal_access_token" \
-H "X-GitHub-OTP: $MFA_CODE" \
"https://api.github.com/repos/$owner/$project/forks"
then
abort "fork failed"
fi
echo "Add GitHub remote branch via SSH (you will be prompted to verify the server's credentials)? (y/n)"
read -r use_ssh
# Check if user has ssh configured with GitHub
if [ -n "$use_ssh" ] && ssh -T git@github.com 2>&1 | grep -qi 'success'; then
remote_prefix="git@github.com:"
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"
git fetch origin
else
# clone forked repo into current dir
git clone "${remote_prefix}${user}/${project}.git" "$project"
# add reference to origin fork so can merge in upstream changes
cd "$project" || exit
git remote add upstream "${remote_prefix}${owner}/${project}.git"
git fetch upstream
fi
|