Browse Source

Implement PlugSnapshot (#97)

Known issue: After running the script, PlugDiff will show "future"
commits.
Junegunn Choi 11 years ago
parent
commit
afc20ecff3
2 changed files with 41 additions and 0 deletions
  1. 1 0
      README.md
  2. 40 0
      plug.vim

+ 1 - 0
README.md

@@ -69,6 +69,7 @@ Reload .vimrc and `:PlugInstall` to install plugins.
 | `PlugUpgrade`                       | Upgrade vim-plug itself                                            |
 | `PlugStatus`                        | Check the status of plugins                                        |
 | `PlugDiff`                          | See the updated changes from the previous PlugUpdate               |
+| `PlugSnapshot [output path]`        | Generate script for restoring the current snapshot of the plugins  |
 
 ### `Plug` options
 

+ 40 - 0
plug.vim

@@ -113,6 +113,7 @@ function! s:define_commands()
   command! -nargs=0 -bar PlugUpgrade if s:upgrade() | execute 'source' s:me | endif
   command! -nargs=0 -bar PlugStatus  call s:status()
   command! -nargs=0 -bar PlugDiff    call s:diff()
+  command! -nargs=? -bar PlugSnapshot call s:snapshot(<f-args>)
 endfunction
 
 function! s:to_a(v)
@@ -1280,6 +1281,45 @@ function! s:revert()
   echo 'Reverted.'
 endfunction
 
+function! s:snapshot(...) abort
+  let [type, var, header] = s:is_win ?
+    \ ['dosbatch', '%PLUG_HOME%',
+    \   ['@echo off', ':: Generated by vim-plug', ':: '.strftime("%c"), '',
+    \    ':: Make sure to PlugUpdate first', '', 'set PLUG_HOME='.s:esc(g:plug_home)]] :
+    \ ['sh', '$PLUG_HOME',
+    \   ['#!/bin/bash',  '# Generated by vim-plug', '# '.strftime("%c"), '',
+    \    'vim +PlugUpdate +qa', '', 'PLUG_HOME='.s:esc(g:plug_home)]]
+
+  call s:prepare()
+  execute 'setf' type
+  call append(0, header)
+  call append('$', '')
+  1
+  redraw
+
+  let dirs = sort(map(values(filter(copy(g:plugs),
+        \'has_key(v:val, "uri") && isdirectory(v:val.dir)')), 'v:val.dir'))
+  let anchor = line('$') - 1
+  for dir in reverse(dirs)
+    execute 'cd' dir
+    let sha = substitute(system('git rev-parse --short HEAD'), '\n', '', '')
+    cd -
+    if !v:shell_error
+      call append(anchor, printf('cd %s && git reset --hard %s',
+            \ substitute(dir, '^'.g:plug_home, var, ''), sha))
+      redraw
+    endif
+  endfor
+
+  if a:0 > 0
+    let fn = s:esc(expand(a:1))
+    call writefile(getline(1, '$'), fn)
+    if !s:is_win | call system('chmod +x ' . fn) | endif
+    echo 'Saved to '.a:1
+    silent execute 'e' fn
+  endif
+endfunction
+
 function! s:split_rtp()
   return split(&rtp, '\\\@<!,')
 endfunction