Skip to content

Commit a2f98fd

Browse files
committed
feat(abbr): improve a bunch of abbrs
1 parent 07422d0 commit a2f98fd

File tree

1 file changed

+158
-53
lines changed

1 file changed

+158
-53
lines changed

conf.d/git_fish_abbr.fish

+158-53
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,22 @@ abbr -a gaa "git add --all"
118118
abbr -a gad "git add (git ls-files --deleted)"
119119
abbr -a gai "git add --interactive"
120120
abbr -a gam "git add (git ls-files --modified)"
121-
abbr -a gap "git add --patch"
121+
function _abbr_git_add_patch
122+
#
123+
# command git status --porcelain
124+
#
125+
126+
echo "git add --patch"
127+
end
128+
129+
abbr -a gap --function _abbr_git_add_patch
130+
122131
abbr -a gau "git add (git ls-files --others --exclude-standard)"
123132

124133
function _abbr_git_add_modified_and_commit_previous
125134
# 1. find the previous commit
126135
set -l expansion "git add (git ls-files --modified)"
136+
# TODO: can this be read from git reflog instead?
127137
set -l previous_commit (history search --max 1 --prefix "git commit --message")
128138
# 2. if there is a previous commit, add it to the command
129139
if test -n "$previous_commit"
@@ -136,6 +146,35 @@ end
136146
# have to add the, now, modified files again and then commit them with the same message.
137147
abbr -a gamcp --set-cursor -f _abbr_git_add_modified_and_commit_previous
138148

149+
# git archive
150+
function _abbr_git_archive
151+
set -l expansion git archive
152+
set -l repo_name (path basename (pwd))
153+
set -l output
154+
switch $argv[1]
155+
case gar
156+
set output "$repo_name.tar"
157+
case garz
158+
set output "$repo_name.zip"
159+
case '*'
160+
# unreachable
161+
end
162+
163+
set -a expansion --output $output
164+
set -a expansion HEAD
165+
echo $expansion
166+
end
167+
168+
abbr -a gar --regex "garz?" --function _abbr_git_archive
169+
170+
# git bisect
171+
function _abbr_git_bisect
172+
173+
# TODO: figure out how to detect if a git bisect is underway
174+
echo "git bisect"
175+
end
176+
abbr -a gbs --function _abbr_git_bisect
177+
139178
# git blame
140179
# abbr -a gb
141180

@@ -306,70 +345,96 @@ function _abbr_git_diff
306345
and printf "GIT_EXTERNAL_DIFF=difft " # then use as the diff tool
307346
# TODO: append --staged if all modified files are staged
308347

309-
echo "git diff"
310-
end
311-
abbr -a gd --set-cursor -f _abbr_git_diff
312-
313-
# TODO: create a function for this similar to `gstatus`
314-
abbr -a gds git diff --staged
315-
316-
function _abbr_git_fetch
317-
if not command -q gum
318-
echo "git fetch"
319-
return
348+
set -l expansion git diff
349+
if string match --regex --quiet s -- $argv[1]
350+
set -a expansion --staged
320351
end
352+
echo $expansion
353+
end
354+
abbr -a gd --regex "gds?" --set-cursor -f _abbr_git_diff
321355

322-
# TODO: add to `gitui` aswell
323-
# fetch "Retrieves changes from the remote"
324-
# fetch --prune "Retrieves changes from the remote and removes deleted remote branches"
325-
# fetch --tags "Retrieves changes from the remote including all tags"
326-
# fetch --all "Retrieves changes from all remotes" # TODO: enumerate known remotes here
327-
# fetch --all --prune "Retrieves changes from all remotes and removes deleted remote branches"
328-
# fetch --all --tags "Retrieves changes from all remotes including all tags"
356+
# function _abbr_git_fetch
357+
# if not command -q gum
358+
# echo "git fetch"
359+
# return
360+
# end
329361

330-
# TODO: handle case where user presses esc gracefully
331-
gum choose
332-
commandline -f repaint
333-
end
362+
# # TODO: add to `gitui` aswell
363+
# set -l options
364+
# set -a options "fetch # Retrieves changes from the remote"
365+
# set -a options "fetch --prune # Retrieves changes from the remote and removes deleted remote branches"
366+
# set -a options "fetch --tags # Retrieves changes from the remote including all tags"
367+
# set -a options "fetch --all # Retrieves changes from all remotes" # TODO: enumerate known remotes here
368+
# set -a options "fetch --all --prune # Retrieves changes from all remotes and removes deleted remote branches"
369+
# set -a options "fetch --all --tags # Retrieves changes from all remotes including all tags"
370+
371+
# printf "%s\n" (printf "%s\n" $options | fish_indent --ansi) | gum choose
372+
# # TODO: handle case where user presses esc gracefully
373+
# # gum choose
374+
# commandline -f repaint
375+
# end
334376

