-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.zsh
More file actions
136 lines (114 loc) · 5.57 KB
/
commands.zsh
File metadata and controls
136 lines (114 loc) · 5.57 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# ─────────────────────────────────────────────────────────────
# AnmolNoor's Custom Commands
# ─────────────────────────────────────────────────────────────
# Source: ~/.scripts/commands.zsh
# These are utility commands for shell management
# ─────────────────────────────────────────────────────────────
# Shell Management
# ─────────────────────────────────────────────────────────────
# reload - Reload .zshrc configuration
alias reload="source ~/.zshrc && echo '✓ .zshrc reloaded'"
# editrc - Open .zshrc in default editor
alias editrc="${EDITOR:-nano} ~/.zshrc"
# ─────────────────────────────────────────────────────────────
# Scripts Update Command
# ─────────────────────────────────────────────────────────────
# update-scripts - Check and update AnmolNoor's scripts from git
update-scripts() {
local scripts_dir="$HOME/.scripts"
local remote_branch="main"
# Check if scripts directory exists and is a git repo
if [[ ! -d "$scripts_dir/.git" ]]; then
echo "❌ Scripts directory is not a git repository"
echo " Run the setup script to install: ~/.scripts/setup.sh"
return 1
fi
echo "🔍 Checking for updates..."
# Fetch latest from remote
if ! git -C "$scripts_dir" fetch origin "$remote_branch" &>/dev/null; then
echo "❌ Failed to fetch from remote"
return 1
fi
# Check if there are updates
local local_commit=$(git -C "$scripts_dir" rev-parse HEAD)
local remote_commit=$(git -C "$scripts_dir" rev-parse "origin/$remote_branch")
if [[ "$local_commit" == "$remote_commit" ]]; then
echo "✓ Scripts are up to date"
return 0
fi
# Show what's new
echo ""
echo "📦 Updates available:"
git -C "$scripts_dir" log --oneline HEAD..origin/$remote_branch
echo ""
# Ask user to confirm update
echo -n "Would you like to update? [Y/n]: "
read -r confirm
confirm="${confirm:-y}"
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo "⬇️ Pulling updates..."
if git -C "$scripts_dir" pull origin "$remote_branch"; then
echo ""
echo "✓ Scripts updated successfully!"
echo "→ Run 'reload' to apply changes"
else
echo "❌ Failed to pull updates"
echo " You may have local changes. Try:"
echo " cd ~/.scripts && git stash && git pull && git stash pop"
return 1
fi
else
echo "Skipped update."
fi
}
# scripts-status - Show current scripts version and status
scripts-status() {
local scripts_dir="$HOME/.scripts"
if [[ ! -d "$scripts_dir/.git" ]]; then
echo "❌ Scripts directory is not a git repository"
return 1
fi
echo "📁 Scripts Directory: $scripts_dir"
echo ""
# Current commit
echo "📌 Current Version:"
git -C "$scripts_dir" log -1 --format=" %h - %s (%cr)"
echo ""
# Check remote status
git -C "$scripts_dir" fetch origin main &>/dev/null
local local_commit=$(git -C "$scripts_dir" rev-parse HEAD)
local remote_commit=$(git -C "$scripts_dir" rev-parse origin/main 2>/dev/null)
if [[ -n "$remote_commit" ]]; then
if [[ "$local_commit" == "$remote_commit" ]]; then
echo "✓ Up to date with origin/main"
else
local behind=$(git -C "$scripts_dir" rev-list --count HEAD..origin/main)
echo "⚠️ $behind commit(s) behind origin/main"
echo " Run 'update-scripts' to update"
fi
fi
echo ""
# Show local changes if any
local changes=$(git -C "$scripts_dir" status --porcelain)
if [[ -n "$changes" ]]; then
echo "📝 Local Changes:"
echo "$changes" | sed 's/^/ /'
fi
}
# ─────────────────────────────────────────────────────────────
# Help for commands
# ─────────────────────────────────────────────────────────────
# chelp - Show all available custom commands
chelp() {
echo ""
echo "┌──────────────────────────────────────────────────────┐"
echo "│ 🛠️ Custom Commands │"
echo "├──────────────────────────────────────────────────────┤"
echo "│ reload Reload .zshrc configuration │"
echo "│ editrc Open .zshrc in editor │"
echo "│ update-scripts Check and update scripts from git │"
echo "│ scripts-status Show scripts version and status │"
echo "│ chelp Show this help │"
echo "└──────────────────────────────────────────────────────┘"
echo ""
}