From a7f7452b2b9b41652a0f3ba158f6a3ee3c61dd28 Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Tue, 5 May 2026 16:25:13 +0200 Subject: [PATCH] add auto to gacp --- README.md | 1 + doc/profile.conf.example | 4 ++++ profile.d/git.sh | 27 +++++++++++++++++++-------- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8ec5e5c..c5f4a6a 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,7 @@ change the default without having to pass flags every time. | `GIT_MAIN_BRANCH` | `main` | Fallback main branch name used when remote HEAD cannot be detected | | `GIT_DEFAULT_REMOTE` | `origin` | Default remote used by git helper functions | | `GIT_WIP_PREFIX` | `wip` | Prefix used by `gwip` when generating automatic checkpoint messages | +| `GIT_GACP_AUTO_ADD` | `1` | Set to `1` to make `gacp` automatically add all modified files when no explicit file list is given; set to `0` to require explicit paths or the `-a` flag | **`[info]`** diff --git a/doc/profile.conf.example b/doc/profile.conf.example index ba886b2..7ec9742 100755 --- a/doc/profile.conf.example +++ b/doc/profile.conf.example @@ -87,6 +87,10 @@ TERM=xterm-256color # Prefix used by gwip when generating automatic checkpoint messages. #GIT_WIP_PREFIX=wip +# gacp: Automatically add all modified files (git add -A) when no explicit file +# list is provided. Set to 0 to require explicit file paths or the -a flag. +#GIT_GACP_AUTO_ADD=1 + # ============================================================================== [info] # meteo: Default city when no argument is given. Leave unset to require an diff --git a/profile.d/git.sh b/profile.d/git.sh index d8de35e..8cd6c06 100755 --- a/profile.d/git.sh +++ b/profile.d/git.sh @@ -40,6 +40,7 @@ : "${GIT_MAIN_BRANCH:=main}" : "${GIT_DEFAULT_REMOTE:=origin}" : "${GIT_WIP_PREFIX:=wip}" +: "${GIT_GACP_AUTO_ADD:=1}" # ------------------------------------------------------------------------------ @@ -231,7 +232,7 @@ export -f gsync gacp() { local PARSED - PARSED=$(getopt -o hm: --long help,message: -n 'gacp' -- "$@") + PARSED=$(getopt -o ham: --long help,auto,message: -n 'gacp' -- "$@") # shellcheck disable=SC2181 # getopt return code is checked immediately after if [[ $? -ne 0 ]]; then disp E "Invalid options, use \"gacp --help\" to display usage." @@ -239,20 +240,26 @@ gacp() fi eval set -- "$PARSED" - local msg="" + local msg="" auto_add="$GIT_GACP_AUTO_ADD" while true; do case "$1" in -h|--help) printf "gacp: Run git add, git commit, and git push in one command.\n" - printf "Usage: gacp -m \"message\" [file1 file2 ...]\n" + printf "Usage: gacp [-a] -m \"message\" [file1 file2 ...]\n" printf "Options:\n" + printf "\t-a, --auto\tAutomatically add all modified files (git add -A)\n" printf "\t-m, --message\tCommit message (mandatory)\n" printf "\n" - printf "If files are provided, only those paths are added.\n" - printf "If no file is provided, all changes are added with git add -A.\n" + printf "If files are provided, only those paths are added (-a is ignored).\n" + printf "If no file is provided and -a is active, all changes are added with git add -A.\n" + printf "Default for -a can be set via GIT_GACP_AUTO_ADD in profile.conf.\n" printf "If the remote branch moved forward, gacp pulls with rebase before pushing.\n" return 0 ;; + -a|--auto) + auto_add=1 + shift + ;; -m|--message) msg="$2" shift 2 @@ -275,16 +282,20 @@ gacp() return 1 fi - local branch upstream remote tracking_branch ahead behind counts + local branch upstream remote tracking_branch behind counts branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) || return 1 upstream=$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null) || true if [[ $# -gt 0 ]]; then + auto_add=0 disp I "Adding selected paths..." git add -- "$@" || return 1 - else + elif [[ $auto_add -eq 1 ]]; then disp I "Adding all changes..." git add -A || return 1 + else + disp E "No files specified. Use -a/--auto or provide file paths." + return 1 fi if git diff --cached --quiet; then @@ -308,7 +319,7 @@ gacp() if git rev-parse --verify --quiet "refs/remotes/${remote}/${tracking_branch}" >/dev/null; then counts=$(git rev-list --left-right --count HEAD..."${remote}/${tracking_branch}" 2>/dev/null) || return 1 - read -r ahead behind <<< "$counts" + read -r _ behind <<< "$counts" if [[ ${behind:-0} -gt 0 ]]; then disp I "Remote branch is ahead, rebasing before push..." git pull --rebase "$remote" "$tracking_branch" || return 1