335377
# git fetch
336-
abbr -a gf --set-cursor "git fetch %"
337-
abbr -a gfa --set-cursor "git fetch --all% # fetch the latest changes from all remote upstream repositories"
338-
abbr -a gft --set-cursor "git fetch --tags% # also fetch tags from the remote upstream repository"
339-
abbr -a gfp --set-cursor "git fetch --prune% # delete local references to remote branches that have been deleted upstream"
378+
# abbr -a gf --function _abbr_git_fetch
379+
abbr -a gf --set-cursor "git fetch --prune --tags --all -v"
380+
# abbr -a gfa --set-cursor "git fetch --all% # fetch the latest changes from all remote upstream repositories"
381+
# abbr -a gft --set-cursor "git fetch --tags% # also fetch tags from the remote upstream repository"
382+
# abbr -a gfp --set-cursor "git fetch --prune% # delete local references to remote branches that have been deleted upstream"
340383

341384
# git grep
342385
function _abbr_git_grep
343-
set -l expansion git grep "'%'"
344-
if string match --regex --groups-only "gg([0-9]+)" -- $argv[1] | read n
386+
set -l expansion git grep --heading
387+
if not command git config grep.lineNumber >/dev/null
388+
set -a expansion --line-number
389+
end
390+
391+
if not command git config grep.column >/dev/null
392+
set -a expansion --column
393+
end
394+
395+
command git config grep.patternType | read patternType
396+
if test $pipestatus[1] -ne 0
397+
set -a expansion --perl-regexp
398+
else if test $patternType = regular; and not command git config grep.extendedRegexp >/dev/null
399+
set -a expansion --extended-regexp
400+
end
401+
402+
if string match --regex --quiet v -- $argv[1]
403+
set -a expansion --invert-match
404+
end
405+
if string match --regex --quiet i -- $argv[1]
406+
set -a expansion --ignore-case
407+
end
408+
409+
if string match --regex --groups-only "([0-9]+)" -- $argv[1] | read n
345410
set -a expansion "HEAD~$n"
346411
end
347412

413+
set -a expansion "'%'"
414+
348415
echo $expansion
349416
end
350-
abbr -a gg --regex "gg[0-9]*" --set-cursor --function _abbr_git_grep
417+
abbr -a gg --regex "ggi?v?[0-9]*" --set-cursor --function _abbr_git_grep
351418
abbr -a gga --set-cursor "git grep '%' -- (git rev-list --all) # This might take some time to execute!"
352419

353420
# git log
354421
function _abbr_git_log
355422
set -l expansion git log
356-
if string match --regex --groups-only "gl([0-9]+)" -- $argv[1] | read n
357-
set -a expansion --max-count $n
423+
if string match --regex --quiet p -- $argv[1]
424+
set -a expansion --patch
425+
else if string match --regex --quiet o -- $argv[1]
426+
set -a expansion --oneline --decorate --graph
427+
else if string match --regex --quiet s -- $argv[1]
428+
set -a expansion --stat
358429
end
359430

360-
echo $expansion
361-
end
362-
abbr -a gl --regex "gl[0-9]*" --function _abbr_git_log
363-
364-
function _abbr_git_log_oneline
365-
set -l expansion git log --oneline --graph
366-
if string match --regex --groups-only "gl([0-9]+)" -- $argv[1] | read n
431+
if string match --regex --groups-only '([0-9]+)$' -- $argv[1] | read n
367432
set -a expansion --max-count $n
368433
end
369434

370435
echo $expansion
371436
end
372-
abbr -a glo --regex "glo[0-9]*" --function _abbr_git_log_oneline
437+
abbr -a gl --regex "gl[ops]?[0-9]*" --function _abbr_git_log
373438

374439
# git ls-files
375440
abbr -a gls git ls-files
@@ -394,9 +459,30 @@ abbr -a gm --set-cursor --function _abbr_git_merge
394459
abbr -a gma git merge --abort
395460
abbr -a gmc git merge --continue
396461

462+
463+
function _abbr_git_mv
464+
if not command git rev-parse --is-inside-work-tree 2>/dev/null >&2
465+
echo mv
466+
else
467+
# https://git-scm.com/docs/git-mv
468+
echo "git mv -k --verbose"
469+
end
470+
end
471+
abbr -a mv --function _abbr_git_mv
472+
473+
function _abbr_git_rm
474+
if not command git rev-parse --is-inside-work-tree 2>/dev/null >&2
475+
echo rm
476+
else
477+
# https://git-scm.com/docs/git-rm
478+
echo "git rm"
479+
end
480+
end
481+
abbr -a rm --function _abbr_git_rm
397482
# git mv
398483
# TODO: try to detect a relevant file to move
399-
abbr -a gmv git mv
484+
# abbr -a gmv git mv
485+
# abbr -a grm git rm
400486

