-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.sh
More file actions
224 lines (197 loc) · 5.76 KB
/
git.sh
File metadata and controls
224 lines (197 loc) · 5.76 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
echo 'Loaded: git.sh'
alias g='git'
alias ga='git add'
alias gaa='git add -A :/'
alias gai='git add -i'
m.git-add-patch() {
git add --patch "$@"
}
alias gd='git diff --color'
alias gds='git diff --staged'
alias gdisc='git diff --ignore-space-change'
alias gpom='git pull origin master'
alias gpum='git push origin master'
alias gc='git commit -v -m'
alias gst='git status'
alias gp='git push'
alias gpl='git pull'
alias gco='git checkout'
alias gcop='git checkout -p'
alias gcp="git cherry-pick"
alias gs='git stash'
alias gsa='git stash apply'
alias gcheckpoint='git stash && git stash apply'
alias gl='git prettylog'
alias glog='git log --oneline --decorate --graph'
alias glgp='git log --stat -p'
alias ggpush='git push origin "$(git_current_branch)"'
alias ggpull='git pull origin "$(git_current_branch)"'
m.git-quickcommit() {
git status
git diff
git commit -m "Update"
git push
}
m.git-quickcommit-all() {
git status
git diff
#pause()
#read -p 'Press any key to continue...'
git add -A :/
git commit -m "Update"
git push
}
m.git-rebase-continue() {
git rebase --continue
}
m.git-stardate() {
echo "./stardate.sh \"last monday 8:07:23pm\" \"init\" "
if [[ "$(uname -s)" == "Linux" ]]; then
stardate=$(date -d "$1" +"%a %b %d %T %Y %z")
else
# macOS BSD date: parse the human-readable date string via -j -f
stardate=$(date -j -f "%a %b %d %T %Y %z" "$1" +"%a %b %d %T %Y %z" 2>/dev/null ||
date -j +"%a %b %d %T %Y %z")
fi
GIT_COMMITTER_DATE="$stardate" git commit --date "$stardate" -m "$2"
# GIT_COMMITTER_DATE="date-here" git commit --amend --no-edit --date "date-here"
}
function m.git-uncommit-last() {
git reset --soft HEAD~1
}
function m.git-unstage-file() {
git reset HEAD
}
function m.git-restore-staged() {
git restore --staged "$1"
}
alias m.grs=m.git-restore-staged
function m.git-show() {
git log --graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr" |
fzf --ansi --no-sort --reverse --tiebreak=index --preview \
'f() { set -- $(echo -- "$@" | grep -o "[a-f0-9]\{7\}"); [ $# -eq 0 ] || git show --color=always $1 | delta --paging=never ; }; f {}' \
--bind "ctrl-d:preview-page-down,ctrl-u:preview-page-up,enter:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | delta') << 'FZF-EOF'
{}
FZF-EOF" --preview-window=right:60%
}
alias m.gs=m.git-show
check_git_status() {
local start_dir="$1"
local max_depth="${2:-3}"
if [ ! -d "$start_dir" ]; then
echo "The directory $start_dir does not exist."
return 1
fi
local tmpdir
tmpdir=$(mktemp -d)
local i=0
# Find repos (maxdepth to skip deeply nested .git dirs),
# run git status in parallel using git -C (no subprocess spawning)
find "$start_dir" -maxdepth "$max_depth" -type d -name '.git' | while read -r dir; do
local repo_dir="${dir%/.git}"
(
git_status=$(git -C "$repo_dir" status -s 2>/dev/null)
if [[ -n "$git_status" ]]; then
printf "\nGIT STATUS IN %s\n%s\n" "$repo_dir/" "$git_status" >"$tmpdir/$i"
fi
) &
((i++))
done
wait
# Print results in stable order
# (N) = zsh NULL_GLOB: no error when glob has no matches
for f in "$tmpdir"/*(N); do
[[ -f "$f" ]] && cat "$f"
done
rm -rf "$tmpdir"
}
# Add a worktree for an existing branch
# See: https://git-scm.com/docs/git-worktree#_commands
# Usage: m.git-tree-add <branch>
m.git-tree-add() {
if [[ $# -lt 1 ]]; then
echo "Usage: m.git-tree-add <branch>"
return 1
fi
local branch="$1"
local repo_root
repo_root=$(git rev-parse --show-toplevel) || return 1
local repo_name
repo_name=$(basename "$repo_root")
local worktree_path="$(dirname "$repo_root")/${repo_name}-${branch//\//-}"
git worktree add "$worktree_path" "$branch"
}
# Create a worktree with a new branch
# See: https://git-scm.com/docs/git-worktree#_commands
# Usage: m.git-tree-create <new-branch> [start-point]
m.git-tree-create() {
if [[ $# -lt 1 ]]; then
echo "Usage: m.git-tree-create <new-branch> [start-point]"
return 1
fi
local branch="$1" start="${2:-main}"
local repo_root
repo_root=$(git rev-parse --show-toplevel) || return 1
local repo_name
repo_name=$(basename "$repo_root")
local worktree_path="$(dirname "$repo_root")/${repo_name}-${branch//\//-}"
git worktree add -b "$branch" "$worktree_path" "$start"
}
# List all worktrees
# See: https://git-scm.com/docs/git-worktree#_commands
# Usage: m.git-tree-list
m.git-tree-list() {
git worktree list
}
# Remove a worktree
# See: https://git-scm.com/docs/git-worktree#_commands
# Usage: m.git-tree-remove <path>
m.git-tree-remove() {
if [[ $# -lt 1 ]]; then
echo "Usage: m.git-tree-remove <path>"
return 1
fi
git worktree remove "$1"
}
# Force-remove a worktree (even if dirty)
# See: https://git-scm.com/docs/git-worktree#_commands
# Usage: m.git-tree-remove-force <path>
m.git-tree-remove-force() {
if [[ $# -lt 1 ]]; then
echo "Usage: m.git-tree-remove-force <path>"
return 1
fi
git worktree remove --force "$1"
}
# Prune stale worktree references
# See: https://git-scm.com/docs/git-worktree#_commands
# Usage: m.git-tree-prune
m.git-tree-prune() {
git worktree prune -v
}
# Lock a worktree (prevent pruning)
# See: https://git-scm.com/docs/git-worktree#_commands
# Usage: m.git-tree-lock <path> [reason]
m.git-tree-lock() {
if [[ $# -lt 1 ]]; then
echo "Usage: m.git-tree-lock <path> [reason]"
return 1
fi
if [[ -n "$2" ]]; then
git worktree lock --reason "$2" "$1"
else
git worktree lock "$1"
fi
}
# Unlock a worktree
# See: https://git-scm.com/docs/git-worktree#_commands
# Usage: m.git-tree-unlock <path>
m.git-tree-unlock() {
if [[ $# -lt 1 ]]; then
echo "Usage: m.git-tree-unlock <path>"
return 1
fi
git worktree unlock "$1"
}