summaryrefslogtreecommitdiff
path: root/contrib/git-jump/git-jump
blob: 79286d811210e3fbfd2ff30489d88f53ce8c861d (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/sh

usage() {
	cat <<\EOF
usage: git jump [--stdout] <mode> [<args>]
   or: git jump [--stdout]

Jump to interesting elements in an editor.
The <mode> parameter is one of the following.
With no <mode> and no <args>, it defaults to "auto".

diff: elements are diff hunks. Arguments are given to diff.

merge: elements are merge conflicts. Arguments are given to ls-files -u.

grep: elements are grep hits. Arguments are given to git grep or, if
      configured, to the command in `jump.grepCmd`.

ws: elements are whitespace errors. Arguments are given to diff --check.

auto: select one of the other modes based on worktree state;
      "merge" if there are unmerged paths, "diff" if there are
      unstaged changes, "ws" if there are whitespace errors.

If the optional argument `--stdout` is given, print the quickfix
lines to standard output instead of feeding it to the editor.
EOF
}

open_editor() {
	editor=`git var GIT_EDITOR`
	case "$editor" in
	*emacs*)
		# Supported editor values are:
		# - emacs
		# - emacsclient
		# - emacsclient -t
		#
		# Wait for completion of the asynchronously executed process
		# to avoid race conditions in case of "emacsclient".
		eval "$editor --eval \"(let ((buf (grep \\\"cat \$1\\\"))) (pop-to-buffer buf) (select-frame-set-input-focus (selected-frame)) (while (get-buffer-process buf) (sleep-for 0.1)))\""
		;;
	*)
		# assume anything else is vi-compatible
		eval "$editor -q \$1"
		;;
	esac
}

mode_diff() {
	git diff --no-prefix --relative "$@" |
	perl -ne '
	if (m{^\+\+\+ (.*?)\t?$}) { $file = $1 eq "/dev/null" ? undef : $1; next }
	defined($file) or next;
	if (m/^@@ .*?\+(\d+)/) { $line = $1; next }
	defined($line) or next;
	if (/^ /) { $line++; next }
	if (/^[-+]\s*(.*)/) {
		print "$file:$line:1: $1\n";
		$line = undef;
	}
	'
}

mode_merge() {
	git ls-files -u "$@" |
	perl -pe 's/^.*?\t//' |
	sort -u |
	while IFS= read fn; do
		grep -Hn '^<<<<<<<' "$fn"
	done
}

# Grep -n generates nice quickfix-looking lines by itself,
# but let's clean up extra whitespace, so they look better if the
# editor shows them to us in the status bar.
mode_grep() {
	cmd=$(git config jump.grepCmd)
	test -n "$cmd" || cmd="git grep -n --column"
	$cmd "$@" |
	perl -pe '
	s/[ \t]+/ /g;
	s/^ *//;
	'
}

mode_ws() {
	git diff --check "$@"
}

mode_auto() {
	if test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" != "true"; then
		usage >&2
		exit 1
	fi
	if test -n "$(git ls-files -u "$@")"; then
		mode_merge "$@"
	elif ! git diff --quiet "$@"; then
		mode_diff "$@"
	else
		usage >&2
		exit 1
	fi
}

use_stdout=
while test $# -gt 0; do
	case "$1" in
	--stdout)
		use_stdout=t
		;;
	--*)
		usage >&2
		exit 1
		;;
	*)
		break
		;;
	esac
	shift
done
if test $# -lt 1; then
	set -- auto
fi
mode=$1; shift
type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }

if test "$use_stdout" = "t"; then
	"mode_$mode" "$@"
	exit 0
fi

trap 'rm -f "$tmp"' 0 1 2 3 15
tmp=`mktemp -t git-jump.XXXXXX` || exit 1
"mode_$mode" "$@" >"$tmp"
test -s "$tmp" || exit 0
open_editor "$tmp"