3 " Last change: June 26 2008
5 " Maintainer: Eustáquio 'TaQ' Rangel
7 " URL: git://github.com/taq/vim-git-branch-info.git
9 " This plugin show branches information on the status line.
10 " To install, just put this file on ~/.vim/plugins and set your status line:
12 " :set statusline=%{GitBranchInfoString}
14 " Of course you can append this configuration to an existing one and make all
15 " the customization you want on the status line, like:
17 " :set statusline=%#ErrorMsg#%{GitBranchInfoString}%#StatusLine#
19 " The command above will show the Git branches info on the same color as the
20 " error messages. You can choose any color scheme you want to. Use
22 " :help highlight-groups
24 " to check for some other options.
26 " There are some customization on the result string based on existing variables
29 " let g:git_branch_status_head_current=1
30 " This will show just the current head branch name
32 " let g:git_branch_status_text="text"
33 " This will show 'text' before the branches. If not set ' Git ' (with a trailing
34 " left space) will be displayed.
36 " let g:git_branch_status_nogit=""
37 " The message when there is no Git repository on the current dir
39 " let g:git_branch_status_around=""
40 " Characters to put around the branch strings. Need to be a pair or characters,
41 " the first will be on the beginning of the branch string and the last on the
44 " let g:git_branch_status_ignore_remotes=1
45 " Ignore the remote branches. If you don't want information about them, this can
46 " make things works faster.
48 " If you want to make your own customizations, you can use the GitBranchInfoTokens()
49 " function. It returns an array with the current branch as the first element and
50 " another array with the other branches as the second element, like:
52 " :set statusline=%#ErrorMsg#%{GitBranchInfoTokens()[0]}%#StatusLine#
56 " :set statusline=%#StatusLineNC#\ Git\ %#ErrorMsg#\ %{GitBranchInfoTokens()[0]}\ %#StatusLine#
58 " will give you a nice custom formatted string.
60 " This will show you the current branch only. No prefix text, no characters
61 " around it. You can also make another functions to use the returned array.
66 let b:git_load_branch = ""
68 autocmd BufEnter * call GitBranchInfoInit()
69 autocmd BufWriteCmd * call GitBranchInfoWriteCheck()
71 function GitBranchInfoCheckGitDir()
72 return exists("b:git_dir") && !empty(b:git_dir)
75 function GitBranchInfoCheckReadable()
76 return filereadable(b:git_dir."/HEAD")
79 function GitBranchInfoWriteCheck()
80 let l:writecmd = v:cmdbang==1 ? "write!" : "write"
81 " not controlled by Git, write this thing!
82 if !GitBranchInfoCheckGitDir()
83 exec l:writecmd expand("<afile>")
87 " just write normal buffers
88 let l:buftype = getbufvar(bufnr("%"),'&buftype')
89 if strlen(l:buftype)>0
91 echo "Not writing if it's not a normal buffer (found a ".l:buftype." buffer)."
96 " if the branches are the same, no problem
97 let l:current = GitBranchInfoTokens()[0]
98 if l:current==b:git_load_branch
99 exec l:writecmd expand("<afile>")
103 " ask what we will do
105 let l:answer = tolower(input("Loaded from \'".b:git_load_branch."\' branch but saving on \'".l:current."\' branch, confirm [y/n]? ","n"))
107 let l:msg = "File ".(l:answer=="y" ? "" : "NOT ")."saved on branch \'".l:current."\'."
109 " ok, save even with different branches
111 exec l:writecmd expand("<afile>")
121 function GitBranchInfoInit()
122 call GitBranchInfoFindDir()
123 let l:current = GitBranchInfoTokens()
124 let b:git_load_branch = l:current[0]
127 function GitBranchInfoFindDir()
128 let l:bufname = bufname("%")
129 let l:buflist = strlen(l:bufname)>0 ? split(l:bufname,"/") : [""]
130 let l:prefix = l:bufname =~ "^/" ? "/" : ""
132 for l:item in l:buflist
133 call remove(l:buflist,-1)
134 let l:path = l:prefix.join(l:buflist,"/").l:prefix.".git"
135 if !empty(finddir(l:path))
136 let b:git_dir = l:path
143 function GitBranchInfoGitDir()
147 function GitBranchInfoLoadBranch()
148 return b:git_load_branch
151 function GitBranchInfoRenewMenu(current,heads,remotes)
152 call GitBranchInfoRemoveMenu()
153 call GitBranchInfoShowMenu(a:current,a:heads,a:remotes)
156 function GitBranchInfoCheckout(branch)
157 let l:tokens = GitBranchInfoTokens()
158 let l:checkout = "git\ checkout\ ".a:branch
159 let l:where = substitute(b:git_dir,".git$","","")
160 let l:cmd = strlen(l:where)>0 ? "!cd\ ".l:where.";\ ".l:checkout : "!".l:checkout
162 call GitBranchInfoRenewMenu(l:tokens[0],l:tokens[1],l:tokens[2])
165 function GitBranchInfoFetch(remote)
166 let l:tokens = GitBranchInfoTokens()
167 let l:fetch = "git\ fetch\ ".a:remote
168 let l:where = substitute(b:git_dir,".git$","","")
169 let l:cmd = strlen(l:where)>0 ? "!cd\ ".l:where.";\ ".l:fetch : "!".l:fetch
173 function GitBranchInfoShowMenu(current,heads,remotes)
178 let l:compare = a:current
179 let l:current = [a:current]
180 let l:heads = len(a:heads)>0 ? a:heads : []
181 let l:remotes = len(a:remotes)>0 ? a:remotes : []
182 let l:locals = sort(extend(l:current,l:heads))
183 for l:branch in l:locals
184 let l:moption = (l:branch==l:compare ? "Working\\ \\on\\ " : "Checkout\\ ").l:branch
185 let l:mcom = (l:branch==l:compare ? ":echo 'Already\ on\ branch\ \''".l:branch."\''.'<CR>" : "call GitBranchInfoCheckout('".l:branch."')<CR><CR>")
186 exe ":menu <silent> Plugin.Git\\ Info.".l:moption." :".l:mcom
188 exe ":menu <silent> Plugin.Git\\ Info.-Local- :"
190 for l:branch in l:remotes
191 let l:tokens = split(l:branch,"/")
192 if l:tokens[0]==l:lastone
195 let l:lastone = l:tokens[0]
196 exe "menu <silent> Plugin.Git\\ Info.Fetch\\ ".l:tokens[0]." :call GitBranchInfoFetch('".l:tokens[0]."')<CR><CR>"
200 function GitBranchInfoRemoveMenu()
201 if !has("gui") || s:menu_on==0
204 exe ":unmenu Plugin.Git\\ Info"
208 function GitBranchInfoString()
209 let l:tokens = GitBranchInfoTokens() " get the tokens
210 if len(l:tokens)==1 " no git here
211 call GitBranchInfoRemoveMenu()
214 let s:current = l:tokens[0] " the current branch is the first one
215 let l:branches = l:tokens[1] " the other branches are the last one
216 let l:remotes = l:tokens[2] " remote branches
217 " check for around characters
218 let l:around = exists("g:git_branch_status_around") ? (strlen(g:git_branch_status_around)==2 ? split(g:git_branch_status_around,'\zs') : ["",""]) : ["[","]"]
219 " find the prefix text
220 let l:text = exists("g:git_branch_status_text") ? g:git_branch_status_text : " Git "
222 call GitBranchInfoShowMenu(l:tokens[0],l:tokens[1],l:tokens[2])
224 return l:text.l:around[0].s:current.l:around[1].(exists("g:git_branch_status_head_current")?"":l:around[0].join(l:branches,",").l:around[1])
227 function GitBranchInfoTokens()
228 if !GitBranchInfoCheckGitDir()
230 return [exists("g:git_branch_status_nogit") ? g:git_branch_status_nogit : "No git."]
232 if !GitBranchInfoCheckReadable()
234 return [s:current,[],[]]
236 let s:current = split(split(readfile(b:git_dir."/HEAD",'',1)[0])[1],"/")[2]
237 if exists("g:git_branch_status_head_current")
240 let l:heads = split(glob(b:git_dir."/refs/heads/*"),"\n")
241 call map(l:heads,'substitute(v:val,b:git_dir."/refs/heads/","","")')
242 call sort(filter(l:heads,'v:val !~ s:current'))
244 if exists("g:git_branch_status_ignore_remotes")
247 let l:remotes = split(glob(b:git_dir."/refs/remotes/*/**"),"\n")
248 call sort(map(l:remotes,'substitute(v:val,b:git_dir."/refs/remotes/","","")'))
250 let l:checking = s:current.join(l:heads).join(l:remotes)
251 if l:checking != s:checking && has("gui")
252 call GitBranchInfoRenewMenu(s:current,l:heads,l:remotes)
254 let s:checking = l:checking
255 return [s:current,l:heads,l:remotes]