From 846bbc3ff7c0ad1cb06f37150b09157ca5f08bb6 Mon Sep 17 00:00:00 2001 From: James Murty Date: Sun, 25 May 2025 23:35:04 +1000 Subject: [PATCH 1/3] Find context definitions across all attributes config files, rather than from just one --- CHANGELOG.md | 2 ++ transcrypt | 33 +++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d71397..07d98c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/transcrypt b/transcrypt index 8280058..98ba41b 100755 --- a/transcrypt +++ b/transcrypt @@ -1159,13 +1159,28 @@ 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=() + 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]}") @@ -1173,10 +1188,12 @@ get_contexts_from_git_attributes() { 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 } From 177ff43ce82ca22db5abca84bbe3fe96092d1b70 Mon Sep 17 00:00:00 2001 From: James Murty Date: Sun, 25 May 2025 23:58:02 +1000 Subject: [PATCH 2/3] Fix linting errors found by shellcheck on Linux run in GitHub, but not by old version 0.7.2 on macOS --- transcrypt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/transcrypt b/transcrypt index 98ba41b..fcd1e93 100755 --- a/transcrypt +++ b/transcrypt @@ -1168,7 +1168,7 @@ get_contexts_from_git_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 + for attr_file in "${POTENTIAL_GITATTRIBUTES_FILES[@]}"; do if [[ -f $attr_file ]]; then git_attributes_files+=("$attr_file") fi @@ -1176,7 +1176,7 @@ get_contexts_from_git_attributes() { # Find contexts defined across all .gitattributes files contexts=() - for git_attributes_file in ${git_attributes_files[*]}; do + for git_attributes_file in "${git_attributes_files[@]}"; do # Capture contents of .gitattributes without comments (leading # hash) attr_lines=$(sed '/^.*#/d' <"$git_attributes_file") extract_context_name_regex="filter=crypt-(${CONTEXT_REGEX})" From 78e1fa215c4a42a6288337b42d0751ac2de82c5d Mon Sep 17 00:00:00 2001 From: James Murty Date: Mon, 26 May 2025 00:43:21 +1000 Subject: [PATCH 3/3] Apply work-around that `shellcheck` doesn't like to fix `unbound variable` error in Bash < 4.4 (e.g. on macOS) --- transcrypt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/transcrypt b/transcrypt index fcd1e93..4a0a414 100755 --- a/transcrypt +++ b/transcrypt @@ -1176,7 +1176,10 @@ get_contexts_from_git_attributes() { # Find contexts defined across all .gitattributes files contexts=() - for git_attributes_file in "${git_attributes_files[@]}"; do + # 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_file") extract_context_name_regex="filter=crypt-(${CONTEXT_REGEX})"