Skip to content

Commit 87e8fb2

Browse files
authored
Merge pull request #6 from tommarshall/v0.3.0-wip
v0.3.0
2 parents b18a938 + 90900c4 commit 87e8fb2

File tree

6 files changed

+200
-5
lines changed

6 files changed

+200
-5
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ PREFIX ?= /usr/local
22

33
install: bin/git-diff-ansible-vault
44
@cp -p $< $(PREFIX)/$<
5+
@cp -p man/git-diff-ansible-vault.1 $(PREFIX)/share/man/man1/
56

67
uninstall:
78
@rm -f $(PREFIX)/bin/git-diff-ansible-vault
9+
@rm -f $(PREFIX)/share/man/man1/git-diff-ansible-vault.1
810

911
setup:
1012
@rm -rf vendor
@@ -14,4 +16,7 @@ setup:
1416
test:
1517
vendor/bats/bin/bats test
1618

17-
.PHONY: install uninstall setup test
19+
man: man/git-diff-ansible-vault.1.ronn
20+
ronn -r --style=80c $<
21+
22+
.PHONY: install uninstall setup test man

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ $ git diff-ansible-vault -h
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
4242
--cached, --staged show diff for staged changes
43+
--no-pager do not pipe output into a pager
4344
--vault-password-file <path> vault password file path, defaults to .vault-pass
4445
--vault-only restrict diff to vault files only
4546
--color, --colour turn on coloured output

bin/git-diff-ansible-vault

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
# https://github.com/tommarshall/git-diff-ansible-vault
88
#
99

10-
VERSION=0.2.0
10+
VERSION=0.3.0
1111
REVISION=
1212
PATH_SCOPE=
1313
GIT_DIFF_ARGS=
14+
NO_PAGER=0
1415
VAULT_PASSWORD_FILE='./.vault-pass'
1516
VAULT_ONLY=0
1617
COLOR=
1718
VERBOSE=0
1819
GIT_CMD='git --no-pager'
20+
PAGER_CMD=
1921

2022
#
2123
# Output version.
@@ -52,6 +54,7 @@ help() {
5254
-r, --revision <revision> show diff for git revision, e.g. master..some-branch
5355
-p, --path <path> restrict diff to path, e.g. support/config.yml
5456
--cached, --staged show diff for staged changes
57+
--no-pager do not pipe output into a pager
5558
--vault-password-file <path> vault password file path, defaults to .vault-pass
5659
--vault-only restrict diff to vault files only
5760
--color, --colour turn on coloured output
@@ -177,6 +180,33 @@ decrypted_vault_contents() {
177180
test $? -eq 0 || warn "Unable to open vault: $FILE_PATH"
178181
}
179182

183+
#
184+
# Check if output is paged.
185+
#
186+
187+
is_paged() {
188+
test -n "$PAGER_CMD" && [[ "$PAGER_CMD" != 'cat'* ]]
189+
}
190+
191+
#
192+
# Set the default pager command and arguments.
193+
#
194+
195+
set_default_pager() {
196+
test $NO_PAGER -eq 1 && return 0;
197+
# ref: https://github.com/git/git/blob/v2.9.2/pager.c#L43-L74
198+
local default_pager=$GIT_PAGER
199+
test -n "$default_pager" || default_pager=$(git config --global pager.diff)
200+
test -n "$default_pager" || default_pager=$(git config --get core.pager)
201+
test -n "$default_pager" || default_pager=$PAGER
202+
case $default_pager in
203+
'') PAGER_CMD='less -FRX' ;;
204+
less*) PAGER_CMD="$default_pager -FRX" ;;
205+
lv*) PAGER_CMD="$default_pager -c" ;;
206+
*) PAGER_CMD="$default_pager" ;;
207+
esac
208+
}
209+
180210
#
181211
# Ensure the `colordiff` binary is available.
182212
#
@@ -226,6 +256,7 @@ set_default_color() {
226256
COLOR='never'
227257
fi
228258
fi
259+
log "DEFAULT_COLOR: $COLOR"
229260
}
230261

