/
git-fork
executable file
·49 lines (39 loc) · 1.18 KB
/
git-fork
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
#!/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 user
[ "$user" = 'n' ] && abort "git username 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" -o -z "$owner" ] && abort "github repo needs to be specified as an argument"
# create pull request
curl -X POST -u "$user" "https://api.github.com/repos/$owner/$project/forks"
[ $? = 0 ] || abort "fork failed"
if [ "$origin" = true ]; then
git remote rename origin upstream
git remote add origin "https://github.com/$user/$project"
git fetch origin
else
# clone forked repo into current dir
git clone "https://github.com/$user/$project" "$project"
# add reference to origin fork so can merge in upstream changes
cd "$project"
git remote add upstream "https://github.com/$owner/$project"
git fetch upstream
fi