threads / patch / 1682

patch, 9 partsNew option --ignore-applied for git-apply.

Subject: [PATCH 7/9] New option --ignore-applied for git-apply.

## tl;dr

5 messages between Aug 28, 2005 and Aug 28, 2005. Diffs are folded; open one to read it.

replies: 4people: 4as markdown or json

Robert Fitzsimons· Aug 28, 2005, 15:24 UTC · lore

[PATCH 6/9] New option --ignore-whitespace for git-apply.

Allow the user to force a patch to be applied even though there might be whitespace differences. Added a test case for the new option.

Signed-off-by: Robert Fitzsimons <robfitz@273k.net>
---
 Documentation/git-apply.txt        |    7 +++-
 apply.c                            |   38 ++++++++++++++++++-
 t/t4107-apply-ignore-whitespace.sh |   72 ++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+), 4 deletions(-)
 create mode 100644 t/t4107-apply-ignore-whitespace.sh
a928441d83ccc347b1d57fc561d413da56b93197
Show changes to 3 files +111 −4

Documentation/git-apply.txt, apply.c, t/t4107-apply-ignore-whitespace.sh

diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
--- a/Documentation/git-apply.txt
+++ b/Documentation/git-apply.txt
@@ -11,7 +11,8 @@ SYNOPSIS
 --------
 'git-apply' [--no-merge] [--stat] [--summary] [--check]
 		[--index] [--show-files] [--apply]
-		[--force-delete] [<patch>...]
+		[--force-delete] [--ignore-whitespace]
+		[<patch>...]
 
 DESCRIPTION
 -----------
@@ -68,6 +69,10 @@ OPTIONS
 	file contents would be left behind, lines added or the
 	patch will not apply.
 
+--ignore-whitespace::
+	When matching the patch to the file contents ignore
+	sequences of tabs or spaces.
+
 Author
 ------
 Written by Linus Torvalds <torvalds@osdl.org>
diff --git a/apply.c b/apply.c
--- a/apply.c
+++ b/apply.c
@@ -33,8 +33,9 @@ static int check = 0;
 static int apply = 1;
 static int show_files = 0;
 static int force_delete = 0;
+static int ignore_whitespace = 0;
 static const char apply_usage[] =
-"git-apply [--no-merge] [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--force-delete] <patch>...";
+"git-apply [--no-merge] [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--force-delete] [--ignore-whitespace] <patch>...";
 
 /*
  * For "diff-stat" like behaviour, we keep track of the biggest change
@@ -109,6 +110,27 @@ static void *read_patch_file(int fd, uns
 	return buffer;
 }
 
+static int memcmp_ignore_whitespace(const char *s1, const char *s2, size_t n)
+{
+	const char *stop = s1 + n;
+	int result;
+
+	if (!n)
+		return 0;
+
+	do {
+		if (((*s1 == ' ') || (*s1 == '\t')) && ((*s2 == ' ') || (*s2 == '\t'))) {
+			while ((*s1 == ' ') || (*s1 == '\t'))
+				s1++;
+			while ((*s2 == ' ') || (*s2 == '\t'))
+				s2++;
+		}
+		result = *s1++ - *s2++;
+	} while (!result && (s1 < stop));
+
+	return result;
+}
+
 static unsigned long linelen(const char *buffer, unsigned long size)
 {
 	unsigned long len = 0;
@@ -839,7 +861,10 @@ static int find_offset(const char *buf, 
 	}
 
 	/* Exact line number? */
