-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-cmp
executable file
·57 lines (46 loc) · 1.42 KB
/
git-cmp
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
#!/usr/bin/env ruby
# Usage:
#
# git cmp [BRANCH]
#
# Opens a GitHub compare view, optionally against a specific [BRANCH]
require 'uri'
Config = Struct.new(
:host, # github.com
:repo, # zachmargolis/margs-dev
)
# https://github.com/zachmargolis/margs-dev.git
def parse_https_url(url)
uri = URI.parse(url)
Config.new(uri.host, clean_up_path(uri.path))
end
# git@github.com:zachmargolis/margs-dev.git
def parse_git_url(url)
_git, rest = url.split('@', 2)
host, path = rest.split(':', 2)
Config.new(host, clean_up_path(path))
end
# "/zachmargolis/margs-dev.git" => "zachmargolis/margs-dev"
def clean_up_path(path)
path.gsub(%r|^/|, '').chomp('.git')
end
def github_compare_url(config, branch_name, from_branch = nil)
"https://#{config.host}/#{config.repo}/compare/" + (from_branch ? "#{from_branch}...#{branch_name}" : branch_name)
end
def gitlab_compare_url(config, branch_name, from_branch = nil)
"https://#{config.host}/#{config.repo}/-/compare/#{from_branch || 'main'}...#{branch_name}"
end
origin_url = `git remote get-url origin`.chomp
branch_name = `git rev-parse --abbrev-ref HEAD`.chomp
config = begin
parse_https_url(origin_url)
rescue URI::InvalidURIError
parse_git_url(origin_url)
end
from_branch = ARGV.first
compare_url = if config.host.include?('github')
github_compare_url(config, branch_name, from_branch)
else
gitlab_compare_url(config, branch_name, from_branch)
end
system('open', compare_url)