-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-search-local.sh
executable file
·96 lines (81 loc) · 2.04 KB
/
git-search-local.sh
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
95
96
#!/bin/bash
REGEX='APP_CONFIG\[.*]'
BRANCH='develop'
STATE='added'
read -r -d '' helpMsg << HelpMessage
Searches for REGEX in code changes in commits unique to local branch compared to BRANCH
Usaage:
$ git-search-local.sh [-r|--regex REGEX] [-b|--branch BRANCH] [-s|--state added|removed]
Example:
$ git-search-local.sh -r 'APP_CONFIG(\[.*\])?' -b master -s removed
Defaults:
REGEX: $REGEX
BRANCH: $BRANCH
STATE: $STATE
HelpMessage
#set -o errexit -o noclobber -o nounset -o pipefail
params="$(getopt -o r:b:s:h -l regex:,branch:,state:,help --name "$0" -- "$@")"
eval set -- "$params"
while true
do
case "$1" in
-r|--regex)
REGEX=$2
shift 2
;;
-b|--branch)
BRANCH=$2
shift 2
;;
-s|--state)
if [ $2 == 'removed' ]; then
STATE=$2
fi
shift 2
;;
-h|--help)
echo ""
echo "$helpMsg"
echo ""
exit
;;
--)
shift
break
;;
*)
echo "Not implemented: $1" >&2
exit 1
;;
esac
done
local_commit_shas=`git rev-list $(git rev-parse --abbrev-ref HEAD) --no-merges ^$BRANCH`
# Sets a regex with polarity of initial character
if [ $STATE == 'removed' ]; then
INITIAL_REGEX="^-"
else
INITIAL_REGEX="^\+"
fi
POLAR_REGEX="$INITIAL_REGEX.*$REGEX"
declare -a found
add_match_to_found(){
match=$(echo $1|grep -oE $REGEX)
found+=($match)
}
for sha in $local_commit_shas
do
r=$(git show --unified=0 --pretty=oneline --no-notes -w -b $sha|grep -oE $POLAR_REGEX)
if [ ${#r} != 0 ]; then
mapfile -t arr <<< "$r"
for line in "${arr[@]}"
do
if echo "$line" | grep -q $INITIAL_REGEX
then
add_match_to_found "$line"
fi
done
fi
done
uniques=($(for v in "${found[@]}"; do echo "$v";done| sort| uniq| xargs))
#echo "${uniques[@]}" #for single line
printf '%s\n' "${uniques[@]}"; #for multiple lines