Last active
February 27, 2023 08:55
-
-
Save pvdb/edfc2f19fbf08dbaad2d540bce5d320b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# frozen_string_literal: true | |
# | |
# INSTALLATION | |
# | |
# ln -s ${PWD}/git-hub $(brew --prefix)/bin/ | |
# sudo ln -s ${PWD}/git-hub /usr/local/bin/ | |
# | |
# CONFIGURATION | |
# | |
# Smart Selection: Git Commit SHA | |
# Regular Expression: ([0-9a-f]{7,40}) | |
# Action Title: Open Commit on GitHub | |
# Parameter: ITERM2_PWD=\d /usr/local/bin/git-hub --commit "\1" | |
# | |
# Smart Selection: GitHub PR Number | |
# Regular Expression: #([0-9]+) | |
# Action Title: Open PR on GitHub | |
# Parameter: ITERM2_PWD=\d /usr/local/bin/git-hub --pr "\1" | |
# | |
# Smart Selection: GitHub Branch Name | |
# Regular Expression: origin/([-_\w]+) | |
# Action Title: Open Branch on GitHub | |
# Parameter: ITERM2_PWD=\d /usr/local/bin/git-hub --tree "\1" | |
# | |
# "ITERM2_PWD" gets set by iTerm2's "smart selection" actions | |
dir = Dir.new(ENV.fetch('ITERM2_PWD', Dir.pwd)) | |
require 'English' | |
module Kernel # rubocop:disable Style/Documentation | |
module_function | |
def open_url(url) | |
`open '#{url}'` | |
end | |
def open_repository(fullname) | |
open_url("https://github.com/#{fullname}") | |
end | |
end | |
class Dir # rubocop:disable Style/Documentation | |
REMOTE_URL_FORMAT = %r{ | |
\A | |
(?<protocol>git@|git://|https://) | |
(?<domain>(?<subdomain>gist\.)?github\.com) | |
(?<separator>[/:]) | |
(?<repo>(?<name>[^/]+/[^/]+)|(?<gist>[0-9a-f]+)) | |
(?<suffix>\.git) | |
\z | |
}x.freeze | |
def self.github_url_for(remote_url) | |
if remote_url.match REMOTE_URL_FORMAT | |
# https://www.git-scm.com/docs/git-clone#_git_urls | |
github_domain = $LAST_MATCH_INFO[:domain] | |
repo_fullname = $LAST_MATCH_INFO[:repo] | |
"https://#{github_domain}/#{repo_fullname}" | |
else | |
remote_url # TODO: return `nil` instead? | |
end | |
end | |
# this is for testing equivalency | |
# in the tests only... don't use! | |
def self.sed_equivalent(remote_url) | |
# rubocop:disable Layout/LineLength | |
`echo "#{remote_url}"|sed -E -e 's#^(git@|git://|https://)((gist\\.)?github\\.com)([/:])(([^/]*/[^/]*)|([0-9a-f]*))(\\.git)\$#https://\\2/\\5#'`.strip | |
# rubocop:enable Layout/LineLength | |
end | |
def git_repo? | |
`git -C #{path} rev-parse --show-toplevel` | |
$CHILD_STATUS.success? | |
end | |
def remote_url | |
`git -C #{path} config --local --get remote.origin.url`.strip | |
end | |
def github_url | |
Dir.github_url_for(remote_url) | |
end | |
def current_branch | |
`git -C #{path} symbolic-ref --quiet --short HEAD`.strip | |
end | |
def head_sha | |
`git -C #{path} rev-parse HEAD`.strip | |
end | |
def open_repo | |
open_url(github_url) | |
end | |
def open_tree(branch_name) | |
open_url(File.join(github_url, 'tree', branch_name)) | |
end | |
def open_branch | |
open_tree(current_branch) | |
end | |
def open_commit(commit_sha) | |
open_url(File.join(github_url, 'commit', commit_sha)) | |
end | |
def open_head | |
open_commit(head_sha) | |
end | |
def open_pr(pr_number) | |
open_url(File.join(github_url, 'pull', pr_number)) | |
end | |
def open_blob(rel_path) | |
open_url(File.join(github_url, 'blob', 'master', rel_path)) | |
end | |
end | |
if false # rubocop:disable Lint/LiteralAsCondition | |
require 'minitest/pride' | |
require 'minitest/autorun' | |
class DirTest < Minitest::Test # rubocop:disable Style/Documentation | |
def test_it_parses_ssh_clone_urls_for_repos | |
remote_url = 'git@github.com:github/hub.git' | |
github_url = 'https://github.com/github/hub' | |
assert_equal github_url, Dir.github_url_for(remote_url) | |
assert_equal github_url, Dir.sed_equivalent(remote_url) | |
end | |
def test_it_parses_git_clone_urls_for_repos | |
remote_url = 'git://github.com/github/hub.git' | |
github_url = 'https://github.com/github/hub' | |
assert_equal github_url, Dir.github_url_for(remote_url) | |
assert_equal github_url, Dir.sed_equivalent(remote_url) | |
end | |
def test_it_parses_https_clone_urls_for_repos | |
remote_url = 'https://github.com/github/hub.git' | |
github_url = 'https://github.com/github/hub' | |
assert_equal github_url, Dir.github_url_for(remote_url) | |
assert_equal github_url, Dir.sed_equivalent(remote_url) | |
end | |
def test_it_parses_ssh_clone_urls_for_gists | |
remote_url = 'git@gist.github.com:edfc2f19fbf08dbaad2d540bce5d320b.git' | |
github_url = 'https://gist.github.com/edfc2f19fbf08dbaad2d540bce5d320b' | |
assert_equal github_url, Dir.github_url_for(remote_url) | |
assert_equal github_url, Dir.sed_equivalent(remote_url) | |
end | |
def test_it_parses_https_clone_urls_for_gists | |
remote_url = 'https://gist.github.com/edfc2f19fbf08dbaad2d540bce5d320b.git' | |
github_url = 'https://gist.github.com/edfc2f19fbf08dbaad2d540bce5d320b' | |
assert_equal github_url, Dir.github_url_for(remote_url) | |
assert_equal github_url, Dir.sed_equivalent(remote_url) | |
end | |
def test_it_doesnt_parse_anything_else | |
assert_equal 'blegga', Dir.github_url_for('blegga') | |
assert_equal 'blegga', Dir.sed_equivalent('blegga') | |
end | |
end | |
exit | |
end | |
case ARGV.map!(&:strip).shift | |
when '--repo' | |
dir.git_repo? && dir.open_repo | |
when '--head' | |
dir.git_repo? && dir.open_head | |
when '--branch' | |
dir.git_repo? && dir.open_branch | |
when '--pr' | |
dir.git_repo? && ARGV.map(&dir.method(:open_pr)) | |
when '--tree' | |
dir.git_repo? && ARGV.map(&dir.method(:open_tree)) | |
when '--blob' | |
dir.git_repo? && ARGV.map(&dir.method(:open_blob)) | |
when '--commit' | |
dir.git_repo? && ARGV.map(&dir.method(:open_commit)) | |
when '--full' | |
ARGV.map(&method(:open_repository)) | |
end | |
# That's all Folks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment