git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCHv1bis 2/2] git apply: preserve original whitespace with --ignore-whitespace

From
Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
Date
Jul 2, 2009, 17:48 UTC
Message-ID
<1246556921-5819-3-git-send-email-giuseppe.bilotta@gmail.com>
In-Reply-To
<1246556921-5819-2-git-send-email-giuseppe.bilotta@gmail.com>
---
 builtin-apply.c |   32 +++++++++++++++++++++++++++++---
 1 files changed, 29 insertions(+), 3 deletions(-)
diff --git a/builtin-apply.c b/builtin-apply.c
index 70cc985..24ab286 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -1644,6 +1644,10 @@ static void update_pre_post_images(struct image *preimage,
 	int i, ctx;
 	char *new, *old, *fixed;
 	struct image fixed_preimage;
+	/* Do we need more space for the postimage? */
+	size_t newlen = postimage->len;
+	if (len > preimage->len)
+		newlen += len - preimage->len;
 
 	/*
 	 * Update the preimage with whitespace fixes.  Note that we
@@ -1658,11 +1662,16 @@ static void update_pre_post_images(struct image *preimage,
 	*preimage = fixed_preimage;
 
 	/*
-	 * Adjust the common context lines in postimage, in place.
+	 * Adjust the common context lines in postimage, in place
+	 * if we are not ignoring whitespace differences.
 	 * This is possible because whitespace fixing does not make
 	 * the string grow.
 	 */
-	new = old = postimage->buf;
+	old = postimage->buf;
+	if (ignore_whitespace)
+		new = postimage->buf = xmalloc(newlen);
+	else
+		new = old;
 	fixed = preimage->buf;
 	for (i = ctx = 0; i < postimage->nr; i++) {
 		size_t len = postimage->line[i].len;
@@ -1738,8 +1747,25 @@ static int match_fragment(struct image *img,
 	     ? (try + preimage->len == img->len)
 	     : (try + preimage->len <= img->len)) &&
 	    lines_match(img->buf + try, img_len,
-	                preimage->buf, preimage->len))
+	                preimage->buf, preimage->len)) {
+		if (ignore_whitespace) {
+			/*
+			 * Replace the preimage whitespace with the original one
+			 */
+			size_t newlen = 0;
+			for (i = 0; i < preimage->nr; i++) {
+				newlen += preimage->line[i].len =
+				          img->line[try_lno + i].len;
+			}
+			fixed_buf = xmalloc(newlen);
+			memcpy(fixed_buf, img->buf + try, newlen);
+
+			update_pre_post_images(
+				preimage, postimage,
+				fixed_buf, newlen);
+		}
 		return 1;
+	}
 
 	if (!ignore_whitespace && (ws_error_action != correct_ws_error))
 		return 0;
-- 
1.6.3.3.511.g0ded0.dirty
Previous: Giuseppe BilottaNext: Junio C Hamano
Message 3 of 12 in “[PATCHv1bis 0/2] git apply: cope with whitespace differences”
  1. Giuseppe BilottaJul 2, 2009
  2. 1/2 git apply: option to ignore whitespace differencesGiuseppe Bilotta, Jul 2, 2009
  3. 2/2 git apply: preserve original whitespace with --ignore-whitespaceGiuseppe Bilotta, Jul 2, 2009
  4. Junio C HamanoJul 2, 2009
  5. Giuseppe BilottaJul 2, 2009
  6. Giuseppe BilottaJul 2, 2009
  7. Junio C HamanoJul 2, 2009
  8. Giuseppe BilottaJul 2, 2009
  9. Junio C HamanoJul 2, 2009
  10. Giuseppe BilottaJul 2, 2009
  11. Junio C HamanoJul 2, 2009
  12. Giuseppe BilottaJul 3, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.