Skip to content

Commit

Permalink
Support pre-commit framework (solves #1).
Browse files Browse the repository at this point in the history
  • Loading branch information
lu0 committed May 13, 2022
1 parent d00d817 commit 09c1312
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 30 deletions.
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repos:
- repo: https://github.com/koalaman/shellcheck-precommit
rev: v0.8.0
hooks:
- id: shellcheck
args: ["--severity=style", "--external-sources", "--check-sourced"]

# Showcase for this repo's hook
- repo: https://github.com/lu0/git-worktree-airflow
rev: v1.1.0
hooks:
- id: airflow-worktree
name: Update .airflowignore to load DAGs from worktree
stages: [post-checkout]
always_run: true
verbose: true
5 changes: 5 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- id: airflow-worktree
name: Update .airflowignore
entry: select-airflow-worktree.sh
language: script
description: "Update .airflowignore to load DAGs from checkedout worktree"
65 changes: 43 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,54 @@ only the DAGs contained in this directory**.
Table of Contents
---
- [Installation](#installation)
- [Using `pre-commit`](#using-pre-commit)
- [Manual](#manual)
- [Usage](#usage)
- [Option 1: Using `git-worktree-wrapper`](#option-1-using-git-worktree-wrapper)
- [Option 2: Using vanilla `git`](#option-2-using-vanilla-git)
- [Recommended option: Using `git-worktree-wrapper`](#recommended-option-using-git-worktree-wrapper)
- [Alternative option: Using vanilla `git`](#alternative-option-using-vanilla-git)
- [Examples](#examples)

# Installation

- Copy or link the `post-checkout.sh` script into the `hooks` directory of your
`dags_folder` (which should be a bare repository), and rename it to
`post-checkout`.
## Using [`pre-commit`](https://pre-commit.com)

Activate this post-checkout hook by adding it to your `.pre-commit-config.yaml`:

```sh
repos:
- repo: https://github.com/lu0/git-worktree-airflow
rev: v1.1.0
hooks:
- id: airflow-worktree
name: Update .airflowignore to load DAGs from worktree
stages: [post-checkout]
always_run: true
verbose: true
```

And installing it:

```sh
pre-commit install --hook-type post-checkout
```

## Manual

- Copy or link the script `select-airflow-worktree.sh` into the `hooks`
directory of your `dags_folder` (which should be a bare repository), and rename
it to `post-checkout`.

- Example:

<pre><code>ln -srf post-checkout.sh <b>/path/to/your/dags_folder</b>/hooks/post-checkout</pre></code>
<pre><code>ln -srf select-airflow-worktree.sh <b>/path/to/your/dags_folder</b>/hooks/post-checkout</pre></code>

*Note*: Make the script executable with `chmod +x post-checkout` if you
***copied** the script instead of linking it.


# Usage

## Option 1: Using [`git-worktree-wrapper`](https://github.com/lu0/git-worktree-wrapper)
## Recommended option: Using [`git-worktree-wrapper`](https://github.com/lu0/git-worktree-wrapper)

- Install [`git-worktree-wrapper`](https://github.com/lu0/git-worktree-wrapper).

Expand All @@ -44,12 +70,10 @@ Table of Contents
```sh
$ git checkout <tree-ish>

post-checkout hook:
.airflowignore updated to load DAGs from <tree-ish>
.airflowignore updated to load DAGs from <tree-ish>
```

## Option 2: Using vanilla `git`

## Alternative option: Using vanilla `git`

1. First ***cd*** into the worktree directory of a tree-ish
```language
Expand All @@ -62,8 +86,7 @@ Table of Contents
```sh
$ git checkout <tree-ish>
post-checkout hook:
.airflowignore updated to load DAGs from <tree-ish>
.airflowignore updated to load DAGs from <tree-ish>
```

## Examples
Expand Down Expand Up @@ -98,29 +121,27 @@ Let's say we have a `dags` folder pointing to a bare repository in `~/dags` with
And you want the Airflow UI to show the DAGs contained in the `feature/dag_4` worktree.
- If you are using
[`git-worktree-wrapper`](https://github.com/lu0/git-worktree-wrapper), just
checkout into the tree-ish. You can checkout from any nested worktree.
[`git-worktree-wrapper`](https://github.com/lu0/git-worktree-wrapper) and
[`pre-commit`](#using-pre-commit), just checkout into the tree-ish. You can
checkout from any nested worktree.
```sh
$ git checkout feature/dag_4
post-checkout hook:
.airflowignore updated to load DAGs from feature/dag_4
```
![hook showcase to feature/dag_4](assets/hook-showcase-0.png)
- If you are using vanilla `git`:
```language
$ cd ~/dags
$ cd feature/dag_4
$ git checkout feature/dag4
$ git checkout feature/dag_4
post-checkout hook:
.airflowignore updated to load DAGs from feature/dag_4
.airflowignore updated to load DAGs from feature/dag_4
```
Either way, the post-checkout hook will create an `.airflowignore` file with the following contents:
Either way, the post-checkout hook will create a file named `.airflowignore` with the following contents:
*Note: Directories and files common to bare repositories are hidden.*
Expand Down
Binary file added assets/hook-showcase-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 13 additions & 8 deletions post-checkout.sh → select-airflow-worktree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
# (changing branches, flag=1).
#

main() {
was_branch_checkout="$3"
set -euo pipefail

select-airflow-worktree() {
# Defaults to argument passed by pre-commit framework if
# not triggered by vanilla git
was_branch_checkout="${3:-${PRE_COMMIT_CHECKOUT_TYPE}}"

# Local override of aliases that might exist for `git`
git() {
Expand All @@ -32,29 +36,30 @@ main() {
if [ "${was_branch_checkout}" = 1 ]; then

if [ "${is_bare_repo}" == false ]; then
info "Not a bare repo, ignoring Airflow hook."
info "Not a bare repo, skipping airflow-worktree"
else
# Informationn 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)
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}/}"
worktree_rel_path="${worktree_abs_path##"${git_dir}"/}"

# Ignore everything but the worktree directory
/usr/bin/env ls -AI "${worktree_rel_path}" "${git_dir}" > "${git_dir}/.airflowignore"

info ".airflowignore updated to load DAGs from ${worktree_rel_path}"
fi
else
info "Not a branch checkout, skipping airflow-worktree."
fi

return 0
}

info() {
echo -e >&2 "\npost-checkout hook:\n\t${1}\n"
echo -e >&2 "\t${1}\n"
}

main "$@"
select-airflow-worktree "$@"

0 comments on commit 09c1312

Please sign in to comment.