├── .rspec ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── plugin ├── github_url.rb └── open-github.vim └── spec ├── plugin └── github_url_spec.rb └── spec_helper.rb /.rspec: -------------------------------------------------------------------------------- 1 | -c 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gem "rake" 4 | gem "pry" 5 | gem "rspec", "~> 3.1.0" 6 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | coderay (1.1.0) 5 | diff-lcs (1.2.5) 6 | method_source (0.8.2) 7 | pry (0.10.1) 8 | coderay (~> 1.1.0) 9 | method_source (~> 0.8.1) 10 | slop (~> 3.4) 11 | rake (10.3.2) 12 | rspec (3.1.0) 13 | rspec-core (~> 3.1.0) 14 | rspec-expectations (~> 3.1.0) 15 | rspec-mocks (~> 3.1.0) 16 | rspec-core (3.1.7) 17 | rspec-support (~> 3.1.0) 18 | rspec-expectations (3.1.2) 19 | diff-lcs (>= 1.2.0, < 2.0) 20 | rspec-support (~> 3.1.0) 21 | rspec-mocks (3.1.3) 22 | rspec-support (~> 3.1.0) 23 | rspec-support (3.1.2) 24 | slop (3.6.0) 25 | 26 | PLATFORMS 27 | ruby 28 | 29 | DEPENDENCIES 30 | pry 31 | rake 32 | rspec (~> 3.1.0) 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-open-github 2 | 3 | NOTE: This is no longer maintained in favor of [tyru/open-browser-github.vim](https://github.com/tyru/open-browser-github.vim). 4 | Please use its `:OpenGithubFile`. 5 | 6 | ## Description 7 | 8 | Quickly open your current buffer in GitHub. 9 | This plugin is a forked version of [vim-to-github](https://github.com/tonchis/vim-to-github) with a support of flexible remote path, such as GitHub Enterprise. 10 | 11 | ![](http://i.gyazo.com/0473edc2f72f1e8bf8b4111023a9993b.gif) 12 | 13 | ## Usage 14 | 15 | ### Basic 16 | 17 | ``` 18 | :OpenGithub 19 | ``` 20 | 21 | Will load origin's url and open current buffer's file in GitHub. 22 | 23 | ### Highlight lines 24 | 25 | ``` 26 | :'<,'>OpenGithub 27 | ``` 28 | 29 | Visual mode is supported. 30 | 31 | ### Copy to pasteboard 32 | 33 | ``` 34 | :'<,'>CopyGithub 35 | ``` 36 | 37 | No need to copy browser's address bar. 38 | 39 | ### Specify branch, tag or revision 40 | 41 | ``` 42 | :OpenGithub v4.2.3 43 | ``` 44 | 45 | You can specify the revision to open. 46 | 47 | ## Installation 48 | 49 | Example for [neobundle.vim](https://github.com/Shougo/neobundle.vim) 50 | 51 | ```vim 52 | NeoBundle 'k0kubun/vim-open-github' 53 | ``` 54 | 55 | ## Development 56 | ### Run tests 57 | 58 | ```bash 59 | $ bundle 60 | $ rake 61 | ``` 62 | 63 | ### Embed Ruby code to vim script 64 | 65 | ```bash 66 | $ rake embed 67 | ``` 68 | 69 | ## Thanks 70 | 71 | - To [tonchis/vim-to-github](https://github.com/tonchis/vim-to-github) for original version 72 | 73 | ## License 74 | 75 | MIT License 76 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | 3 | desc "Embed ruby code into vim script" 4 | task :embed do 5 | vim_src_path = File.expand_path('../plugin/open-github.vim', __FILE__) 6 | ruby_src_path = File.expand_path('../plugin/github_url.rb', __FILE__) 7 | 8 | vim_src = File.read(vim_src_path) 9 | ruby_src = File.read(ruby_src_path) 10 | 11 | new_vim_src = vim_src.gsub(/<:p') 2 | 3 | function! s:calculate_github_url(line1, line2, args) 4 | if !has('ruby') 5 | echo "Your vim is not compiled as has('ruby'). Try building vim with ruby support." 6 | return 7 | endif 8 | 9 | ruby <, , ) 138 | command! -nargs=* -range CopyGithub :call CopyGithub(, , ) 139 | 140 | " Thanks to https://github.com/mattn/gist-vim 141 | function! s:get_browser_command() 142 | let browser_command = get(g:, 'browser_command', '') 143 | if browser_command == '' 144 | if has('win32') || has('win64') 145 | let browser_command = '!start rundll32 url.dll,FileProtocolHandler %URL%' 146 | elseif has('mac') || has('macunix') || has('gui_macvim') || system('uname') =~? '^darwin' 147 | let browser_command = 'open %URL%' 148 | elseif executable('xdg-open') 149 | let browser_command = 'xdg-open %URL%' 150 | elseif executable('firefox') 151 | let browser_command = 'firefox %URL% &' 152 | else 153 | let browser_command = '' 154 | endif 155 | endif 156 | return browser_command 157 | endfunction 158 | 159 | function! s:open_browser(url) 160 | let cmd = s:get_browser_command() 161 | if len(cmd) == 0 162 | redraw 163 | echohl WarningMsg 164 | echo "It seems that you don't have general web browser. Open URL below." 165 | echohl None 166 | echo a:url 167 | return 168 | endif 169 | if cmd =~ '^!' 170 | let cmd = substitute(cmd, '%URL%', '\=shellescape(a:url)', 'g') 171 | silent! exec cmd 172 | elseif cmd =~ '^:[A-Z]' 173 | let cmd = substitute(cmd, '%URL%', '\=a:url', 'g') 174 | exec cmd 175 | else 176 | let cmd = substitute(cmd, '%URL%', '\=shellescape(a:url)', 'g') 177 | call system(cmd) 178 | endif 179 | endfunction 180 | 181 | " Thanks to https://github.com/tonchis/vim-to-github 182 | function! s:copy_to_clipboard(url) 183 | if exists('g:to_github_clip_command') 184 | call system(g:to_github_clip_command, a:url) 185 | elseif system('which pbcopy') || !v:shell_error 186 | call system('pbcopy', a:url) 187 | elseif has('unix') && !has('xterm_clipboard') 188 | let @" = a:url 189 | else 190 | let @+ = a:url 191 | endif 192 | 193 | echo "Copied " . a:url . " to clipboard" 194 | endfunction 195 | -------------------------------------------------------------------------------- /spec/plugin/github_url_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | describe GithubUrl do 4 | let!(:github_url) { described_class.new(start_line, end_line, file_name) } 5 | 6 | describe "#generate" do 7 | subject { github_url.generate } 8 | 9 | let(:start_line) { 1 } 10 | let(:end_line) { 1 } 11 | let(:repo_root) { `pwd`.strip } 12 | let(:target_file_path) { "/plugin/open-github.vim" } 13 | let(:file_name) { "#{repo_root}#{target_file_path}" } 14 | let(:user) { `git config remote.origin.url`.strip.split('/')[-2] } 15 | let(:revision) { `git rev-parse --abbrev-ref @ | xargs git rev-parse`.strip } 16 | let(:github_repo_url) { "https://github.com/#{user}/vim-open-github/blob/#{revision}#{target_file_path}" } 17 | let(:remote_origin) { "git@github.com:/#{user}/vim-open-github.git" } 18 | 19 | let(:user) do 20 | remote = `git config remote.origin.url` 21 | if res = remote.match(/github.com[\/:]([\w\d])/) 22 | res[1] 23 | end 24 | end 25 | 26 | before do 27 | allow(github_url).to receive(:repository_root).and_return(repo_root) 28 | allow(github_url).to receive(:remote_origin).and_return(remote_origin) 29 | end 30 | 31 | it { is_expected.to eq("#{github_repo_url}#L1") } 32 | 33 | describe "line anchor" do 34 | context "when not visual mode or selecting one line" do 35 | let(:start_line) { 2 } 36 | let(:end_line) { 2 } 37 | 38 | it "returns url highlighted one line" do 39 | expect(subject).to eq("#{github_repo_url}#L2") 40 | end 41 | end 42 | 43 | context "when selecting multiple lines" do 44 | let(:start_line) { 2 } 45 | let(:end_line) { 8 } 46 | 47 | it "returns url highlighted one line" do 48 | expect(subject).to eq("#{github_repo_url}#L2-L8") 49 | end 50 | end 51 | end 52 | 53 | describe "url scheme" do 54 | context "when url starts with https://" do 55 | let(:remote_origin) { "https://github.com/#{user}/vim-open-github.git" } 56 | 57 | it { is_expected.to eq("#{github_repo_url}#L1") } 58 | end 59 | 60 | context "when url does not contain user" do 61 | let(:remote_origin) { "github.com:/#{user}/vim-open-github.git" } 62 | 63 | it { is_expected.to eq("#{github_repo_url}#L1") } 64 | end 65 | end 66 | 67 | describe "host" do 68 | context "when host is GitHub Enterprise" do 69 | let(:remote_origin) { "ghe.example.co:k0kubun/vim-open-github.git" } 70 | 71 | it { is_expected.to eq("https://ghe.example.co/k0kubun/vim-open-github/blob/#{revision}#{target_file_path}#L1") } 72 | end 73 | 74 | context "when host is Github Enterprise" do 75 | let(:remote_origin) { "http://ghe.example.com/k0kubun/vim-open-github.vim" } 76 | 77 | it { is_expected.to eq("http://ghe.example.com/k0kubun/vim-open-github.vim/blob/#{revision}#{target_file_path}#L1") } 78 | end 79 | end 80 | 81 | describe "bufname" do 82 | let(:file) { "README.md" } 83 | context "when bufname is relative path" do 84 | let(:file_name) { file } 85 | let(:target_file_path) { "/#{file}" } 86 | 87 | it { is_expected.to eq("#{github_repo_url}#L1") } 88 | end 89 | 90 | context "when bufname is absolute path" do 91 | let(:target_file_path) { "/#{file}" } 92 | 93 | it { is_expected.to eq("#{github_repo_url}#L1") } 94 | end 95 | end 96 | 97 | context 'given arguments' do 98 | subject { github_url.generate(version) } 99 | let(:version) { 'v4.2.3' } 100 | 101 | it { is_expected.to eq("https://github.com/#{user}/vim-open-github/blob/#{version}#{target_file_path}#L1") } 102 | 103 | context 'when given the -b flag' do 104 | subject { github_url.generate('-b', version) } 105 | 106 | it { is_expected.to eq("https://github.com/#{user}/vim-open-github/blame/#{version}#{target_file_path}#L1") } 107 | end 108 | 109 | context 'when given the --blame flag' do 110 | subject { github_url.generate('--blame', version) } 111 | 112 | it { is_expected.to eq("https://github.com/#{user}/vim-open-github/blame/#{version}#{target_file_path}#L1") } 113 | end 114 | end 115 | end 116 | end 117 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | module VIM 2 | class << self 3 | def evaluate(*args) 4 | "" 5 | end 6 | 7 | def command(*args) 8 | "" 9 | end 10 | end 11 | end 12 | 13 | require "pry" 14 | require_relative "../plugin/github_url" 15 | --------------------------------------------------------------------------------