Generate github compare link

A bash script to easily generate a compare link for Github between current and previous branch

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
#!/bin/bash

# Get previous branch
prev=$(git rev-parse --abbrev-ref @{-1})

# Get current branch
current=$(git rev-parse --abbrev-ref HEAD)

# Get remote URL, assumes ssh
remote=$(git config --get remote.origin.url)

# Replace some parts
remote=${remote/git@github.com:/}
remote=${remote/\.git/}

# Final URL
url=https://github.com/$remote/compare/$prev...$current?expand=1

# Output to screen
echo $url

# Mac OSX copy to clipboard with pbcopy
( which pbcopy >/dev/null 2>&1 ) &&
echo "$url" | pbcopy &&
echo "[+] Copied to clipboard"

# Linux copy to both the selection buffer and clipboard with xclip.
( which xclip >/dev/null 2>&1 ) &&
echo "$url" | xclip -i -sel c -f |xclip -i -sel p &&
echo "[+] Copied to clipboard"