Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain Tim Pope's Effortless Ctags with Git

Tags:

git

unix

ctags

Just installing Tim Pope's Effortless Ctags with Git here. It's a bit light on explanation. I get the general idea, but could someone explicate the details? Specifically:

trap 'rm -f "$dir/$$.tags"' EXIT 

Why remove this file via a trap, rather than just every time the script is run?

git ls-files | \
    ctags --tag-relative -L - -f"$dir/$$.tags" --languages=-javascript,sql
mv "$dir/$$.tags" "$dir/tags"

Why put the tags file into a temporary variable $$.tags and then move it to tags on the next line?

like image 628
Sasgorilla Avatar asked Jan 29 '26 06:01

Sasgorilla


1 Answers

The answers are all in the comments. I hope it's OK to restate those comments here in an answer, just so that this question may be able to be marked as "answered."

Why remove this file via a trap, rather than just every time the script is run?

I think Evan is right about this. Note that Mr. Pope is using set -e, so the script will exit immediately if git or mv fail. If he just put the rm at the end of the script instead of in a trap, you might end up littering *.tags files around if any commands fail before the rm is reached. I like this use of trap ... EXIT for cleaning up temporary files.

Why put the tags file into a temporary variable $$.tags and then move it to tags on the next line?

And here I think Mr. Thompson is correct: You probably wouldn't want your editor/IDE to try and use a half-written tags file. Better to let the potentially lengthy rebuild of tags complete and move the complete database into place in one atomic move. (Please no comments about the atomicity of rename(2)!)

Also, what if you checkout one branch, then checkout another before the first ctags command has completed? While his scripts don't look to prevent multiple firings of ctags into the background, writing to $$.tags will at least ensure that the last ctags to complete will result in a complete (if not reasonable) tags file. Contrast this with multiple ctags processes all writing to a single tags file simultaneously, which I bet could leave you with an invalid database.

like image 78
Dale Avatar answered Jan 30 '26 20:01

Dale