401487
# git pull
402488
set --query git_fish_abbr_git_pull_merge_strategy
@@ -446,11 +532,13 @@ function __git::abbr::git_push
446532
echo "# no commits to push ¯\\_(ツ)_/¯"
447533
end
448534

449-
set -l git_push_opts
535+
set -l git_push_opts --verbose
450536
set -l branch (command git rev-parse --abbrev-ref HEAD)
451537
set -l remote_branch (command git rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null)
452538
if test $status -ne 0
539+
# FIXME: handle case where no remote has been confiugred yet
453540
# Local branch has no remote branch, so create one
541+
# TODO: print comment note of where the remote is
454542
echo "git push $git_push_opts --set-upstream origin $branch% # no remote branch found, creating one"
455543
else
456544
set -l unpushed_tags (command git push --tags --dry-run &| string match --regex --groups-only '\* \[new tag\]\s+(\S+)')
@@ -581,13 +669,17 @@ function _abbr_git_show
581669
set -a expansion HEAD
582670
end
583671

672+
if string match --regex --quiet s -- $argv[1]
673+
set -a expansion --summary
674+
end
675+
584676
if command --query difft; and not set --query --export GIT_EXTERNAL_DIFF
585677
set -p expansion "GIT_EXTERNAL_DIFF=difft"
586678
set -a expansion --ext-diff
587679
end
588680
echo $expansion
589681
end
590-
abbr -a gsh --regex "gsh[0-9]*" --function _abbr_git_show
682+
abbr -a gsh --regex "gshs?[0-9]*" --function _abbr_git_show
591683

592684
# git show-branch
593685
abbr -a gsb git show-branch
@@ -713,14 +805,25 @@ abbr -a gwtrm git worktree remove
713805
abbr -a gwtrmf git worktree remove --force
714806

715807
function _abbr_git_clone
716-
set -l args --recurse-submodules
808+
# TODO: detect if the user has permission to create a directory in $PWD, and print a warning if not
809+
810+
if command git rev-parse --inside-work-tree 2>/dev/null >&2
811+
echo "# ERROR: You are already in a git directory"
812+
echo "%"
813+
# TODO: move this further down, and suggest to add the repo (if it is a repo) as a submodule
814+
# echo "# HINT: maybe you want to "
815+
# echo "# git submodule add"
816+
return
817+
end
818+
819+
set -l args --recurse-submodules --verbose --progress
717820
set -l postfix_args
718821
# TODO: handle case where clipboard can not be read
719-
set -l clipboard_contents (fish_clipboard_paste)
822+
set -l clipboard_contents (fish_clipboard_paste | string trim)
720823

721-
# You ctrl+l && ctrl+c a git url
824+
# You ctrl+l && ctrl+c a git url in a browser
722825
if string match --quiet --regex "^(https?://|git@).*\.git\$" -- "$clipboard_contents"
723-
set -a args $clipboard_contents
826+
set -a args "'$clipboard_contents'"
724827
# Parse the directory name from the url
725828
set -a postfix_args '&& cd'
726829
set -a postfix_args (string replace --all --regex '^.*/(.*)\.git$' '$1' $clipboard_contents)
@@ -732,24 +835,26 @@ function _abbr_git_clone
732835
set -a postfix_args "&& cd $reponame"
733836
else if string match --groups-only --regex "^\s*git clone https://git(hub|lab)\.com/([^/]+)/(.+)" $clipboard_contents | read --line _hub owner repository
734837
# example: git clone https://github.com/bevyengine/bevy
735-
set -a postfix_args $clipboard_contents
838+
set -a postfix_args "'$clipboard_contents'"
736839
set -a postfix_args "&& cd $repository"
737840
end
738841

739-
# TODO: use (capture) instead
740-
set -l depth (string replace --all --regex '[^0-9]' '' $argv[1])
741-
if test -n $depth
842+
if string match --regex --groups-only "([0-9]+)" -- $argv[1] | read depth
742843
set -a args --depth=$depth
743844
end
744845

745846
echo git clone $args $postfix_args
746847
end
747848

748-
abbr -a git_clone_at_depth --regex "gc[0-9]*" --function _abbr_git_clone
849+
abbr -a git_clone_at_depth --regex "gc[0-9]*" --set-cursor --function _abbr_git_clone
749850

750851

751852
# TODO: detect if the last commit had message `squash! WIP` and you were the author, and suggest to use amend instead
752-
abbr -a gwip "git add (git ls-files --modified) && git commit --message 'squash! WIP' --no-verify"
853+
function _abbr_gwip
854+
855+
echo "git add (git ls-files --modified) && git commit --message 'squash! WIP' --no-verify"
856+
end
857+
abbr -a gwip --function _abbr_gwip
753858

754859
# unstage a file
755860

0 commit comments

Comments
 (0)