Changed the version number
[vim-git-branch-info.git] / git-branch-info.vim
blob3e12174c3bb862d92da2c422b9a0ce6cab64f81b
2 " Git branch info
3 " Last change: June 19 2008
4 " Version> 0.0.7
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:writable = filewritable(expand("<afile>"))
81         if !GitBranchInfoCheckGitDir()
82                 if l:writable
83                         exec "write"
84                 endif                   
85                 return 1
86         endif
87         " if the branches are the same, no problem
88         let l:current = GitBranchInfoTokens()[0]
89         if l:current==b:git_load_branch
90                 if l:writable
91                         exec "write"
92                 endif                   
93                 return 1
94         endif
95         " ask what we will do
96         echohl ErrorMsg
97         let l:answer = tolower(input("Loaded from \'".b:git_load_branch."\' branch but saving on \'".l:current."\' branch, confirm [y/n]? ","n"))
98         echohl None
99         let l:msg = "File ".(l:answer=="y" ? "" : "NOT ")."saved on branch \'".l:current."\'."
100         " ok, save even with different branches
101         if l:answer=="y"
102                 exec "write"
103         endif
104         " show message
105         echohl WarningMsg
106         echo l:msg
107         echohl None
108         return l:answer=="y"
109 endfunction
111 function GitBranchInfoInit()
112         call GitBranchInfoFindDir()
113         let l:current = GitBranchInfoTokens()
114         let b:git_load_branch = l:current[0]
115 endfunction
117 function GitBranchInfoFindDir()
118         let l:bufname   = bufname("%")
119         let l:buflist   = strlen(l:bufname)>0 ? split(l:bufname,"/") : [""]
120         let l:prefix    = l:bufname =~ "^/" ? "/" : ""
121         let b:git_dir   = ""
122         for l:item in l:buflist
123                 call remove(l:buflist,-1)
124                 let l:path = l:prefix.join(l:buflist,"/").l:prefix.".git"
125                 if !empty(finddir(l:path))
126                         let b:git_dir = l:path
127                         break
128                 endif
129         endfor
130         return b:git_dir
131 endfunction
133 function GitBranchInfoGitDir()
134         return b:git_dir
135 endfunction
137 function GitBranchInfoLoadBranch()
138         return b:git_load_branch
139 endfunction
141 function GitBranchInfoRenewMenu(current,heads,remotes)
142         call GitBranchInfoRemoveMenu()
143         call GitBranchInfoShowMenu(a:current,a:heads,a:remotes)
144 endfunction
146 function GitBranchInfoCheckout(branch)
147         let l:tokens    = GitBranchInfoTokens()
148         let l:checkout  = "git\ checkout\ ".a:branch 
149         let l:where             = substitute(b:git_dir,".git$","","")
150         let l:cmd               = strlen(l:where)>0 ? "!cd\ ".l:where.";\ ".l:checkout : "!".l:checkout
151         exe l:cmd
152         call GitBranchInfoRenewMenu(l:tokens[0],l:tokens[1],l:tokens[2])
153 endfunction
155 function GitBranchInfoFetch(remote)
156         let l:tokens    = GitBranchInfoTokens()
157         let l:fetch             =  "git\ fetch\ ".a:remote
158         let l:where             = substitute(b:git_dir,".git$","","")
159         let l:cmd               = strlen(l:where)>0 ? "!cd\ ".l:where.";\ ".l:fetch : "!".l:fetch
160         exe l:cmd
161 endfunction
163 function GitBranchInfoShowMenu(current,heads,remotes)
164         if !has("gui")
165                 return
166         endif
167         let s:menu_on   = 1
168         let l:compare   = a:current
169         let l:current   = [a:current]
170         let l:heads             = len(a:heads)>0         ? a:heads       : []
171         let l:remotes   = len(a:remotes)>0 ? a:remotes : []
172         let l:locals    = sort(extend(l:current,l:heads))
173         for l:branch in l:locals
174                 let l:moption   = (l:branch==l:compare ? "Working\\ \\on\\ " : "Checkout\\ ").l:branch
175                 let l:mcom              = (l:branch==l:compare ? ":echo 'Already\ on\ branch\ \''".l:branch."\''.'<CR>" : "call GitBranchInfoCheckout('".l:branch."')<CR><CR>")
176                 exe ":menu <silent> Plugin.Git\\ Info.".l:moption." :".l:mcom
177         endfor
178         exe ":menu <silent> Plugin.Git\\ Info.-Local- :"
179         let l:lastone = ""
180         for l:branch in l:remotes
181                 let l:tokens    = split(l:branch,"/")
182                 if l:tokens[0]==l:lastone
183                         continue
184                 endif
185                 let l:lastone = l:tokens[0]
186                 exe "menu <silent> Plugin.Git\\ Info.Fetch\\ ".l:tokens[0]." :call GitBranchInfoFetch('".l:tokens[0]."')<CR><CR>"
187         endfor
188 endfunction
190 function GitBranchInfoRemoveMenu()
191         if !has("gui") || s:menu_on==0
192                 return
193         endif
194         exe ":unmenu Plugin.Git\\ Info" 
195         let s:menu_on = 0
196 endfunction
198 function GitBranchInfoString()
199         let l:tokens    = GitBranchInfoTokens() " get the tokens
200         if len(l:tokens)==1                                                     " no git here
201                 call GitBranchInfoRemoveMenu()
202                 return l:tokens[0]
203         end
204         let s:current   = l:tokens[0]                           " the current branch is the first one
205         let l:branches  = l:tokens[1]                           " the other branches are the last one
206         let l:remotes   = l:tokens[2]                           " remote branches
207         " check for around characters
208         let l:around    = exists("g:git_branch_status_around") ? (strlen(g:git_branch_status_around)==2 ? split(g:git_branch_status_around,'\zs') : ["",""]) : ["[","]"]
209         " find the prefix text
210         let l:text              = exists("g:git_branch_status_text")   ? g:git_branch_status_text : " Git "
211         if s:menu_on == 0
212                 call GitBranchInfoShowMenu(l:tokens[0],l:tokens[1],l:tokens[2])
213         endif
214         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])
215 endfunction
217 function GitBranchInfoTokens()
218         if !GitBranchInfoCheckGitDir()
219                 let s:current = ''
220                 return [exists("g:git_branch_status_nogit") ? g:git_branch_status_nogit : "No git."]
221         endif
222         if !GitBranchInfoCheckReadable()
223                 let s:current = ''
224                 return [s:current,[],[]]
225         endif
226         let s:current   = split(split(readfile(b:git_dir."/HEAD",'',1)[0])[1],"/")[2]
227         if exists("g:git_branch_status_head_current")
228                 let l:heads     = []
229         else            
230                 let l:heads     = split(glob(b:git_dir."/refs/heads/*"),"\n")
231                 call map(l:heads,'substitute(v:val,b:git_dir."/refs/heads/","","")')
232                 call sort(filter(l:heads,'v:val !~ s:current'))
233         endif           
234         if exists("g:git_branch_status_ignore_remotes")
235                 let l:remotes = []
236         else
237                 let l:remotes   = split(glob(b:git_dir."/refs/remotes/*/**"),"\n")
238                 call sort(map(l:remotes,'substitute(v:val,b:git_dir."/refs/remotes/","","")'))
239         endif           
240         let l:checking = s:current.join(l:heads).join(l:remotes)
241         if l:checking != s:checking && has("gui")
242                 call GitBranchInfoRenewMenu(s:current,l:heads,l:remotes)
243         endif
244         let s:checking = l:checking
245         return [s:current,l:heads,l:remotes]
246 endfunction