Skip to content

Find context definitions across all attributes config files, rather than from just one #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ system, you must also run the `--upgrade` command in each repository:
dirty files are expected (#201)
- Show useful error instead of `unbound variable` when --long option is missing
a value, e.g. `transcrypt --context`
- Find context definitions across all attributes config files: repo's
_.gitattributes_; repo's _info/attributes_; path in `core.attributesFile`

## [2.3.1] - 2025-02-24

Expand Down
36 changes: 28 additions & 8 deletions transcrypt
Original file line number Diff line number Diff line change
Expand Up @@ -1159,24 +1159,44 @@ get_contexts_from_git_config() {

# Echo space-delimited list of context names defined in the .gitattributes file
get_contexts_from_git_attributes() {
if [[ -f $GIT_ATTRIBUTES ]]; then
IFS=$'\n'

# Find all .gitattributes files that apply to this git repository
POTENTIAL_GITATTRIBUTES_FILES=(
"${REPO}/.gitattributes"
"${GIT_DIR}/info/attributes"
"$(git config --get --local --path core.attributesFile 2>/dev/null || git config --get --path core.attributesFile 2>/dev/null || printf '')"
)
git_attributes_files=()
for attr_file in "${POTENTIAL_GITATTRIBUTES_FILES[@]}"; do
if [[ -f $attr_file ]]; then
git_attributes_files+=("$attr_file")
fi
done

# Find contexts defined across all .gitattributes files
contexts=()
# Must not quote variable using '[@]:-' or it will break for Bash < 4.4,
# see https://github.com/koalaman/shellcheck/issues/2387
# shellcheck disable=SC2068
for git_attributes_file in ${git_attributes_files[@]:-}; do
# Capture contents of .gitattributes without comments (leading # hash)
attr_lines=$(sed '/^.*#/d' <"$GIT_ATTRIBUTES")
attr_lines=$(sed '/^.*#/d' <"$git_attributes_file")
extract_context_name_regex="filter=crypt-(${CONTEXT_REGEX})"
recognise_crypt_regex="filter=crypt"
contexts=()
IFS=$'\n'
for attr_line in ${attr_lines}; do
if [[ $attr_line =~ $extract_context_name_regex ]]; then
contexts+=("${BASH_REMATCH[1]}")
elif [[ $attr_line =~ $recognise_crypt_regex ]]; then
contexts+=('default')
fi
done
unset IFS
if [[ "${contexts:-}" ]]; then
trim "$(printf "%q " "${contexts[@]}")"
fi
done

unset IFS

if [[ "${contexts:-}" ]]; then
trim "$(printf "%q " "${contexts[@]}")"
fi
}

Expand Down