Skip to content

Instantly share code, notes, and snippets.

@b4n
Last active August 29, 2015 14:24
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 b4n/b4465983d81a75c99f16 to your computer and use it in GitHub Desktop.
Save b4n/b4465983d81a75c99f16 to your computer and use it in GitHub Desktop.
mergetags -- A tool to update entries for a single file in a CTags tag file
#!/bin/sh
#
# FIXME: better error checking. As many commands are piped, their return code
# is not properly checked.
export LANG=C
set -e
### argument parsing
sort=true
tagfile=tags
ctags=ctags
usage() {
cat <<EOF
USAGE: $0 [-h] [-c CTAGS] [-o TAGFILE] [-s|-S] [-v|-q] FILE
-c CTAGS Use CTAGS as the CTags executable instead of "$ctags"
-h Show this help and exit
-o TAGFILE Update TAGFILE instead of "$tagfile"
-q Be quiet
-s Sort output
-S Do not sort output
-v Be verbose
EOF
exit "$1"
}
while getopts 'c:ho:sSvq' o; do
case "$o" in
c) ctags="$OPTARG";;
h) usage 0;;
o) tagfile="$OPTARG";;
q) set +x;;
s) sort=true;;
S) sort=false;;
v) set -x;;
*) usage 1 >&2;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 1 ] || usage 1 >&2
input="$1"
### main logic
tmp=$(mktemp -p "$(dirname "$tagfile")" "$(basename "$tagfile").XXXXXX")
(
# remove the existing tags for the file
grep -vF " $input " "$tagfile"
# and add new at the end
"$ctags" -o- "$input"
) | (
if "$sort"; then
# extract the pseudo-tags
sed -n '/^!_TAG_/!q;p' "$tagfile"
# and sort the rest
sed '/^!_TAG_/d' | sort -u
else
cat
fi
) > "$tmp"
mv -f "$tmp" "$tagfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment