-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-grep
executable file
·42 lines (39 loc) · 1.55 KB
/
git-grep
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
#!/usr/bin/env bash
#
# Create a temporary directory
temp_dir=$(mktemp -d)
# Store the list of existing worktree directories
existing_worktrees=$(git worktree list --porcelain | grep 'worktree' | awk '{print $2}')
# Ensure the temporary directory is deleted and only newly created worktree directories are removed when the script exits
cleanup() {
rm -rf "$temp_dir"
new_worktrees=$(git worktree list --porcelain | grep 'worktree' | awk '{print $2}')
for new_worktree in $new_worktrees; do
if ! echo "$existing_worktrees" | grep -q -F "$new_worktree"; then
git worktree remove "$new_worktree"
fi
done
}
trap cleanup EXIT
git grep -n "$@" $(git rev-list --all) | awk -F: '{print $1":"$2":"$3}' |
fzf --cycle -i -e --no-multi --ansi \
--preview '
COMMIT=$(echo {} | cut -d: -f1);
FILE=$(echo {} | cut -d: -f2);
LINE=$(echo {} | cut -d: -f3);
TMPFILE="'"$temp_dir"'/$COMMIT/$FILE";
mkdir -p "$(dirname "$TMPFILE")";
git show $COMMIT:$FILE > "$TMPFILE";
bat --color always --highlight-line=$LINE --line-range $LINE:+100 "$TMPFILE"' \
--preview-window=up:60% \
--bind 'enter:execute:
TMPFILE_LINE="'"$temp_dir"'/$(echo {} | sed "s#:#/#")";
COMMIT=$(echo {} | cut -d: -f1);
WORKTREE="'"$temp_dir"'/$COMMIT"
test "$WORKTREE" != / && rm -rf "$WORKTREE"
git worktree add -f "$WORKTREE" $COMMIT
code "$WORKTREE"
code --goto "$TMPFILE_LINE"' \
--bind 'ctrl-p:toggle-preview' \
--bind 'ctrl-g:preview-page-up,ctrl-h:preview-page-down' \
--header "enter: vscode, C-p:toggle, C-g:up, C-h:down"