Sfoglia il codice sorgente

Clean up usage messages for executable scripts

Usage messages are hard to see when bash displays the script file name
and offending line, instead of just "bash: <error message>". Since we
only display one string anyway, we change to using an explicit echo
instead of the built-in variable error mechanism.
Weiyi Lou 9 anni fa
parent
commit
f75c57fad0
5 ha cambiato i file con 38 aggiunte e 20 eliminazioni
  1. 3 3
      bin/linkup
  2. 3 3
      bin/version-compare
  3. 3 3
      bin/zfs-backup
  4. 1 1
      shell/bash/tmuxinator.bash
  5. 28 10
      vim/autoload/plug.vim

+ 3 - 3
bin/linkup

@@ -4,9 +4,9 @@ IFS=$'\n\t'
 
 # Create symlink after backing up the target.
 
-usage="Usage: ${0##*/} source_file target_file"
-src=${1:?$usage}
-tgt=${2:?$usage}
+[[ $# != 2 ]] && echo "Usage: ${0##*/} source_file target_file" && exit 1
+src=$1
+tgt=$2
 bak=~/.backup
 
 [[ ! -d $bak ]] && mkdir $bak

+ 3 - 3
bin/version-compare

@@ -12,9 +12,9 @@ set -euo pipefail
 
 # Split strings on '.'.
 IFS=.
-usage="Usage: ${0##*/} ver1 ver2"
-ver1=(${1:?$usage})
-ver2=(${2:?$usage})
+[[ $# != 2 ]] && echo "Usage: ${0##*/} ver1 ver2" && exit 1
+ver1=($1)
+ver2=($2)
 declare i fields
 
 # Number of version fields to be compared = highest array count.

+ 3 - 3
bin/zfs-backup

@@ -8,9 +8,9 @@ IFS=$'\n\t'
 # and its descendents. Snapshots created to facilitate this are formatted as
 # "manualbackup-YYYY-MM-DD".
 
-usage="Usage: ${0##*/} target_filesystem backup_filesystem"
-fsFrom=${1:?$usage}
-fsTo=${2:?$usage}
+[[ $# != 2 ]] && echo "Usage: ${0##*/} target_filesys backup_filesys" && exit 1
+fsFrom=$1
+fsTo=$2
 
 # Check that filesystems exist. Also checks that `zfs` exists.
 zfs list -Hd 0 $fsFrom >/dev/null

+ 1 - 1
shell/bash/tmuxinator.bash

@@ -8,7 +8,7 @@ _tmuxinator() {
     if [ "$COMP_CWORD" -eq 1 ]; then
         local commands="$(compgen -W "$(tmuxinator commands)" -- "$word")"
         local projects="$(compgen -W "$(tmuxinator completions start)" -- "$word")"
- 
+
         COMPREPLY=( $commands $projects )
     elif [ "$COMP_CWORD" -eq 2 ]; then
         local words

+ 28 - 10
vim/autoload/plug.vim

@@ -977,7 +977,7 @@ function! s:update_finish()
         call s:log4(name, 'Updating submodules. This may take a while.')
         let out .= s:bang('git submodule update --init --recursive 2>&1', spec.dir)
       endif
-      let msg = printf('%s %s: %s', v:shell_error ? 'x': '-', name, get(s:lines(out), -1, ''))
+      let msg = printf('%s %s: %s', v:shell_error ? 'x': '-', name, s:lastline(out))
       if v:shell_error
         call add(s:update.errors, name)
         call s:regress_bar()
@@ -1145,7 +1145,7 @@ while 1 " Without TCO, Vim stack is bound to explode
 
   let has_tag = has_key(spec, 'tag')
   if !new
-    let error = s:git_validate(spec, 0)
+    let [error, _] = s:git_validate(spec, 0)
     if empty(error)
       if pull
         let fetch_opt = (has_tag && !empty(globpath(spec.dir, '.git/shallow'))) ? '--depth 99999999' : ''
@@ -1860,11 +1860,18 @@ function! s:git_validate(spec, check_branch)
         let err = printf('Invalid branch: %s (expected: %s). Try PlugUpdate.',
               \ branch, a:spec.branch)
       endif
+      if empty(err)
+        let commits = len(s:lines(s:system(printf('git rev-list origin/%s..HEAD', a:spec.branch), a:spec.dir)))
+        if !v:shell_error && commits
+          let err = join([printf('Diverged from origin/%s by %d commit(s).', a:spec.branch, commits),
+                        \ 'Reinstall after PlugClean.'], "\n")
+        endif
+      endif
     endif
   else
     let err = 'Not found'
   endif
-  return err
+  return [err, err =~# 'PlugClean']
 endfunction
 
 function! s:rm_rf(dir)
@@ -1875,15 +1882,23 @@ endfunction
 
 function! s:clean(force)
   call s:prepare()
-  call append(0, 'Searching for unused plugins in '.g:plug_home)
+  call append(0, 'Searching for invalid plugins in '.g:plug_home)
   call append(1, '')
 
   " List of valid directories
   let dirs = []
+  let errs = {}
   let [cnt, total] = [0, len(g:plugs)]
   for [name, spec] in items(g:plugs)
-    if !s:is_managed(name) || empty(s:git_validate(spec, 0))
+    if !s:is_managed(name)
       call add(dirs, spec.dir)
+    else
+      let [err, clean] = s:git_validate(spec, 1)
+      if clean
+        let errs[spec.dir] = s:lines(err)[0]
+      else
+        call add(dirs, spec.dir)
+      endif
     endif
     let cnt += 1
     call s:progress_bar(2, repeat('=', cnt), total)
@@ -1907,11 +1922,14 @@ function! s:clean(force)
     if !has_key(allowed, f) && isdirectory(f)
       call add(todo, f)
       call append(line('$'), '- ' . f)
+      if has_key(errs, f)
+        call append(line('$'), '    ' . errs[f])
+      endif
       let found = filter(found, 'stridx(v:val, f) != 0')
     end
   endwhile
 
-  normal! G
+  4
   redraw
   if empty(todo)
     call append(line('$'), 'Already clean.')
@@ -1920,12 +1938,12 @@ function! s:clean(force)
       for dir in todo
         call s:rm_rf(dir)
       endfor
-      call append(line('$'), 'Removed.')
+      call append(3, ['Removed.', ''])
     else
-      call append(line('$'), 'Cancelled.')
+      call append(3, ['Cancelled.', ''])
     endif
   endif
-  normal! G
+  4
 endfunction
 
 function! s:upgrade()
@@ -1972,7 +1990,7 @@ function! s:status()
   for [name, spec] in items(g:plugs)
     if has_key(spec, 'uri')
       if isdirectory(spec.dir)
-        let err = s:git_validate(spec, 1)
+        let [err, _] = s:git_validate(spec, 1)
         let [valid, msg] = [empty(err), empty(err) ? 'OK' : err]
       else
         let [valid, msg] = [0, 'Not found. Try PlugInstall.']