Skip to contentSkip to conversation

add: introduce new --exclude option

PATCH In review Alexander Kuleshov 3 participants 7 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 add: introduce new --exclude option

We already have core.excludesfile configuration variable which indicates a path to file which contains patterns to exclude. This patch provides ability to pass --exclude option to the git add command to exclude paths from command line in addition to which specified in the ignore files.

This option can be useful in a case when we have a directory with some *.ext files which have changes and we want to commit all files besides one for now. It can be too annoying to touch .gitignore for this.

---
 builtin/add.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
builtin/add.c +12 -0
index 3390933..e165fbc 100644
@@ -244,6 +244,8 @@ static int ignore_removal_cb(const struct option *opt, const char *arg, int unse
 	return 0;
 }
 
+static struct string_list exclude_list = STRING_LIST_INIT_NODUP;
+
 static struct option builtin_add_options[] = {
 	OPT__DRY_RUN(&show_only, N_("dry run")),
 	OPT__VERBOSE(&verbose, N_("be verbose")),
@@ -255,6 +257,8 @@ static struct option builtin_add_options[] = {
 	OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
 	OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
 	OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
+	OPT_STRING_LIST(0, "exclude", &exclude_list, N_("pattern"),
+			N_("do not add files matching pattern to index")),
 	{ OPTION_CALLBACK, 0, "ignore-removal", &addremove_explicit,
 	  NULL /* takes no arguments */,
 	  N_("ignore paths removed in the working tree (same as --no-all)"),
@@ -305,6 +309,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	int add_new_files;
 	int require_pathspec;
 	char *seen = NULL;
+	struct exclude_list *el;
 
 	git_config(add_config, NULL);
 
@@ -379,8 +384,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		/* Set up the default git porcelain excludes */
 		memset(&dir, 0, sizeof(dir));
 		if (!ignored_too) {
+			int i;
 			dir.flags |= DIR_COLLECT_IGNORED;
 			setup_standard_excludes(&dir);
+
+			el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
+			for (i = 0; i < exclude_list.nr; i++)
+				add_exclude(exclude_list.items[i].string, "", 0, el, -(i+1));
+
 		}
 
 		memset(&empty_pathspec, 0, sizeof(empty_pathspec));
@@ -446,5 +457,6 @@ finish:
 			die(_("Unable to write new index file"));
 	}
 
+	string_list_clear(&exclude_list, 0);
 	return exit_status;
 }
Signature · 1 line
2.3.3.472.g20ceeac.dirty
#1 lore raw

PATCH v2 2/3 Documentation/git-add.txt: describe --exclude option

---
 Documentation/git-add.txt | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Documentation/git-add.txt +5 -1
index f2eb907..fee97ed 100644
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'git add' [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
-	  [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
+	  [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]] [--exclude=<pattern>]
 	  [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing]
 	  [--] [<pathspec>...]
 
@@ -164,6 +164,10 @@ for "git add --no-all <pathspec>...", i.e. ignored removed files.
 	be ignored, no matter if they are already present in the work
 	tree or not.
 
+--exclude=<pattern>::
+	Also ignore files matching <pattern>, a .gitignore-like
+	pattern. Option can be used multiply times.
+
 \--::
 	This option can be used to separate command-line options from
 	the list of files, (useful when filenames might be mistaken
Signature · 1 line
2.3.3.472.g20ceeac.dirty
#3 lore raw

PATCH v2 3/3 t3700-add: added test for --exclude option

In reply to Alexander Kuleshov
---
 t/t3700-add.sh | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
t/t3700-add.sh +18 -0
index f7ff1f5..6f71c67 100755
@@ -332,4 +332,22 @@ test_expect_success 'git add --dry-run --ignore-missing of non-existing file out
 	test_i18ncmp expect.err actual.err
 '
 
+test_expect_success 'Test that "git add --exclude" works' '
+	>foo &&
+	>bar &&
+	git add --exclude=bar . &&
+	! (git ls-files | grep bar)
+	(git ls-files | grep foo)
+'
+
+test_expect_success 'Test multiply --exclude' '
+	>foo &&
+	>bar &&
+	>"b a z" &&
+	git add --exclude="bar" --exclude="b a z" . &&
+	(git ls-files | grep foo)
+	! (git ls-files | grep "b a z")
+	! (git ls-files | grep "baz")
+'
+
 test_done
Signature · 1 line
2.3.3.472.g20ceeac.dirty
#2 lore raw

Re: [PATCH 2/3 v2] Documentation/git-add.txt: describe --exclude option

In reply to Alexander Kuleshov
Quoted 17 lines · Alexander Kuleshov

---

Documentation/git-add.txt +1 -1
index f2eb907..fee97ed 100644
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'git add' [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
-         [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
+         [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]] [--exclude=<pattern>]
          [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing]
          [--] [<pathspec>...]
 

Documentation/git-add.txt for "git add --no-all <pathspec>...", i.e. ignored removed files. · line 169

Quoted 6 lines
Documentation/git-add.txt +3 -0
@@ -164,6 +164,10 @@ for "git add --no-all <pathspec>...", i.e. ignored removed files.
        be ignored, no matter if they are already present in the work
        tree or not.
 
+--exclude=<pattern>::
+       Also ignore files matching <pattern>, a .gitignore-like
+       pattern. Option can be used multiply times.

s/multiply/multiple/

Quoted 6 lines
Documentation/git-add.txt +1 -1
+
 \--::
        This option can be used to separate command-line options from
        the list of files, (useful when filenames might be mistaken
--

2.3.3.472.g20ceeac.dirty

#4 lore raw

Re: [PATCH 3/3 v2] t3700-add: added test for --exclude option

In reply to Alexander Kuleshov
Quoted 1 line · Alexander Kuleshov

t3700-add: added test for --exclude option

s/added/add/

t/t3700-add.sh test_expect_success 'git add --dry-run --ignore-missing of non-existing file out · line 335

Quoted 11 lines

---

t/t3700-add.sh +1 -0
index f7ff1f5..6f71c67 100755
@@ -332,4 +332,22 @@ test_expect_success 'git add --dry-run --ignore-missing of non-existing file out
        test_i18ncmp expect.err actual.err
 '
 
+test_expect_success 'Test that "git add --exclude" works' '

Rather than inventing a new title style, try to match the style of the existing tests titles in this file.

t/t3700-add.sh test_expect_success 'git add --dry-run --ignore-missing of non-existing file out · line 339

Quoted 4 lines
t/t3700-add.sh +4 -0
+       >foo &&
+       >bar &&
+       git add --exclude=bar . &&
+       ! (git ls-files | grep bar)

Broken &&-chain.

t/t3700-add.sh test_expect_success 'git add --dry-run --ignore-missing of non-existing file out · line 340

Quoted 1 line
t/t3700-add.sh +1 -0
+       (git ls-files | grep foo)

Unnecessary additional git-ls-files invocation. You could just save the output to a file and then process that file.

t/t3700-add.sh test_expect_success 'git add --dry-run --ignore-missing of non-existing file out · line 343

Quoted 3 lines
t/t3700-add.sh +3 -0
+'
+
+test_expect_success 'Test multiply --exclude' '

s/multiply/multiple/

Ditto: Match existing title style.

t/t3700-add.sh test_expect_success 'git add --dry-run --ignore-missing of non-existing file out · line 350

Quoted 7 lines
t/t3700-add.sh +7 -0
+       >foo &&
+       >bar &&
+       >"b a z" &&
+       git add --exclude="bar" --exclude="b a z" . &&
+       (git ls-files | grep foo)
+       ! (git ls-files | grep "b a z")
+       ! (git ls-files | grep "baz")

Broken &&-chain throughout.

Ditto: Unnecessary git-ls-files invocations.

Quoted 5 lines
t/t3700-add.sh +2 -1
+'
+
 test_done
--

2.3.3.472.g20ceeac.dirty

#5 lore raw
In reply to Alexander Kuleshov
Quoted 8 lines · Alexander Kuleshov

We already have core.excludesfile configuration variable which indicates a path to file which contains patterns to exclude. This patch provides ability to pass --exclude option to the git add command to exclude paths from command line in addition to which specified in the ignore files.

This option can be useful in a case when we have a directory with some *.ext files which have changes and we want to commit all files besides one for now. It can be too annoying to touch .gitignore for this.

Won't this lead to unintuitive behavior? The 'excludes' mechanism does not unconditionally ignore; instead, it ignores _untracked_ files. Consider file "foo" which is already tracked. Make a temporary change to "foo" which you don't intend to commit. Since "foo" is tracked, the command 'git add . --exclude=foo' will still add "foo" to the index, despite the use of --exclude. Most people would probably find such behavior surprising and undesirable.

The negative pathspec mentioned by Junio[1], on the other hand, does not suffer this shortcoming.

More below.

[1]: http://thread.gmane.org/gmane.comp.version-control.git/265493/focus=265518

Quoted 28 lines

---

builtin/add.c +4 -0
index 3390933..e165fbc 100644
@@ -244,6 +244,8 @@ static int ignore_removal_cb(const struct option *opt, const char *arg, int unse
        return 0;
 }
 
+static struct string_list exclude_list = STRING_LIST_INIT_NODUP;
+
 static struct option builtin_add_options[] = {
        OPT__DRY_RUN(&show_only, N_("dry run")),
        OPT__VERBOSE(&verbose, N_("be verbose")),
@@ -255,6 +257,8 @@ static struct option builtin_add_options[] = {
        OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
        OPT_BOOL('N', "intent-to-add", &intent_to_add, N_("record only the fact that the path will be added later")),
        OPT_BOOL('A', "all", &addremove_explicit, N_("add changes from all tracked and untracked files")),
+       OPT_STRING_LIST(0, "exclude", &exclude_list, N_("pattern"),
+                       N_("do not add files matching pattern to index")),
        { OPTION_CALLBACK, 0, "ignore-removal", &addremove_explicit,
          NULL /* takes no arguments */,
          N_("ignore paths removed in the working tree (same as --no-all)"),

builtin/add.c int cmd_add(int argc, const char **argv, const char *prefix) · line 312

Quoted 4 lines
builtin/add.c +1 -0
@@ -305,6 +309,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
        int add_new_files;
        int require_pathspec;
        char *seen = NULL;
+       struct exclude_list *el;

This variable is only used within the 'if (!ignored_too)' block below, so its declaration should be moved there.

Quoted 26 lines
builtin/add.c +7 -1
        git_config(add_config, NULL);
 
@@ -379,8 +384,14 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                /* Set up the default git porcelain excludes */
                memset(&dir, 0, sizeof(dir));
                if (!ignored_too) {
+                       int i;
                        dir.flags |= DIR_COLLECT_IGNORED;
                        setup_standard_excludes(&dir);
+
+                       el = add_exclude_list(&dir, EXC_CMDL, "--exclude option");
+                       for (i = 0; i < exclude_list.nr; i++)
+                               add_exclude(exclude_list.items[i].string, "", 0, el, -(i+1));
+
                }
 
                memset(&empty_pathspec, 0, sizeof(empty_pathspec));
@@ -446,5 +457,6 @@ finish:
                        die(_("Unable to write new index file"));
        }
 
+       string_list_clear(&exclude_list, 0);
        return exit_status;
 }
--

2.3.3.472.g20ceeac.dirty

#6 lore raw
In reply to Alexander Kuleshov

From: "Alexander Kuleshov" <[email protected]>

Quoted 4 lines

While the kudo points are nice to have, I wouldn't say my minor contribution was worthy of the extra Helped-by.

I'd be happy for it to be dropped if you have any rewrites.

Quoted 2 lines

---

--
Philip