|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | abort() { |
4 | | - echo $@ |
| 4 | + echo "$@" |
5 | 5 | exit 1 |
6 | 6 | } |
7 | 7 |
|
8 | 8 | url="$1" |
9 | | - |
10 | | -test -z "$url" && url=`git remote get-url origin 2> /dev/null` && origin=true |
11 | | - |
| 9 | +test -z "$url" && url=$(git remote get-url origin 2> /dev/null) && origin=true |
12 | 10 | # validate repo url |
13 | 11 | test -z "$url" && abort "github repo needs to be specified as an argument" |
14 | 12 |
|
15 | 13 | # validate user |
16 | 14 | echo "Enter your github username" |
17 | 15 | read user |
18 | | -[ "$user" = 'n' ] && abort "git username required" |
| 16 | +[ -n "$user" ] || abort "git username required" |
| 17 | +echo "Enter github two-factor authentication code (leave blank if not set up)" |
| 18 | +read MFA_CODE |
19 | 19 |
|
20 | 20 | # extract owner + project from repo url |
21 | 21 | project=${url##*/} |
|
31 | 31 | [ -z "$project" -o -z "$owner" ] && abort "github repo needs to be specified as an argument" |
32 | 32 |
|
33 | 33 | # create fork |
34 | | -curl -X POST -u "$user" "https://api.github.com/repos/$owner/$project/forks" |
| 34 | +curl -qs \ |
| 35 | + -X POST \ |
| 36 | + -u "$user" \ |
| 37 | + -H "X-GitHub-OTP: $MFA_CODE" \ |
| 38 | + "https://api.github.com/repos/$owner/$project/forks" |
| 39 | + |
35 | 40 | [ $? = 0 ] || abort "fork failed" |
36 | 41 |
|
| 42 | +echo "Add GitHub remote branch via SSH (you will be prompted to verify the server's credentials)? (y/n)" |
| 43 | +read use_ssh |
37 | 44 | # Check if user has ssh configured with GitHub |
38 | | -ssh -o BatchMode=yes [email protected] exit >/dev/null 2>&1 |
39 | | -ssh_exit_code=$? |
40 | | -if [[ "$ssh_exit_code" = 0 ]] || [[ "$ssh_exit_code" = 1 ]]; then |
| 45 | +if [ -n "$use_ssh" ] && ssh -T [email protected] 2>&1 | grep -qi 'success'; then |
41 | 46 | remote_prefix= "[email protected]:" |
42 | 47 | else |
43 | 48 | remote_prefix="https://github.com/" |
44 | 49 | fi |
45 | 50 |
|
46 | 51 | if [ "$origin" = true ]; then |
47 | 52 | git remote rename origin upstream |
48 | | - git remote add origin "$remote_prefix$user/$project" |
| 53 | + git remote add origin "${remote_prefix}${user}/${project}.git" |
49 | 54 | git fetch origin |
50 | 55 | else |
51 | 56 | # clone forked repo into current dir |
52 | | - git clone "$remote_prefix$user/$project" "$project" |
53 | | - |
| 57 | + git clone "${remote_prefix}${user}/${project}.git" "$project" |
54 | 58 | # add reference to origin fork so can merge in upstream changes |
55 | 59 | cd "$project" |
56 | | - git remote add upstream "$remote_prefix$owner/$project" |
| 60 | + git remote add upstream "${remote_prefix}${owner}/${project}.git" |
57 | 61 | git fetch upstream |
58 | 62 | fi |
0 commit comments