Skip to content

Commit c42e206

Browse files
committed
Update tags via git hooks
Based on http://tbaggery.com/2011/08/08/effortless-ctags-with-git.html. Makes use of vim-gutentags update script.
1 parent 71febe3 commit c42e206

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

config/git/template/hooks/ctags

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/sh
2+
# TODO: only update changed files, e.g. in post-commit hook!?
3+
# - post-merge: git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD
4+
# - post-commit: git log --name-only --pretty=format: -n1
5+
# - Does it include deleted files?
6+
7+
hook_name=${1:-$(basename $0)}
8+
# echo "Updating tags via git hook ($hook_name)…" >&2
9+
10+
mkdir .git/tags_lock 2>/dev/null || {
11+
echo "Lockfile exists.. skipping." >&2
12+
exit 0
13+
}
14+
(
15+
trap 'rmdir .git/tags_lock' EXIT
16+
17+
gtags=~/.dotfiles/vim/neobundles/gutentags/plat/unix/update_tags.sh
18+
if ! [ -f "$gtags" ]; then
19+
echo "$gtags not found. Skipping." >&2
20+
exit
21+
fi
22+
23+
export CTAGS="-L -"
24+
git ls-files | "$gtags" -e ctags -t tags -p . >/dev/null
25+
) &

config/git/template/hooks/post-checkout

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,40 @@
55
# new_head=$2
66
# changing_branch=$3
77

8-
if [ "$3" = 0 ]; then
8+
is_clone=0
9+
if [ "$3" = 1 ]; then
10+
if [ "$1" = "0000000000000000000000000000000000000000" ]; then
11+
is_clone=1
12+
prog="$(ps -p $(ps -p "$PPID" -o ppid=) -o comm=)"
13+
# if [ "${PWD#/tmp/pacaurtmp}" != "$PWD" ]; then
14+
if [ "$prog" = "pacaur" ]; then
15+
exit
16+
fi
17+
fi
18+
else
19+
# Checking out a file.
20+
.git/hooks/ctags post-checkout &
921
exit
1022
fi
1123

1224
# Special template/display for "pip" cloning/checking out a VCS URL.
13-
if [ $(ps -p $(ps -p $PPID -o ppid=) -o comm=) = 'pip' ]; then
25+
if [ -z "$prog" ]; then
26+
prog="$(ps -p $(ps -p "$PPID" -o ppid=) -o comm=)"
27+
fi
28+
if [ "$prog" = 'pip' ]; then
1429
# Get "git" command from 2nd "args" entry of git parent process.
30+
# TODO: move to clone block above? / use is_clone flag for pip.
1531
mode=$(ps -f -p $PPID -o args= | cut -f2 -d\ )
16-
if [ $mode = checkout ]; then # "checkout" might follow "clone".
17-
git --no-pager log -1 --pretty=format:"%C(green) checkout: $(basename $PWD): %C(blue)%s%C(reset) (%cr, %h, %cN)"
32+
if [ "$mode" = checkout ]; then # "checkout" might follow "clone".
33+
git --no-pager log -1 --pretty=format:"%C(green) checkout: $(basename "$PWD"): %C(blue)%s%C(reset) (%cr, %h, %cN)"
1834
else
19-
git --no-pager log -1 --pretty=format:"%C(green)pip-$mode: $(basename $PWD): %C(blue)%s%C(reset) (%cr, %h, %cN)"
35+
git --no-pager log -1 --pretty=format:"%C(green)pip-$mode: $(basename "$PWD"): %C(blue)%s%C(reset) (%cr, %h, %cN)"
2036
fi
2137

2238
else
2339
git --no-pager log -1 --pretty=format:"Last commit: %C(blue)%s%C(reset) (%cr, %h, %cN)"
40+
.git/hooks/ctags post-checkout &
2441
fi
2542
# Note: --no-pager does not add a newline.
2643
echo
44+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ctags
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ctags
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
if [ "$1" = "rebase" ]; then
3+
exec .git/hooks/post-merge
4+
fi

0 commit comments

Comments
 (0)