git-commit: say add/update in the default message
authorDenis Ovsienko <denis@ovsienko.info>
Tue, 22 Jan 2019 15:18:33 +0000 (15:18 +0000)
committerDenis Ovsienko <denis@ovsienko.info>
Tue, 22 Jan 2019 15:18:33 +0000 (15:18 +0000)
The script used to log "update <filename>" regardless if it was a new
file or a change to an existing file. To make it easier to follow the
history, have it log "add <filename>" in the former case. Replace a
long if() wrapper with an early return while at it.

[skip ci]

gateways/git-commit

index ce44757..82bcff1 100755 (executable)
@@ -154,43 +154,48 @@ git_commit_or_exit()
                exit 3
        }
 
+       local message
        # git-diff exits with 0 if the file is not in the repository.
-       if ! git cat-file -e HEAD:"$FILEPATH" 2>/dev/null || ! git diff --quiet -- "$FILEPATH"; then
-               git add -- "$FILEPATH" || {
-                       print_error "failed to run 'git add'"
-                       exit 9
+       if ! git cat-file -e HEAD:"$FILEPATH" 2>/dev/null; then
+               message="add $FILEPATH"
+       else
+               git diff --quiet -- "$FILEPATH" && return # in the repository and has not changed
+               message="update $FILEPATH"
+       fi
+
+       git add -- "$FILEPATH" || {
+               print_error "failed to run 'git add'"
+               exit 9
+       }
+       # getopts validates the value of MESSAGEMODE.
+       case "$MESSAGEMODE" in
+       replace)
+               [ -n "$COMMITMSG" ] && message="$COMMITMSG"
+               ;;
+       append)
+               # No space in between -- the appended part may skip straight to
+               # the commit description.
+               message="$message$COMMITMSG"
+               ;;
+       esac
+       git commit $VLEVEL \
+               --message="$message" \
+               ${AUTHOR:+--author="$AUTHOR"} \
+               ${COMMITDATE:+--date="$COMMITDATE"} \
+               -- "$FILEPATH" || \
+       {
+               print_error "failed to run 'git commit', trying to roll back."
+               git reset --quiet -- "$FILEPATH" || {
+                       print_error "failed to run 'git reset'"
+                       exit 13
                }
-               local message="update $FILEPATH"
-               # getopts validates the value of MESSAGEMODE.
-               case "$MESSAGEMODE" in
-               replace)
-                       [ -n "$COMMITMSG" ] && message="$COMMITMSG"
-                       ;;
-               append)
-                       # No space in between -- the appended part may skip straight to
-                       # the commit description.
-                       message="$message$COMMITMSG"
-                       ;;
-               esac
-               git commit $VLEVEL \
-                       --message="$message" \
-                       ${AUTHOR:+--author="$AUTHOR"} \
-                       ${COMMITDATE:+--date="$COMMITDATE"} \
-                       -- "$FILEPATH" || \
-               {
-                       print_error "failed to run 'git commit', trying to roll back."
-                       git reset --quiet -- "$FILEPATH" || {
-                               print_error "failed to run 'git reset'"
-                               exit 13
-                       }
-                       git checkout --quiet -- "$FILEPATH" || {
-                               print_error "failed to run 'git checkout'"
-                               exit 14
-                       }
-                       exit 10
+               git checkout --quiet -- "$FILEPATH" || {
+                       print_error "failed to run 'git checkout'"
+                       exit 14
                }
-               [ "$1" = "and_push" ] && git_push_or_exit
-       fi
+               exit 10
+       }
+       [ "$1" = "and_push" ] && git_push_or_exit
 }
 
 # Both callScript() and GNU getopt support both short and long option formats.