Don't like ads? PRO users don't see any ads ;-)
Guest

context3.sh

By: a guest on Jun 15th, 2012  |  syntax: Bash  |  size: 0.65 KB  |  hits: 61  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. function usage
  4. {
  5.     echo "Usage $0 <file name> <line> <context>"
  6.     exit 1
  7. }
  8.  
  9. code_file=$1
  10. lineno=$2
  11. context=$3
  12.  
  13.  
  14. if [ "$lineno" = "" ] ; then
  15.     usage
  16. fi
  17.  
  18. if [ "$context" = "" ] ; then
  19.     context=6
  20. fi
  21.  
  22. if [ "$context" -lt "4" ] ; then
  23.    context=4
  24. fi
  25.  
  26. if [ $(($context % 2 )) != "0" ] ; then
  27.         context=$(($context + 1))
  28. fi
  29.  
  30. start_line=$(($lineno - ($context / 2)))
  31. end_line=$(($lineno + ($context / 2)))
  32.  
  33. cat -n $code_file | tail -n +$start_line | head -n $(($context + 1))
  34.  
  35. echo
  36. echo ---
  37. echo
  38.  
  39. hash=$(git blame $code_file -L $lineno,$lineno |cut -d " " -f 1)
  40. git --no-pager show $hash $code_file