add auto to gacp

This commit is contained in:
fatalerrors
2026-05-05 16:25:13 +02:00
parent bc67399ebc
commit a7f7452b2b
3 changed files with 24 additions and 8 deletions

View File

@@ -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_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_DEFAULT_REMOTE` | `origin` | Default remote used by git helper functions |
| `GIT_WIP_PREFIX` | `wip` | Prefix used by `gwip` when generating automatic checkpoint messages | | `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]`** **`[info]`**

View File

@@ -87,6 +87,10 @@ TERM=xterm-256color
# Prefix used by gwip when generating automatic checkpoint messages. # Prefix used by gwip when generating automatic checkpoint messages.
#GIT_WIP_PREFIX=wip #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] [info]
# meteo: Default city when no argument is given. Leave unset to require an # meteo: Default city when no argument is given. Leave unset to require an

View File

@@ -40,6 +40,7 @@
: "${GIT_MAIN_BRANCH:=main}" : "${GIT_MAIN_BRANCH:=main}"
: "${GIT_DEFAULT_REMOTE:=origin}" : "${GIT_DEFAULT_REMOTE:=origin}"
: "${GIT_WIP_PREFIX:=wip}" : "${GIT_WIP_PREFIX:=wip}"
: "${GIT_GACP_AUTO_ADD:=1}"
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@@ -231,7 +232,7 @@ export -f gsync
gacp() gacp()
{ {
local PARSED 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 # shellcheck disable=SC2181 # getopt return code is checked immediately after
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
disp E "Invalid options, use \"gacp --help\" to display usage." disp E "Invalid options, use \"gacp --help\" to display usage."
@@ -239,20 +240,26 @@ gacp()
fi fi
eval set -- "$PARSED" eval set -- "$PARSED"
local msg="" local msg="" auto_add="$GIT_GACP_AUTO_ADD"
while true; do while true; do
case "$1" in case "$1" in
-h|--help) -h|--help)
printf "gacp: Run git add, git commit, and git push in one command.\n" 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 "Options:\n"
printf "\t-a, --auto\tAutomatically add all modified files (git add -A)\n"
printf "\t-m, --message\tCommit message (mandatory)\n" printf "\t-m, --message\tCommit message (mandatory)\n"
printf "\n" printf "\n"
printf "If files are provided, only those paths are added.\n" printf "If files are provided, only those paths are added (-a is ignored).\n"
printf "If no file is provided, all changes are added with git add -A.\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" printf "If the remote branch moved forward, gacp pulls with rebase before pushing.\n"
return 0 return 0
;; ;;
-a|--auto)
auto_add=1
shift
;;
-m|--message) -m|--message)
msg="$2" msg="$2"
shift 2 shift 2
@@ -275,16 +282,20 @@ gacp()
return 1 return 1
fi 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 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 upstream=$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null) || true
if [[ $# -gt 0 ]]; then if [[ $# -gt 0 ]]; then
auto_add=0
disp I "Adding selected paths..." disp I "Adding selected paths..."
git add -- "$@" || return 1 git add -- "$@" || return 1
else elif [[ $auto_add -eq 1 ]]; then
disp I "Adding all changes..." disp I "Adding all changes..."
git add -A || return 1 git add -A || return 1
else
disp E "No files specified. Use -a/--auto or provide file paths."
return 1
fi fi
if git diff --cached --quiet; then 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 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 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 if [[ ${behind:-0} -gt 0 ]]; then
disp I "Remote branch is ahead, rebasing before push..." disp I "Remote branch is ahead, rebasing before push..."
git pull --rebase "$remote" "$tracking_branch" || return 1 git pull --rebase "$remote" "$tracking_branch" || return 1