@@ -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
430439run_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() {
13901412context=' '
13911413cipher=' '
13921414display_config=' '
1415+ add_pattern=' '
13931416list_contexts_command=' '
13941417flush_creds=' '
13951418gpg_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
15831617elif [[ $uninstall ]]; then
15841618 uninstall_transcrypt
15851619 exit 0
1620+ elif [[ $add_pattern ]]; then
1621+ add_pattern " $add_pattern "
1622+ exit 0
15861623elif [[ $upgrade ]]; then
15871624 upgrade_transcrypt
15881625 exit 0
0 commit comments