Skip to content

Commit 55fcb59

Browse files
authored
Merge pull request #985 from unixorn/add-jevans-fzf-tools
Add `git-review-commits`
2 parents f4540a0 + 389e823 commit 55fcb59

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ If you wrote one of these scripts and want it removed from this collection, plea
132132
| `git-plotrepo` | Matthew McCullogh's [scripts collection](https://github.com/matthewmccullough/scripts/blob/master/git-plotrepo.rb) | Uses dot to draw a graph of the repository. |
133133
| `git-pr-fetch` | Joe Block <jpb@unixorn.net> | Fetch PR branches by refspec from one of a repository's remotes. |
134134
| `git-pr-list` | Joe Block <jpb@unixorn.net> | Lists pull requests. Requires `gh`. |
135+
| `git-review-commits` | Julia Evans'[blog](https://jvns.ca/til/fzf-preview-git-commits/) | Use [fzf](https://github.com/junegunn/fzf) topreview commits |
135136
| `git-promote` | Trevor's **Improving My git Workflow** blog post (404 now) | Promotes a local topic branch to a remote tracking branch of the same name. |
136137
| `git-prune-branches` | Michael Demmer in [jut-io/git-scripts](https://github.com/jut-io/git-scripts/blob/master/bin/git-prune-branches) | Deletes each fully merged branch after prompting for confirmation, than asks if you want the deleted branches deleted from your upstream remotes. |
137138
| `git-pruneall` | Ryan Tomayko's dotfiles | Prune branches from specified remotes, or all remotes when no remote is specified. |
@@ -309,6 +310,7 @@ If you aren't using any ZSH frameworks, or if you're using `bash`, `fish` or ano
309310
- [commit-helper](https://github.com/andre-filho/commit-helper) - A python script that helps you write commits following commit conventions.
310311
- [diff-so-fancy](https://github.com/so-fancy/diff-so-fancy) - Better looking `git` diffs.
311312
- [dunk](https://github.com/darrenburns/dunk) - Another tool for prettier `git` diffs.
313+
- [fzf-git-worktree](https://github.com/banyan/zsh-fzf-git-worktree) - Manage `git` worktrees with [fzf](https://github.com/junegunn/fzf) integration.
312314
- [gig](https://dev.to/shihanng/gig-a-gitignore-generator-opc) - a CLI `.gitignore` generator
313315
- [git ZSH plugin](https://github.com/davidde/git) - A replacement for the stock oh-my-zsh `git` plugin. Provides quite a few useful aliases and functions that are more consistent in their naming that the relatively unintuitive ones in the stock plugin.
314316
- [git-absorb](https://github.com/tummychow/git-absorb) - Essentially, when your working directory has uncommitted changes on top of draft changesets, you can run `git absorb` and the uncommitted modifications are automagically folded ("absorbed") into the appropriate draft ancestor commits. The command essentially looks at the lines that were modified, finds a commit modifying those lines, and amends that commit to include your uncommitted changes. If the changes can't be made without conflicts, they remain uncommitted. This workflow is insanely useful for things like applying review feedback.

bin/git-review-commits

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env bash
2+
#
3+
# bin/git-review-commits
4+
#
5+
# Based on https://jvns.ca/til/fzf-preview-git-commits/
6+
7+
set -o pipefail
8+
if [[ -n "$DEBUG" ]]; then
9+
# shellcheck disable=SC2086
10+
if [[ "$(echo $DEBUG | tr '[:upper:]' '[:lower:]')" == "verbose" ]]; then
11+
set -x
12+
fi
13+
fi
14+
15+
function echo-stderr() {
16+
printf '%s
17+
' "$1" >&2 ## Send message to stderr. Exclude >&2 if you don't want it that way.
18+
}
19+
20+
function debug() {
21+
if [[ -n "$DEBUG" ]]; then
22+
echo-stderr "$@"
23+
fi
24+
}
25+
26+
function fail() {
27+
echo-stderr "$1"
28+
exit "${2-1}" ## Return a code specified by $2 or 1 by default.
29+
}
30+
31+
function has() {
32+
# Check if a command is in $PATH
33+
which "$@" > /dev/null 2>&1
34+
}
35+
36+
function check-dependency() {
37+
if ! (builtin command -V "$1" >/dev/null 2>&1); then
38+
fail "missing dependency: can't find $1 in your PATH"
39+
fi
40+
}
41+
42+
function check-dependencies() {
43+
debug "Checking dependencies..."
44+
# shellcheck disable=SC2041
45+
# Placeholders for whatever programs you really need
46+
for dep in "$@"
47+
do
48+
if ! has "$dep"; then
49+
fail "Can't find $dep in your $PATH"
50+
else
51+
debug "- Found $dep"
52+
fi
53+
done
54+
}
55+
56+
# If you need to restrict to a specific os, use
57+
# only-run-on Darwin
58+
# or
59+
# only-run-on Linux
60+
61+
# Placeholders for your script's real dependencies
62+
check-dependencies fzf git grep
63+
64+
commit=${1:-HEAD}
65+
git show --stat=120 --format="" "$commit" | \
66+
grep -E '^\s*\S+.*\|' | \
67+
fzf --ansi \
68+
--disabled \
69+
--bind 'j:down,k:up,q:abort' \
70+
--preview="echo {} | sed 's/|.*//' | xargs -I% git show --color=always $commit -- %" \
71+
--preview-window=right:60%

0 commit comments

Comments
 (0)