Skip to content

Commit 5025875

Browse files
committed
Add pager support
Because: * `git diff` supports paging to provide a better experience on long diffs. This would also be useful for files encrypted with ansible vault. This change: * Attempts to identify the default pager setup the same way that git itself does, and uses the same default args, ref: https://github.com/git/git/blob/v2.9.2/pager.c#L43-L74 * Adds support for git's `--no-pager` argument, but not `-p`/ `--paginate` as this would colide with `-p`/`--path`. May want to revisit this later once #4 is resolved. Fixes #3
1 parent 14bbd05 commit 5025875

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

+1
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

+41-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ VERSION=0.2.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"
@@ -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

0 commit comments

Comments
 (0)