contrib: add git-jump script
[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.
15EOF
16}
17
18open_editor() {
19 editor=`git var GIT_EDITOR`
20 eval "$editor -q \$1"
21}
22
23mode_diff() {
24 git diff --relative "$@" |
25 perl -ne '
26 if (m{^\+\+\+ b/(.*)}) { $file = $1; next }
27 defined($file) or next;
28 if (m/^@@ .*\+(\d+)/) { $line = $1; next }
29 defined($line) or next;
30 if (/^ /) { $line++; next }
31 if (/^[-+]\s*(.*)/) {
32 print "$file:$line: $1\n";
33 $line = undef;
34 }
35 '
36}
37
38mode_merge() {
39 git ls-files -u |
40 perl -pe 's/^.*?\t//' |
41 sort -u |
42 while IFS= read fn; do
43 grep -Hn '^<<<<<<<' "$fn"
44 done
45}
46
47# Grep -n generates nice quickfix-looking lines by itself,
48# but let's clean up extra whitespace, so they look better if the
49# editor shows them to us in the status bar.
50mode_grep() {
51 git grep -n "$@" |
52 perl -pe '
53 s/[ \t]+/ /g;
54 s/^ *//;
55 '
56}
57
58if test $# -lt 1; then
59 usage >&2
60 exit 1
61fi
62mode=$1; shift
63
64trap 'rm -f "$tmp"' 0 1 2 3 15
65tmp=`mktemp -t git-jump.XXXXXX` || exit 1
66type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }
67"mode_$mode" "$@" >"$tmp"
68test -s "$tmp" || exit 0
69open_editor "$tmp"