| | 657 | " VCS branch name "{{{2 |
| | 658 | " Returns the name of the current branch of the given directory. |
| | 659 | " BUGS: git is only supported. |
| | 660 | let s:_vcs_branch_name_cache = {} " dir_path = [branch_name, key_file_mtime] |
| | 661 | |
| | 662 | |
| | 663 | function! s:vcs_branch_name(dir) |
| | 664 | let cache_entry = get(s:_vcs_branch_name_cache, a:dir, 0) |
| | 665 | if cache_entry is 0 |
| | 666 | \ || cache_entry[1] < getftime(s:_vcs_branch_name_key_file(a:dir)) |
| | 667 | unlet cache_entry |
| | 668 | let cache_entry = s:_vcs_branch_name(a:dir) |
| | 669 | let s:_vcs_branch_name_cache[a:dir] = cache_entry |
| | 670 | endif |
| | 671 | |
| | 672 | return cache_entry[0] |
| | 673 | endfunction |
| | 674 | |
| | 675 | |
| | 676 | function! s:_vcs_branch_name_key_file(dir) |
| | 677 | return a:dir . '/.git/HEAD' |
| | 678 | endfunction |
| | 679 | |
| | 680 | |
| | 681 | function! s:_vcs_branch_name(dir) |
| | 682 | let head_file = s:_vcs_branch_name_key_file(a:dir) |
| | 683 | let branch_name = '' |
| | 684 | |
| | 685 | if filereadable(head_file) |
| | 686 | let ref_info = s:first_line(head_file) |
| | 687 | if ref_info =~ '^\x\{40}$' |
| | 688 | let remote_branches = split(glob(a:dir . '/.git/refs/remotes/*'), "\n") |
| | 689 | call filter(remote_branches, 's:first_line(v:val) ==# ref_info') |
| | 690 | if 1 <= len(remote_branches) |
| | 691 | let branch_name = matchlist(remote_branches[0], '/\([^/]*\)$')[1] |
| | 692 | endif |
| | 693 | else |
| | 694 | let branch_name = matchlist(ref_info, '^ref: refs/heads/\(\S\+\)$')[1] |
| | 695 | if branch_name == '' |
| | 696 | let branch_name = ref_info |
| | 697 | endif |
| | 698 | endif |
| | 699 | endif |
| | 700 | |
| | 701 | return [branch_name, getftime(head_file)] |
| | 702 | endfunction |
| | 703 | |
| | 704 | |
| | 705 | |
| | 706 | |
| 783 | | " Returns the name of the current branch of the given directory. |
| 784 | | " BUGS: git is only supported. |
| 785 | | function! s:ReposBranch(dir) |
| 786 | | let head_file = a:dir . '/.git/HEAD' |
| 787 | | let branch_name = '?' |
| 788 | | |
| 789 | | if filereadable(head_file) |
| 790 | | let ref_info = s:first_line(head_file) |
| 791 | | if ref_info =~ '^\x\{40}$' |
| 792 | | let remote_branches = split(glob(a:dir . '/.git/refs/remotes/*'), "\n") |
| 793 | | call filter(remote_branches, 's:first_line(v:val) ==# ref_info') |
| 794 | | if 1 <= len(remote_branches) |
| 795 | | let branch_name = matchlist(remote_branches[0], '/\([^/]*\)$')[1] |
| 796 | | endif |
| 797 | | else |
| 798 | | let branch_name = matchlist(ref_info, '^ref: refs/heads/\(\S\+\)$')[1] |
| 799 | | if branch_name == '' |
| 800 | | let branch_name = ref_info |
| 801 | | endif |
| 802 | | endif |
| 803 | | endif |
| 804 | | |
| 805 | | return branch_name |
| 806 | | endfunction |
| 807 | | |
| 808 | | |