-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfzf_git_files.sh
More file actions
77 lines (66 loc) · 2.7 KB
/
Copy pathfzf_git_files.sh
File metadata and controls
77 lines (66 loc) · 2.7 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
#!/usr/bin/env bash
function fgf() {
local -r prompt_add="Add > "
local -r prompt_reset="Reset > "
local -r git_root_dir=$(git rev-parse --show-toplevel)
local -r git_unstaged_files="git ls-files --modified --deleted --other --exclude-standard --deduplicate $git_root_dir"
local git_staged_files='git status --short | grep "^[A-Z]" | awk "{print \$NF}"'
local -r git_reset="git reset -- {+}"
local -r enter_cmd="($git_unstaged_files | grep {} && git add {+}) || $git_reset"
local -r preview_status_label="[ Status ]"
local -r preview_status="git status --short"
local -r header=$(cat <<-EOF
> CTRL-S to switch between Add Mode and Reset mode
> CTRL_T for status preview | CTRL-F for diff preview | CTRL-B for blame preview
> ALT-E to open files in your editor
> ALT-C to commit | ALT-A to append to the last commit
EOF
)
local -r add_header=$(cat <<-EOF
$header
> ENTER to add files
> ALT-P to add patch
EOF
)
local -r reset_header=$(cat <<-EOF
$header
> ENTER to reset files
> ALT-D to reset and checkout files
EOF
)
local -r mode_reset="change-prompt($prompt_reset)+reload($git_staged_files)+change-header($reset_header)+unbind(alt-p)+rebind(alt-d)"
local -r mode_add="change-prompt($prompt_add)+reload($git_unstaged_files)+change-header($add_header)+rebind(alt-p)+unbind(alt-d)"
eval "$git_unstaged_files" | fzf \
--multi \
--reverse \
--no-sort \
--prompt="Add > " \
--preview-label="$preview_status_label" \
--preview="$preview_status" \
--header "$add_header" \
--header-first \
--bind='start:unbind(alt-d)' \
--bind="ctrl-t:change-preview-label($preview_status_label)" \
--bind="ctrl-t:+change-preview($preview_status)" \
--bind='ctrl-f:change-preview-label([ Diff ])' \
--bind='ctrl-f:+change-preview(git diff --color=always {} | sed "1,4d")' \
--bind='ctrl-b:change-preview-label([ Blame ])' \
--bind='ctrl-b:+change-preview(git blame --color-by-age {})' \
--bind="ctrl-s:transform:[[ \$FZF_PROMPT =~ '$prompt_add' ]] && echo '$mode_reset' || echo '$mode_add'" \
--bind="enter:execute($enter_cmd)" \
--bind="enter:+reload([[ \$FZF_PROMPT =~ '$prompt_add' ]] && $git_unstaged_files || $git_staged_files)" \
--bind="enter:+refresh-preview" \
--bind='alt-p:execute(git add --patch {+})' \
--bind="alt-p:+reload($git_unstaged_files)" \
--bind="alt-d:execute($git_reset && git checkout {+})" \
--bind="alt-d:+reload($git_staged_files)" \
--bind='alt-c:execute(git commit)+abort' \
--bind='alt-a:execute(git commit --amend)+abort' \
--bind='alt-e:execute(${EDITOR:-vim} {+})' \
--bind='f1:toggle-header' \
--bind='f2:toggle-preview' \
--bind='ctrl-y:preview-up' \
--bind='ctrl-e:preview-down' \
--bind='ctrl-u:preview-half-page-up' \
--bind='ctrl-d:preview-half-page-down'
}