bin/git-ctags

Sat, 10 Dec 2016 14:21:31 -0800

author
Meredith Howard <mhoward@roomag.org>
date
Sat, 10 Dec 2016 14:21:31 -0800
changeset 444
1e9fc079c697
parent 443
e731ef81637c
child 445
1201037900f5
permissions
-rwxr-xr-x

update for self-forking

180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
1 #!/usr/bin/env ruby
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
2 exit if ENV['CTAGS_SKIP']
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
3 ARGV.each { |o| exec('perldoc', $0) if o.match('help') }
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
16
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
17 target = "#{dir}/.tags"
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
18 tmp = "#{dir}/.tags.#{$$}~"
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
19
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
20 opts = File.exists?(conf) ? "--options=#{conf}" : ''
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
26
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
27 open(target, File::RDONLY|File::CREAT, 0644) do |f|
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
28 if ENV['CTAGS_HOOK']
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
29 exit unless f.flock(File::LOCK_EX|File::LOCK_NB)
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
30 exit unless (Time.now - f.mtime) > 60
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
31 end
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
32
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
36 && mv #{tmp} #{target}
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
37 CMD
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
38 end
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
39
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
40 exit 0
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
41
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
42 <<'=cut'
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
43 =head1 NAME
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
44
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
45 git-ctags - run ctags on git tracked files
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
46
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
47 =head1 SYNOPSIS
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
48
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
49 git ctags
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
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
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
52
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
53 git checkout some/branch
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
54 CTAGS_SKIP=1 git checkout some/branch
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
55
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
56 =head1 DESCRIPTION
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
57
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
58 Create a .tags file (target) using git's list of tracked files. If
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
59 C<.git/ctags.conf> exists in the repo it is passed to the ctags invocation.
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
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
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
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
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
91
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
92 =cut

mercurial