~kris/dots

srice

ref: e9b48d06a8541f3eda5c4db90382ab3c77183afb srice/.local/bin/misc/commit -rw-r--r-- 2.8 KiB
e9b48d06 — Kris Yotam xprofile: systemd-aware pipewire start + blueman-applet; sb-internet: tolerate missing /proc/net/wireless 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
# ============================================================
# File: commit
# Author: Kris Yotam
# License: MIT
# Date: 2025-09-28
# Purpose: Minimal TUI helper for committing to the main branch.
#          Modular, clean, suckless-style implementation.
# ============================================================

set -e

# --- Functions ---

ensure_repo() {
  if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
    echo "[Notice] Not inside a git repository."
    read -rp "Initialize this directory as a git repo? (y/n): " ans
    [ "$ans" = "y" ] && git init . || { echo "[Aborted] Not a git repository."; exit 0; }
  fi
}

ensure_remote() {
  if ! git remote | grep -q "origin"; then
    echo "[Notice] No 'origin' remote set."
    read -rp "Set a remote now? (y/n): " ans
    if [ "$ans" = "y" ]; then
      read -rp "Enter remote URL: " url
      git remote add origin "$url"
    else
      echo "[Skipped] No remote set. Push will fail."
    fi
  fi
}

ensure_branch() {
  if git show-ref --verify --quiet refs/heads/main; then
    git checkout main >/dev/null 2>&1 || echo "[Warn] Could not switch to main."
  else
    echo "[Notice] 'main' branch not found."
    read -rp "Create and switch to 'main'? (y/n): " ans
    [ "$ans" = "y" ] && git checkout -b main
  fi
}

stage_changes() {
  git add -A
}

get_commit_message() {
  while true; do
    read -rp "Enter commit message: " msg
    [ -z "$msg" ] && { echo "[Warn] Commit message cannot be empty."; continue; }
    echo "Commit message: \"$msg\""
    read -rp "Use this message? (y/n): " confirm
    [ "$confirm" = "y" ] && break
  done
}

make_commit() {
  if git commit -m "$msg"; then
    commit_hash=$(git rev-parse --short HEAD)
    commit_num=$(git rev-list --count HEAD)
  else
    echo "[Notice] Nothing to commit."
    exit 0
  fi
}

push_changes() {
  if git remote | grep -q "origin"; then
    git push -u origin main || echo "[Warn] Push failed."
  else
    echo "[Notice] No remote to push to."
  fi
}

summary() {
  echo "========================================"
  echo " Commit #$commit_num"
  echo " Hash: $commit_hash"
  echo " Message: $msg"
  echo " Branch: main"
  if git remote get-url origin >/dev/null 2>&1; then
    echo " Remote: $(git remote get-url origin)"
  fi
  echo "========================================"
}

# --- Main flow ---
ensure_repo        # makes sure you’re in a git repo or initializes one
ensure_remote      # checks for a remote, lets you add one if missing
ensure_branch      # switches to main, or offers to create it
stage_changes      # stages all file changes (git add -A)
get_commit_message # asks you for a commit message and confirms it
make_commit        # commits and stores commit hash + number
push_changes       # pushes to origin/main if remote exists
summary            # prints a clean summary block