bin/git-ctags

Wed, 13 Feb 2019 01:31:50 -0600

author
Meredith Howard <mhoward@roomag.org>
date
Wed, 13 Feb 2019 01:31:50 -0600
changeset 734
294238afafac
parent 446
acd0b08d467e
child 752
bdbfa576e84f
permissions
-rwxr-xr-x

Actually... hs.execute blocks everything

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
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
31
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
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
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
34 exit unless f.flock(File::LOCK_EX|File::LOCK_NB)
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
35 exit unless (Time.now - f.mtime) > 60
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
36 end
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
37
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
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
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
41 && mv #{tmp} #{target}
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
42 CMD
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
43 end
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
44
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
45 exit 0
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
46
445
1201037900f5 Accept just a 'hook' arg to look nicer
Meredith Howard <mhoward@roomag.org>
parents: 443
diff changeset
47 __END__
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
48 =head1 NAME
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
49
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
50 git-ctags - run ctags on git tracked files
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
51
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
52 =head1 SYNOPSIS
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
53
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
54 git ctags
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
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
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
57
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
58 git checkout some/branch
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
59 CTAGS_SKIP=1 git checkout some/branch
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
60
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
61 =head1 DESCRIPTION
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
62
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
63 Create a .tags file (target) using git's list of tracked files. If
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
64 C<.git/ctags.conf> exists in the repo it is passed to the ctags invocation.
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
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
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
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
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
96
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
97 =cut

mercurial