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

[PATCH] git-apply - Add --include=PATH

From
Joe Perches <joe@perches.com>
Date
Aug 23, 2008, 20:37 UTC
Message-ID
<1219523869.18365.106.camel@localhost>
Add similar capability to --exclude=

Allows selection of files to patch from a large patchset.

Signed-off-by: Joe Perches <joe@perches.com>
 Documentation/git-apply.txt |    8 +++++++-
 builtin-apply.c             |   27 +++++++++++++++++++++++++--
 2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
index feb51f1..2467e62 100644
--- a/Documentation/git-apply.txt
+++ b/Documentation/git-apply.txt
@@ -14,7 +14,8 @@ SYNOPSIS
 	  [--allow-binary-replacement | --binary] [--reject] [-z]
 	  [-pNUM] [-CNUM] [--inaccurate-eof] [--recount] [--cached]
 	  [--whitespace=<nowarn|warn|fix|error|error-all>]
-	  [--exclude=PATH] [--directory=<root>] [--verbose] [<patch>...]
+	  [--exclude=PATH] [--include=PATH] [--directory=<root>]
+	  [--verbose] [<patch>...]
 
 DESCRIPTION
 -----------
@@ -137,6 +138,11 @@ discouraged.
 	be useful when importing patchsets, where you want to exclude certain
 	files or directories.
 
+--include=<path-pattern>::
+	Apply changes to files matching the given path pattern. This can
+	be useful when importing patchsets, where you want to include certain
+	files or directories.
+
 --whitespace=<action>::
 	When applying a patch, detect a new or modified line that has
 	whitespace errors.  What are considered whitespace errors is
diff --git a/builtin-apply.c b/builtin-apply.c
index 2216a0b..121a6d0 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -46,7 +46,7 @@ static const char *fake_ancestor;
 static int line_termination = '\n';
 static unsigned long p_context = ULONG_MAX;
 static const char apply_usage[] =
-"git apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [--reverse] [--reject] [--verbose] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|fix|error|error-all>] <patch>...";
+"git apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [--reverse] [--reject] [--verbose] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|fix|error|error-all>] [--exclude=PATH] [--include=PATH] <patch>...";
 
 static enum ws_error_action {
 	nowarn_ws_error,
@@ -2996,10 +2996,16 @@ static struct excludes {
 	const char *path;
 } *excludes;
 
+static struct includes {
+	struct includes *next;
+	const char *path;
+} *includes;
+
 static int use_patch(struct patch *p)
 {
 	const char *pathname = p->new_name ? p->new_name : p->old_name;
 	struct excludes *x = excludes;
+	struct includes *y = includes;
 	while (x) {
 		if (fnmatch(x->path, pathname, 0) == 0)
 			return 0;
@@ -3011,7 +3017,17 @@ static int use_patch(struct patch *p)
 		    memcmp(prefix, pathname, prefix_length))
 			return 0;
 	}
-	return 1;
+
+	if (!y || !y->path)
+		return 1;
+
+	while (y && y->path) {
+		if (fnmatch(y->path, pathname, 0) == 0)
+			return 1;
+		y = y->next;
+	}
+
+	return 0;
 }
 
 static void prefix_one(char **name)
@@ -3160,6 +3176,13 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 			excludes = x;
 			continue;
 		}
+		if (!prefixcmp(arg, "--include=")) {
+			struct includes *y = xmalloc(sizeof(*y));
+			y->path = arg + 10;
+			y->next = includes;
+			includes = y;
+			continue;
+		}
 		if (!prefixcmp(arg, "-p")) {
 			p_value = atoi(arg + 2);
 			p_value_known = 1;
Next: Junio C Hamano
Message 1 of 4 in “git-apply - Add --include=PATH”
  1. git-apply - Add --include=PATHJoe Perches, Aug 23, 2008
  2. Junio C HamanoAug 24, 2008
  3. Joe PerchesAug 24, 2008
  4. Junio C HamanoAug 25, 2008

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.