code996.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env bash
  2. Help()
  3. {
  4. echo "你也可以使用自定义参数进行指定查询"
  5. echo
  6. echo "格式: bash $0 [2022-01-01] [2022-06-24] [author]"
  7. echo "示例: bash code996.sh 2022-01-01 2022-06-24 digua"
  8. echo "参数:"
  9. echo "1st 分析的起始时间."
  10. echo "2nd 分析的结束时间."
  11. echo "3rd 指定提交用户,可以是 name 或 email."
  12. echo
  13. }
  14. OS_DETECT()
  15. {
  16. # Detect OS
  17. case "$(uname -s)" in
  18. Linux)
  19. # echo 'Linux'
  20. open_url="xdg-open"
  21. ;;
  22. Darwin)
  23. # echo 'macOS'
  24. open_url="open"
  25. ;;
  26. CYGWIN*|MINGW32*|MSYS*|MINGW*)
  27. # echo 'Windows'
  28. open_url="start"
  29. ;;
  30. *)
  31. echo 'Other OS'
  32. echo "trying to use xdg-open to open the url"
  33. open_url="xdg-open"
  34. ;;
  35. esac
  36. }
  37. OS_DETECT
  38. time_start=$1
  39. if [ "$1" == "--help" ]
  40. then
  41. Help
  42. exit 0
  43. elif [ "$1" == "-h" ]
  44. then
  45. Help
  46. exit 0
  47. fi
  48. if [ -z $1 ]
  49. then
  50. time_start="2022-01-01"
  51. fi
  52. time_end=$2
  53. if [ -z $2 ]
  54. then
  55. time_end=$(date "+%Y-%m-%d")
  56. fi
  57. author=$3
  58. if [ -z $3 ]
  59. then
  60. author=""
  61. fi
  62. by_day_output=`git -C $PWD log --author=$author --date=format:%u --after="$time_start" --before="$time_end" |grep "Date:"|awk '{print $2}'|sort|uniq -c`
  63. by_hour_output=`git -C $PWD log --author=$author --date=format:%H --after="$time_start" --before="$time_end" |grep "Date:"|awk '{print $2}'|sort|uniq -c`
  64. for i in "${by_day_output[@]}"
  65. do
  66. by_day_result=`echo "$i"|sed -E 's/^ +//g'|sed 's/ /_/g'|tr '\n' ','`
  67. done
  68. # should modify by day format %a or %A
  69. # day_sorted=('Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday' 'Sunday')
  70. # day_sorted=('Mon' 'Tue' 'Wed' 'Thu' 'Fri' 'Sat' 'Sun')
  71. RED='\033[1;91m'
  72. NC='\033[0m' # No Color
  73. echo -e "${RED}统计时间范围:$time_start 至 $time_end"
  74. for i in "${by_day_output[@]}"
  75. do
  76. echo
  77. echo -e "${NC}一周七天 commit 分布${RED}"
  78. echo -e " 总提交次数 星期\n$i"|column -t
  79. by_day_result=`echo "$i"|sed -E 's/^ +//g'|sed "s/ /_/g"|tr '\n' ','`
  80. done
  81. for i in "${by_hour_output[@]}"
  82. do
  83. echo
  84. echo -e "${NC}24小时 commit 分布${RED}"
  85. echo -e " 总提交次数 小时\n$i"|column -t
  86. by_hour_result=`echo "$i"|sed -E 's/^ +//g'|sed "s/ /_/g"|tr '\n' ','`
  87. done
  88. by_day_result=`echo "$by_day_result"|sed -E 's/,$//g'`
  89. by_hour_result=`echo "$by_hour_result"|sed -E 's/,$//g'`
  90. result=$time_start"_"$time_end"&week="$by_day_result"&hour="$by_hour_result
  91. # url
  92. github_url="https://hellodigua.github.io/code996/#/result?time=$result"
  93. vercel_url="https://code996.vercel.app/#/result?time=$result"
  94. gitee_url="https://hellodigua.gitee.io/code996/#/result?time=$result"
  95. echo
  96. echo -e "${NC}复制以下url以查看可视化分析结果:"
  97. echo -e "${RED}$github_url"
  98. echo -e "${NC}"
  99. echo -e "${NC}若 GitHub 访问过慢,也可以访问以下镜像链接:"
  100. echo -e "${NC}Vercel节点:"
  101. echo -e "${RED}$vercel_url"
  102. echo -e "${NC}"
  103. echo -e "${NC}Gitee节点:"
  104. echo -e "${RED}$gitee_url"
  105. echo -e "${NC}"
  106. $open_url "$github_url"