1# macOS hands GUI-launched processes a 256 fd soft limit, which bundler
2# file watchers exhaust during their first build.
3ulimit -n 65536 2>/dev/null
4
5ZSH_AUTOSUGGEST_MANUAL_REBIND=1
6
7function _set_prompt_theme() {
8 if [[ "$1" == "Dark" ]]; then
9 PS1='%F{207}%m%F{15}:%F{87}%~ %f$ '
10 else
11 PS1='%F{168}%m%F{238}:%F{33}%~ %f$ '
12 fi
13}
14
15function _prompt_theme_ready() {
16 local theme previous_prompt="$PS1"
17 if IFS= read -r theme <&$1; then
18 _set_prompt_theme "$theme"
19 fi
20
21 zle -F "$1"
22 exec {_prompt_theme_fd}<&-
23 unset _prompt_theme_fd
24
25 if [[ "$previous_prompt" == *"$PS1"* ]]; then
26 PS1="$previous_prompt"
27 else
28 zle reset-prompt
29 fi
30}
31
32function _update_prompt_theme() {
33 [[ -z ${_prompt_theme_fd-} ]] || return 0
34 (( SECONDS > _prompt_theme_checked_at )) || return 0
35
36 _prompt_theme_checked_at=$SECONDS
37 exec {_prompt_theme_fd}< <(defaults read -g AppleInterfaceStyle 2>/dev/null || print -r -- Light)
38 zle -F "$_prompt_theme_fd" _prompt_theme_ready
39}
40
41_set_prompt_theme "$(defaults read -g AppleInterfaceStyle 2>/dev/null)"
42_prompt_theme_checked_at=$SECONDS
43precmd_functions+=(_update_prompt_theme)
44
45function python() {
46 dirname=$(dirname $1 2>/dev/null)
47 if [ -z "$dirname" ]; then
48 dirname=$(pwd)
49 fi
50 uv run --project "$dirname" "$@"
51}
52
53function bun() {
54 if [[ "$1" = "build" ]]; then
55 command bun run "$@"
56 elif [[ "$1" = "bundle" ]]; then
57 shift
58 command bun build "$@"
59 else
60 command bun "$@"
61 fi
62}
63
64function pr() {
65 local branch remote repo url
66 branch="$(
67 command jj log -r '@ | @-' -T 'bookmarks.map(|b| b.name()).join("\n") ++ "\n"' --no-graph 2>/dev/null |
68 while IFS= read -r bookmark; do
69 case "$bookmark" in
70 "" | main | master | trunk | deployment*) ;;
71 *) printf '%s' "$bookmark"; break ;;
72 esac
73 done
74 )"
75 if [ -z "$branch" ]; then
76 printf 'no jj bookmark found on @ or @-\n' >&2
77 return 1
78 fi
79
80 remote="$(
81 command jj git remote list 2>/dev/null |
82 while IFS=' ' read -r name remote_url _; do
83 if [ "$name" = "origin" ]; then
84 printf '%s' "$remote_url"
85 break
86 fi
87 done
88 )"
89 case "$remote" in
90 git@github.com:*) repo="${remote#git@github.com:}" ;;
91 ssh://git@github.com/*) repo="${remote#ssh://git@github.com/}" ;;
92 https://github.com/*) repo="${remote#https://github.com/}" ;;
93 http://github.com/*) repo="${remote#http://github.com/}" ;;
94 *)
95 printf 'origin is not a github remote: %s\n' "$remote" >&2
96 return 1
97 ;;
98 esac
99
100 repo="${repo%.git}"
101 url="https://github.com/$repo/pull/new/$branch?labels=auto-merge"
102 command open "$url"
103}
104
105function _jjImmutableRevision() {
106 emulate -L zsh
107 setopt extendedglob
108 local line
109 while IFS= read -r line; do
110 line="${line//$'\e'\[[0-9;]##m/}"
111 if [[ "$line" = *"Could not modify commit: "* ]]; then
112 line="${line#*Could not modify commit: }"
113 printf '%s' "${line%% *}"
114 return 0
115 fi
116 done < "$1"
117 return 1
118}
119
120function _runJjStreaming() {
121 emulate -L zsh
122 setopt no_multios
123 local stderr arg has_color
124 local -a jj_color_arg pipe_status
125 stderr="$1"
126 shift
127 if [[ -t 1 && -t 2 ]]; then
128 has_color=false
129 for arg in "$@"; do
130 if [[ "$arg" = "--color" || "$arg" = --color=* ]]; then
131 has_color=true
132 break
133 fi
134 done
135 if ! $has_color; then
136 jj_color_arg=(--color=always)
137 fi
138 fi
139 {
140 command jj "$jj_color_arg[@]" "$@" 2>&1 1>&3 | tee "$stderr" >&2
141 pipe_status=("${pipestatus[@]}")
142 } 3>&1
143 return $pipe_status[1]
144}
145
146function _jjHandleFailure() {
147 local stderr jj_status reply
148 stderr="$1"
149 jj_status="$2"
150 shift 2
151 if [[ $jj_status -ne 0 ]]; then
152 if _jjImmutableRevision "$stderr" >/dev/null && [[ -t 0 ]]; then
153 printf 'Rewrite immutable commit anyways [y/N] ' >&2
154 read -r reply
155 case "$reply" in
156 [Yy] | [Yy][Ee][Ss])
157 _runJj "$@" --ignore-immutable
158 return $?
159 ;;
160 esac
161 return $jj_status
162 fi
163 fi
164 return $jj_status
165}
166
167function _runJj() {
168 local stderr jj_status
169 stderr="$(mktemp)"
170 _runJjStreaming "$stderr" "$@"
171 jj_status=$?
172 _jjHandleFailure "$stderr" "$jj_status" "$@"
173 jj_status=$?
174 rm -f "$stderr"
175 return $jj_status
176}
177
178function wrapWithAutoInstall() {
179 local root file cmd before after wrapped_status
180 root="$(command jj workspace root)"
181 if [ -n "$root" ]; then
182 if [ -e "$root/bun.lock" ]; then
183 file="$root/bun.lock"
184 cmd="bun i -f"
185 elif [ -e "$root/package-lock.json" ]; then
186 file="$root/package-lock.json"
187 cmd="npm i"
188 elif [ -e "$root/pnpm-lock.yaml" ]; then
189 file="$root/pnpm-lock.yaml"
190 cmd="pnpm i"
191 elif [ -e "$root/Cargo.lock" ]; then
192 file="$root/Cargo.lock"
193 cmd="cargo fetch"
194 fi
195 fi
196 if [ -n "$file" ]; then
197 before="$(sha1sum "$file")"
198 fi
199 _runJj "$@"
200 wrapped_status=$?
201 if [[ $wrapped_status -ne 0 ]]; then
202 return $wrapped_status
203 fi
204 if [ -n "$file" ] && [ -e "$file" ]; then
205 after="$(sha1sum "$file")"
206 if [ "$after" != "$before" ]; then
207 eval "$cmd"
208 return $?
209 fi
210 fi
211 return $wrapped_status
212}
213
214function jj() {
215 if [[ "$1" = "new" && "$#" -eq 2 && "$2" != *@* && "$2" != -* ]]; then
216 if ! command jj log -r "$2" --no-graph -T "" >/dev/null 2>&1 && command jj log -r "$2@origin" --no-graph -T "" >/dev/null 2>&1; then
217 wrapWithAutoInstall new "$2@origin"
218 else
219 wrapWithAutoInstall "$@"
220 fi
221 elif [[ "$*" = "git push"* ]] && [[ "$*" != *"--dry-run"* ]]; then
222 # confirm before pushing, but only when the dry-run has something to push
223 local dry dry_status
224 dry="$(command jj "$@" --dry-run 2>&1)"
225 dry_status=$?
226 print -r -- "$dry"
227 if [[ $dry_status -ne 0 ]]; then
228 return $dry_status
229 fi
230 if [[ "$dry" = *"No bookmarks/tags found in the default push revset"* && "$dry" = *"Nothing changed."* && -n "$(command jj log -r '@ & ~empty()' --no-graph -T change_id 2>/dev/null)" ]]; then
231 local answer
232 printf "No bookmark on @; move previous bookmark to @ and push? [y/N] "
233 read -r answer
234 case "$answer" in
235 y|Y|yes|YES)
236 bup || return $?
237 dry="$(command jj "$@" --dry-run 2>&1)"
238 dry_status=$?
239 print -r -- "$dry"
240 if [[ $dry_status -ne 0 ]]; then
241 return $dry_status
242 fi
243 if [[ "$dry" != *"Nothing changed."* ]]; then
244 _runJj "$@"
245 return $?
246 fi
247 ;;
248 esac
249 fi
250 if [[ "$dry" != *"Nothing changed."* ]]; then
251 printf "Press any key to continue... "
252 read -r
253 _runJj "$@"
254 return $?
255 fi
256 return 0
257 elif [[ "$1" = "undo" || "$1" = "redo" ]]; then
258 _runJj "$@"
259 else
260 # auto-install deps when switching commits
261 wrapWithAutoInstall "$@"
262 fi
263}
264
265function dp() {
266 local -a diff_command=(diff)
267 if [[ "$OSTYPE" == darwin* && -t 0 && -t 1 && -z "${SSH_CONNECTION-}${SSH_CLIENT-}${SSH_TTY-}" ]]; then
268 diff_command=(diffedit --tool knife)
269 fi
270
271 _runJj "$diff_command[@]" --from 'fork_point(trunk() | @)' --to @ "$@"
272}
273
274function bup() {
275 if [[ -z "$(command jj log -r @ -T description --no-graph 2>/dev/null)" ]]; then
276 command jj desc -m 'chore: follow-up changes'
277 fi
278 command jj b m --from 'heads(::@- & bookmarks()) ~ (trunk() | bookmarks(glob:"deployment*"))' --to '@'
279}