Sat, 10 Dec 2016 14:21:31 -0800
update for self-forking
180 | 1 | #!/usr/bin/env ruby |
2 | exit if ENV['CTAGS_SKIP'] | |
441 | 3 | ARGV.each { |o| exec('perldoc', $0) if o.match('help') } |
180 | 4 | |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
5 | ctags_bin = ENV['CTAGS_BIN'] || 'ctags' |
180 | 6 | |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
7 | 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
|
8 | list_cmd = 'git ls-files' |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
9 | conf = "#{dir}/.git/ctags.conf" |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
10 | 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
|
11 | list_cmd = 'hg stat -Aqn' |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
12 | conf = "#{dir}/.hg/ctags.conf" |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
13 | else |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
14 | 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
|
15 | end |
180 | 16 | |
17 | target = "#{dir}/.tags" | |
440 | 18 | tmp = "#{dir}/.tags.#{$$}~" |
180 | 19 | |
20 | opts = File.exists?(conf) ? "--options=#{conf}" : '' | |
21 | ||
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
22 | if ENV['CTAGS_HOOK'] |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
23 | fork and exit |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
24 | sleep 5 |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
25 | end |
180 | 26 | |
440 | 27 | open(target, File::RDONLY|File::CREAT, 0644) do |f| |
180 | 28 | if ENV['CTAGS_HOOK'] |
440 | 29 | exit unless f.flock(File::LOCK_EX|File::LOCK_NB) |
30 | exit unless (Time.now - f.mtime) > 60 | |
180 | 31 | end |
32 | ||
440 | 33 | 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
|
34 | #{list_cmd} \ |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
35 | | #{ctags_bin} --tag-relative -L - -f"#{tmp}" #{opts} \ |
180 | 36 | && mv #{tmp} #{target} |
37 | CMD | |
38 | end | |
39 | ||
441 | 40 | exit 0 |
180 | 41 | |
441 | 42 | <<'=cut' |
43 | =head1 NAME | |
180 | 44 | |
441 | 45 | git-ctags - run ctags on git tracked files |
46 | ||
47 | =head1 SYNOPSIS | |
48 | ||
49 | git ctags | |
50 | ||
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
51 | echo 'CTAGS_HOOK=1 git ctags' >> .git/hooks/post-checkout |
441 | 52 | |
53 | git checkout some/branch | |
54 | CTAGS_SKIP=1 git checkout some/branch | |
55 | ||
56 | =head1 DESCRIPTION | |
57 | ||
58 | Create a .tags file (target) using git's list of tracked files. If | |
59 | C<.git/ctags.conf> exists in the repo it is passed to the ctags invocation. | |
60 | ||
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
61 | When run with C<CTAGS_HOOK> set, additional behavior is enabled to avoid excess |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
62 | re-runs during multiple VCS operations, and the work is moved to background as |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
63 | well. |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
64 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
65 | =head2 Mercurial |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
66 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
67 | 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
|
68 | 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
|
69 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
70 | [hooks] |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
71 | update = CTAGS_HOOK=1 git ctags |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
72 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
73 | In a Mercurial repo, additional options can be put in C<.hg/ctags.conf>. |
441 | 74 | |
443
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
75 | =head1 ENVIRONMENT |
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 | =head2 CTAGS_SKIP |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
78 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
79 | If true, exit immediately. |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
80 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
81 | =head2 CTAGS_HOOK |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
82 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
83 | If true, run as a VCS hook. This causes git-ctags to fork and exit, wait five |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
84 | 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
|
85 | the last minute. |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
86 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
87 | =head2 CTAGS_BIN |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
88 | |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
89 | If set, use this to invoke ctags rather than assuming C<ctags> can be found in |
e731ef81637c
support hg in git-ctags, deduplicate hg helper stuff
Meredith Howard <mhoward@roomag.org>
parents:
441
diff
changeset
|
90 | path. |
441 | 91 | |
92 | =cut |