Sat, 16 Mar 2019 04:30:15 -0500
disable ansi perldoc until consistent
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 |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
29 | sleep 5 |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
30 | end |
180 | 31 | |
440 | 32 | 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
|
33 | if running_hook |
440 | 34 | exit unless f.flock(File::LOCK_EX|File::LOCK_NB) |
35 | exit unless (Time.now - f.mtime) > 60 | |
180 | 36 | end |
37 | ||
440 | 38 | 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
|
39 | #{list_cmd} \ |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
40 | | #{ctags_cmd} --tag-relative -L - -f"#{tmp}" #{opts} \ |
180 | 41 | && mv #{tmp} #{target} |
42 | CMD | |
43 | end | |
44 | ||
441 | 45 | exit 0 |
180 | 46 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
47 | __END__ |
441 | 48 | =head1 NAME |
180 | 49 | |
441 | 50 | git-ctags - run ctags on git tracked files |
51 | ||
52 | =head1 SYNOPSIS | |
53 | ||
54 | git ctags | |
55 | ||
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
56 | echo 'git ctags hook 2>/dev/null' >> .git/hooks/post-checkout |
441 | 57 | |
58 | git checkout some/branch | |
59 | CTAGS_SKIP=1 git checkout some/branch | |
60 | ||
61 | =head1 DESCRIPTION | |
62 | ||
63 | Create a .tags file (target) using git's list of tracked files. If | |
64 | C<.git/ctags.conf> exists in the repo it is passed to the ctags invocation. | |
65 | ||
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
66 | 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
|
67 | 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
|
68 | 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
|
69 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
70 | =head2 Mercurial |
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 | 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
|
73 | 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
|
74 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
75 | [hooks] |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
76 | update = git ctags hook |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
77 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
78 | In a Mercurial repo, additional options can be put in C<.hg/ctags.conf>. |
441 | 79 | |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
80 | =head1 ENVIRONMENT |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
81 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
82 | =head2 CTAGS_SKIP |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
83 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
84 | If defined, exit immediately. |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
85 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
86 | =head2 CTAGS_HOOK |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
87 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
88 | 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
|
89 | 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
|
90 | the last minute. |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
91 | |
445
1201037900f5
Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents:
443
diff
changeset
|
92 | =head2 CTAGS_CMD |
443
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 | 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
|
95 | path. |
441 | 96 | |
97 | =cut |