aboutsummaryrefslogtreecommitdiff
path: root/posts/curing_a_case_of_git-UX.md
blob: 556df1bf07335bb03cab1eca486fcedbdb20187e (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
Git worktrees are great, but they fall behind the venerable
`git checkout` sometimes. I attempted to fix that with
[fzf](https://github.com/junegunn/fzf) and
a bit of bash. 

[![](https://asciinema.org/a/D297ztKRzpE4gAHbPTPmkqYps.svg)](https://asciinema.org/a/D297ztKRzpE4gAHbPTPmkqYps)

Fear not if you haven't heard of "worktrees", I have
included a primer here.  
[Skip the primer
->](#what-makes-them-clunky).

### Why Worktrees?

Picture this. You are whacking away on a feature branch.
Halfway there, in fact. Your friend asks you fix something
urgently. You proceed to do one of three things:

- create a temporary branch, make a WIP commit, begin
  working on the fix
- stash away your changes, begin working on the fix
- unfriend said friend for disturbing your flow

All of these options are ... subpar. With the temporary
branch, you are forced to create a partial, non-working
commit, and then reset said commit once done with the fix.
With the stash approach, you are required to now keep a
mental model of the stash, be aware of untracked files that
don't get stashed by default, etc. Why won't git just let
you work on two things at the same time without _thinking_
so much? 

That is exactly what worktrees let you do. Worktrees let you
have more than one checkout at a time, each checkout in a
separate directory. Like creating a new clone, but safer (it
disallows checking out the same branch twice) and a lot more
space efficient (the new working tree is "linked" to the
"main" worktree, and a good amount of stuff is shared). When
your friend asks you to make the fix, you proceed like so:

1. Create a new working tree with:
  ```bash
  # git worktree add -b <branch-name> <path> <from>
  git worktree add -b fix-stuff /path/to/tree master
  ```
2. `cd` into `/path/to/tree`
3. Fix, test, commit, push, party
4. Go back to your work, `cd -`

Easy as cake. You didn't have to settle for a partially
working commit, you didn't to deal with this "stash" thing,
_and_ you didn't have to unfriend your friend. Treating each
branch as a directory just _feels_ more intuitive, more
UNIX-y.

A few weeks later, you find yourself singing in praise of
worktrees, working on several things simultaneously. And at
the same time, cursing them for being a little ... clunky.

### What makes them clunky?

Worktrees are great at what they claim to do. They stay out
of the way when you need a checkout posthaste. However, as
you start using them regularly, you realize they are not as
flexible as `git checkout` or `git switch`.

#### Branch-hopping
You can `git checkout <branch>` from anywhere within a git
repository. You can't "jump" to a worktree in the same
fashion. The closest you can get, is to run `git worktree
list`, copy the path corresponding to your branch, and `cd`
into it.

Branch-hopping with the good ol' git-checkout:
```bash
# anywhere, anytime
λ git checkout feature/is-ascii-octdigit
```

Meanwhile, in worktree world:
```bash
# keeping these paths in your head is hard
λ git worktree list
~/worktrees/rustc/master                 eac6c33bc63 [master]
~/worktrees/rustc/improve-std-char-docs  94cba88553e [improve-std-char-docs]
~/worktrees/rustc/is-ascii-octdigit      bc57be3af7a [feature/is-ascii-octdigit]
~/my/other/path/oh/god                   op57or3ns7n [fix/some-error]

λ cd ~/worktrees/rustc/is-ascii-octdigit
```

#### Branch-previewing

You can "preview" branches with `git branch -v`. However, to
get an idea of what "recent activity" on a worktree looks
like, you might need some juggling. You can't glean much
info about a worktree in a jiffy.

Branch-previewing with the good ol' git-branch:
```bash
λ git branch -v
+ feature/is-ascii-octdigit bc57be3af7a introduce {char, u8}::is_ ...
+ improve-std-char-docs     94cba88553e add whitespace in assert ...
* master                    eac6c33bc63 Auto merge of #100869 - n ...
```

Meanwhile in worktree wonderland:
```
λ git worktree list
~/worktrees/rustc/master                 eac6c33bc63 [master]
~/worktrees/rustc/improve-std-char-docs  94cba88553e [improve-std-char-docs]
~/worktrees/rustc/is-ascii-octdigit      bc57be3af7a [feature/is-ascii-octdigit]

# aha, so ../is-ascii-octdigit corresponds to `feature/is-ascii-octdigit`
λ git log feature/is-ascii-octdigit
bc57be3af7a introduce {char, u8}::is_ascii_octdigit
eac6c33bc63 Auto merge of #100869 - nnethercote:repl ...
b32223fec10 Auto merge of #100707 - dzvon:fix-typo,  ...
aa857eb953e Auto merge of #100537 - petrochenkov:pic ...

# extra work to make the branch <-> worktree correspondence
```

#### Shell completions

Lastly, you can bank on shell completions to fill in your
branch whilst using `git checkout`. Worktrees have no such
conveniences.

We can mend these minor faults with fzf.

### Unclunkifying worktrees

I'd suggest looking up
[fzf](https://github.com/junegunn/fzf) (or
[skim](https://github.com/lotabout/skim) or
[fzy](https://github.com/jhawthorn/fzy)). These things make
it cake-easy to add interactivity to your shell. Onto fixing
the first minor fault, the inability to "jump" to a worktree
from anywhere within a git repository.

I have a little function called `gwj` which stands for "git
worktree jump". The idea is to list all the worktrees,
select one with fzf, and `cd` to it upon selection:

```bash
gwj () {
  local out
  out=$(git worktree list | fzf | awk '{print $1}')
  cd $out
}
```

That is all of it really. Head into a git repository:

```bash
# here, "master" is a directory, which contains my main
# worktree: a checkout of the master branch on rust-lang/rust 
λ cd ~/worktrees/rustc/master/library/core/src
λ # hack away
```

Preferably one with a few worktrees:

```bash
λ git worktree list
~/worktrees/rustc/master                 eac6c33bc63 [master]
~/worktrees/rustc/improve-std-char-docs  94cba88553e [improve-std-char-docs]
~/worktrees/rustc/is-ascii-octdigit      bc57be3af7a [feature/is-ascii-octdigit]
```

And hit `gwj` (pretend that the pipe, |, is your cursor):

```bash
λ gwj
> |
  4/4
> ~/worktrees/rustc/master                 eac6c33bc63 [master]
  ~/worktrees/rustc/improve-std-char-docs  94cba88553e [improve-std-char-docs]
  ~/worktrees/rustc/is-ascii-octdigit      bc57be3af7a [feature/is-ascii-octdigit]
```

Approximately type in your branch of choice:

```bash
λ gwj
> docs|
  4/4
> ~/worktrees/rustc/improve-std-char-docs  94cba88553e [improve-std-char-docs]
```

And hit enter. You should find yourself in the selected
worktree. 

Onward, to the next fault, lack of preview-bility. We can
utilize fzf's aptly named `--preview` flag, to, well,
preview our worktree before performing a selection:

```bash
gwj () {
  local out
  out=$(
    git worktree list |
    fzf --preview='git log --oneline -n10 {2}' |
    awk '{print $1}'
  )
  cd $out
}
```

Once again, hit `gwj` inside a git repository with linked worktrees:

```bash
λ gwj
╭─────────────────────────────────────────────────────────╮
│ eac6c33bc63 Auto merge of 100869 nnethercote:replace... │
│ b32223fec10 Auto merge of 100707 dzvon:fix-typo, r=d... │
│ aa857eb953e Auto merge of 100537 petrochenkov:picche... │
│ 3892b7074da Auto merge of 100210 mystor:proc_macro_d... │
│ db00199d999 Auto merge of 101249 matthiaskrgr:rollup... │
│ 14d216d33ba Rollup merge of 101240 JohnTitor:JohnTit... │
│ 3da66f03531 Rollup merge of 101236 thomcc:winfs-noze... │
│ 0620f6e90af Rollup merge of 101230 davidtwco:transla... │
│ c30c42ee299 Rollup merge of 101229 mgeisler:link-try... │
│ e5356712b9e Rollup merge of 101165 ldm0:drain_to_ite... │
╰─────────────────────────────────────────────────────────╯
>
  4/4
> /home/np/worktrees/compiler/master                 eac6c...
  /home/np/worktrees/compiler/improve-std-char-docs  94cba...
  /home/np/worktrees/compiler/is-ascii-octdigit      bc57b...
```

A fancy preview of the last 10 commits on the branch that
the selected worktree corresponds to. In other words, sight
for sore eyes. Our little script is already shaping up to be
useful, you hit `gwj`, browse through your worktrees,
preview each one and automatically `cd` to your selection.
But we are not done yet.

The last fault was lack shell completions. A quick review of
what a shell completion really does:

```bash
λ git checkout f<tab>
feature/is-ascii-octdigit
fix/some-error
format-doc-tests

λ git checkout feat<tab>

λ git checkout feature/is-ascii-octdigit
```

Each time you hit "tab", the shell produces a few
"completion candidates", and once you have just a single
candidate left, the shell inserts that for you directly into
your edit line. Of course, this process varies from shell to
shell.

fzf narrows down your options as you type into the prompt,
but you still have to:

1. Type `gwj`
2. Hit enter
3. Type out a query and narrow down your search
4. Hit enter

We can speed that up a bit, have fzf narrow down the
candidates on startup, just like our shell does:

```bash
gwj () {
  local out query
  query="${1:- }"
  out=$(
    git worktree list |
    fzf --preview='git log --oneline -n10 {2}' --query "$query" -1 |
    awk '{print $1}'
  )
  cd $out
}
```

The change is extremely tiny, blink-and-you'll-miss-it kinda
tiny. We added a little `--query` flag, that allows you to
prefill the prompt, and the `-1` flag, that avoids the
interactive finder if only one match exists on startup:

```bash
# skip through the fzf prompt:
λ gwj master
# cd -- ~/worktrees/rustc/master

# more than one option, we end up in the interactive finder
λ gwj improve
╭─────────────────────────────────────────────────────────╮
│ eac6c33bc63 Auto merge of 100869 nnethercote:replace... │
│ b32223fec10 Auto merge of 100707 dzvon:fix-typo, r=d... │
│ aa857eb953e Auto merge of 100537 petrochenkov:picche... │
╰─────────────────────────────────────────────────────────╯
> improve
  2/2
> /home/np/worktrees/compiler/improve-const-perf     eac6c...
  /home/np/worktrees/compiler/improve-std-char-docs  94cba...
```

Throw some error handling in there, hook up a similar script
to improve the UX of `git worktree remove`, go wild. A few
more helpers I've got:

```bash
# gwa /path/to/branch-name
# creates a new branch and "switches" to it
function gwa () {
  git worktree add "$1" && cd "$1"
}

alias gwls="git worktree list"
```