Skip to content

Commit 6774dff

Browse files
committed
Add commands to more easily add file patterns to .gitattributes: transcrypt --add and git add-crypt #125
2 parents 9c4d950 + 2ab6f20 commit 6774dff

5 files changed

Lines changed: 93 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ system, you must also run the `--upgrade` command in each repository:
3434

3535
## [Unreleased]
3636

37+
### Added
38+
39+
- New commands make it easier to add file patterns to .gitattributes:
40+
`transcrypt --add` and `git add-crypt` (#125)
41+
3742
### Changed
3843

3944
- Improve check for incorrect password to avoid false report when transcrypt

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ config. If that pattern matches a file in your repository, the file will be
104104
transparently encrypted once you stage and commit it:
105105

106106
$ cd <path-to-your-repo>/
107-
$ echo 'sensitive_file filter=crypt diff=crypt merge=crypt' >> .gitattributes
107+
$ transcrypt --add sensitive_file
108108
$ git add .gitattributes sensitive_file
109109
$ git commit -m 'Add encrypted version of a sensitive file'
110110

@@ -210,6 +210,9 @@ directory.
210210
-y, --yes
211211
assume yes and accept defaults for non-specified options
212212

213+
--add, --add=pattern
214+
add a file pattern to encrypt to the .gitattributes file
215+
213216
-d, --display
214217
display the current repository's cipher and password
215218

@@ -326,8 +329,7 @@ to encrypt a file \_top-secret* in a "super" context:
326329
$ transcrypt --context=super
327330

328331
# Add a pattern to .gitattributes with "crypt-super" values
329-
$ echo >> .gitattributes \\
330-
'top-secret filter=crypt-super diff=crypt-super merge=crypt-super'
332+
$ transcrypt --context=super --add=top-secret
331333

332334
# Add and commit your top-secret and .gitattribute files
333335
$ git add .gitattributes top-secret

tests/test_crypt.bats

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,46 @@ SECRET_CONTENT_ENC="U2FsdGVkX1/6ilR0PmJpAyCF7iG3+k4aBwbgVd48WaQXznsg42nXbQrlWsf/
206206
rm "$FILENAME"
207207
}
208208

