-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_upstream
executable file
·120 lines (105 loc) · 4.15 KB
/
search_upstream
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
## Find remote_include references in the docs corpus and follow
## them upstream to search their contents. Must be run from within a
## downstream docs branch. Matches against the following directive:
##
## {% remote_include https://raw.githubusercontent.com/cockroachdb/cockroach ... %}
##
## Optionally, supply an exclusion string to exclude searching
## specific directories. Supports the same regex glob style
## as VS Code.
##
## For example, the following is a basic exclude string from VS Code:
## archived/*,v1.0/*,v1.1/*,v2.0/*,v2.1/*,v19.1/*,v19.2/*,v20.1/*,v20.2/*,v21.1/*,v21.2/*,_includes/releases/*,releases/*
##
## Currently, this script does not support excluding by filename globs:
## you may only specify directories to the -e flag (as show above).
##
## This script uses grep -i, for case insensitivy.
############################################################
## BEGIN SCRIPT ##
##################
EXCLUDE_STRING=""
PARAMETER=""
SEARCHED_FILES=""
MATCHED_FILES=""
## Check for arguments passed to this script:
while getopts ":e:" opt; do
case ${opt} in
e )
shift
EXCLUDE_STRING="$OPTARG"
;;
\? )
echo -e "\nERROR: Invalid option: $OPTARG" 1>&2
echo " Only the -e flag is supported for providing a"
echo " comma-separated list of directories (or regexes"
echo " matching directories) to exclude from the search."
exit;
;;
: )
echo -e "\nERROR: If you specify -$OPTARG, you must provide an exclusion string" 1>&2
;;
esac
shift
done
## Collect search string from provided parameter:
PARAMETER=$1
# Parse user-provided parameter and determine what to do:
if [ -z $PARAMETER ]; then
echo -e "\nERROR: No search string provided."
echo -e " Exiting ...\n"
exit;
elif [ ! -z $2 ]; then
echo -e "\nERROR: Too many parameters provided. You must provide only one."
echo -e " Exiting ...\n"
exit;
else
SEARCH_STRING=$PARAMETER
fi
## Exit if not in a valid git repo (we also use $GITROOT later as root of grep -r search):
GITROOT=`git rev-parse --show-toplevel 2>&1`
if [ `echo $GITROOT | grep -c '^fatal: not a git repository'` -gt 0 ]; then
echo -e "\nERROR: Not in a git repo!"
echo -e " You must run this program from within the docs directory.\n"
exit;
fi
TMPFILE=/tmp/search_remote_includes$$.tmp
## Prepare $EXCLUDE_STRING for use by grep, namely by removing *. This allows us
## to use the same exclude glob here that we already use with VS Code, etc:
EXCLUDE_LIST="{`echo "$EXCLUDE_STRING" | sed 's%/\*%%g'`}"
## First, find all files in the corpus that:
## 1. Contain the line:
## {% remote_include https://raw.githubusercontent.com/cockroachdb/cockroach ... %}
## 2. Are not excluded via $EXCLUDE_STRING
##
## Apologies for eval here, couldn't get {exclude,string,syntax} working any other way:
eval "grep -r '{% remote_include https://raw.githubusercontent.com/cockroachdb/cockroach' --exclude-dir=$EXCLUDE_LIST $GITROOT >> $TMPFILE.grepout"
# Set Bash FS to \n:
IFS="
"
## Loop through search results as returned above, and perform intended search pattern grep:
for LINE in `cat $TMPFILE.grepout`; do
FILENAME=`echo $LINE | awk -F ':' '{print $1}'`
UPSTREAM_INCLUDE=`echo $LINE | awk -F '{% remote_include ' '{print $2}' | sed 's/ %}//g'`
curl -s $UPSTREAM_INCLUDE | grep -i --color=always $SEARCH_STRING > $TMPFILE.mostrecentsearch
if [ $? -lt 1 ]; then
echo -e "\n################################################################################################################"
echo -e "FOUND MATCH:"
echo " DOCS copy : $FILENAME"
echo " ENG autogen : $UPSTREAM_INCLUDE"
echo -e " Matched line(s):\n"
cat $TMPFILE.mostrecentsearch
echo -e "################################################################################################################\n"
((MATCHED_FILES++))
fi
((SEARCHED_FILES++))
done
## Report on final results:
if [ $MATCHED_FILES ]; then
echo -e "\nDONE: $MATCHED_FILES matched file(s) out of $SEARCHED_FILES searched."
else
echo -e "\nDONE: No matches found in $SEARCHED_FILES files searched."
fi
## Cleanup:
rm -f $TMPFILE.*