Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Version 1.1.0: Initial upload

  • Loading branch information...
commit f1c144a16eddac925e894532e485c1c76696132a 0 parents
Peter Antoine authored committed
Showing with 673 additions and 0 deletions.
  1. +7 −0 README
  2. +45 −0 README.md
  3. +113 −0 doc/gitlog.txt
  4. +508 −0 plugin/gitlog.vim
7 README
@@ -0,0 +1,7 @@
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=4294
+
+This plugin simply will show the commit history of the current file in the window and allow you to diff this against older commits. It also allows you to diff it against different commits in different branches.
+
+You can find more information at: https://github.com/PAntoine/vimgitlog
+
+Please can you report and problems there, as it is easier to track them in one place.
45 README.md
@@ -0,0 +1,45 @@
+vimgitlog
+=========
+
+Version: 1.1.0
+
+Git log and diff plugin for vim.
+
+Introduction
+------------
+
+This is a simple Vim plugin that will bring up the history of a given file. It will list the history
+in the window-pane on the left of the screen. If you hit enter it will diff that commit against the
+current revision loaded.
+
+In the log window \_\_gitlog\_\_ the two following commands work:
+
+ _o_ opens the file. This will simply open the file in a new window.
+ _\<cr\>_ This will open the file and diff it against the window that was active when it was lauched.
+
+In the Branch window:
+
+ <cr> This will change the log window to the branch selected. It does not change the current
+ branch of the given repository.
+
+Installation
+------------
+
+Simply copy the contents of the plugin directory to the plugin directory in your git installation.
+
+You will need to map the toggle function to use it.
+
+ map <silent> <f7> :call GITLOG_ToggleWindows()
+
+And the should be it.
+
+TODO
+----
+
+1. It is not colour coded yet, it will be.
+
+Licence and Copyright
+---------------------
+ Copyright (c) 2012 Peter Antoine
+ All rights Reserved.
+ Released Under the Artistic Licence
113 doc/gitlog.txt
@@ -0,0 +1,113 @@
+*gitlog.txt* For Vim version 7.0 Last change: 2012 October 26
+
+
+ ,----. ,--. ,--. ,--. ~
+ ' .-./ `--',-' '-.| | ,---. ,---. ~
+ | | .---.,--.'-. .-'| | | .-. || .-. | ~
+ ' '--' || | | | | '--.' '-' '' '-' ' ~
+ `------' `--' `--' `-----' `---' .`- / ~
+ `---' ~
+
+
+Author: Peter Antoine
+Date: October 26, 2012
+Version: 1.1.0
+HomePage: https://github.com/PAntoine/vimgitlog
+
+
+For instructions on installing this file, type:
+>
+ :help add-local-help |add-local-help| inside Vim.
+<
+
+==============================================================================
+1. Contents *gitlog* *gitlog-contents*
+
+ 1. Contents...............................: |gitlog-contents|
+ 2. Description............................: |gitlog-description|
+ 3. Usage..................................: |gitlog-usage|
+ 4. Mapping GitLog Functions...............: |gitlog-mapping|
+ 5. History................................: |gitlog-history|
+
+==============================================================================
+2. Description *gitlog-description*
+
+This is a simple pure vim plugin that interfaces with git. It allows you
+to simply diff different version of the file in the main window.
+
+This plugin tries to make access to the history the git stores easier to
+access.
+
+For details on the changes between versions see |gitlog-history|.
+
+==============================================================================
+3. Usage *gitlog-usage*
+
+GitLog expects that you are in a sub-directory of the Git repository. When
+GitLog is activated it will check to see if there is a .git directory in the
+path. It will error if it cannot find one. It will use this Git repository
+for all operations.
+
+The only function that really should be called outside of GitLog is the toggle
+function 'GITLOG_ToggleWindows()' function. This will do (as really should be
+expected) toggle the GitLog windows.
+
+What it will display is a full-height window split into two. The top window
+will have the current branch name followed by the commits on that branch. The
+bottom left window will have all the local branches that are found in the
+repository.
+
+The windows will look as follows:
+
+ -------------------------------------------------------------------
+ | branch: master | |
+ | * xxxxxxx commit th| |
+ | * xxxxxxx commit th| |
+ . . .
+ . . .
+ . . .
+ |====================| |
+ | branch_1 xxxxxxx s| |
+ | branch_2 xxxxxxx s| |
+ |* branch_3 xxxxxxx s| |
+ | branch_4 xxxxxxx s| |
+ | | |
+ | | |
+ -------------------------------------------------------------------
+
+The '*' in the branch window represents the current branch that the repository
+is on. GitLog does not change this. Internally it may list other branches but
+it does not change the branch in the repository.
+
+GitLog, does not support many commands. If you press <cr> within the log window
+then the revision matching the commit will be diff'ed against the file that is
+in the main window. If 'c' is pressed then the file will be opened in a window
+alongside the file.
+
+In the branch window <cr> will change the log window to list the commits in
+that branch. It will NOT change the branch that the repository is in.
+
+==============================================================================
+4. Mapping GitLog Functions *gitlog-mapping*
+
+There is only one function that really required mapping and that is the
+toggle function. As this is a single operation function I would assume that
+you would attach it to a function key, so the following mapping would be useful
+>
+ :map <silent> <f7> :call GITLOG_ToggleWindows()<cr>
+<
+This will install GITLOG on the <f7> key.
+
+==============================================================================
+5. History *gitlog-history*
+
+ 1.1.0: October 27, 2012:
+ PA: Added functionality to the branch window. Now selecting the branch
+ will update the log window to reflect the commits in that branch.
+
+ PA: Added the help file.
+
+ 1.0.0: <some time in the past>
+ PA: Initial release.
+
+vim: ts=4 ft=help tw=78
508 plugin/gitlog.vim
@@ -0,0 +1,508 @@
+" vim: ts=4 tw=4 fdm=marker :
+" ---------------------------------------------------------------------------------
+" Name : gitlog
+" Desc : This plugin is a tool for looking at the GIT history of a file.
+"
+" The simplest way to use this plugin is to map the GITLOG_ToggleWindows
+" function to the key of your choice (I use <F7>) and it will create
+" the log window on creation and will delete all the windows (including
+" the diffs that have been created) when it toggles off.
+"
+" In the log window the two following commands work.
+" o - will open the file.
+" <cr> - will open the revision for diff'ing.
+"
+" In the branch window it will use the following commands:
+" <cr> - will swap the GIT_LOG view of the branch - does not effect th
+" actual branch that is being used.
+"
+" It is that simple.
+"
+" Author : peterantoine
+" version: 1.1.0
+" Date : 29/09/2012 14:42:03
+" ---------------------------------------------------------------------------------
+" Copyright (c) 2012 Peter Antoine
+" All rights Reserved.
+" Released Under the Artistic Licence
+" ---------------------------------------------------------------------------------
+"
+" PUBLIC FUNCTIONS
+" FUNCTION: GITLOG_GetHistory(filename) "{{{
+"
+" This function will open the log window and load the history for the given file.
+" If the file does not exist within the given branch then then function will
+" produce a message that states that and then it will do nothing. It will used
+" the current value od s:gitlog_current_branch as the branch to search on.
+"
+" vars:
+" filename the filename to search for history for.
+"
+function! GITLOG_GetHistory(filename)
+ " have to get the files that it uses first
+ let s:repository_root = s:GITLOG_FindRespositoryRoot()
+
+ if (s:repository_root == "")
+ return 0
+ else
+ if (a:filename == "")
+ echohl WarningMsg
+ echomsg "No file in the buffer, can't get history"
+ echohl Normal
+ return 0
+ else
+ let s:revision_path = substitute(a:filename,s:repository_root,"","")
+ let s:original_window = bufwinnr("%")
+
+ silent execute "!git cat-file -e " . "HEAD:" . s:revision_path
+ if v:shell_error
+ echohl WarningMsg
+ echomsg "File " . s:gitlog_current_branch . ":" . s:revision_path . " is not tracked"
+ echohl Normal
+ return 0
+ else
+ call s:GITLOG_OpenLogWindow(a:filename)
+ call s:GITLOG_OpenBranchWindow()
+ return 1
+ endif
+ endif
+ endif
+endfunction "}}}
+" FUNCITON: GITLOG_DiffRevision() {{{
+"
+" This function will open a revision for diff'ing.
+" It will create a new window and diff the new window/buffer against the original
+" buffer that was used to launch gitlog.
+"
+" vars:
+" none
+"
+" returns:
+" nothing
+"
+function! GITLOG_DiffRevision()
+ let commit = s:GITLOG_GetCommitHash()
+
+ if (commit != "")
+ silent execute "!git cat-file -e " . commit . ":" . s:revision_path
+ if v:shell_error
+ echohl Normal
+ echomsg "The repository does not have this file"
+ echohl WarningMsg
+ else
+ call s:GITLOG_OpenDiffWindow(commit,s:revision_path)
+ endif
+ endif
+endfunction "}}}
+" FUNCITON: GITLOG_OpenRevision() {{{
+"
+" This function will open a revision for viewing.
+" It will create a new window and load the revision into that window.
+"
+" vars:
+" none
+"
+" returns:
+" nothing
+"
+function! GITLOG_OpenRevision()
+ let commit = s:GITLOG_GetCommitHash()
+
+ if (commit != "")
+ silent execute "!git cat-file -e " . commit . ":" . s:revision_path
+ if v:shell_error
+ echohl Normal
+ echomsg "The repository does not have this file"
+ echohl WarningMsg
+ else
+ call s:GITLOG_OpenCodeWindow(commit,s:revision_path)
+ endif
+ endif
+endfunction "}}}
+" FUNCTION: GITLOG_CloseWindows() {{{
+"
+" This function closes all the GitLog windows. It will search through all the
+" windows looking for the known named buffers (__gitbranch__ and __gitlog__) also
+" for windows with the __XXXXXXX:<some_text>__ pattern and close them all. It will
+" also call diffoff! to make tidy up.
+"
+" vars:
+" node
+"
+" returns:
+" nothing
+"
+function! GITLOG_CloseWindows()
+ " close the log window
+ if bufwinnr(bufnr("__gitlog__")) != -1
+ exe bufwinnr(bufnr("__gitlog__")) . "wincmd w"
+ exe bufwinnr(bufnr("__gitlog__")) . "wincmd q"
+ endif
+
+ "close the branch window
+ if bufwinnr(bufnr("__gitbranch__")) != -1
+ exe bufwinnr(bufnr("__gitbranch__")) . "wincmd w"
+ exe bufwinnr(bufnr("__gitbranch__")) . "wincmd q"
+ endif
+
+ " close all the diff windows
+ for b in range(1, bufnr('$'))
+ if (bufexists(b))
+ if (substitute(bufname(b),"\\x\\x\\x\\x\\x\\x\\x:.\\+$","","") == "")
+ exe bufwinnr(b) . "wincmd w"
+ exe bufwinnr(b) . "wincmd q"
+ endif
+ endif
+ endfor
+
+ " and finally the original buffer
+ exe bufwinnr(bufnr(s:revision_file)) . "wincmd w"
+ diffoff
+
+endfunction "}}}
+" FUNCTION: GITLOG_ToggleWindows() {{{
+"
+" This function toggles the gitlog windows. It will use the file in the current
+" window to use for loading the log.
+"
+" vars:
+" node
+"
+" returns:
+" nothing
+"
+function! GITLOG_ToggleWindows()
+
+ if !exists("s:gitlog_loaded")
+ let s:gitlog_current_branch = GITLOG_GetBranch()
+ let s:revision_file = expand('%:p')
+
+
+ if (GITLOG_GetHistory(s:revision_file))
+ let s:gitlog_loaded = 1
+ endif
+ else
+ unlet s:gitlog_loaded
+ call GITLOG_CloseWindows()
+ endif
+endfunction "}}}
+" FUNCTION: GITLOG_SwitchLocalBranch() {{{
+"
+" This function will set the s:gitlog_current_branch to the name of the
+" branch that is under the cursor in the branch window.
+"
+" vars:
+" none
+"
+" returns:
+" nothing
+"
+function! GITLOG_SwitchLocalBranch()
+ let new_branch = s:GITLOG_GetBranchName()
+
+ if (new_branch != "")
+ let s:gitlog_current_branch = new_branch
+
+ if (!GITLOG_GetHistory(expand(s:revision_file)))
+ echohl Normal
+ echomsg "The branch " . s:gitlog_current_branch . " does not have this file"
+ echohl WarningMsg
+ endif
+ endif
+endfunction "}}}
+"
+" INTERNAL FUNCTIONS BELOW --- Do not call directly
+" FUNCITON: GITLOG_GetCommitHash {{{
+"
+" This function will search for the hash on the current line in the buffer. It is
+" searching for a space then 7 hex digits then another space. If it does not find
+" this pattern on the line then it will return an empty string.
+"
+" vars:
+" none
+"
+" returns:
+" the 7 hex digits of the commit hash, else the empty string.
+"
+function! s:GITLOG_GetCommitHash()
+ let x = getline(".")
+
+ if (stridx(x,"*") >= 0)
+ let commit = substitute(x,"^.*\\*\\s\\+\\(\\x\\x\\x\\x\\x\\x\\x\\) .\\+$","\\1","")
+ else
+ let commit = ""
+ endif
+
+ return commit
+endfunction "}}}
+" FUNCITON: GITLOG_GetBranchName {{{
+"
+" This function will search for the branch name on the current line of the buffer. The name
+" starts 2 characters into the line and goes until the first whitespace character is found.
+"
+" vars:
+" none
+"
+" returns:
+" the 7 hex digits of the commit hash, else the empty string.
+"
+function! s:GITLOG_GetBranchName()
+ let x = getline(".")
+
+ let branch_name = substitute(x,"^..\\(\\S\\+\\) .\\+$","\\1","")
+
+ return branch_name
+endfunction "}}}
+" FUNCTION: GITLOG_FindRespositoryRoot() {{{
+"
+" This function will search the tree UPWARDS to find the git repository that the
+" file belongs to. If it cannot find the repository then it will generate an error
+" and then return an empty string.
+"
+" vars:
+" none
+"
+" returns:
+" If there is a .git directory in the tree, it returns the directory that the .git
+" repository is in, else it returns the empty string.
+"
+function! s:GITLOG_FindRespositoryRoot()
+ let root = finddir(".git",expand('%:h'). "," . expand('%:p:h') . ";" . $HOME)
+
+ if (root == "")
+ echohl WarningMsg
+ echomsg "This does not look to be a git repository as can't find a .git dir"
+ echohl Normal
+ elseif (root == '.git')
+ let root = getcwd() . '/'
+ else
+ let root = substitute(root,"\\.git","","")
+ endif
+
+ return root
+endfunction "}}}
+" FUNCTION: GITLOG_MapLogBufferKeys() {{{
+"
+" This function maps the keys that the buffer will respond to. All the keys are
+" local to the buffer.
+"
+" vars:
+" none
+"
+" returns:
+" nothing
+"
+function! s:GITLOG_MapLogBufferKeys()
+ map <buffer> <silent> <cr> :call GITLOG_DiffRevision()<cr>
+ map <buffer> <silent> o :call GITLOG_OpenRevision()<cr>
+endfunction "}}}
+" FUNCTION: GITLOG_OpenLogWindow() {{{
+"
+" This function will open the log window if it is not already open. It will
+" fill it with the output of git rev list window.
+"
+" vars:
+" none
+"
+" returns:
+" nothing
+"
+function! s:GITLOG_OpenLogWindow(file_name)
+ if bufwinnr(bufnr("__gitlog__")) != -1
+ " window already open - just go to it
+ silent exe bufwinnr(bufnr("__gitlog__")) . "wincmd w"
+ setlocal modifiable
+ exe "% delete"
+ else
+ " window not open need to create it
+ let s:buf_number = bufnr("__gitlog__",1)
+ silent topleft 40 vsplit
+ set winfixwidth
+ set winwidth=40
+ set winminwidth=40
+ silent exe "buffer " . s:buf_number
+ setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
+ endif
+
+ "need to change the window
+ setlocal modifiable
+
+ " now get the file history for the window
+ redir => gitdiff_history
+ silent execute "!git rev-list " . s:gitlog_current_branch . " --oneline --graph -- " . a:file_name
+ redir END
+ let git_array = split(substitute(gitdiff_history,'[\x00]',"","g"),"\x0d")
+ call remove(git_array,0)
+ call setline(1,[ 'branch: ' . s:gitlog_current_branch] + git_array)
+
+ " set the keys on the Log window
+ call s:GITLOG_MapLogBufferKeys()
+
+ setlocal nomodifiable
+endfunction "}}}
+" FUNCTION: GITLOG_MapBranchBufferKeys() {{{
+"
+" This function maps the keys that the buffer will respond to. All the keys are
+" local to the buffer.
+"
+" vars:
+" none
+"
+" returns:
+" nothing
+"
+function! s:GITLOG_MapBranchBufferKeys()
+ map <buffer> <silent> <cr> :call GITLOG_SwitchLocalBranch()<cr>
+endfunction "}}}
+" FUNCTION: GITLOG_OpenBranchWindow() {{{
+"
+" This function will open the branch window.
+"
+" vars:
+" none
+"
+" returns:
+" nothing
+"
+function! s:GITLOG_OpenBranchWindow()
+ let current_window = bufwinnr(bufnr("%"))
+
+ if bufwinnr(bufnr("__gitbranch__")) != -1
+ " window already open - just go to it
+ silent exe bufwinnr(bufnr("__gitbranch__")) . "wincmd w"
+ else
+ " window not open need to create it
+ let s:buf_number = bufnr("__gitbranch__",1)
+ bel 10 split
+ set winfixwidth
+ set winwidth=40
+ set winminwidth=40
+ silent exe "buffer " . s:buf_number
+ setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
+ endif
+
+ "need to change the window
+ setlocal modifiable
+
+ " now get the list of branches
+ redir => gitbranch_history
+ silent execute "!git branch -v"
+ redir END
+ let git_array = split(substitute(gitbranch_history,'[\x00]',"","g"),"\x0d")
+ call remove(git_array,0)
+ call setline(1,git_array)
+
+ " set the keys on the branch window
+ call s:GITLOG_MapBranchBufferKeys()
+
+ setlocal nomodifiable
+
+ " want to be in the log window - as the branch window is not important
+ exe current_window . "wincmd w"
+endfunction "}}}
+" FUNCTION: GITLOG_OpenDiffWindow(revision) {{{
+"
+" This function will open the specified revision as a diff, and diff it
+" against the file in the current buffer. The revision is a diff spec of
+" the type that can be passed to the git show, expecting XXXXXXX:<name>
+" the XXXXXXX is the commit hash for the revision that is required and the
+" <name> is the file name. This names needs to be from the root of the
+" git repository or it wont be found.
+"
+" vars:
+" revision The XXXXXXX:<name> formatted revision to diff against.
+"
+" returns:
+" nothing
+"
+function! s:GITLOG_OpenDiffWindow(commit,file_path)
+ let revision = a:commit . ":" . a:file_path
+ let buffname = a:commit . ":" . fnamemodify(a:file_path,":t")
+
+ if bufwinnr(bufnr(buffname)) != -1
+ " window already open - just go to it
+ exe bufwinnr(bufnr(buffname)) . "wincmd w"
+ setlocal modifiable
+ diffthis
+ else
+ " window not open need to create it
+ let s:buf_number = bufnr(buffname,1)
+ exe bufwinnr(bufnr(s:revision_file)) . "wincmd w"
+ let file_type = &filetype
+ diffthis
+ silent botright vsplit
+ exe "buffer " . s:buf_number
+
+ redir => gitlog_file
+ silent execute "!git --no-pager show " . revision
+ redir END
+
+ " now write the captured text to the a new buffer - after removing
+ " the \x00's from the text and splitting into an array.
+ let git_array = split(substitute(gitlog_file,'[\x00]',"","g"),"\x0d")
+ call remove(git_array,0)
+ call setline(1,git_array)
+ setlocal buftype=nofile bufhidden=wipe nobuflisted nomodifiable noswapfile nowrap
+ diffthis
+ exe "setlocal filetype=" . file_type
+ endif
+endfunction "}}}
+" FUNCTION: GITLOG_OpenCodeWindow(revision) {{{
+"
+" This function will open the specified revision.
+"
+" vars:
+" revision The XXXXXXX:<name> formatted revision to open.
+"
+" returns:
+" nothing
+"
+function! s:GITLOG_OpenCodeWindow(commit,file_path)
+ let revision = a:commit . ":" . a:file_path
+ let buffname = a:commit . ":" . fnamemodify(a:file_path,":t")
+
+ if bufwinnr(bufnr(buffname)) != -1
+ " window already open - just go to it
+ exe bufwinnr(bufnr(buffname)) . "wincmd w"
+ setlocal modifiable
+ diffthis
+ else
+ " window not open need to create it
+ let s:buf_number = bufnr(buffname,1)
+ exe bufwinnr(bufnr(s:revision_file)) . "wincmd w"
+ let file_type = &filetype
+ silent botright vsplit
+ exe "buffer " . s:buf_number
+
+ redir => gitlog_file
+ silent execute "!git --no-pager show " . revision
+ redir END
+
+ " now write the captured text to the a new buffer - after removing
+ " the \x00's from the text and splitting into an array.
+ let git_array = split(substitute(gitlog_file,'[\x00]',"","g"),"\x0d")
+ call remove(git_array,0)
+ call setline(1,git_array)
+ setlocal buftype=nofile bufhidden=wipe nobuflisted nomodifiable noswapfile nowrap
+ exe "setlocal filetype=" . file_type
+ endif
+endfunction "}}}
+" FUNCTION: GITLOG_GetBranch() {{{
+"
+" This function will get the current branch that the editor is in.
+"
+" vars:
+" none
+"
+" returns:
+" nothing
+"
+function! GITLOG_GetBranch()
+ let branch = system("git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* //'")
+ if branch != ''
+ return substitute(branch, '\n', '', 'g')
+ else
+ return ''
+ endif
+endfunction "}}}
+
Please sign in to comment.
Something went wrong with that request. Please try again.