Files
nixos-config/home/shell/git.sh
T
kiskaadee a6afe699fb feat(shell): add p3 alias and remove auto-commit from gitignore()
- Add `p3 = "python3";` to `programs.bash.shellAliases` in `home/shell.nix` for quick Python 3 invocation

- Remove automatic `git add`, `git commit`, and `git push` from `gitignore()` in `home/shell/git.sh` so it only appends new patterns to `.gitignore`
2026-11-14 21:36:42 -05:00

64 lines
1.8 KiB
Bash

# shellcheck shell=bash
# 🌟 Custom Git Automation & Helper Utilities
# Sourced in .bashrc to streamline gitignore modifications, commit-push workflows, and new repo setups.
# 1. gitignore: Quickly appends patterns to the repository's root .gitignore file.
gitignore() {
if [ $# -eq 0 ]; then
echo "Usage: gitignore <pattern> [pattern...]"
return 1
fi
# Find the top-level repository directory path
local GIT_ROOT
GIT_ROOT=$(git rev-parse --show-toplevel 2> /dev/null)
if [ -z "$GIT_ROOT" ]; then
echo "Error: Not a Git repository."
return 1
fi
local GIT_IGNORE_FILE="$GIT_ROOT/.gitignore"
if [ ! -f "$GIT_IGNORE_FILE" ]; then
touch "$GIT_IGNORE_FILE"
fi
# Ensure the gitignore file ends with a trailing newline if it has content
if [ -s "$GIT_IGNORE_FILE" ] && [ "$(tail -c1 "$GIT_IGNORE_FILE" | wc -l)" -eq 0 ]; then
echo "" >> "$GIT_IGNORE_FILE"
fi
local added_count=0
for pattern in "$@"; do
# Check if the pattern is already present in the .gitignore file
if rg -Fxq "$pattern" "$GIT_IGNORE_FILE"; then
echo "Skipping '$pattern' (already in .gitignore)"
else
echo "$pattern" >> "$GIT_IGNORE_FILE"
echo "Added '$pattern'"
((added_count++))
fi
done
if [ "$added_count" -eq 0 ]; then
echo "No new patterns were added."
fi
}
# 2. gacp: Git Add, Commit, and Push shorthand
# Performs a global staging, commits with the specified message, and pushes to origin on the active branch.
gacp() {
if [ -z "$1" ]; then
echo "Usage: gacp <commit-message>"
return 1
fi
git add -A
git commit -m "$1"
local branch_name
branch_name=$(git branch --show-current)
git push origin "${branch_name}"
echo "Pushed to origin/${branch_name}"
}