Sync with maint
[git/git.git] / contrib / git-jump / git-jump
CommitLineData
21e4631c
JK
1#!/bin/sh
2
3usage() {
4 cat <<\EOF
5usage: git jump <mode> [<args>]
6
7Jump to interesting elements in an editor.
8The <mode> parameter is one of:
9
10diff: elements are diff hunks. Arguments are given to diff.
11
12merge: elements are merge conflicts. Arguments are ignored.
13
14grep: elements are grep hits. Arguments are given to grep.
1af9c609
JK
15
16ws: elements are whitespace errors. Arguments are given to diff --check.
21e4631c
JK
17EOF
18}
19
20open_editor() {
21 editor=`git var GIT_EDITOR`
22 eval "$editor -q \$1"
23}
24
25mode_diff() {
6108b04b 26 git diff --no-prefix --relative "$@" |
21e4631c 27 perl -ne '
6108b04b 28 if (m{^\+\+\+ (.*)}) { $file = $1; next }
21e4631c 29 defined($file) or next;
74a7fa44 30 if (m/^@@ .*?\+(\d+)/) { $line = $1; next }
21e4631c
JK
31 defined($line) or next;
32 if (/^ /) { $line++; next }
33 if (/^[-+]\s*(.*)/) {
34 print "$file:$line: $1\n";
35 $line = undef;
36 }
37 '
38}
39
40mode_merge() {
41 git ls-files -u |
42 perl -pe 's/^.*?\t//' |
43 sort -u |
44 while IFS= read fn; do
45 grep -Hn '^<<<<<<<' "$fn"
46 done
47}
48
49# Grep -n generates nice quickfix-looking lines by itself,
50# but let's clean up extra whitespace, so they look better if the
51# editor shows them to us in the status bar.
52mode_grep() {
53 git grep -n "$@" |
54 perl -pe '
55 s/[ \t]+/ /g;
56 s/^ *//;
57 '
58}
59
1af9c609
JK
60mode_ws() {
61 git diff --check "$@"
62}
63
21e4631c
JK
64if test $# -lt 1; then
65 usage >&2
66 exit 1
67fi
68mode=$1; shift
69
70trap 'rm -f "$tmp"' 0 1 2 3 15
71tmp=`mktemp -t git-jump.XXXXXX` || exit 1
72type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }
73"mode_$mode" "$@" >"$tmp"
74test -s "$tmp" || exit 0
75open_editor "$tmp"