209+
@test "crypt: add file patterns to .gitattributes" {
210+
# git add-crypt add file to gitattributes
211+
212+
# add file 1 via `add-crypt`
213+
git add-crypt foobar
214+
run cat .gitattributes
215+
[[ "$status" -eq 0 ]]
216+
[[ "${#lines[@]}" = "2" ]]
217+
[[ "${lines[1]}" = "foobar filter=crypt diff=crypt merge=crypt" ]]
218+
219+
# add pattern 2 via `add-crypt`
220+
git add-crypt config/*.json
221+
run cat .gitattributes
222+
[[ "$status" -eq 0 ]]
223+
[[ "${#lines[@]}" = "3" ]]
224+
[[ "${lines[2]}" = "config/*.json filter=crypt diff=crypt merge=crypt" ]]
225+
226+
# add patterns 3 & 4 via `transcrypt --add`
227+
"$BATS_TEST_DIRNAME"/../transcrypt --add pattern2
228+
"$BATS_TEST_DIRNAME"/../transcrypt --add=*.secret
229+
run cat .gitattributes
230+
[[ "$status" -eq 0 ]]
231+
[[ "${#lines[@]}" = "5" ]]
232+
[[ "${lines[3]}" = "pattern2 filter=crypt diff=crypt merge=crypt" ]]
233+
[[ "${lines[4]}" = "*.secret filter=crypt diff=crypt merge=crypt" ]]
234+
235+
# test ignore adding duplicate pattern
236+
git add-crypt foobar
237+
git add-crypt config/*.json
238+
git add-crypt pattern2
239+
git add-crypt *.secret
240+
run cat .gitattributes
241+
[[ "$status" -eq 0 ]]
242+
[[ "${#lines[@]}" = "5" ]] # no new line added
243+
[[ "${lines[1]}" = "foobar filter=crypt diff=crypt merge=crypt" ]]
244+
[[ "${lines[2]}" = "config/*.json filter=crypt diff=crypt merge=crypt" ]]
245+
[[ "${lines[3]}" = "pattern2 filter=crypt diff=crypt merge=crypt" ]]
246+
[[ "${lines[4]}" = "*.secret filter=crypt diff=crypt merge=crypt" ]]
247+
}
248+
209249
@test "crypt: transcrypt --upgrade applies new merge driver" {
210250
VERSION=$("$BATS_TEST_DIRNAME"/../transcrypt -v | awk '{print $2}')
211251

tests/test_init.bats

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ SETUP_SKIP_INIT_TRANSCRYPT=1
5050
[ "$(git config --get merge.crypt.name)" = "Merge transcrypt secret files" ]
5151

5252
[ "$(git config --get alias.ls-crypt)" = '!"$(git config transcrypt.crypt-dir 2>/dev/null || printf %s/crypt ""$(git rev-parse --git-dir)"")"/transcrypt --list' ]
53+
54+
[ "$(git config --get alias.add-crypt)" = '!"$(git config transcrypt.crypt-dir 2>/dev/null || printf %s/crypt ""$(git rev-parse --git-dir)"")"/transcrypt --add' ]
5355
}
5456

5557
@test "init: show details for --display" {

transcrypt

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,15 @@ git_pre_commit() {
426426
unset IFS
427427
}
428428

429+
# Add file patterns to .gitattributes
430+
add_pattern() {
431+
for var in "$@"; do
432+
line="$var filter=crypt${CONTEXT_CRYPT_SUFFIX} diff=crypt${CONTEXT_CRYPT_SUFFIX} merge=crypt${CONTEXT_CRYPT_SUFFIX}"
433+
grep -qxF "$line" "${GIT_ATTRIBUTES}" || echo "$line" >>"${GIT_ATTRIBUTES}"
434+
sync
435+
done
436+
}
437+
429438
# verify that all requirements have been met
430439
run_safety_checks() {
431440
# validate that we're in a git repository
@@ -722,6 +731,9 @@ save_configuration() {
722731
# List files with gitattribute 'filter=crypt-<CONTEXT>'
723732
git config "alias.ls-crypt-${CONTEXT}" "!$transcrypt_path --context=${CONTEXT} --list"
724733
fi
734+
735+
# Add git alias `add-crypt` to add file pattern to .gitattributes
736+
git config alias.add-crypt "!$transcrypt_path --add"
725737
}
726738

727739
# display the current configuration settings
@@ -895,6 +907,9 @@ uninstall_transcrypt() {
895907
fi
896908
[[ -f "$pre_commit_hook_installed" ]] && rm "$pre_commit_hook_installed"
897909

910+
# remove the `git add-crypt` alias.
911+
git config --unset alias.add-crypt 2>/dev/null || true
912+
898913
# remove context settings: cipher & password config, ls-crypt alias variant,
899914
# crypt filter/diff/merge attributes. We do it here instead of `clean_gitconfig`
900915
# to avoid interfering with flushing of credentials
@@ -1263,6 +1278,9 @@ help() {
12631278
-d, --display
12641279
display the current repository's cipher and password
12651280
1281+
--add, --add=pattern
1282+
add a file pattern to encrypt to the .gitattributes file
1283+
12661284
-r, --rekey
12671285
re-encrypt all encrypted files using new credentials
12681286
@@ -1328,8 +1346,10 @@ help() {
13281346
matches a file in your repository, the file will be transparently
13291347
encrypted once you stage and commit it:
13301348
1331-
$ echo >> .gitattributes \\
1332-
'sensitive_file filter=crypt diff=crypt merge=crypt'
1349+
$ transcrypt --add sensitive_file
1350+
1351+
$ cat .gitattributes
1352+
sensitive_file filter=crypt diff=crypt merge=crypt
13331353
13341354
$ git add .gitattributes sensitive_file
13351355
$ git commit -m 'Add encrypted version of a sensitive file'
@@ -1360,8 +1380,10 @@ help() {
13601380
$ transcrypt --context=super
13611381
13621382
# Add a pattern to .gitattributes with "crypt-super" values
1363-
$ echo >> .gitattributes \\
1364-
'top-secret filter=crypt-super diff=crypt-super merge=crypt-super'
1383+
$ transcrypt --context=super --add=top-secret
1384+
1385+
$ cat .gitattributes
1386+
top-secret filter=crypt-super diff=crypt-super merge=crypt-super
13651387
13661388
# Add and commit your top-secret and .gitattribute files
13671389
$ git add .gitattributes top-secret
@@ -1390,6 +1412,7 @@ help() {
13901412
context=''
13911413
cipher=''
13921414
display_config=''
1415+
add_pattern=''
13931416
list_contexts_command=''
13941417
flush_creds=''
13951418
gpg_import_file=''
@@ -1451,6 +1474,17 @@ while [[ "${1:-}" != '' ]]; do
14511474
password=${1#*=}
14521475
[[ $password ]] || die 1 'empty password'
14531476
;;
1477+
--add)
1478+
add_pattern=$2
1479+
[[ $add_pattern ]] || die 1 'empty pattern'
1480+
requires_clean_repo=''
1481+
shift
1482+
;;
1483+
--add=*)
1484+
add_pattern=${1#*=}
1485+
[[ $add_pattern ]] || die 1 'empty pattern'
1486+
requires_clean_repo=''
1487+
;;
14541488
-C | --context)
14551489
context=$2
14561490
shift
@@ -1583,6 +1617,9 @@ if [[ $list ]]; then
15831617
elif [[ $uninstall ]]; then
15841618
uninstall_transcrypt
15851619
exit 0
1620+
elif [[ $add_pattern ]]; then
1621+
add_pattern "$add_pattern"
1622+
exit 0
15861623
elif [[ $upgrade ]]; then
15871624
upgrade_transcrypt
15881625
exit 0

0 commit comments

Comments
 (0)