Argh, another bug fixed. When trying to write to a file different from the current
[vim-git-branch-info.git] / git-branch-info.vim
blobaf0205c6d8dee681601007899532075ccb0750ae
2 " Git branch info
3 " Last change: June 26 2008
4 " Version> 0.0.8
5 " Maintainer: Eustáquio 'TaQ' Rangel
6 " License: GPL
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 
27 " like:
29 " let g:git_branch_status_head_current=1
30 " This will show just the current head branch name 
31
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
42 " end.
43
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#
54 " or
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.
63 let s:menu_on   = 0
64 let s:checking = ""
65 let b:git_dir   = ""
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)
73 endfunction
75 function GitBranchInfoCheckReadable()
76         return filereadable(b:git_dir."/HEAD")
77 endfunction
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>")
84                 return 1
85         endif
87         " just write normal buffers
88         let l:buftype = getbufvar(bufnr("%"),'&buftype')
89         if strlen(l:buftype)>0
90                 echohl WarningMsg
91                 echo "Not writing if it's not a normal buffer (found a ".l:buftype." buffer)."
92                 echohl None
93                 return 0
94         endif
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>")
100                 return 1
101         endif
103         " ask what we will do
104         echohl ErrorMsg
105         let l:answer = tolower(input("Loaded from \'".b:git_load_branch."\' branch but saving on \'".l:current."\' branch, confirm [y/n]? ","n"))
106         echohl None
107         let l:msg = "File ".(l:answer=="y" ? "" : "NOT ")."saved on branch \'".l:current."\'."
109         " ok, save even with different branches
110         if l:answer=="y"
111                 exec l:writecmd expand("<afile>")
112         endif
114         " show message
115         echohl WarningMsg
116         echo l:msg
117         echohl None
118         return l:answer=="y"
119 endfunction
121 function GitBranchInfoInit()
122         call GitBranchInfoFindDir()
123         let l:current = GitBranchInfoTokens()
124         let b:git_load_branch = l:current[0]
125 endfunction
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 =~ "^/" ? "/" : ""
131         let b:git_dir   = ""
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
137                         break
138                 endif
139         endfor
140         return b:git_dir
141 endfunction
143 function GitBranchInfoGitDir()
144         return b:git_dir
145 endfunction
147 function GitBranchInfoLoadBranch()
148         return b:git_load_branch
149 endfunction
151 function GitBranchInfoRenewMenu(current,heads,remotes)
152         call GitBranchInfoRemoveMenu()
153         call GitBranchInfoShowMenu(a:current,a:heads,a:remotes)
154 endfunction
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
161         exe l:cmd
162         call GitBranchInfoRenewMenu(l:tokens[0],l:tokens[1],l:tokens[2])
163 endfunction
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
170         exe l:cmd
171 endfunction
173 function GitBranchInfoShowMenu(current,heads,remotes)
174         if !has("gui")
175                 return
176         endif
177         let s:menu_on   = 1
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
187         endfor
188         exe ":menu <silent> Plugin.Git\\ Info.-Local- :"
189         let l:lastone = ""
190         for l:branch in l:remotes
191                 let l:tokens    = split(l:branch,"/")
192                 if l:tokens[0]==l:lastone
193                         continue
194                 endif
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>"
197         endfor
198 endfunction
200 function GitBranchInfoRemoveMenu()
201         if !has("gui") || s:menu_on==0
202                 return
203         endif
204         exe ":unmenu Plugin.Git\\ Info" 
205         let s:menu_on = 0
206 endfunction
208 function GitBranchInfoString()
209         let l:tokens    = GitBranchInfoTokens() " get the tokens
210         if len(l:tokens)==1                                                     " no git here
211                 call GitBranchInfoRemoveMenu()
212                 return l:tokens[0]
213         end
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 "
221         if s:menu_on == 0
222                 call GitBranchInfoShowMenu(l:tokens[0],l:tokens[1],l:tokens[2])
223         endif
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])
225 endfunction
227 function GitBranchInfoTokens()
228         if !GitBranchInfoCheckGitDir()
229                 let s:current = ''
230                 return [exists("g:git_branch_status_nogit") ? g:git_branch_status_nogit : "No git."]
231         endif
232         if !GitBranchInfoCheckReadable()
233                 let s:current = ''
234                 return [s:current,[],[]]
235         endif
236         let s:current   = split(split(readfile(b:git_dir."/HEAD",'',1)[0])[1],"/")[2]
237         if exists("g:git_branch_status_head_current")
238                 let l:heads     = []
239         else            
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'))
243         endif           
244         if exists("g:git_branch_status_ignore_remotes")
245                 let l:remotes = []
246         else
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/","","")'))
249         endif           
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)
253         endif
254         let s:checking = l:checking
255         return [s:current,l:heads,l:remotes]
256 endfunction