Generating tags for git repositories (mytags)

shell
Author

Arumoy Shome

Published

November 17, 2024

Abstract

mytags is a wrapper around ctags which respects your gitignore files.

Often while working on remote servers where I don’t have permissions to install language servers, I find ctags to be an effective tool to navigate code. mytags is a simple Bash script I wrote which wraps around ctags with some added functionality.

Here is the script as of 2024-11-27, you can find the latest version in my dotfiles repo.

mytags
#!/usr/bin/env bash

__is_git_repo() {
  git rev-parse &>/dev/null
}

__git_toplevel() {
  git rev-parse --show-toplevel
}

__ctags() {
  if [[ -x "$(command -v fd)" ]]
  then
    command ctags "$@" $(fd --type f --hidden --exclude '.git')
  else
    command ctags "$@" $(git ls-files --exclude-standard)
  fi
}

if ! __is_git_repo
then
  echo "mytags: not inside git repo."
  exit 1
fi

(
  cd $(__git_toplevel) &&
  __ctags "$@"
)
1
mytags check that we are inside a git repo, if not exit gracefully.
2
When inside a git repo, start a new subshell and…
3
cd to the top-level of the repo (i.e. where the .git folder is located), this allows us to run mytags from any subdirectory inside the git repo
4
execute ctags with relevant files, either using fd if it exists or git ls-files
5
any additional arguments are passed along to ctags
Back to top