Skip to content

Commit f5c2569

Browse files
committed
Add support for --cached and --staged
Because: * `git diff` supports `--cached` to allow you to view the changes you have staged for the next commit. This would also be useful for files encrypted with ansible vault. This change: * Adds `--cached` option (with `--staged` alias). Closes #2
1 parent 99d0d09 commit f5c2569

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ $ git diff-ansible-vault -h
3939
```
4040
-r, --revision <revision> show diff for git revision, e.g. master..some-branch
4141
-p, --path <path> restrict diff to path, e.g. support/config.yml
42+
--cached, --staged show diff for staged changes
4243
--vault-password-file <path> vault password file path, defaults to .vault-pass
4344
--vault-only restrict diff to vault files only
4445
--color, --colour turn on coloured output

bin/git-diff-ansible-vault

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
VERSION=0.1.0
1111
REVISION=
1212
PATH_SCOPE=
13+
GIT_DIFF_ARGS=
1314
VAULT_PASSWORD_FILE='./.vault-pass'
1415
VAULT_ONLY=0
1516
COLOR=
@@ -50,6 +51,7 @@ help() {
5051
Options:
5152
-r, --revision <revision> show diff for git revision, e.g. master..some-branch
5253
-p, --path <path> restrict diff to path, e.g. support/config.yml
54+
--cached, --staged show diff for staged changes
5355
--vault-password-file <path> vault password file path, defaults to .vault-pass
5456
--vault-only restrict diff to vault files only
5557
--color, --colour turn on coloured output
@@ -233,23 +235,24 @@ set_default_color() {
233235
git_diff_ansible_vault() {
234236
log "REVISION: $REVISION"
235237
log "PATH_SCOPE: $PATH_SCOPE"
238+
log "GIT_DIFF_ARGS: $GIT_DIFF_ARGS"
236239
log "VAULT_PASSWORD_FILE: $VAULT_PASSWORD_FILE"
237240
log "VAULT_ONLY: $VAULT_ONLY"
238241
log "COLOR: $COLOR"
239242

240-
for FILE_PATH in $($GIT_CMD diff --name-only $REVISION $PATH_SCOPE); do
243+
for FILE_PATH in $($GIT_CMD diff $GIT_DIFF_ARGS --name-only $REVISION $PATH_SCOPE); do
241244
if is_vault $FILE_PATH; then
242245
log "$FILE_PATH is vault"
243246
# extract the old revision encrypted file contents
244-
local old=$($GIT_CMD diff $REVISION $FILE_PATH \
247+
local old=$($GIT_CMD diff $GIT_DIFF_ARGS $REVISION $FILE_PATH \
245248
| grep -E '^-[^-]|\$ANSIBLE_VAULT' | sed -E 's/^([^- ]*)[- ]/\1/')
246249

247250
# extract the new revision encrypted file contents
248-
local new=$($GIT_CMD diff $REVISION $FILE_PATH \
251+
local new=$($GIT_CMD diff $GIT_DIFF_ARGS $REVISION $FILE_PATH \
249252
| grep -E '^\+[^\+]|\$ANSIBLE_VAULT' | sed -E 's/^([^+ ]*)[+ ]/\1/')
250253

251254
# print the diff heading from git diff so we get the real paths
252-
$GIT_CMD -c color.ui=$COLOR diff $REVISION $FILE_PATH | head -n 4
255+
$GIT_CMD -c color.ui=$COLOR diff $GIT_DIFF_ARGS $REVISION $FILE_PATH | head -n 4
253256

254257
# print the diff body from the opened vault diff
255258
diff -u \
@@ -258,7 +261,7 @@ git_diff_ansible_vault() {
258261
| tail -n +3 | colordiff_wrap
259262
elif [ $VAULT_ONLY -eq 0 ]; then
260263
log "$FILE_PATH is not vault"
261-
$GIT_CMD -c color.ui=$COLOR diff $REVISION $FILE_PATH
264+
$GIT_CMD -c color.ui=$COLOR diff $GIT_DIFF_ARGS $REVISION $FILE_PATH
262265
fi
263266
done
264267
}
@@ -272,6 +275,7 @@ while test $# -ne 0; do
272275
case $ARG in
273276
-r|--revision) REVISION=$1; shift ;;
274277
-p|--path) PATH_SCOPE=$1; shift ;;
278+
--cached|--staged) GIT_DIFF_ARGS="$GIT_DIFF_ARGS --cached" ;;
275279
--vault-password-file) set_vault_password_file_path $1; shift ;;
276280
--vault-only) VAULT_ONLY=1 ;;
277281
--color|--colour) ensure_colordiff; COLOR='always' ;;

test/git-diff-ansible-vault.bats

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,36 @@ EOF
106106
)"
107107
}
108108

109+
@test "with --staged shows all staged changes" {
110+
run git diff-ansible-vault --staged
111+
assert_success
112+
echo "$output" > "$BATS_TEST_DIRNAME/../output.log"
113+
assert_output "$(cat <<EOF
114+
diff --git a/public.yml b/public.yml
115+
index 1f613c2..497da2b 100644
116+
--- a/public.yml
117+
+++ b/public.yml
118+
@@ -6,3 +6,4 @@ fruits:
119+
- Strawberry
120+
- Mango
121+
- Banana
122+
+ - Pineapple
123+
diff --git a/vault.yml b/vault.yml
124+
index 5fd6cf8..ff6110d 100644
125+
--- a/vault.yml
126+
+++ b/vault.yml
127+
@@ -8,6 +8,7 @@
128+
- perl
129+
- pascal
130+
- ruby
131+
+ - go
132+
- tabitha:
133+
name: Tabitha Bitumen
134+
job: Developer
135+
EOF
136+
)"
137+
}
138+
109139
@test "without a git repository exits with an error" {
110140
cd /tmp && touch .vault-pass
111141
run git diff-ansible-vault

0 commit comments

Comments
 (0)