bin/git-ctags

Fri, 09 Dec 2016 15:20:22 -0800

author
Meredith Howard <mhoward@roomag.org>
date
Fri, 09 Dec 2016 15:20:22 -0800
changeset 441
650cbc95b9b0
parent 440
aacbd4032a4c
child 443
e731ef81637c
permissions
-rwxr-xr-x

doc it

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
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
5 ctags = 'ctags'
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
6
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
7 dir = `git rev-parse --show-toplevel`.chomp
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
8
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
9 conf = "#{dir}/.git/ctags.conf"
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
10 target = "#{dir}/.tags"
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
11 tmp = "#{dir}/.tags.#{$$}~"
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
12
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
13 opts = File.exists?(conf) ? "--options=#{conf}" : ''
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
14
187
84c6fa7ebc05 only wait 5 sec to try updating tags
Meredith Howard <mhoward@roomag.org>
parents: 180
diff changeset
15 sleep 5 if ENV['CTAGS_HOOK']
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
16
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
17 open(target, File::RDONLY|File::CREAT, 0644) do |f|
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
18 if ENV['CTAGS_HOOK']
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
19 exit unless f.flock(File::LOCK_EX|File::LOCK_NB)
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
20 exit unless (Time.now - f.mtime) > 60
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
21 end
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
22
440
aacbd4032a4c Fixup git-ctags
Meredith Howard <mhoward@roomag.org>
parents: 187
diff changeset
23 system(<<-CMD) or exit $?.exitstatus
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
24 git ls-files \
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
25 | #{ctags} --tag-relative -L - -f"#{tmp}" #{opts} \
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
26 && mv #{tmp} #{target}
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
27 CMD
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
28 end
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
29
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
30 exit 0
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
31
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
32 <<'=cut'
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
33 =head1 NAME
180
96dbe6a6008d ctags tweaking
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
34
441
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
35 git-ctags - run ctags on git tracked files
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
36
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
37 =head1 SYNOPSIS
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
38
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
39 git ctags
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
40
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
41 echo 'CTAGS_HOOK=1 git ctags &' >> .git/hooks/post-checkout
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
42
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
43 git checkout some/branch
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
44 CTAGS_SKIP=1 git checkout some/branch
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
45
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
46 =head1 DESCRIPTION
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
47
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
48 Create a .tags file (target) using git's list of tracked files. If
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
49 C<.git/ctags.conf> exists in the repo it is passed to the ctags invocation.
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
50
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
51 When run w/o env, immediately run ctags and replace tags file.
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
52
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
53 When run with C<CTAGS_HOOK>, it's assumed to be a BG process and we want to
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
54 wait for git to be done applying changes no matter how the hook is triggered.
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
55 Wait five seconds, then bail if we can't get a lock on the tags file or if it's
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
56 been updated within the last minute. Only then, run ctags and replace the tags
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
57 file.
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
58
Meredith Howard <mhoward@roomag.org>
parents: 440
diff changeset
59 =cut

mercurial