-	if (!memcmp(buf + start, fragment, fragsize))
+	if (ignore_whitespace) {
+		if (!memcmp_ignore_whitespace(buf + start, fragment, fragsize))
+			return start;
+	} else if (!memcmp(buf + start, fragment, fragsize))
 		return start;
 
 	/*
@@ -873,7 +898,10 @@ static int find_offset(const char *buf, 
 
 		if (try + fragsize > size)
 			continue;
-		if (memcmp(buf + try, fragment, fragsize))
+		if (ignore_whitespace) {
+			if (memcmp_ignore_whitespace(buf + try, fragment, fragsize))
+				continue;
+		} else if (memcmp(buf + try, fragment, fragsize))
 			continue;
 		n = (i >> 1)+1;
 		if (i & 1)
@@ -1575,6 +1603,10 @@ int main(int argc, char **argv)
 			force_delete = 1;
 			continue;
 		}
+		if (!strcmp(arg, "--ignore-whitespace")) {
+			ignore_whitespace = 1;
+			continue;
+		}
 		fd = open(arg, O_RDONLY);
 		if (fd < 0)
 			usage(apply_usage);
diff --git a/t/t4107-apply-ignore-whitespace.sh b/t/t4107-apply-ignore-whitespace.sh
new file mode 100644
--- /dev/null
+++ b/t/t4107-apply-ignore-whitespace.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+# Copyright (c) 2005 Robert Fitzsimons
+#
+
+test_description='git-apply --ignore-whitespace.
+
+'
+. ./test-lib.sh
+
+# setup
+
+cat > patch1.patch <<\EOF
+diff --git a/main.c b/main.c
+new file mode 100644
+--- /dev/null
++++ b/main.c
+@@ -0,0 +1,23 @@
++#include <stdio.h>
++
++void print_int(int num);
++int func(int num);
++
++int main() {
++       int i;
++
++       for (i = 0; i < 10; i++) {
++               print_int(func(i));
++       }
++
++       return 0;
++}
++
++int func(int num) {
++       return num * num;
++}
++
++void print_int(int num) {
++       printf("%d", num);
++}
++
+EOF
+cat > patch2.patch <<\EOF
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -10,6 +10,8 @@
+ 		print_int(func(i));
+ 	}
+ 
++	printf("\n");
++
+ 	return 0;
+ }
+EOF
+
+test_expect_success "S = patch1" \
+    'git-apply patch1.patch'
+
+test_expect_failure "F = patch2" \
+    'git-apply patch2.patch'
+
+test_expect_success "S = patch2 (--ignore-whitespace)" \
+    'git-apply --ignore-whitespace patch2.patch'
+
+rm -f main.c
+test_expect_success "S = patch1 (--ignore-whitespace)" \
+    'git-apply --ignore-whitespace patch1.patch'
+
+test_done
+
Robert Fitzsimons· Aug 28, 2005, 15:24 UTC · re: Robert Fitzsimons · lore

Allow the user to allow a patch which has some hunks (fragments) already applied to succeed. Added test case and documentation.

Signed-off-by: Robert Fitzsimons <robfitz@273k.net>
---
 Documentation/git-apply.txt     |    6 ++
 apply.c                         |   12 +++++
 t/t4108-apply-ignore-applied.sh |   94 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+), 2 deletions(-)
 create mode 100644 t/t4108-apply-ignore-applied.sh
5e36f3b7c8ca0710096552ce3f20f16da04e601b
Show changes to 3 files +107 −2

Documentation/git-apply.txt, apply.c, t/t4108-apply-ignore-applied.sh

diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
--- a/Documentation/git-apply.txt
+++ b/Documentation/git-apply.txt
@@ -12,7 +12,7 @@ SYNOPSIS
 'git-apply' [--no-merge] [--stat] [--summary] [--check]
 		[--index] [--show-files] [--apply]
 		[--force-delete] [--ignore-whitespace]
-		[<patch>...]
+		[--ignore-applied] [<patch>...]
 
 DESCRIPTION
 -----------
@@ -73,6 +73,10 @@ OPTIONS
 	When matching the patch to the file contents ignore
 	sequences of tabs or spaces.
 
+--ignore-applied::
+	If a patch hunk (fragment) fails to apply, reverse the
+	hunk and check if the hunk has already been applied.
+
 Author
 ------
 Written by Linus Torvalds <torvalds@osdl.org>
diff --git a/apply.c b/apply.c
--- a/apply.c
+++ b/apply.c
@@ -34,8 +34,9 @@ static int apply = 1;
 static int show_files = 0;
 static int force_delete = 0;
 static int ignore_whitespace = 0;
+static int ignore_applied = 0;
 static const char apply_usage[] =
-"git-apply [--no-merge] [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--force-delete] [--ignore-whitespace] <patch>...";
+"git-apply [--no-merge] [--stat] [--summary] [--check] [--index] [--apply] [--show-files] [--force-delete] [--ignore-whitespace] [--ignore-applied] <patch>...";
 
 /*
  * For "diff-stat" like behaviour, we keep track of the biggest change
@@ -985,6 +986,11 @@ static int apply_one_fragment(struct buf
 		memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
 		memcpy(buf + offset, new, newsize);
 		offset = 0;
+	} else if (ignore_applied) {
+		offset = find_offset(buf, desc->size, new, newsize, frag->newpos);
+		if (offset >= 0) {
+			offset = 0;
+		}
 	}
 
 	free(old);
@@ -1607,6 +1613,10 @@ int main(int argc, char **argv)
 			ignore_whitespace = 1;
 			continue;
 		}
+		if (!strcmp(arg, "--ignore-applied")) {
+			ignore_applied = 1;
+			continue;
+		}
 		fd = open(arg, O_RDONLY);
 		if (fd < 0)
 			usage(apply_usage);
diff --git a/t/t4108-apply-ignore-applied.sh b/t/t4108-apply-ignore-applied.sh
new file mode 100644
--- /dev/null
+++ b/t/t4108-apply-ignore-applied.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+# Copyright (c) 2005 Robert Fitzsimons
+#
+
+test_description='git-apply --ignore-applied.
+
+'
+. ./test-lib.sh
+
+# setup
+
+cat > patch1.patch <<\EOF
+diff --git a/main.c b/main.c
+new file mode 100644
+--- /dev/null
++++ b/main.c
+@@ -0,0 +1,23 @@
++#include <stdio.h>
++
++void print_int(int num);
++int func(int num);
++
++int main() {
++	int i;
++
++	for (i = 0; i < 10; i++) {
++		print_int(func(i));
++	}
++
++	return 0;
++}
++
++int func(int num) {
++	return num * num;
++}
++
++void print_int(int num) {
++	printf("%d", num);
++}
++
+EOF
+cat > patch2.patch <<\EOF
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -10,6 +10,8 @@
+ 		print_int(func(i));
+ 	}
+ 
++	printf("\n");
++
+ 	return 0;
+ }
+ 
+EOF
+cat > patch3.patch <<\EOF
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -10,6 +10,8 @@
+ 		print_int(func(i));
+ 	}
+ 
++	printf("\n");
++
+ 	return 0;
+ }
+ 
+@@ -18,6 +20,6 @@
+ }
+ 
+ void print_int(int num) {
+-	printf("%d", num);
++	printf("%d ", num);
+ }
+ 
+EOF
+
+test_expect_failure "F = test 1" \
+    'cat patch1.patch patch2.patch patch2.patch | git-apply --check'
+
+test_expect_success "S = test 2" \
+    'cat patch1.patch patch2.patch patch2.patch | git-apply --check --ignore-applied'
+
+test_expect_failure "F = test 3" \
+    'cat patch1.patch patch3.patch patch3.patch | git-apply --check'
+
+test_expect_success "S = test 4" \
+    'cat patch1.patch patch2.patch patch3.patch | git-apply --check --ignore-applied'
+
+test_done
+
Linus Torvalds· Aug 28, 2005, 16:58 UTC · re: Robert Fitzsimons · lore

Re: [PATCH 6/9] New option --ignore-whitespace for git-apply.

On Sun, 28 Aug 2005, Robert Fitzsimons wrote:
>
> Allow the user to force a patch to be applied even though there might
> be whitespace differences.  Added a test case for the new option.

If you ignore whitespace, then you should probably accept patches that are whitespace corrupted in another way: lines missing the ' ' at the beginning. Especially lines that are empty. That's actually the most common form of whitespace corruption there is.

		Linus
A Large Angry SCM· Aug 28, 2005, 20:49 UTC · re: Linus Torvalds · lore

Re: [PATCH 6/9] New option --ignore-whitespace for git-apply.

Linus Torvalds wrote:
Show 8 quoted lines
> On Sun, 28 Aug 2005, Robert Fitzsimons wrote:
>>Allow the user to force a patch to be applied even though there might
>>be whitespace differences.  Added a test case for the new option.
> 
> If you ignore whitespace, then you should probably accept patches that are
> whitespace corrupted in another way: lines missing the ' ' at the
> beginning. Especially lines that are empty. That's actually the most 
> common form of whitespace corruption there is.

Actually, a more restricted from of --ignore-whitespace may be useful, --ignore-trailing-whitespace (needs a shorter name!). Ignore only trailing whitespace differences (including empty context lines without the initial space).

Junio C Hamano· Aug 28, 2005, 21:06 UTC · re: Robert Fitzsimons · lore

Re: [PATCH 6/9] New option --ignore-whitespace for git-apply.

Robert Fitzsimons <robfitz@273k.net> writes:
> Allow the user to force a patch to be applied even though there might
> be whitespace differences.  Added a test case for the new option.

It might be worth doing fuzz in general, but treating whitespace-only fuzz specially is a good idea, since they are usually more benign.

← back to recent threads