Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New cmd to add file to crypt list (.gitattributes) #125

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/test_crypt.bats
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,43 @@ function check_repo_is_clean {
rm "$FILENAME"
}

@test "crypt: add file to crypt" {
# git add-crypt add file to gitattributes

# add file 1
git add-crypt foobar
run cat .gitattributes
echo "${lines[@]}"
[[ "$status" -eq 0 ]]
[[ "${#lines[@]}" = "2" ]]
[[ "${lines[1]}" = "foobar filter=crypt diff=crypt merge=crypt" ]]

# add pattern 2
git add-crypt config/*.json
run cat .gitattributes
[[ "$status" -eq 0 ]]
[[ "${#lines[@]}" = "3" ]]
[[ "${lines[2]}" = "config/*.json filter=crypt diff=crypt merge=crypt" ]]

# add pattern 2
git add-crypt pattern2
run cat .gitattributes
[[ "$status" -eq 0 ]]
[[ "${#lines[@]}" = "4" ]]
[[ "${lines[3]}" = "pattern2 filter=crypt diff=crypt merge=crypt" ]]

# test ignore adding duplicate pattern
git add-crypt pattern2
git add-crypt foobar
git add-crypt config/*.json
run cat .gitattributes
[[ "$status" -eq 0 ]]
[[ "${#lines[@]}" = "4" ]] # no new line added
[[ "${lines[1]}" = "foobar filter=crypt diff=crypt merge=crypt" ]]
[[ "${lines[2]}" = "config/*.json filter=crypt diff=crypt merge=crypt" ]]
[[ "${lines[3]}" = "pattern2 filter=crypt diff=crypt merge=crypt" ]]
}

@test "crypt: transcrypt --upgrade applies new merge driver" {
VERSION=$(../transcrypt -v | awk '{print $2}')

Expand Down
2 changes: 2 additions & 0 deletions tests/test_init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ SETUP_SKIP_INIT_TRANSCRYPT=1
[[ "$(git config --get merge.renormalize)" = "true" ]]

[[ "$(git config --get alias.ls-crypt)" = "!git -c core.quotePath=false ls-files | git -c core.quotePath=false check-attr --stdin filter | awk 'BEGIN { FS = \":\" }; /crypt$/{ print \$1 }'" ]]
# shellcheck disable=SC2016
[[ "$(git config --get alias.add-crypt)" = '!"$(git rev-parse --git-common-dir)"/crypt/transcrypt add' ]]
}

@test "init: show details for --display" {
Expand Down
36 changes: 22 additions & 14 deletions transcrypt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ realpath() {
}

# establish repository metadata and directory handling
# shellcheck disable=SC2155
gather_repo_metadata() {
# whether or not transcrypt is already configured
readonly CONFIGURED=$(git config --get --local transcrypt.version 2>/dev/null)
Expand Down Expand Up @@ -197,6 +198,15 @@ git_merge() {
cat "$5" | ./.git/crypt/clean "$5" >"$2"
}

git_add() {
gather_repo_metadata
for var in "$@"; do
line="$var filter=crypt diff=crypt merge=crypt"
grep -qxF "$line" "${GIT_ATTRIBUTES}" || echo "$line" >>"${GIT_ATTRIBUTES}"
sync
done
}

# verify that all requirements have been met
run_safety_checks() {
# validate that we're in a git repository
Expand Down Expand Up @@ -505,23 +515,15 @@ save_configuration() {
# this allows us to support multiple working trees via git-worktree
# ...but the --git-common-dir flag was only added in November 2014
# shellcheck disable=SC2016
git config filter.crypt.clean '"$(git rev-parse --git-common-dir)"/crypt/clean %f'
# shellcheck disable=SC2016
git config filter.crypt.smudge '"$(git rev-parse --git-common-dir)"/crypt/smudge'
# shellcheck disable=SC2016
git config diff.crypt.textconv '"$(git rev-parse --git-common-dir)"/crypt/textconv'
# shellcheck disable=SC2016
git config merge.crypt.driver '"$(git rev-parse --git-common-dir)"/crypt/merge %O %A %B %L %P'
git_dir='"$(git rev-parse --git-common-dir)"'
else
# shellcheck disable=SC2016
git config filter.crypt.clean '"$(git rev-parse --git-dir)"/crypt/clean %f'
# shellcheck disable=SC2016
git config filter.crypt.smudge '"$(git rev-parse --git-dir)"/crypt/smudge'
# shellcheck disable=SC2016
git config diff.crypt.textconv '"$(git rev-parse --git-dir)"/crypt/textconv'
# shellcheck disable=SC2016
git config merge.crypt.driver '"$(git rev-parse --git-dir)"/crypt/merge %O %A %B %L %P'
git_dir='"$(git rev-parse --git-dir)"'
fi
git config filter.crypt.clean "$git_dir"'/crypt/clean %f'
git config filter.crypt.smudge "$git_dir"'/crypt/smudge'
git config diff.crypt.textconv "$git_dir"'/crypt/textconv'
git config merge.crypt.driver "$git_dir"'/crypt/merge %O %A %B %L %P'
git config filter.crypt.required 'true'
git config diff.crypt.cachetextconv 'true'
git config diff.crypt.binary 'true'
Expand All @@ -530,6 +532,7 @@ save_configuration() {

# add a git alias for listing encrypted files
git config alias.ls-crypt "!git -c core.quotePath=false ls-files | git -c core.quotePath=false check-attr --stdin filter | awk 'BEGIN { FS = \":\" }; /crypt$/{ print \$1 }'"
git config alias.add-crypt "!$git_dir"'/crypt/transcrypt add'
}

# display the current configuration settings
Expand Down Expand Up @@ -1029,6 +1032,11 @@ while [[ "${1:-}" != '' ]]; do
git_merge "$@"
exit $?
;;
add)
shift
git_add "$@"
exit $?
;;
-c | --cipher)
cipher=$2
shift
Expand Down