dotfiles/zsh/.config/zsh/functions/git.zsh

169 lines
3.7 KiB
Bash

#!/bin/zsh
function dc() {
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: dc [commit_message]"
echo "Quickly commit all changes and push to remote"
echo "If no message provided, uses timestamp-based message"
return 0
fi
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
echo "Error: Not in a git repository" >&2
return 1
fi
if [[ -z $(git status --porcelain) ]]; then
echo "No changes to commit"
return 0
fi
git add .
local commit_msg
if [[ -n "$1" ]]; then
commit_msg="$1"
else
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
commit_msg="dev: automated commit - ${timestamp}"
fi
if git commit -m "$commit_msg" --no-gpg-sign; then
if git push &>/dev/null; then
echo "Committed and pushed successfully"
else
echo "Warning: Commit successful but push failed" >&2
return 1
fi
else
echo "Commit failed" >&2
return 1
fi
}
function nu() {
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: nu [commit_message]"
echo "Quickly commit NixOS changes and push to remote"
echo "If no message provided, uses timestamp-based message"
return 0
fi
local original_dir=$(pwd)
if [[ -z "$XDG_CONFIG_HOME" ]]; then
echo "Error: XDG_CONFIG_HOME not set" >&2
return 1
fi
cd "$XDG_CONFIG_HOME/nixos" || {
echo "Error: Cannot access $XDG_CONFIG_HOME/nixos" >&2
return 1
}
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
echo "Error: Not in a git repository" >&2
cd "$original_dir"
return 1
fi
if [[ -z $(git status --porcelain) ]]; then
echo "No changes to commit"
cd "$original_dir"
return 0
fi
git add .
local commit_msg
if [[ -n "$1" ]]; then
commit_msg="$1"
else
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
commit_msg="nixos: automated commit - ${timestamp}"
fi
if git commit -m "$commit_msg" --no-gpg-sign; then
if git push &>/dev/null; then
echo "NixOS changes committed and pushed successfully"
else
echo "Warning: Commit successful but push failed" >&2
cd "$original_dir"
return 1
fi
else
echo "Commit failed" >&2
cd "$original_dir"
return 1
fi
cd "$original_dir"
}
function zrepo() {
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: zrepo <repo-name>"
echo "Create a new bare git repository on remote server"
echo "Example: zrepo my-project"
return 0
fi
if [[ -z "$1" ]]; then
echo "Error: Repository name required" >&2
echo "Usage: zrepo <repo-name>" >&2
return 1
fi
local REPO="$1"
local SERVER="git@zvps"
local PATH_ON_SERVER="/var/git/$REPO.git"
echo "Creating repository '$REPO' on $SERVER..."
ssh "$SERVER" "
if [ -d $PATH_ON_SERVER ]; then
echo 'Error: Repository '$REPO' already exists'
exit 1
fi
mkdir -p $PATH_ON_SERVER &&
git init --bare $PATH_ON_SERVER &&
chown -R git:git $PATH_ON_SERVER &&
chmod -R 755 $PATH_ON_SERVER
" || {
echo "Failed to create repository" >&2
return 1
}
echo "Repository created on $SERVER:$PATH_ON_SERVER"
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
git remote add origin "$SERVER:$PATH_ON_SERVER"
echo "Remote 'origin' added to your local repository"
else
echo "To clone this repository, run:"
echo " git clone $SERVER:$PATH_ON_SERVER"
fi
}
_dc_completion() {
local -a messages
messages=(
'fix:'
'feat:'
'docs:'
'style:'
'refactor:'
'test:'
'chore:'
)
_describe 'commit types' messages
}
_zrepo_completion() {
local -a repos
# Could be enhanced to fetch from remote server
repos=()
_describe 'repository names' repos
}
compdef _dc_completion dc
compdef _zrepo_completion zrepo