Fri, 24 Apr 2020 14:15:37 -0500
no automatic autosquash
180 | 1 | #!/usr/bin/env ruby |
2 | exit if ENV['CTAGS_SKIP'] | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
3 | running_hook = ENV['CTAGS_HOOK'] || false |
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
4 | ctags_cmd = ENV['CTAGS_CMD'] || 'ctags' |
180 | 5 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
6 | case ARGV[0] |
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
7 | when 'help' |
446
acd0b08d467e
skip pager for tiny perldocs
Meredith Howard <mhoward@roomag.org>
parents:
445
diff
changeset
|
8 | exec 'perldoc', '-T', $0 |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
9 | when 'hook' |
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
10 | running_hook = true |
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
11 | end |
180 | 12 | |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
13 | if (dir = `git rev-parse --show-toplevel 2>/dev/null`.chomp) != '' |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
14 | list_cmd = 'git ls-files' |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
15 | conf = "#{dir}/.git/ctags.conf" |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
16 | elsif (dir = `hg root 2>/dev/null`.chomp) != '' |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
17 | list_cmd = 'hg stat -Aqn' |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
18 | conf = "#{dir}/.hg/ctags.conf" |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
19 | else |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
20 | abort 'not an hg or git repository' |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
21 | end |
180 | 22 | |
23 | target = "#{dir}/.tags" | |
440 | 24 | tmp = "#{dir}/.tags.#{$$}~" |
180 | 25 | opts = File.exists?(conf) ? "--options=#{conf}" : '' |
26 | ||
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
27 | if running_hook |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
28 | fork and exit |
753
fadb5356ae65
Need to reopen the pipes actually
Meredith Howard <mhoward@roomag.org>
parents:
752
diff
changeset
|
29 | [STDIN, STDOUT, STDERR].each {|p| p.reopen '/dev/null'} |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
30 | sleep 5 |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
31 | end |
180 | 32 | |
440 | 33 | open(target, File::RDONLY|File::CREAT, 0644) do |f| |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
34 | if running_hook |
440 | 35 | exit unless f.flock(File::LOCK_EX|File::LOCK_NB) |
754
0b6dae901ee5
Don't abort when we just created the tags file
Meredith Howard <mhoward@roomag.org>
parents:
753
diff
changeset
|
36 | exit unless f.size == 0 || (Time.now - f.mtime) > 60 |
180 | 37 | end |
38 | ||
440 | 39 | system(<<-CMD) or exit $?.exitstatus |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
40 | #{list_cmd} \ |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
41 | | #{ctags_cmd} --tag-relative -L - -f"#{tmp}" #{opts} \ |
180 | 42 | && mv #{tmp} #{target} |
43 | CMD | |
44 | end | |
45 | ||
441 | 46 | exit 0 |
180 | 47 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
48 | __END__ |
441 | 49 | =head1 NAME |
180 | 50 | |
441 | 51 | git-ctags - run ctags on git tracked files |
52 | ||
53 | =head1 SYNOPSIS | |
54 | ||
55 | git ctags | |
56 | ||
752
bdbfa576e84f
git likes to wait on hooks if you keep STD* open
Meredith Howard <mhoward@roomag.org>
parents:
446
diff
changeset
|
57 | echo 'git ctags hook 2>/dev/null' >> .git/hooks/post-checkout && |
bdbfa576e84f
git likes to wait on hooks if you keep STD* open
Meredith Howard <mhoward@roomag.org>
parents:
446
diff
changeset
|
58 | chmod +x $_ |
441 | 59 | |
60 | git checkout some/branch | |
61 | CTAGS_SKIP=1 git checkout some/branch | |
62 | ||
63 | =head1 DESCRIPTION | |
64 | ||
65 | Create a .tags file (target) using git's list of tracked files. If | |
66 | C<.git/ctags.conf> exists in the repo it is passed to the ctags invocation. | |
67 | ||
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
68 | When run with the C<hook> argument or C<CTAGS_HOOK> set, additional behavior is |
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
69 | enabled to avoid excess re-runs during multiple VCS operations, and the work is |
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
70 | moved to background as well. |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
71 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
72 | =head2 Mercurial |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
73 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
74 | Mercurial repositories are supported too, with git taking priority when finding |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
75 | a repo root. To add a hook, in the repository C<.hg/hgrc>, add: |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
76 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
77 | [hooks] |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
78 | update = git ctags hook |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
79 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
80 | In a Mercurial repo, additional options can be put in C<.hg/ctags.conf>. |
441 | 81 | |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
82 | =head1 ENVIRONMENT |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
83 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
84 | =head2 CTAGS_SKIP |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
85 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
86 | If defined, exit immediately. |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
87 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
88 | =head2 CTAGS_HOOK |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
89 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
90 | If defined, run as a VCS hook. This causes git-ctags to fork and exit, wait five |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
91 | seconds, then try to lock and update the tags file if it hasn't been updated in |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
92 | the last minute. |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
93 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
94 | =head2 CTAGS_CMD |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
95 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
96 | If defined, use this to invoke ctags rather than assuming C<ctags> can be found in |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
97 | path. |
441 | 98 | |
99 | =cut |