From 8e77a808fa110489f32729b97400f7f36a5039d7 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 8 Mar 2018 03:15:40 -0500 Subject: initial implementation --- LICENSE | 19 +++++++++++++ README.md | 23 +++++++++++++++ doc/diff-changes.txt | 38 +++++++++++++++++++++++++ plugin/diff-changes.vim | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 doc/diff-changes.txt create mode 100644 plugin/diff-changes.vim diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d59eb6e --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright 2018 Jesse Luehrs + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a9f881c --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Overview + +This plugin adds commands which diff the current buffer against the file it +was loaded from, either the state on disk or the state in version control +(similar to `vimdiff` except without requiring writing the buffer state to +disk). + +# Commands + +## `:DiffAgainstFilesystem` + +Opens a diff window which diffs the buffer contents against the contents of +the file on disk. Use |:DiffStop| to close the diff window. + +## `:DiffAgainstVCS` + +Opens a diff window which diffs the buffer contents against the contents of +the file in the most recent commit in version control. Use |:DiffStop| to +close the diff window. Supports git, svn, darcs, and cvs. + +## `:DiffStop` + +Closes an active diff window. diff --git a/doc/diff-changes.txt b/doc/diff-changes.txt new file mode 100644 index 0000000..0f56a0b --- /dev/null +++ b/doc/diff-changes.txt @@ -0,0 +1,38 @@ +*diff-changes.txt* Diff buffer contents against file on disk or in the VCS + +Author: Jesse Luehrs +License: MIT + +============================================================================= +OVERVIEW *diff-changes-overview* + +This plugin adds commands which diff the current buffer against the file it +was loaded from, either the state on disk or the state in version control +(similar to `vimdiff` except without requiring writing the buffer state to +disk). + +============================================================================= +COMMANDS *diff-changes-commands* + + *:DiffAgainstFilesystem* +> + :DiffAgainstFilesystem +< +Opens a diff window which diffs the buffer contents against the contents of +the file on disk. Use |:DiffStop| to close the diff window. + + *:DiffAgainstVCS* +> + :DiffAgainstVCS +< +Opens a diff window which diffs the buffer contents against the contents of +the file in the most recent commit in version control. Use |:DiffStop| to +close the diff window. Supports git, svn, darcs, and cvs. + + *:DiffStop* +> + :DiffStop +< +Closes an active diff window. + + vim:tw=78:et:ft=help:norl: diff --git a/plugin/diff-changes.vim b/plugin/diff-changes.vim new file mode 100644 index 0000000..acd2cbd --- /dev/null +++ b/plugin/diff-changes.vim @@ -0,0 +1,75 @@ +if exists('g:loaded_diff_changes') || &cp + finish +endif +let g:loaded_diff_changes = 1 + +let s:foldmethod = &foldmethod +let s:foldenable = &foldenable +let s:diffwindow = 0 + +function! s:diffstart(read_cmd) + if s:diffwindow != 0 + return + endif + let s:foldmethod = &foldmethod + let s:foldenable = &foldenable + let l:filetype = &filetype + vert new + let s:diffwindow = winnr() + set buftype=nofile + try + exe a:read_cmd + catch /.*/ + echohl ErrorMsg + echo v:exception + echohl NONE + call s:diffstop() + return + endtry + let &filetype = l:filetype + diffthis + wincmd p + diffthis +endfunction + +function! s:diffstop() + if s:diffwindow == 0 + return + endif + diffoff! + exe s:diffwindow . 'wincmd w' + bdelete + let &foldmethod = s:foldmethod + let &foldenable = s:foldenable + if &foldenable + if &foldmethod == 'marker' + normal! zv + else + normal! zE + end + endif + let s:diffwindow = 0 +endfunction + +function! s:vcs_orig(file) + " XXX: would be nice to use a:file rather than # here... + let l:dir = expand('#:p:h') + if filewritable(l:dir . '/.svn') == 2 + return system('svn cat ' . a:file) + elseif filewritable(l:dir . '/CVS') == 2 + return system("AFILE=" . a:file . "; MODFILE=`tempfile`; DIFF=`tempfile`; cp $AFILE $MODFILE && cvs diff -u $AFILE > $DIFF; patch -R $MODFILE $DIFF 2>&1 > /dev/null && cat $MODFILE; rm $MODFILE $DIFF") + elseif finddir('_darcs', l:dir . ';') =~# '_darcs' + return system('darcs show contents ' . a:file) + elseif finddir('.git', l:dir . ';') =~# '.git' + let l:prefix = system('git rev-parse --show-prefix') + let l:prefix = substitute(l:prefix, '\n', '', 'g') + let l:cmd = 'git show HEAD:'.l:prefix.a:file + return system(l:cmd) + else + throw 'No VCS directory found' + endif +endfunction + +command DiffAgainstFilesystem call diffstart('read # | normal! ggdd') +command DiffAgainstVCS call diffstart('call append(0, split(s:vcs_orig(expand("#:.")), "\n", 1)) | normal! Gdddd') +command DiffStop call diffstop() -- cgit v1.2.3-54-g00ecf