From eb48c6c284a5cadd5720428275099da131531630 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Sat, 30 Jan 2010 00:31:10 -0500 Subject: [PATCH] In :Gdiff, newest file is shown on right --- doc/fugitive.txt | 3 ++- plugin/fugitive.vim | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/doc/fugitive.txt b/doc/fugitive.txt index 73aeeca..957c794 100644 --- a/doc/fugitive.txt +++ b/doc/fugitive.txt @@ -120,7 +120,8 @@ that are part of Git repositories). given revision. With no argument, the version in the index is used (which means a three-way diff during a merge conflict, making it a git-mergetool - alternative). Use |do| and |dp| and write to the + alternative). The newer of the two files is placed + to the right. Use |do| and |dp| and write to the index file to simulate "git add --patch". *fugitive-:Gmove* diff --git a/plugin/fugitive.vim b/plugin/fugitive.vim index 36df254..61e7143 100644 --- a/plugin/fugitive.vim +++ b/plugin/fugitive.vim @@ -958,6 +958,28 @@ augroup fugitive_diff autocmd BufWinEnter * if winnr('$') == 1 && &diff && getbufvar(+expand(''), 'git_dir') !=# '' | diffoff | endif augroup END +function! s:buffer_compare_age(commit) dict abort + let scores = {':0': 1, ':1': 2, ':2': 3, ':': 4, ':3': 5} + let my_score = get(scores,':'.self.commit(),0) + let their_score = get(scores,':'.a:commit,0) + if my_score || their_score + return my_score < their_score ? -1 : my_score != their_score + elseif self.commit() ==# a:commit + return 0 + endif + let base = self.repo().git_chomp('merge-base',self.commit(),a:commit) + if base ==# self.commit() + return -1 + elseif base ==# a:commit + return 1 + endif + let my_time = self.repo().git_chomp('log','--max-count=1','--pretty=format:%at',self.commit()) + let their_time = self.repo().git_chomp('log','--max-count=1','--pretty=format:%at',a:commit) + return my_time < their_time ? -1 : my_time != their_time +endfunction + +call s:add_methods('buffer',['compare_age']) + function! s:Diff(...) abort if exists(':DiffGitCached') return 'DiffGitCached' @@ -993,7 +1015,13 @@ function! s:Diff(...) abort let file = s:buffer().path(s:buffer().commit() == '' ? ':0:' : '/') endif try - vsplit `=fugitive#buffer().repo().translate(file)` + let spec = s:repo().translate(file) + let commit = matchstr(spec,'\C[^:/]//\zs\x\+') + if s:buffer().compare_age(commit) < 0 + rightbelow vsplit `=spec` + else + leftabove vsplit `=spec` + endif diffthis wincmd p diffthis