/
push
executable file
·54 lines (46 loc) · 1.72 KB
/
push
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
#!/bin/sh
#
# Usage: push
#
# Send your local branch changes to the remote branch,
# and copy the relevant GitHub 'compare' URL to your clipboard (!)
# This is Mac-only for the time being
#
# Any extra args to this command will be passed through to 'git push',
# e.g. for doing "push -f"
#
branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') || exit $?
remote=$(git config "branch.${branch}.remote") || exit $?
remote_branch=$(git config "branch.${branch}.merge" | awk -F '/' '{ print $3 }') || exit $?
# Push & save output
push=$(git push --set-upstream $* $remote $remote_branch 2>&1)
exit_code=$?
if [ $exit_code != 0 ]; then
echo "Ouch, push failed! code=$exit_code, output=$push"
exit $exit_code
elif echo $push | grep "Everything up-to-date" >/dev/null; then
echo "git says everything is up-to-date!"
exit 0
fi
# Parse relevant commit refs and let user know what we did
# 1st-time push to new branch gets special treatment
if echo $push | grep "\[new branch\]" >/dev/null; then
refs="master...$branch"
echo "Pushed new branch '$branch' remotely"
else
refs=$(echo $push | awk '{ print $3}' | sed 's/\.\./\.\.\./')
echo $push
fi
# Parse output into a sexy GitHub compare URL!
remote_url=$(git remote show $remote -n | grep Push | awk '{ print $3 }')
if [[ "$remote_url" =~ "github.com" ]]; then
repo_name=$(echo $remote_url | sed 's/.*\:\(.*\)\.git/\1/')
github_url="https://github.com/$repo_name/compare/$refs"
copied="Compare URL copied to clipboard!"
which -s pbcopy >& /dev/null && echo $github_url | pbcopy && echo $copied
which xclip >& /dev/null && echo $github_url | xclip -selection clipboard && echo $copied
# which -s open && open -g $github_url
echo $github_url
echo
fi
exit 0