diff options
| author | Matt Hunter <m@lfurio.us> | 2026-02-21 17:31:57 -0500 |
|---|---|---|
| committer | Matt Hunter <m@lfurio.us> | 2026-02-22 22:07:05 -0500 |
| commit | a2e8081a27cd9be48f0358a720e89a8ae0e4b27a (patch) | |
| tree | a6ea007f89a477d5620815ce10efa67fe0ce2cbd | |
| parent | def77dc0d6a0755849ad6593b744e7e5546794db (diff) | |
| download | git-sonar-a2e8081a27cd9be48f0358a720e89a8ae0e4b27a.tar.gz git-sonar-a2e8081a27cd9be48f0358a720e89a8ae0e4b27a.zip | |
Rework option parsing and drop extraneous calls to is_repo()
Rewrite option parsing such that all script operations are contained
within one is_repo() guard. Some bashisms are removed in the process.
Now that we check is_repo() up front, remove extra checks throughout the
script.
Signed-off-by: Matt Hunter <m@lfurio.us>
| -rwxr-xr-x | git-sonar | 73 |
1 files changed, 35 insertions, 38 deletions
@@ -98,19 +98,14 @@ is_repo() { } record_timestamp() { - if is_repo; then - touch "$(dot_git)/lastupdatetime" - fi + touch "$(dot_git)/lastupdatetime" } timestamp() { - if is_repo; then - if [[ $OSTYPE == darwin* ]];then - printf '%s' "$(stat -f%m "$(dot_git)/lastupdatetime" 2>/dev/null || printf '%s' "0")" - else - printf '%s' "$(stat -c %Y "$(dot_git)/lastupdatetime" 2>/dev/null || printf '%s' "0")" - fi; - + if [[ $OSTYPE == darwin* ]]; then + printf '%s' "$(stat -f%m "$(dot_git)/lastupdatetime" 2>/dev/null || printf '%s' "0")" + else + printf '%s' "$(stat -c %Y "$(dot_git)/lastupdatetime" 2>/dev/null || printf '%s' "0")" fi } @@ -120,16 +115,12 @@ time_now() { time_to_update() { last_time_updated="${1:-$FETCH_TIME}" - if is_repo; then - local timesincelastupdate="$(($(time_now) - $(timestamp)))" - if (( $timesincelastupdate > $last_time_updated )); then - # time to update return 0 (which is true) - return 0 - else - # not time to update return 1 (which is false) - return 1 - fi + local timesincelastupdate="$(($(time_now) - $(timestamp)))" + if (( $timesincelastupdate > $last_time_updated )); then + # time to update return 0 (which is true) + return 0 else + # not time to update return 1 (which is false) return 1 fi } @@ -145,9 +136,7 @@ fetch() { } commit_short_sha() { - if is_repo; then - printf '%s' "$(git rev-parse --short HEAD 2>/dev/null)" - fi + printf '%s' "$(git rev-parse --short HEAD 2>/dev/null)" } branch_name() { @@ -424,9 +413,7 @@ color_remote_commits() { } readable_branch_name() { - if is_repo; then - printf $PRINT_F_OPTION "$COLOR_BRANCH$(branch_name || printf '%s' "detached@$(commit_short_sha)")$RESET_COLOR_BRANCH" - fi + printf $PRINT_F_OPTION "$COLOR_BRANCH$(branch_name || printf '%s' "detached@$(commit_short_sha)")$RESET_COLOR_BRANCH" } stashed_status() { @@ -585,23 +572,33 @@ usage() { echo " --zsh # (does nothing - for compatibility with git-radar)" echo " --bash # (does nothing - for compatibility with git-radar)" echo " --fish # (does nothing - for compatibility with git-radar)" -} - -while [[ $# -gt 0 ]]; do - command="$1" + exit +} + +do_fetch="" + +while true; do + case "$1" in + --help) usage ;; + -h) usage ;; + --fetch) do_fetch="true" ;; + -f) do_fetch="true" ;; + --zsh) ;; + --bash) ;; + --fish) ;; + *) break + esac shift - - if [[ "$command" == "-h" ]] || [[ "$command" == "--help" ]]; then - usage - exit - fi - - if [[ "$command" == "-f" ]] || [[ "$command" == "--fetch" ]]; then - fetch >/dev/null 2>&1 - fi done +if [ $# -ne 0 ]; then + printf 'sonar: Unrecognized option given: %s\n' "$1" + exit 1 +fi + +# Guard all active operations by this is_repo check if is_repo; then + [ -n "$do_fetch" ] && fetch >/dev/null 2>&1 prepare_colors render_prompt fi |