231262
#
@@ -236,6 +267,7 @@ git_diff_ansible_vault() {
236267
log "REVISION: $REVISION"
237268
log "PATH_SCOPE: $PATH_SCOPE"
238269
log "GIT_DIFF_ARGS: $GIT_DIFF_ARGS"
270+
log "PAGER_CMD: $PAGER_CMD"
239271
log "VAULT_PASSWORD_FILE: $VAULT_PASSWORD_FILE"
240272
log "VAULT_ONLY: $VAULT_ONLY"
241273
log "COLOR: $COLOR"
@@ -252,7 +284,7 @@ git_diff_ansible_vault() {
252284
| grep -E '^\+[^\+]|\$ANSIBLE_VAULT' | sed -E 's/^([^+ ]*)[+ ]/\1/')
253285

254286
# print the diff heading from git diff so we get the real paths
255-
$GIT_CMD -c color.ui=$COLOR diff $GIT_DIFF_ARGS $REVISION $FILE_PATH | head -n 4
287+
echo "$($GIT_CMD -c color.ui=$COLOR diff $GIT_DIFF_ARGS $REVISION $FILE_PATH)" | head -n 4
256288

257289
# print the diff body from the opened vault diff
258290
diff -u \
@@ -276,6 +308,7 @@ while test $# -ne 0; do
276308
-r|--revision) REVISION=$1; shift ;;
277309
-p|--path) PATH_SCOPE=$1; shift ;;
278310
--cached|--staged) GIT_DIFF_ARGS="$GIT_DIFF_ARGS --cached" ;;
311+
--no-pager) NO_PAGER=1 ;;
279312
--vault-password-file) set_vault_password_file_path $1; shift ;;
280313
--vault-only) VAULT_ONLY=1 ;;
281314
--color|--colour) ensure_colordiff; COLOR='always' ;;
@@ -301,8 +334,15 @@ ensure_git_repository
301334

302335
ensure_vault_password
303336

337+
set_default_pager
338+
304339
set_default_color
305340

306-
git_diff_ansible_vault
341+
if is_paged && [ -t 1 ]; then
342+
test "$COLOR" == 'auto' && COLOR='always'
343+
git_diff_ansible_vault | eval "$PAGER_CMD"
344+
else
345+
git_diff_ansible_vault
346+
fi
307347

308348
delete_tmp_vault_password_file

man/git-diff-ansible-vault.1

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
.\" generated with Ronn/v0.7.3
2+
.\" http://github.com/rtomayko/ronn/tree/0.7.3
3+
.
4+
.TH "GIT\-DIFF\-ANSIBLE\-VAULT" "1" "August 2016" "" ""
5+
.
6+
.SH "NAME"
7+
\fBgit\-diff\-ansible\-vault\fR \- Diff ansible vault encrypted files in git history
8+
.
9+
.SH "SYNOPSIS"
10+
git diff\-ansible\-vault [\-r \fIrevision\fR] \fI\-p \fIpath\fR\fR
11+
.
12+
.P
13+
\fIrevision\fR is the git revision to diff\. \fIpath\fR is the file system path to scope the diff to\.
14+
.
15+
.SH "DESCRIPTION"
16+
Running \fBgit diff\fR on files encrypted with ansible vault results in some pretty unhelpful output\.
17+
.
18+
.P
19+
\fBgit diff\-ansible\-vault\fR is custom git command that detects files encrypted with ansible vault within the diff, and safely decrypts them to reveal the true changes\.
20+
.
21+
.SH "EXAMPLES"
22+
.
23+
.nf
24+
25+
$ git diff\-ansible\-vault
26+
27+
$ git diff\-ansible\-vault \-r master\.\.some\-branch \-p path/to/dir
28+
29+
$ git diff\-ansible\-vault \-\-vault\-password\-file \.vaultpass
30+
.
31+
.fi
32+
.
33+
.SH "OPTIONS"
34+
.
35+
.TP
36+
\fB\-r\fR, \fB\-\-revision <revision>\fR
37+
Show diff for git revision, e\.g\. master\.\.some\-branch
38+
.
39+
.TP
40+
\fB\-p\fR, \fB\-\-path <path>\fR
41+
Restrict diff to path, e\.g\. support/config\.yml
42+
.
43+
.TP
44+
\fB\-\-cached\fR, \fB\-\-staged\fR
45+
Show diff for staged changes
46+
.
47+
.TP
48+
\fB\-\-no\-pager\fR
49+
Do not pipe output into a pager
50+
.
51+
.TP
52+
\fB\-\-vault\-password\-file <path>\fR
53+
Vault password file path, defaults to \.vault\-pass
54+
.
55+
.TP
56+
\fB\-\-vault\-only\fR
57+
Restrict diff to vault files only
58+
.
59+
.TP
60+
\fB\-\-color\fR, \fB\-\-colour\fR
61+
Turn on coloured output
62+
.
63+
.TP
64+
\fB\-\-no\-color\fR, \fB\-\-no\-colour\fR
65+
Turn off coloured diff
66+
.
67+
.TP
68+
\fB\-\-verbose\fR
69+
Display verbose output
70+
.
71+
.TP
72+
\fB\-v\fR, \fB\-\-version\fR
73+
Output program version
74+
.
75+
.TP
76+
\fB\-h\fR, \fB\-\-help\fR
77+
Output help information
78+
.
79+
.SH "EXIT STATUS"
80+
\fBgit diff\-ansible\-vault\fR exits with a value of \fB0\fR if the diff is able to complete without error, and \fB1\fR if an error occurs\.
81+
.
82+
.SH "COPYRIGHT"
83+
Git Diff Ansible Vault is copyright (c) 2016, Tom Marshall\. Released under the MIT license\.
84+
.
85+
.SH "SEE ALSO"
86+
\fBgit\-diff\fR(1)

