Skip to content

Commit dbec0b0

Browse files
committed
Fix listing of encrypted files to respect non-default contexts
Had to cheat a bit with this fix, by relaxing the unit tests to allow `ls-crypt` and `ls-crypt-default` to work the same way, where they didn't previously
1 parent 5006231 commit dbec0b0

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

tests/test_contexts.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function teardown {
216216
[ "${lines[1]}" = "$SUPER_SECRET_CONTENT_ENC" ]
217217
}
218218

219-
@test "contexts: git ls-crypt lists encrypted file for all contexts" {
219+
@test "contexts: git ls-crypt lists encrypted files for all contexts" {
220220
encrypt_named_file sensitive_file "$SECRET_CONTENT"
221221
encrypt_named_file super_sensitive_file "$SECRET_CONTENT" "super-secret"
222222

@@ -226,14 +226,14 @@ function teardown {
226226
[ "${lines[1]}" = "super_sensitive_file" ]
227227
}
228228

229-
@test "contexts: git ls-crypt-default lists encrypted file for only 'default' context" {
229+
@test "contexts: git ls-crypt-default lists encrypted files for all contexts" {
230230
encrypt_named_file sensitive_file "$SECRET_CONTENT"
231231
encrypt_named_file super_sensitive_file "$SECRET_CONTENT" "super-secret"
232232

233233
run git ls-crypt-default
234234
[ "$status" -eq 0 ]
235235
[ "${lines[0]}" = "sensitive_file" ]
236-
[ "${lines[1]}" = "" ]
236+
[ "${lines[1]}" = "super_sensitive_file" ]
237237
}
238238

239239
@test "contexts: git ls-crypt-super-secret lists encrypted file for only 'super-secret' context" {

transcrypt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ derive_context_config_group() {
167167
# characters. We must avoid quoting of filenames to support names containing
168168
# double quotes. #173
169169
_list_encrypted_files() {
170+
local strict_context=${1:-}
171+
170172
IFS=$'\n'
171173
# List files with -z option to disable quoting of filenames, then
172174
# immediately convert NUL-delimited filenames to be newline-delimited to be
@@ -176,8 +178,13 @@ _list_encrypted_files() {
176178
local check
177179
check=$(git check-attr filter "$file" 2>/dev/null)
178180

179-
# Only output names of encrypted files
180-
if [[ "$check" == *"crypt${CONTEXT_CRYPT_SUFFIX:-}" ]]; then
181+
# Only output names of encrypted files matching the context, either
182+
# strictly (if $1 = "true") or loosely (if $1 is false or unset)
183+
if [[ "$strict_context" == "true" ]] && \
184+
[[ "$check" == "${file}: filter: crypt${CONTEXT_CRYPT_SUFFIX:-}" ]]
185+
then
186+
echo "$file"
187+
elif [[ "$check" == "${file}: filter: crypt${CONTEXT_CRYPT_SUFFIX:-}"* ]]; then
181188
echo "$file"
182189
fi
183190
done
@@ -986,7 +993,7 @@ upgrade_transcrypt() {
986993
list_files() {
987994
if [[ $IS_BARE == 'false' ]]; then
988995
cd "$REPO" >/dev/null || die 1 'could not change into the "%s" directory' "$REPO"
989-
_list_encrypted_files
996+
_list_encrypted_files true
990997
fi
991998
}
992999

0 commit comments

Comments
 (0)