Skip to content

Commit 109df84

Browse files
committed
Merge branch 'find-contexts-across-multiple-attr-files'
* find-contexts-across-multiple-attr-files: Apply work-around that `shellcheck` doesn't like to fix `unbound variable` error in Bash < 4.4 (e.g. on macOS) Fix linting errors found by shellcheck on Linux run in GitHub, but not by old version 0.7.2 on macOS Find context definitions across all attributes config files, rather than from just one
2 parents a6225e3 + 78e1fa2 commit 109df84

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ system, you must also run the `--upgrade` command in each repository:
5858
dirty files are expected (#201)
5959
- Show useful error instead of `unbound variable` when --long option is missing
6060
a value, e.g. `transcrypt --context`
61+
- Find context definitions across all attributes config files: repo's
62+
_.gitattributes_; repo's _info/attributes_; path in `core.attributesFile`
6163

6264
## [2.3.1] - 2025-02-24
6365

transcrypt

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,24 +1163,44 @@ get_contexts_from_git_config() {
11631163

11641164
# Echo space-delimited list of context names defined in the .gitattributes file
11651165
get_contexts_from_git_attributes() {
1166-
if [[ -f $GIT_ATTRIBUTES ]]; then
1166+
IFS=$'\n'
1167+
1168+
# Find all .gitattributes files that apply to this git repository
1169+
POTENTIAL_GITATTRIBUTES_FILES=(
1170+
"${REPO}/.gitattributes"
1171+
"${GIT_DIR}/info/attributes"
1172+
"$(git config --get --local --path core.attributesFile 2>/dev/null || git config --get --path core.attributesFile 2>/dev/null || printf '')"
1173+
)
1174+
git_attributes_files=()
1175+
for attr_file in "${POTENTIAL_GITATTRIBUTES_FILES[@]}"; do
1176+
if [[ -f $attr_file ]]; then
1177+
git_attributes_files+=("$attr_file")
1178+
fi
1179+
done
1180+
1181+
# Find contexts defined across all .gitattributes files
1182+
contexts=()
1183+
# Must not quote variable using '[@]:-' or it will break for Bash < 4.4,
1184+
# see https://github.com/koalaman/shellcheck/issues/2387
1185+
# shellcheck disable=SC2068
1186+
for git_attributes_file in ${git_attributes_files[@]:-}; do
11671187
# Capture contents of .gitattributes without comments (leading # hash)
1168-
attr_lines=$(sed '/^.*#/d' <"$GIT_ATTRIBUTES")
1188+
attr_lines=$(sed '/^.*#/d' <"$git_attributes_file")
11691189
extract_context_name_regex="filter=crypt-(${CONTEXT_REGEX})"
11701190
recognise_crypt_regex="filter=crypt"
1171-
contexts=()
1172-
IFS=$'\n'
11731191
for attr_line in ${attr_lines}; do
11741192
if [[ $attr_line =~ $extract_context_name_regex ]]; then
11751193
contexts+=("${BASH_REMATCH[1]}")
11761194
elif [[ $attr_line =~ $recognise_crypt_regex ]]; then
11771195
contexts+=('default')
11781196
fi
11791197
done
1180-
unset IFS
1181-
if [[ "${contexts:-}" ]]; then
1182-
trim "$(printf "%q " "${contexts[@]}")"
1183-
fi
1198+
done
1199+
1200+
unset IFS
1201+
1202+
if [[ "${contexts:-}" ]]; then
1203+
trim "$(printf "%q " "${contexts[@]}")"
11841204
fi
11851205
}
11861206

0 commit comments

Comments
 (0)