Skip to content

Commit 9b5714f

Browse files
authored
Do not hide error messages when trying to return early (#318)
Most of forgits functions allow to invoke git directly when arguments were passed. In most of these functions we only return early when the git command that is executed returns successfully. This hides error messages from the user. As discussed with @carlfriedrich in #316, I've modified this behavior to also return early when the git command was not successful, so we're transparent about gits error messages and return values. This also fixes an infinite loop printing an error message when invalid arguments were passed to gsp. Because gbl and gclean allow passing arguments to the git command that is invoked when we don't return early, I've modified their behavior to only invoke git directly (and return early) in the case that non flag arguments were passed. I've also modified gclean to use the -q flag instead of piping it's output to /dev/null.
1 parent f1ac9e3 commit 9b5714f

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

bin/git-forgit

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ _forgit_previous_commit() {
5353
fi
5454
}
5555

56+
_forgit_contains_non_flags() {
57+
while (("$#")); do
58+
case "$1" in
59+
-*) shift ;;
60+
*)
61+
return 0
62+
;;
63+
esac
64+
done
65+
return 1
66+
}
67+
5668
# optional render emoji characters (https://github.com/wfxr/emoji-cli)
5769
hash emojify &>/dev/null && _forgit_emojify='|emojify'
5870

@@ -157,7 +169,7 @@ _forgit_add() {
157169
local git_add changed unmerged untracked files opts preview extract
158170
git_add="git add $FORGIT_ADD_GIT_OPTS"
159171
# Add files if passed as arguments
160-
[[ $# -ne 0 ]] && $git_add "$@" && git status -su && return
172+
[[ $# -ne 0 ]] && { $git_add "$@" && git status -su; return $?; }
161173

162174
changed=$(git config --get-color color.status.changed red)
163175
unmerged=$(git config --get-color color.status.unmerged red)
@@ -196,7 +208,7 @@ _forgit_reset_head() {
196208
_forgit_inside_work_tree || return 1
197209
local git_reset_head cmd files opts rootdir
198210
git_reset_head="git reset -q $FORGIT_RESET_HEAD_GIT_OPTS HEAD"
199-
[[ $# -ne 0 ]] && $git_reset_head "$@" && git status --short && return
211+
[[ $# -ne 0 ]] && { $git_reset_head "$@" && git status --short; return $?; }
200212
rootdir=$(git rev-parse --show-toplevel)
201213
cmd="git diff --staged --color=always -- $rootdir/{} | $_forgit_diff_pager "
202214
opts="
@@ -216,7 +228,7 @@ _forgit_stash_show() {
216228
_forgit_inside_work_tree || return 1
217229
local git_stash_show git_stash_list cmd opts
218230
git_stash_show="git stash show --color=always --ext-diff"
219-
[[ $# -ne 0 ]] && $git_stash_show "$@" && return
231+
[[ $# -ne 0 ]] && { $git_stash_show "$@"; return $?; }
220232
git_stash_list="git stash list $FORGIT_STASH_SHOW_GIT_OPTS"
221233
cmd="echo {} |cut -d: -f1 |xargs -I% $git_stash_show % |$_forgit_diff_pager"
222234
opts="
@@ -249,7 +261,7 @@ _forgit_stash_push() {
249261
# ignore -u as it's used implicitly
250262
-u|--include-untracked) shift ;;
251263
# pass to git directly when encountering anything else
252-
*) $git_stash_push "${args[@]}" && return $?
264+
*) $git_stash_push "${args[@]}"; return $?
253265
esac
254266
done
255267
local opts preview files
@@ -274,7 +286,7 @@ _forgit_stash_push() {
274286
# git clean selector
275287
_forgit_clean() {
276288
_forgit_inside_work_tree || return 1
277-
[[ $# -ne 0 ]] && git clean "$@" &>/dev/null && return 0
289+
_forgit_contains_non_flags "$@" && { git clean -q "$@"; return $?; }
278290
local git_clean files opts
279291
git_clean="git clean $FORGIT_CLEAN_GIT_OPTS"
280292
opts="
@@ -522,7 +534,7 @@ _forgit_branch_delete() {
522534
_forgit_inside_work_tree || return 1
523535
local git_branch preview opts cmd branches
524536
git_branch="git branch $FORGIT_BRANCH_DELETE_GIT_OPTS"
525-
[[ $# -ne 0 ]] && $git_branch -D "$@" && return
537+
[[ $# -ne 0 ]] && { $git_branch -D "$@"; return $?; }
526538
preview="git log {1} $_forgit_log_preview_options"
527539

528540
opts="
@@ -582,7 +594,7 @@ _forgit_blame() {
582594
_forgit_inside_work_tree || return 1
583595
local git_blame opts flags preview file
584596
git_blame="git blame $FORGIT_BLAME_GIT_OPTS"
585-
[[ $# -ne 0 ]] && $git_blame "$@" && return 0
597+
_forgit_contains_non_flags "$@" && { $git_blame "$@"; return $?; }
586598
opts="
587599
$FORGIT_FZF_DEFAULT_OPTS
588600
$FORGIT_BLAME_FZF_OPTS

0 commit comments

Comments
 (0)