Skip to content

Instantly share code, notes, and snippets.

@craSH
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craSH/1b0370d4224e621789bc to your computer and use it in GitHub Desktop.
Save craSH/1b0370d4224e621789bc to your computer and use it in GitHub Desktop.
Index code with glimpse, ctags, and cscope. Store the indexes and such in a defined cache directory, which may live on a fast SSD
#!/usr/bin/env bash
# Original credit to tecknicaltom@neg9.org for creating this script and concept - thanks!
# Store indexes and such in a cache located on an SSD, and symlink to them
# Ensure the cache location is at the same or better security level (encrypted drive) as the data you are indexing.
cache_root="${HOME}/.indexcodecache"
cur_proj="$(basename -a ${PWD} | tr -d '\n')-$(echo ${PWD} | shasum -a 512 | cut -c 1-16)"
cache_dir="${cache_root}/${cur_proj}"
# Set umask to create files as mode 0600 (user r/w, go none)
umask 0077 || (echo "[ERROR] Setting restrictive umask (0077), aborting."; exit 1)
if [ -d "$cache_dir" ]; then
echo "[Info] Cache directory exists: ${cache_dir}"
else
echo "[Info] Creating cache directory: ${cache_dir}"
mkdir -p "$cache_dir" || (echo "[ERROR] Failed to create cache dir, aborting."; exit 1)
fi
# Make destination directories as needed and create symlinks locally
ln -sf "${cache_dir}/tags" "${PWD}/tags"
mkdir -p "${cache_dir}/glimpse"
ln -sf "${cache_dir}/glimpse" "${PWD}/.glimpse"
ln -sf "${cache_dir}/cscope.files" "${PWD}/cscope.files"
# The cscope utility deletes its database files if they exist, so we have to move + symlink them vs create links before
ctags .
cat <<- EOF > .glimpse/.glimpse_exclude
*/tags
.svn
YASAT
.glimpse
doxygen
.git
EOF
LC_ALL=C glimpseindex -M 64 -B -f -n -H .glimpse/ .
# cscope
find . -type f \
-and \
\( -iname '*.py' \
-or -iname '*.java' \
-or -iname '*.[ch]' \
-or -iname '*.cpp' \
-or -iname '*.cc' \
-or -iname '*.hpp' \
-or -iname '*.l' \
-or -iname '*.y' \) \
-and -exec echo '"{}"' ';' \
> "cscope.files"
if [ -s "cscope.files" ]; then
# -b: just build
# -q: create inverted index
cscope -b -q || (echo "[ERROR] Failed to execute cscope -b -q \"cscope.files\", aborting."; exit 1)
# The cscope utility deletes its database files if they exist, so we have to move + symlink them vs create links before
mv "${PWD}/cscope.out" "${PWD}/cscope.in.out" "${PWD}/cscope.po.out" "${cache_dir}/"
ln -sf "${cache_dir}/cscope.in.out" "${PWD}/cscope.in.out"
ln -sf "${cache_dir}/cscope.out" "${PWD}/cscope.out"
ln -sf "${cache_dir}/cscope.po.out" "${PWD}/cscope.po.out"
else
echo "[Info] No cscope.files content, not running cscope."
rm -f "cscope.files"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment