Merge branch 'jc/maint-apply-match-beginning'

* jc/maint-apply-match-beginning:
  Fix "git apply" to correctly enforce "match at the beginning"
This commit is contained in:
Junio C Hamano 2008-04-06 20:04:29 -07:00
Родитель aba201c6e8 ee5a317e01
Коммит a1c0dca43a
2 изменённых файлов: 29 добавлений и 13 удалений

Просмотреть файл

@ -1937,21 +1937,24 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
trailing = frag->trailing; trailing = frag->trailing;
/* /*
* If we don't have any leading/trailing data in the patch, * A hunk to change lines at the beginning would begin with
* we want it to match at the beginning/end of the file. * @@ -1,L +N,M @@
* *
* But that would break if the patch is generated with * And a hunk to add to an empty file would begin with
* --unified=0; sane people wouldn't do that to cause us * @@ -0,0 +N,M @@
* trouble, but we try to please not so sane ones as well. *
* In other words, a hunk that is (frag->oldpos <= 1) with or
* without leading context must match at the beginning.
*/ */
if (unidiff_zero) { match_beginning = frag->oldpos <= 1;
match_beginning = (!leading && !frag->oldpos);
match_end = 0; /*
} * A hunk without trailing lines must match at the end.
else { * However, we simply cannot tell if a hunk must match end
match_beginning = !leading && (frag->oldpos == 1); * from the lack of trailing lines if the patch was generated
match_end = !trailing; * with unidiff without any context.
} */
match_end = !unidiff_zero && !trailing;
pos = frag->newpos ? (frag->newpos - 1) : 0; pos = frag->newpos ? (frag->newpos - 1) : 0;
preimage.buf = oldlines; preimage.buf = oldlines;

Просмотреть файл

@ -112,4 +112,17 @@ do
' '
done done
test_expect_success 'two lines' '
>file &&
git add file &&
echo aaa >file &&
git diff >patch &&
git add file &&
echo bbb >file &&
git add file &&
test_must_fail git apply --check patch
'
test_done test_done