Don't like ads? PRO users don't see any ads ;-)
Guest

update-tags

By: a guest on May 8th, 2012  |  syntax: Bash  |  size: 2.56 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. # Update tags file. Written by Laszlo Ersek <lersek@redhat.com>. Public domain.
  4. #
  5. # This script is to be invoked by git hooks. These hooks are run by git in the
  6. # root directory of the repository; this script relies on that. If the user is
  7. # careful enough, the script can be run manually.
  8. #
  9. # The script collects "relevant" source files that git (or the user) has
  10. # changed (as in, ctime) since the script last updated the tags file. The
  11. # script excludes the tags belonging to the changed files from the old tags
  12. # file, then it merges the remaining tags with the tags that it (re)generates
  13. # for the changed files. Tags belonging to deleted files are not removed.
  14. #
  15. # The tags file is kept sorted by relative pathname. The list of changed files
  16. # is also sorted by relative pathname. This way exclusion and merging is linear
  17. # in the number of tags plus changed files, at the price of having to sort the
  18. # list of changed files (O(n log n)). You should remove your current tags file
  19. # before running this script for the first time.
  20. #
  21. # The script relies on the "sponge" utility from the "moreutils" package,
  22. # because merging produces output before all inputs are read to end. Yet
  23. # another temporary file could have worked in "sponge"'s place.
  24. #
  25. # Some function-like macros are already identified as such for ctags.
  26.  
  27. set -e -u -C -o pipefail
  28.  
  29. unset CTAGS LANG || true
  30. export LC_ALL=POSIX
  31.  
  32. BNAME="$(basename -- "$0")"
  33. TMPSTEM="${TMPDIR:-/tmp}/$BNAME.$$"
  34.  
  35. trap 'rm -f -- "$TMPSTEM".list "$TMPSTEM".tags' EXIT
  36.  
  37. if test -e tags; then
  38.   printf -- '%s: partial tags update\n' "$BNAME" >&2
  39.   FIND_RESTR='-cnewer tags'
  40.   OLDTAGS=tags
  41. else
  42.   printf -- '%s: full tags update\n' "$BNAME" >&2
  43.   FIND_RESTR=
  44.   OLDTAGS=/dev/null
  45. fi
  46.  
  47. ARCHES='i386|ia64|x86_64|x86'
  48.  
  49. find . -regextype posix-extended \
  50.     -type d \( \
  51.       -regex     '\./(\.git|\.hg|Documentation)' \
  52.       -o -regex  '\./arch/[^/]+' \
  53.         ! -regex '\./arch/('"$ARCHES"')' \
  54.       -o -regex  '\./include/asm-[^/]+' \
  55.         ! -regex '\./include/asm-('"$ARCHES"'|generic)' \
  56.     \) -prune \
  57.     -o $FIND_RESTR -type f -regex '[^-[:space:]][^[:space:]]*\.[chS]' \
  58.       -print \
  59. | sort \
  60. | tee -- "$TMPSTEM".list \
  61. | ctags --filter --sort=no --langmap=c:.c.h -I EXPORT_SYMBOL+ \
  62.       -I DECLARE_TASKLET+ -I module_init+ -I MODULE_LICENSE+ \
  63.       -I EXPORT_SYMBOL_GPL+ > "$TMPSTEM".tags
  64.  
  65. # "--nocheck-order" could have been passed to "join" too
  66. join -1 1 -2 2 -t $'\t' -v 2 -- "$TMPSTEM".list "$OLDTAGS" \
  67. | sort -t $'\t' --merge --stable -k 2,2 -- - "$TMPSTEM".tags \
  68. | sponge tags