From cc3641e6337cfeee6b6a72cd2e9ffd627c18440e Mon Sep 17 00:00:00 2001 From: epriestley Date: Sun, 6 Nov 2011 17:48:43 -0800 Subject: [PATCH] Prevent ArcanistBundle from generating patches with overlapping hunks Summary: We currently may generate patches which have hunks that include overlapping context. Git was okay with this until at least 1.7.3.4, but started rejecting these patches some time later. We already attempt to detect and prevent this condition, we just don't do a very good job of it. Fix the check so that we avoid generating overlapping hunks. Test Plan: - Cleanly applied troublesome patches under Git 1.7.7.2. - Used "arc export --git" to verify patches generate without overlapping sections. - Locally changed $context to 2 and 1, verified patch behavior was reasonable. Reviewers: kdeggelman, bizrad6, jonathanhester, jungejason, nh, tuomaspelkonen, aran CC: Differential Revision: 1084 --- src/parser/bundle/ArcanistBundle.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/parser/bundle/ArcanistBundle.php b/src/parser/bundle/ArcanistBundle.php index 10320ec3..5f997ca6 100644 --- a/src/parser/bundle/ArcanistBundle.php +++ b/src/parser/bundle/ArcanistBundle.php @@ -301,7 +301,14 @@ class ArcanistBundle { $last_change = $jj; for (; $jj < $n; ++$jj) { if ($lines[$jj][0] == ' ') { - if ($jj - $last_change > $context) { + // NOTE: We must use "context * 2" or we may generate overlapping + // hunks. For instance, if we have "context = 3" and four unchanged + // lines between hunks, we'll include unchanged lines 1, 2, 3 in + // the first hunk and 2, 3, and 4 in the second hunk -- that is, lines + // 2 and 3 will appear twice in the patch. Some time after 1.7.3.4, + // Git stopped cleanly applying patches with overlapping hunks, so be + // careful to avoid generating them. + if ($jj - $last_change > ($context * 2)) { break; } } else { -- 2.43.0