bin/git-ctags

Fri, 15 Nov 2019 23:03:46 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Fri, 15 Nov 2019 23:03:46 -0500
changeset 823
da66fb550c74
parent 754
0b6dae901ee5
permissions
-rwxr-xr-x

clean up

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']
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
22
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
23 target = "#{dir}/.tags"
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
24 tmp = "#{dir}/.tags.#{$$}~"
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
25 opts = File.exists?(conf) ? "--options=#{conf}" : ''
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
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
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 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
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
37 end
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
38
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
42 && mv #{tmp} #{target}
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
43 CMD
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
44 end
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
45
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
46 exit 0
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
47
445
1201037900f5 Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents: 443
diff changeset
48 __END__
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
49 =head1 NAME
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
50
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
51 git-ctags - run ctags on git tracked files
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
52
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
53 =head1 SYNOPSIS
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
54
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
55 git ctags
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
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
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
59
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
60 git checkout some/branch
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
61 CTAGS_SKIP=1 git checkout some/branch
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
62
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
63 =head1 DESCRIPTION
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
64
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
65 Create a .tags file (target) using git's list of tracked files. If
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
66 C<.git/ctags.conf> exists in the repo it is passed to the ctags invocation.
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
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
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
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
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
98
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
99 =cut

mercurial