Skip to content

Commit

Permalink
(#7) Refactor Worktree Selector to use Git's built-in commands
Browse files Browse the repository at this point in the history
Merge pull request #7 from lu0/refactor-worktree-selector
  • Loading branch information
lu0 committed Feb 18, 2024
2 parents 23a4d36 + 0571aad commit 8398a2f
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions select-airflow-worktree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,25 @@ select-airflow-worktree() {
/usr/bin/env git "$@"
}

# $GIT_DIR is not the root directory of a bare repository.
# Next command extracts it on both "normal" and bare repositories.
# DOES NOT SUPPORT SPACES IN NAME OF BRANCHES/WORKTREES/DIRECTORIES.
git_dir=$(head -n 1 < <(git worktree list) | cut -d" " -f1)

# Returns true or false
is_bare_repo=$(git config --get core.bare)

if [ "${was_branch_checkout}" = 1 ]; then

if [ "${is_bare_repo}" == false ]; then
info "Not a bare repo, skipping airflow-worktree"
else
# Information of the worktree we are in
worktree_info=$(git worktree list | grep "$PWD " | xargs)

# DOES NOT SUPPORT SPACES IN NAME OF BRANCHES/WORKTREES/DIRECTORIES.
worktree_abs_path=$(echo "${worktree_info}" | cut -d" " -f1)

# Path to the workspace relative to the repository's root directory
worktree_rel_path="${worktree_abs_path##"${git_dir}"/}"

# Ignore all directories except root and worktree
/usr/bin/env find "${git_dir}" -maxdepth 1 -type d \
-not -path "${git_dir}" \
-not -path "${worktree_abs_path}" \
-printf '^%P/\n' | sort \
> "${git_dir}/.airflowignore"
git_root_dir=$(git rev-parse --git-common-dir)
worktree_abs_path=$(git rev-parse --show-toplevel)
worktree_rel_path="${worktree_abs_path##"${git_root_dir}"/}"

(
git worktree list |
awk '{print $1}' | # Use the first column (worktree paths)
grep -vE "^${git_root_dir}$" | # Exclude the git's root dir
grep -vE "^${worktree_abs_path}$" | # Exclude the current worktree
sed "s|${git_root_dir}/||g" | # Convert worktrees to paths relative to the git's root dir
sed 's/.*/^&\//' # Append ^ at start and / at end to ignore directory matches
) > "${git_root_dir}/.airflowignore"

info ".airflowignore updated to load DAGs from ${worktree_rel_path}"
fi
Expand Down

0 comments on commit 8398a2f

Please sign in to comment.