Skip to content

Commit

Permalink
Refactor some files
Browse files Browse the repository at this point in the history
  • Loading branch information
alpaca-tc committed Dec 17, 2013
1 parent 4028dd8 commit 632441b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
28 changes: 15 additions & 13 deletions autoload/giti/branch.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,30 @@ endfunction"}}}

function! giti#branch#github_list_all() "{{{
let remotes = filter(giti#remote#github_list(), 'v:val.type == "(fetch)"')
let remote_branch = filter(giti#branch#list_all(), 'v:val.is_remote && v:val.full_name !~ "->"')
let list_all_with_branch = []
for branch in remote_branch
let branch_name_splited = split(branch.name, '/')
let remote_name = remove(branch_name_splited, 0)
let remote_branches = filter(giti#branch#list_all(), 'v:val.is_remote && v:val.full_name !~ "->"')

let github_branches = []
for branch in remote_branches
let [remote_name; branch_name_splited] = split(branch.name, '/')
let branch_name = join(branch_name_splited, '/')
let match_remotes = filter(copy(remotes), 'v:val.name == "'.remote_name.'"')
let matched = filter(copy(remotes), 'v:val.name == "'.remote_name.'"')

if empty(match_remotes)
if empty(matched)
" TODO Change message
throw 'Error occured:'
else
let remote = match_remotes[0]
let account = remote.github.account
let branch.name_with_remote_name = join([account, branch_name], ':')
let branch.remote_name = remote.name
let g:r = remotes
let g:n = remote_name
let g:h = matched
let remote = matched[-1]
let branch.head_name = join([remote.github.account, branch_name], ':')
let branch.remote = remote
endif

call add(list_all_with_branch, branch)
call add(github_branches, branch)
endfor

return list_all_with_branch
return github_branches
endfunction"}}}

function! giti#branch#current_name()"{{{
Expand Down
20 changes: 8 additions & 12 deletions autoload/giti/remote.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,21 @@ function! giti#remote#show_verbose()"{{{
endfunction"}}}

function! giti#remote#list_all() "{{{
let all_remotes = split(system('git remote -v'), '\n')
if empty(all_remotes)
return []
endif

let remote_list = []
for line in all_remotes
for line in giti#remote#show_verbose()
let remote = {}
let [remote.name, url_with_type] = split(line, '\t')
let [remote.url, remote.type] = split(url_with_type, ' ')
let remote.is_github = line =~ 'github\.com'

if remote.is_github
if remote.url =~ 'github\.com.\+\(\.git\)\?' || remote.url =~ '[^/:]\+/[^/:]\+\(\.git\)\?$'
let github = {}
let github.full_name = substitute(remote.url,
\ '\v.*/([^:/]{-}/[^/]{-})(\.git)?$', '\1', 'g')
let github.full_name = matchstr(remote.url, '\(.\+[/:]\)\@=[^/:]\+/[^/:]\+\(\.git\)\?$')
let [github.account, github.name] = split(github.full_name, '/')
let remote.is_github = 1
let remote.github = github
endif
else
let remote.is_github = 0
let remote.github = {}
end

call add(remote_list, remote)
endfor
Expand Down
46 changes: 29 additions & 17 deletions autoload/unite/sources/giti/pull_request.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ function! unite#sources#giti#pull_request#define() "{{{
endif
endfunction"}}}

let s:source = {
\ 'name' : '',
\ 'description' : '',
\ 'source__base_or_head' : '',
\ }

function! s:source.gather_candidates(args, context) "{{{
let selected_repo = empty(a:args) ? '' : a:args[0]
let base_or_head = self.source__base_or_head
call s:print_message(a:args, base_or_head)

let branches = giti#branch#github_list_all()
let candidates = map(branches, 's:remote2candidate(v:val, base_or_head, selected_repo)')
let message_candidate = {
\ 'word': '-- Please select ' . base_or_head. ' repository --',
\ 'is_selected' : 1,
\ 'is_dummy' : 1,
\ }
call insert(candidates, message_candidate, 0)

return candidates
endfunction"}}}

function! s:enabled_hub_command() "{{{
if !exists('s:enabled_hub')
let s:enabled_hub = system(g:giti_git_command) =~ 'hub'
Expand All @@ -23,20 +46,10 @@ function! s:enabled_hub_command() "{{{
endfunction"}}}

function! s:define_source(base_or_head) "{{{
let base_or_head = a:base_or_head
let source = {
\ 'name' : 'giti/pull_request/' . base_or_head,
\ 'description' : 'select '. base_or_head .' repository',
\ 'source__base_or_head' : base_or_head,
\ }

function! source.gather_candidates(args, context)
let selected_repo = empty(a:args) ? '' : a:args[0]
let base_or_head = self.source__base_or_head
call s:print_message(a:args, base_or_head)

return s:get_candidates(base_or_head, selected_repo)
endfunction
let source = copy(s:source)
let source.name = 'giti/pull_request/' . a:base_or_head
let source.description = 'select '. a:base_or_head .' repository'
let source.source__base_or_head = a:base_or_head

return source
endfunction"}}}
Expand All @@ -55,9 +68,8 @@ function! s:get_candidates(base_or_head, selected_repo) "{{{
endfunction"}}}

function! s:remote2candidate(branch, base_or_head, selected_repo) "{{{
let remote_name = a:branch.remote_name
let name = a:branch.name_with_remote_name
let abbr = printf('%-12s %s', '[' . a:branch.remote_name . ']', name)
let name = a:branch.head_name
let abbr = printf('%-15s %-35s (%s)', '[' . a:branch.remote.name . ']', name, a:branch.remote.url)

let [base_repo, head_repo] = a:base_or_head == 'base' ?
\ [name, a:selected_repo] : [a:selected_repo, name]
Expand Down

0 comments on commit 632441b

Please sign in to comment.