Skip to content

Commit 324bc2a

Browse files
committed
Merge branch 'jk/git-tricks'
* jk/git-tricks: completion: match ctags symbol names in grep patterns contrib: add git-jump script contrib: add diff highlight script
2 parents e8e1c29 + 29eec71 commit 324bc2a

File tree

5 files changed

+355
-0
lines changed

5 files changed

+355
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,10 @@ _git_gitk ()
14301430
_gitk
14311431
}
14321432

1433+
__git_match_ctag() {
1434+
awk "/^${1////\\/}/ { print \$1 }" "$2"
1435+
}
1436+
14331437
_git_grep ()
14341438
{
14351439
__git_has_doubledash && return
@@ -1452,6 +1456,15 @@ _git_grep ()
14521456
;;
14531457
esac
14541458

1459+
case "$cword,$prev" in
1460+
2,*|*,-*)
1461+
if test -r tags; then
1462+
__gitcomp "$(__git_match_ctag "$cur" tags)"
1463+
return
1464+
fi
1465+
;;
1466+
esac
1467+
14551468
__gitcomp "$(__git_refs)"
14561469
}
14571470

contrib/diff-highlight/README

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
diff-highlight
2+
==============
3+
4+
Line oriented diffs are great for reviewing code, because for most
5+
hunks, you want to see the old and the new segments of code next to each
6+
other. Sometimes, though, when an old line and a new line are very
7+
similar, it's hard to immediately see the difference.
8+
9+
You can use "--color-words" to highlight only the changed portions of
10+
lines. However, this can often be hard to read for code, as it loses
11+
the line structure, and you end up with oddly formatted bits.
12+
13+
Instead, this script post-processes the line-oriented diff, finds pairs
14+
of lines, and highlights the differing segments. It's currently very
15+
simple and stupid about doing these tasks. In particular:
16+
17+
1. It will only highlight a pair of lines if they are the only two
18+
lines in a hunk. It could instead try to match up "before" and
19+
"after" lines for a given hunk into pairs of similar lines.
20+
However, this may end up visually distracting, as the paired
21+
lines would have other highlighted lines in between them. And in
22+
practice, the lines which most need attention called to their
23+
small, hard-to-see changes are touching only a single line.
24+
25+
2. It will find the common prefix and suffix of two lines, and
26+
consider everything in the middle to be "different". It could
27+
instead do a real diff of the characters between the two lines and
28+
find common subsequences. However, the point of the highlight is to
29+
call attention to a certain area. Even if some small subset of the
30+
highlighted area actually didn't change, that's OK. In practice it
31+
ends up being more readable to just have a single blob on the line
32+
showing the interesting bit.
33+
34+
The goal of the script is therefore not to be exact about highlighting
35+
changes, but to call attention to areas of interest without being
36+
visually distracting. Non-diff lines and existing diff coloration is
37+
preserved; the intent is that the output should look exactly the same as
38+
the input, except for the occasional highlight.
39+
40+
Use
41+
---
42+
43+
You can try out the diff-highlight program with:
44+
45+
---------------------------------------------
46+
git log -p --color | /path/to/diff-highlight
47+
---------------------------------------------
48+
49+
If you want to use it all the time, drop it in your $PATH and put the
50+
following in your git configuration:
51+
52+
---------------------------------------------
53+
[pager]
54+
log = diff-highlight | less
55+
show = diff-highlight | less
56+
diff = diff-highlight | less
57+
---------------------------------------------
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/perl
2+
3+
# Highlight by reversing foreground and background. You could do
4+
# other things like bold or underline if you prefer.
5+
my $HIGHLIGHT = "\x1b[7m";
6+
my $UNHIGHLIGHT = "\x1b[27m";
7+
my $COLOR = qr/\x1b\[[0-9;]*m/;
8+
9+
my @window;
10+
11+
while (<>) {
12+
# We highlight only single-line changes, so we need
13+
# a 4-line window to make a decision on whether
14+
# to highlight.
15+
push @window, $_;
16+
next if @window < 4;
17+
if ($window[0] =~ /^$COLOR*(\@| )/ &&
18+
$window[1] =~ /^$COLOR*-/ &&
19+
$window[2] =~ /^$COLOR*\+/ &&
20+
$window[3] !~ /^$COLOR*\+/) {
21+
print shift @window;
22+
show_pair(shift @window, shift @window);
23+
}
24+
else {
25+
print shift @window;
26+
}
27+
28+
# Most of the time there is enough output to keep things streaming,
29+
# but for something like "git log -Sfoo", you can get one early
30+
# commit and then many seconds of nothing. We want to show
31+
# that one commit as soon as possible.
32+
#
33+
# Since we can receive arbitrary input, there's no optimal
34+
# place to flush. Flushing on a blank line is a heuristic that
35+
# happens to match git-log output.
36+
if (!length) {
37+
local $| = 1;
38+
}
39+
}
40+
41+
# Special case a single-line hunk at the end of file.
42+
if (@window == 3 &&
43+
$window[0] =~ /^$COLOR*(\@| )/ &&
44+
$window[1] =~ /^$COLOR*-/ &&
45+
$window[2] =~ /^$COLOR*\+/) {
46+
print shift @window;
47+
show_pair(shift @window, shift @window);
48+
}
49+
50+
# And then flush any remaining lines.
51+
while (@window) {
52+
print shift @window;
53+
}
54+
55+
exit 0;
56+
57+
sub show_pair {
58+
my @a = split_line(shift);
59+
my @b = split_line(shift);
60+
61+
# Find common prefix, taking care to skip any ansi
62+
# color codes.
63+
my $seen_plusminus;
64+
my ($pa, $pb) = (0, 0);
65+
while ($pa < @a && $pb < @b) {
66+
if ($a[$pa] =~ /$COLOR/) {
67+
$pa++;
68+
}
69+
elsif ($b[$pb] =~ /$COLOR/) {
70+
$pb++;
71+
}
72+
elsif ($a[$pa] eq $b[$pb]) {
73+
$pa++;
74+
$pb++;
75+
}
76+
elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') {
77+
$seen_plusminus = 1;
78+
$pa++;
79+
$pb++;
80+
}
81+
else {
82+
last;
83+
}
84+
}
85+
86+
# Find common suffix, ignoring colors.
87+
my ($sa, $sb) = ($#a, $#b);
88+
while ($sa >= $pa && $sb >= $pb) {
89+
if ($a[$sa] =~ /$COLOR/) {
90+
$sa--;
91+
}
92+
elsif ($b[$sb] =~ /$COLOR/) {
93+
$sb--;
94+
}
95+
elsif ($a[$sa] eq $b[$sb]) {
96+
$sa--;
97+
$sb--;
98+
}
99+
else {
100+
last;
101+
}
102+
}
103+
104+
print highlight(\@a, $pa, $sa);
105+
print highlight(\@b, $pb, $sb);
106+
}
107+
108+
sub split_line {
109+
local $_ = shift;
110+
return map { /$COLOR/ ? $_ : (split //) }
111+
split /($COLOR*)/;
112+
}
113+
114+
sub highlight {
115+
my ($line, $prefix, $suffix) = @_;
116+
117+
return join('',
118+
@{$line}[0..($prefix-1)],
119+
$HIGHLIGHT,
120+
@{$line}[$prefix..$suffix],
121+
$UNHIGHLIGHT,
122+
@{$line}[($suffix+1)..$#$line]
123+
);
124+
}

contrib/git-jump/README

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
git-jump
2+
========
3+
4+
Git-jump is a script for helping you jump to "interesting" parts of your
5+
project in your editor. It works by outputting a set of interesting
6+
spots in the "quickfix" format, which editors like vim can use as a
7+
queue of places to visit (this feature is usually used to jump to errors
8+
produced by a compiler). For example, given a diff like this:
9+
10+
------------------------------------
11+
diff --git a/foo.c b/foo.c
12+
index a655540..5a59044 100644
13+
--- a/foo.c
14+
+++ b/foo.c
15+
@@ -1,3 +1,3 @@
16+
int main(void) {
17+
- printf("hello word!\n");
18+
+ printf("hello world!\n");
19+
}
20+
-----------------------------------
21+
22+
git-jump will feed this to the editor:
23+
24+
-----------------------------------
25+
foo.c:2: printf("hello word!\n");
26+
-----------------------------------
27+
28+
Obviously this trivial case isn't that interesting; you could just open
29+
`foo.c` yourself. But when you have many changes scattered across a
30+
project, you can use the editor's support to "jump" from point to point.
31+
32+
Git-jump can generate three types of interesting lists:
33+
34+
1. The beginning of any diff hunks.
35+
36+
2. The beginning of any merge conflict markers.
37+
38+
3. Any grep matches.
39+
40+
41+
Using git-jump
42+
--------------
43+
44+
To use it, just drop git-jump in your PATH, and then invoke it like
45+
this:
46+
47+
--------------------------------------------------
48+
# jump to changes not yet staged for commit
49+
git jump diff
50+
51+
# jump to changes that are staged for commit; you can give
52+
# arbitrary diff options
53+
git jump diff --cached
54+
55+
# jump to merge conflicts
56+
git jump merge
57+
58+
# jump to all instances of foo_bar
59+
git jump grep foo_bar
60+
61+
# same as above, but case-insensitive; you can give
62+
# arbitrary grep options
63+
git jump grep -i foo_bar
64+
--------------------------------------------------
65+
66+
67+
Related Programs
68+
----------------
69+
70+
You can accomplish some of the same things with individual tools. For
71+
example, you can use `git mergetool` to start vimdiff on each unmerged
72+
file. `git jump merge` is for the vim-wielding luddite who just wants to
73+
jump straight to the conflict text with no fanfare.
74+
75+
As of git v1.7.2, `git grep` knows the `--open-files-in-pager` option,
76+
which does something similar to `git jump grep`. However, it is limited
77+
to positioning the cursor to the correct line in only the first file,
78+
leaving you to locate subsequent hits in that file or other files using
79+
the editor or pager. By contrast, git-jump provides the editor with a
80+
complete list of files and line numbers for each match.
81+
82+
83+
Limitations
84+
-----------
85+
86+
This scripts was written and tested with vim. Given that the quickfix
87+
format is the same as what gcc produces, I expect emacs users have a
88+
similar feature for iterating through the list, but I know nothing about
89+
how to activate it.
90+
91+
The shell snippets to generate the quickfix lines will almost certainly
92+
choke on filenames with exotic characters (like newlines).

contrib/git-jump/git-jump

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/sh
2+
3+
usage() {
4+
cat <<\EOF
5+
usage: git jump <mode> [<args>]
6+
7+
Jump to interesting elements in an editor.
8+
The <mode> parameter is one of:
9+
10+
diff: elements are diff hunks. Arguments are given to diff.
11+
12+
merge: elements are merge conflicts. Arguments are ignored.
13+
14+
grep: elements are grep hits. Arguments are given to grep.
15+
EOF
16+
}
17+
18+
open_editor() {
19+
editor=`git var GIT_EDITOR`
20+
eval "$editor -q \$1"
21+
}
22+
23+
mode_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+
38+
mode_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.
50+
mode_grep() {
51+
git grep -n "$@" |
52+
perl -pe '
53+
s/[ \t]+/ /g;
54+
s/^ *//;
55+
'
56+
}
57+
58+
if test $# -lt 1; then
59+
usage >&2
60+
exit 1
61+
fi
62+
mode=$1; shift
63+
64+
trap 'rm -f "$tmp"' 0 1 2 3 15
65+
tmp=`mktemp -t git-jump.XXXXXX` || exit 1
66+
type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }
67+
"mode_$mode" "$@" >"$tmp"
68+
test -s "$tmp" || exit 0
69+
open_editor "$tmp"

0 commit comments

Comments
 (0)