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