π¦ chore: update smartcommit.sh
swadhinbiswas committed 3 months ago
4fae0b9
1 parent 1938743
commit 4fae0b9
1 file changed
+117 -0
Showing 1 changed file+117-0
βΌsmartcommit.sh
+117-0
| @@ -0,0 +1,117 @@ | ||
1 | +#!/usr/bin/env bash | |
2 | + | |
3 | +set -euo pipefail | |
4 | + | |
5 | +GREEN="\033[0;32m" | |
6 | +BLUE="\033[0;34m" | |
7 | +YELLOW="\033[1;33m" | |
8 | +RED="\033[0;31m" | |
9 | +NC="\033[0m" | |
10 | + | |
11 | +git update-index --refresh >/dev/null 2>&1 || true | |
12 | + | |
13 | +FILES=$(git status --porcelain | awk '{print substr($0,4)}') | |
14 | + | |
15 | +if [[ -z "$FILES" ]]; then | |
16 | + echo -e "${GREEN}β Nothing to commit.${NC}" | |
17 | + exit 0 | |
18 | +fi | |
19 | + | |
20 | +commit_message() { | |
21 | + local file="$1" | |
22 | + local emoji="" | |
23 | + local message="" | |
24 | + | |
25 | + case "$file" in | |
26 | + *.md) | |
27 | + emoji="π" | |
28 | + message="docs: update $(basename "$file" .md)" | |
29 | + ;; | |
30 | + *.css) | |
31 | + emoji="π¨" | |
32 | + message="style: improve $(basename "$file")" | |
33 | + ;; | |
34 | + *.astro) | |
35 | + emoji="β¨" | |
36 | + message="feat: update $(basename "$file") page" | |
37 | + ;; | |
38 | + *.tsx|*.jsx) | |
39 | + emoji="β¨" | |
40 | + message="feat: improve $(basename "$file")" | |
41 | + ;; | |
42 | + *.ts|*.js) | |
43 | + emoji="β»οΈ" | |
44 | + message="refactor: update $(basename "$file")" | |
45 | + ;; | |
46 | + *.json|*.yaml|*.yml|*.toml) | |
47 | + emoji="π§" | |
48 | + message="config: update $(basename "$file")" | |
49 | + ;; | |
50 | + *.sql) | |
51 | + emoji="ποΈ" | |
52 | + message="db: update $(basename "$file")" | |
53 | + ;; | |
54 | + tests/*|*test*) | |
55 | + emoji="β
" | |
56 | + message="test: update $(basename "$file")" | |
57 | + ;; | |
58 | + src/db/*) | |
59 | + emoji="ποΈ" | |
60 | + message="db: update $(basename "$file")" | |
61 | + ;; | |
62 | + src/pages/api/*) | |
63 | + emoji="π" | |
64 | + message="api: update $(basename "$file")" | |
65 | + ;; | |
66 | + src/components/*) | |
67 | + emoji="π" | |
68 | + message="ui: improve $(basename "$file")" | |
69 | + ;; | |
70 | + docs/*) | |
71 | + emoji="π" | |
72 | + message="docs: add $(basename "$file")" | |
73 | + ;; | |
74 | + *) | |
75 | + emoji="π¦" | |
76 | + message="chore: update $(basename "$file")" | |
77 | + ;; | |
78 | + esac | |
79 | + | |
80 | + echo "$emoji $message" | |
81 | +} | |
82 | + | |
83 | +echo -e "${BLUE}Found $(echo "$FILES" | wc -l) changed files.${NC}" | |
84 | +echo | |
85 | + | |
86 | +while IFS= read -r file | |
87 | +do | |
88 | + [[ -z "$file" ]] && continue | |
89 | + | |
90 | + if [[ ! -e "$file" ]]; then | |
91 | + echo -e "${YELLOW}Skipping deleted file: $file${NC}" | |
92 | + continue | |
93 | + fi | |
94 | + | |
95 | + echo -e "${BLUE}β $file${NC}" | |
96 | + | |
97 | + git add "$file" | |
98 | + | |
99 | + msg=$(commit_message "$file") | |
100 | + | |
101 | + if git diff --cached --quiet; then | |
102 | + echo -e "${YELLOW}No staged changes.${NC}" | |
103 | + continue | |
104 | + fi | |
105 | + | |
106 | + if git commit -m "$msg"; then | |
107 | + echo -e "${GREEN}β $msg${NC}" | |
108 | + else | |
109 | + echo -e "${RED}β Failed: $file${NC}" | |
110 | + git reset HEAD "$file" >/dev/null 2>&1 || true | |
111 | + fi | |
112 | + | |
113 | + echo | |
114 | + | |
115 | +done <<< "$FILES" | |
116 | + | |
117 | +echo -e "${GREEN}π Finished committing all files.${NC}" | |
0 | 118 | |