man/git-diff-ansible-vault.1.ronn

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# git-diff-ansible-vault(1) -- Diff ansible vault encrypted files in git history
2+
3+
## SYNOPSIS
4+
5+
git diff-ansible-vault [-r <revision>] [-p <path>] [options]
6+
7+
<revision> is the git revision to diff. <path> is the file system path to
8+
scope the diff to.
9+
10+
## DESCRIPTION
11+
12+
Running `git diff` on files encrypted with ansible vault results in some pretty
13+
unhelpful output.
14+
15+
`git diff-ansible-vault` is custom git command that detects files encrypted
16+
with ansible vault within the diff, and safely decrypts them to reveal the true
17+
changes.
18+
19+
## EXAMPLES
20+
21+
$ git diff-ansible-vault
22+
23+
$ git diff-ansible-vault -r master..some-branch -p path/to/dir
24+
25+
$ git diff-ansible-vault --vault-password-file .vaultpass
26+
27+
## OPTIONS
28+
29+
* `-r`, `--revision <revision>`:
30+
Show diff for git revision, e.g. master..some-branch
31+
* `-p`, `--path <path>`:
32+
Restrict diff to path, e.g. support/config.yml
33+
* `--cached`, `--staged`:
34+
Show diff for staged changes
35+
* `--no-pager`:
36+
Do not pipe output into a pager
37+
* `--vault-password-file <path>`:
38+
Vault password file path, defaults to .vault-pass
39+
* `--vault-only`:
40+
Restrict diff to vault files only
41+
* `--color`, `--colour`:
42+
Turn on coloured output
43+
* `--no-color`, `--no-colour`:
44+
Turn off coloured diff
45+
* `--verbose`:
46+
Display verbose output
47+
* `-v`, `--version`:
48+
Output program version
49+
* `-h`, `--help`:
50+
Output help information
51+
52+
## EXIT STATUS
53+
54+
`git diff-ansible-vault` exits with a value of `0` if the diff is able to
55+
complete without error, and `1` if an error occurs.
56+
57+
## COPYRIGHT
58+
59+
Git Diff Ansible Vault is copyright (c) 2016, Tom Marshall. Released under the
60+
MIT license.
61+
62+
## SEE ALSO
63+
64+
`git-diff`(1)

test/git-diff-ansible-vault.bats

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ EOF
109109
@test "with --staged shows all staged changes" {
110110
run git diff-ansible-vault --staged
111111
assert_success
112-
echo "$output" > "$BATS_TEST_DIRNAME/../output.log"
113112
assert_output "$(cat <<EOF
114113
diff --git a/public.yml b/public.yml
115114
index 1f613c2..497da2b 100644

0 commit comments

Comments
 (0)