Skip to content

Commit b18a938

Browse files
authored
Merge pull request #5 from tommarshall/add-support-for-cached-option
Add support for cached option
2 parents bffc1ec + 423b05a commit b18a938

10 files changed

+68
-32
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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
# https://github.com/tommarshall/git-diff-ansible-vault
88
#
99

10-
VERSION=0.1.0
10+
VERSION=0.2.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/fixture/repo.git/git/index

-19 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/fixture/repo.git/public.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ fruits:
66
- Strawberry
77
- Mango
88
- Banana
9+
- Pineapple
910
- Peach

test/fixture/repo.git/vault.yml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
$ANSIBLE_VAULT;1.1;AES256
2-
34643163373064663534393164356661373631326239666236343634636264393465326330636433
3-
3562333137656266636330306366663634376439376437370a333430366636346434653331303830
4-
66306665316338663563666663373234613263663337636464303962643232633733656263633939
5-
6335376631333265320a303039303061613735326634396230336333393632386164653438316539
6-
39353736613264336166623965663236373565623736383936636133656131316562646236366463
7-
64303538363864336438656533393165313064643264313730316163623332323163613139306639
8-
38376364313332616137396532656135353962353939346339653333383936653439636335613865
9-
38336266306432326338396566653033653066666261646662383463663936356637373131663265
10-
37643530646265613734323136626433646466653932376162626635353438333463666261366564
11-
35343264396161323230613862626331636431316135636365313764666164346639333937613233
12-
30336333313536313037323433656466326333353133356137366661666232333830363534336133
13-
38376535316430343035393331666438393333666234346336303334623261323261326439313961
14-
31343832373634383030393766303334653937383163366332656436653565643365393638303161
15-
62656337393762316362323934373439363163613961623238323135353663636266323865616163
16-
38633531363664663430666435663339313932363363613135323131326162303463363164316530
17-
36616538353265383031646339623237393062333838643037346335353338313065323737383933
18-
3132
2+
66326137633863336537383736396238386230383731383937633936373264353339613939326532
3+
3736626665353833336330626464653631343536316135310a303464383230333866653432366539
4+
62363935303930383365656232383033343735656566366162333165346639626635316536643334
5+
6138313766376364390a313135393835316564636566353432336165383632363039626331613037
6+
63626265303939666239323036636131653332363763616437323562623462323833393833616136
7+
33373631323863363137373566356266646434366236376437303565656165396234616161323238
8+
31643339666263323564616136373065616331376435343466366339373766623031303139393234
9+
35366632303635313466636632313233643262623461663463333136346437616264636234353932
10+
36656464396534396364633437653835643333363961323139333463663836663864333563643837
11+
33316532333237303834393337646236663265633465393066653766393164646566363162666566
12+
37333734393963396666633137366164353665396636343337303365336539663936343638326664
13+
33366464643864343066306165633232623230323731323630303066613138646666343263363939
14+
38313238336135376432663164326365616638633665383663666664396131393733353731636666
15+
33366630303031653039336231653339623933363635333431643539613831316138343966386339
16+
34633636303965666138373264333934326139656563353638306135383135643865336536383365
17+
66333134353862613739653332643731666138346137663135306261613331373434356564656231
18+
3562

test/git-diff-ansible-vault.bats

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ teardown() {
1919
assert_success
2020
assert_output "$(cat <<EOF
2121
diff --git a/public.yml b/public.yml
22-
index 1f613c2..4e1e398 100644
22+
index 497da2b..bb68218 100644
2323
--- a/public.yml
2424
+++ b/public.yml
25-
@@ -6,3 +6,4 @@ fruits:
26-
- Strawberry
25+
@@ -7,3 +7,4 @@ fruits:
2726
- Mango
2827
- Banana
28+
- Pineapple
2929
+ - Peach
3030
diff --git a/vault.yml b/vault.yml
31-
index 5fd6cf8..6b45548 100644
31+
index ff6110d..f5b9bd7 100644
3232
--- a/vault.yml
3333
+++ b/vault.yml
34-
@@ -15,3 +15,4 @@
34+
@@ -16,3 +16,4 @@
3535
- lisp
3636
- fortran
3737
- erlang
@@ -74,10 +74,10 @@ EOF
7474
assert_success
7575
assert_output "$(cat <<EOF
7676
diff --git a/vault.yml b/vault.yml
77-
index 5fd6cf8..6b45548 100644
77+
index ff6110d..f5b9bd7 100644
7878
--- a/vault.yml
7979
+++ b/vault.yml
80-
@@ -15,3 +15,4 @@
80+
@@ -16,3 +16,4 @@
8181
- lisp
8282
- fortran
8383
- erlang
@@ -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
@@ -136,10 +166,10 @@ EOF
136166
assert_success
137167
assert_output "$(cat <<EOF
138168
diff --git a/vault.yml b/vault.yml
139-
index 5fd6cf8..6b45548 100644
169+
index ff6110d..f5b9bd7 100644
140170
--- a/vault.yml
141171
+++ b/vault.yml
142-
@@ -15,3 +15,4 @@
172+
@@ -16,3 +16,4 @@
143173
- lisp
144174
- fortran
145175
- erlang

0 commit comments

Comments
 (0)