Repository navigation
Expand file tree
/
Copy pathmarkdown-headers-added
More file actions
executable file
·94 lines (83 loc) · 2.26 KB
/
Copy pathmarkdown-headers-added
File metadata and controls
executable file
·94 lines (83 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
REPO_ROOT=$(git rev-parse --show-toplevel 2>/dev/null)
SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
S="$HOME/scripts"
SHORT_MODE=
FILE_NAME=""
FILTER_HEADERS=("Комментарии" "Comments")
function echoerr {
printf "%s\n" "$*" >&2;
}
function help_exit { EXIT_CODE="$1";
if [[ -z "$EXIT_CODE" ]]; then
EXIT_CODE=1
fi
echoerr "Usage: $SCRIPT_NAME [-s|--short] [FILE_NAME]"
exit $EXIT_CODE
}
# Format header: remove leading '#', '/', trim whitespace, filter excluded headers
function format_short_header {
local FILTER_PATTERN=$(printf '^%s$\n' "${FILTER_HEADERS[@]}" | paste -sd'|')
echo "$1" | sed 's/^#* *//' | sed 's/ *\/.*$//' | sed 's/, *[0-9]\{4\}.*$//' | sed 's/^ *//' | sed 's/ *$//' | grep -Ev "$FILTER_PATTERN"
}
# Process added headers for a single file
function process_file {
local FILE_ARG="$1"
local DIFF_OUTPUT
git reset >/dev/null
git add "$FILE_ARG"
# DIFF_OUTPUT=$(git --no-pager diff -- "$FILE_ARG" | grep "^+#" | sed 's/^+//')
DIFF_OUTPUT=$(git --no-pager diff --cached | grep "^+#" | sed 's/^+//')
git reset >/dev/null
if [[ -z "$DIFF_OUTPUT" ]]; then
return
fi
if [[ -n $SHORT_MODE ]]; then
echo "$DIFF_OUTPUT" | while IFS= read -r line; do
format_short_header "$line"
done
else
echo "$DIFF_OUTPUT"
fi
}
while [[ $# -gt 0 ]]; do
case "$1" in
-s|--short)
SHORT_MODE=1
shift
;;
-h|--help)
help_exit 0
;;
-*)
echoerr "Unknown option: $1"
help_exit 100
;;
*)
if [[ -z "$FILE_NAME" ]]; then
FILE_NAME="$1"
else
echoerr "Too many arguments"
help_exit 110
fi
shift
;;
esac
done
if [[ -n "$FILE_NAME" ]]; then
process_file "$FILE_NAME"
else
# Process all changed files (exclude deleted files starting with " D")
cd "$REPO_ROOT"
git status --porcelain | grep -v "^ D" | while IFS= read -r line; do
# Cut first 3 chars (status flags) to get file name
FILE_ARG=$(echo "$line" | cut -c4-)
FILE_RESULT=$(process_file "$FILE_ARG")
if [[ -n "$FILE_RESULT" ]]; then
echo
echo "=== $FILE_ARG ==="
echo "$FILE_RESULT"
fi
done
fi