Skip to content

Commit 8cf666c

Browse files
committed
Merge branch 'cb/diff-fname-optim'
* cb/diff-fname-optim: diff: avoid repeated scanning while looking for funcname do not search functions for patch ID add rebase patch id tests
2 parents 6a2e93f + c099789 commit 8cf666c

File tree

3 files changed

+124
-25
lines changed

3 files changed

+124
-25
lines changed

diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3894,7 +3894,7 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
38943894

38953895
xpp.flags = 0;
38963896
xecfg.ctxlen = 3;
3897-
xecfg.flags = XDL_EMIT_FUNCNAMES;
3897+
xecfg.flags = 0;
38983898
xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
38993899
&xpp, &xecfg);
39003900
}

t/t3419-rebase-patch-id.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/bash
2+
3+
test_description='git rebase - test patch id computation'
4+
5+
. ./test-lib.sh
6+
7+
test_set_prereq NOT_EXPENSIVE
8+
test -n "$GIT_PATCHID_TIMING_TESTS" && test_set_prereq EXPENSIVE
9+
test -x /usr/bin/time && test_set_prereq USR_BIN_TIME
10+
11+
count()
12+
{
13+
i=0
14+
while test $i -lt $1
15+
do
16+
echo "$i"
17+
i=$(($i+1))
18+
done
19+
}
20+
21+
scramble()
22+
{
23+
i=0
24+
while read x
25+
do
26+
if test $i -ne 0
27+
then
28+
echo "$x"
29+
fi
30+
i=$(((i+1) % 10))
31+
done < "$1" > "$1.new"
32+
mv -f "$1.new" "$1"
33+
}
34+
35+
run()
36+
{
37+
echo \$ "$@"
38+
/usr/bin/time "$@" >/dev/null
39+
}
40+
41+
test_expect_success 'setup' '
42+
git commit --allow-empty -m initial
43+
git tag root
44+
'
45+
46+
do_tests()
47+
{
48+
pr=$1
49+
nlines=$2
50+
51+
test_expect_success $pr "setup: $nlines lines" "
52+
rm -f .gitattributes &&
53+
git checkout -q -f master &&
54+
git reset --hard root &&
55+
count $nlines >file &&
56+
git add file &&
57+
git commit -q -m initial &&
58+
git branch -f other &&
59+
60+
scramble file &&
61+
git add file &&
62+
git commit -q -m 'change big file' &&
63+
64+
git checkout -q other &&
65+
: >newfile &&
66+
git add newfile &&
67+
git commit -q -m 'add small file' &&
68+
69+
git cherry-pick master >/dev/null 2>&1
70+
"
71+
72+
test_debug "
73+
run git diff master^\!
74+
"
75+
76+
test_expect_success $pr 'setup attributes' "
77+
echo 'file binary' >.gitattributes
78+
"
79+
80+
test_debug "
81+
run git format-patch --stdout master &&
82+
run git format-patch --stdout --ignore-if-in-upstream master
83+
"
84+
85+
test_expect_success $pr 'detect upstream patch' "
86+
git checkout -q master &&
87+
scramble file &&
88+
git add file &&
89+
git commit -q -m 'change big file again' &&
90+
git checkout -q other^{} &&
91+
git rebase master &&
92+
test_must_fail test -n \"\$(git rev-list master...HEAD~)\"
93+
"
94+
95+
test_expect_success $pr 'do not drop patch' "
96+
git branch -f squashed master &&
97+
git checkout -q -f squashed &&
98+
git reset -q --soft HEAD~2 &&
99+
git commit -q -m squashed &&
100+
git checkout -q other^{} &&
101+
test_must_fail git rebase squashed &&
102+
rm -rf .git/rebase-apply
103+
"
104+
}
105+
106+
do_tests NOT_EXPENSIVE 500
107+
do_tests EXPENSIVE 50000
108+
109+
test_done

xdiff/xemit.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,6 @@ static long def_ff(const char *rec, long len, char *buf, long sz, void *priv)
8585
return -1;
8686
}
8787

88-
static void xdl_find_func(xdfile_t *xf, long i, char *buf, long sz, long *ll,
89-
find_func_t ff, void *ff_priv) {
90-
91-
/*
92-
* Be quite stupid about this for now. Find a line in the old file
93-
* before the start of the hunk (and context) which starts with a
94-
* plausible character.
95-
*/
96-
97-
const char *rec;
98-
long len;
99-
100-
while (i-- > 0) {
101-
len = xdl_get_rec(xf, i, &rec);
102-
if ((*ll = ff(rec, len, buf, sz, ff_priv)) >= 0)
103-
return;
104-
}
105-
*ll = 0;
106-
}
107-
108-
10988
static int xdl_emit_common(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
11089
xdemitconf_t const *xecfg) {
11190
xdfile_t *xdf = &xe->xdf1;
@@ -127,6 +106,7 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
127106
xdchange_t *xch, *xche;
128107
char funcbuf[80];
129108
long funclen = 0;
109+
long funclineprev = -1;
130110
find_func_t ff = xecfg->find_func ? xecfg->find_func : def_ff;
131111

132112
if (xecfg->flags & XDL_EMIT_COMMON)
@@ -150,9 +130,19 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
150130
*/
151131

152132
if (xecfg->flags & XDL_EMIT_FUNCNAMES) {
153-
xdl_find_func(&xe->xdf1, s1, funcbuf,
154-
sizeof(funcbuf), &funclen,
155-
ff, xecfg->find_func_priv);
133+
long l;
134+
for (l = s1 - 1; l >= 0 && l > funclineprev; l--) {
135+
const char *rec;
136+
long reclen = xdl_get_rec(&xe->xdf1, l, &rec);
137+
long newfunclen = ff(rec, reclen, funcbuf,
138+
sizeof(funcbuf),
139+
xecfg->find_func_priv);
140+
if (newfunclen >= 0) {
141+
funclen = newfunclen;
142+
break;
143+
}
144+
}
145+
funclineprev = s1 - 1;
156146
}
157147
if (xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2,
158148
funcbuf, funclen, ecb) < 0)

0 commit comments

Comments
 (0)