All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/10] dropping more unused parameters
@ 2020-09-30 12:27 Jeff King
  2020-09-30 12:27 ` [PATCH 01/10] convert: drop unused crlf_action from check_global_conv_flags_eol() Jeff King
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:27 UTC (permalink / raw)
  To: git

This is the last of my series of patches to drop (or use) unused
function parameters. I've been holding on to some of these for almost 2
years, because I wanted to make sure they weren't actually bugs
(dropping unused parameters can never _introduce_ a bug, but it's a good
opportunity to investigate whether the parameter _should_ have been
used). I'm still not entirely convinced that patch 10 isn't actually a
bug, but I wasn't able to puzzle it out either way, and it didn't seem
worth holding up the topic any longer.

After this lands, I have a follow-on series that annotates cases where
we can't drop parameters (e.g., functions which have to conform to a
callback interface, but don't care about some of the parameters). And
then after that we can flip on -Wunused-parameters going forward. Which
I think is worth doing, as it has found some bugs (fixed in earlier
series).

  [01/10]: convert: drop unused crlf_action from check_global_conv_flags_eol()
  [02/10]: drop unused argc parameters
  [03/10]: env--helper: write to opt->value in parseopt helper
  [04/10]: assert PARSE_OPT_NONEG in parse-options callbacks
  [05/10]: push: drop unused repo argument to do_push()
  [06/10]: sequencer: drop repository argument from run_git_commit()
  [07/10]: sparse-checkout: fill in some options boilerplate
  [08/10]: test-advise: check argument count with argc instead of argv
  [09/10]: sequencer: handle ignore_footer when parsing trailers
  [10/10]: dir.c: drop unused "untracked" from treat_path_fast()

 builtin/add.c                                |  4 +--
 builtin/am.c                                 |  2 ++
 builtin/commit-graph.c                       |  2 ++
 builtin/commit.c                             | 12 +++----
 builtin/env--helper.c                        | 13 ++++---
 builtin/push.c                               |  4 +--
 builtin/sparse-checkout.c                    | 37 ++++++++++++++++++++
 commit.h                                     |  2 +-
 convert.c                                    |  4 +--
 dir.c                                        |  3 +-
 parse-options-cb.c                           |  2 ++
 revision.c                                   |  6 ++--
 sequencer.c                                  | 20 +++++++----
 t/helper/test-advise.c                       |  4 +--
 t/helper/test-submodule-nested-repo-config.c |  6 ++--
 15 files changed, 88 insertions(+), 33 deletions(-)

-Peff

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 01/10] convert: drop unused crlf_action from check_global_conv_flags_eol()
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
@ 2020-09-30 12:27 ` Jeff King
  2020-09-30 12:28 ` [PATCH 02/10] drop unused argc parameters Jeff King
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:27 UTC (permalink / raw)
  To: git

The crlf_action parameter hasn't been used since a0ad53c181 (convert:
Correct NNO tests and missing `LF will be replaced by CRLF`,
2016-08-13), where that part of the function was hoisted out to a
separate will_convert_lf_to_crlf() helper. Let's drop the useless
parameter.

Signed-off-by: Jeff King <peff@peff.net>
---
 convert.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/convert.c b/convert.c
index 8e6c292421..ee360c2f07 100644
--- a/convert.c
+++ b/convert.c
@@ -195,7 +195,7 @@ static enum eol output_eol(enum crlf_action crlf_action)
 	return core_eol;
 }
 
-static void check_global_conv_flags_eol(const char *path, enum crlf_action crlf_action,
+static void check_global_conv_flags_eol(const char *path,
 			    struct text_stat *old_stats, struct text_stat *new_stats,
 			    int conv_flags)
 {
@@ -547,7 +547,7 @@ static int crlf_to_git(const struct index_state *istate,
 			new_stats.crlf += new_stats.lonelf;
 			new_stats.lonelf = 0;
 		}
-		check_global_conv_flags_eol(path, crlf_action, &stats, &new_stats, conv_flags);
+		check_global_conv_flags_eol(path, &stats, &new_stats, conv_flags);
 	}
 	if (!convert_crlf_into_lf)
 		return 0;
-- 
2.28.0.1173.gad90222cf0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 02/10] drop unused argc parameters
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
  2020-09-30 12:27 ` [PATCH 01/10] convert: drop unused crlf_action from check_global_conv_flags_eol() Jeff King
@ 2020-09-30 12:28 ` Jeff King
  2020-09-30 12:28 ` [PATCH 03/10] env--helper: write to opt->value in parseopt helper Jeff King
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:28 UTC (permalink / raw)
  To: git

Many functions take an argv/argc pair, but never actually look at argc.
This makes it useless at best (we use the NULL sentinel in argv to find
the end of the array), and misleading at worst (what happens if the argc
count does not match the argv NULL?).

In each of these instances, the argv NULL does match the argc count, so
there are no bugs here. But let's tighten the interfaces to make it
harder to get wrong (and to reduce some -Wunused-parameter complaints).

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/add.c                                |  4 ++--
 builtin/commit.c                             | 12 ++++++------
 commit.h                                     |  2 +-
 revision.c                                   |  6 +++---
 t/helper/test-submodule-nested-repo-config.c |  6 +++---
 5 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/builtin/add.c b/builtin/add.c
index 26b6ced09e..a825887c50 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -239,7 +239,7 @@ int run_add_interactive(const char *revision, const char *patch_mode,
 	return status;
 }
 
-int interactive_add(int argc, const char **argv, const char *prefix, int patch)
+int interactive_add(const char **argv, const char *prefix, int patch)
 {
 	struct pathspec pathspec;
 
@@ -451,7 +451,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 	if (add_interactive) {
 		if (pathspec_from_file)
 			die(_("--pathspec-from-file is incompatible with --interactive/--patch"));
-		exit(interactive_add(argc - 1, argv + 1, prefix, patch_interactive));
+		exit(interactive_add(argv + 1, prefix, patch_interactive));
 	}
 	if (legacy_stash_p) {
 		struct pathspec pathspec;
diff --git a/builtin/commit.c b/builtin/commit.c
index 42b964e0ca..1dfd799ec5 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -326,7 +326,7 @@ static void refresh_cache_or_die(int refresh_flags)
 		die_resolve_conflict("commit");
 }
 
-static const char *prepare_index(int argc, const char **argv, const char *prefix,
+static const char *prepare_index(const char **argv, const char *prefix,
 				 const struct commit *current_head, int is_status)
 {
 	struct string_list partial = STRING_LIST_INIT_DUP;
@@ -378,7 +378,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
 		old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
 		setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
 
-		if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
+		if (interactive_add(argv, prefix, patch_interactive) != 0)
 			die(_("interactive add failed"));
 
 		the_repository->index_file = old_repo_index_file;
@@ -1241,13 +1241,13 @@ static int parse_and_validate_options(int argc, const char *argv[],
 	return argc;
 }
 
-static int dry_run_commit(int argc, const char **argv, const char *prefix,
+static int dry_run_commit(const char **argv, const char *prefix,
 			  const struct commit *current_head, struct wt_status *s)
 {
 	int committable;
 	const char *index_file;
 
-	index_file = prepare_index(argc, argv, prefix, current_head, 1);
+	index_file = prepare_index(argv, prefix, current_head, 1);
 	committable = run_status(stdout, index_file, prefix, 0, s);
 	rollback_index_files();
 
@@ -1584,8 +1584,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 		verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
 
 	if (dry_run)
-		return dry_run_commit(argc, argv, prefix, current_head, &s);
-	index_file = prepare_index(argc, argv, prefix, current_head, 0);
+		return dry_run_commit(argv, prefix, current_head, &s);
+	index_file = prepare_index(argv, prefix, current_head, 0);
 
 	/* Set up everything for writing the commit object.  This includes
 	   running hooks, writing the trees, and interacting with the user.  */
diff --git a/commit.h b/commit.h
index e6f8f7c26f..5467786c7b 100644
--- a/commit.h
+++ b/commit.h
@@ -248,7 +248,7 @@ struct oid_array;
 struct ref;
 int for_each_commit_graft(each_commit_graft_fn, void *);
 
-int interactive_add(int argc, const char **argv, const char *prefix, int patch);
+int interactive_add(const char **argv, const char *prefix, int patch);
 int run_add_interactive(const char *revision, const char *patch_mode,
 			const struct pathspec *pathspec);
 
diff --git a/revision.c b/revision.c
index d9dc5781ac..aa62212040 100644
--- a/revision.c
+++ b/revision.c
@@ -2580,8 +2580,8 @@ static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void
 }
 
 static int handle_revision_pseudo_opt(const char *submodule,
-				struct rev_info *revs,
-				int argc, const char **argv, int *flags)
+				      struct rev_info *revs,
+				      const char **argv, int *flags)
 {
 	const char *arg = argv[0];
 	const char *optarg;
@@ -2752,7 +2752,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
 			int opts;
 
 			opts = handle_revision_pseudo_opt(submodule,
-						revs, argc - i, argv + i,
+						revs, argv + i,
 						&flags);
 			if (opts > 0) {
 				i += opts - 1;
diff --git a/t/helper/test-submodule-nested-repo-config.c b/t/helper/test-submodule-nested-repo-config.c
index bc97929bbc..c5fd4527dc 100644
--- a/t/helper/test-submodule-nested-repo-config.c
+++ b/t/helper/test-submodule-nested-repo-config.c
@@ -1,7 +1,7 @@
 #include "test-tool.h"
 #include "submodule-config.h"
 
-static void die_usage(int argc, const char **argv, const char *msg)
+static void die_usage(const char **argv, const char *msg)
 {
 	fprintf(stderr, "%s\n", msg);
 	fprintf(stderr, "Usage: %s <submodulepath> <config name>\n", argv[0]);
@@ -14,13 +14,13 @@ int cmd__submodule_nested_repo_config(int argc, const char **argv)
 	const struct submodule *sub;
 
 	if (argc < 3)
-		die_usage(argc, argv, "Wrong number of arguments.");
+		die_usage(argv, "Wrong number of arguments.");
 
 	setup_git_directory();
 
 	sub = submodule_from_path(the_repository, &null_oid, argv[1]);
 	if (repo_submodule_init(&subrepo, the_repository, sub)) {
-		die_usage(argc, argv, "Submodule not found.");
+		die_usage(argv, "Submodule not found.");
 	}
 
 	/* Read the config of _child_ submodules. */
-- 
2.28.0.1173.gad90222cf0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 03/10] env--helper: write to opt->value in parseopt helper
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
  2020-09-30 12:27 ` [PATCH 01/10] convert: drop unused crlf_action from check_global_conv_flags_eol() Jeff King
  2020-09-30 12:28 ` [PATCH 02/10] drop unused argc parameters Jeff King
@ 2020-09-30 12:28 ` Jeff King
  2020-09-30 12:29 ` [PATCH 04/10] assert PARSE_OPT_NONEG in parse-options callbacks Jeff King
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:28 UTC (permalink / raw)
  To: git

We use OPT_CALLBACK_F() to call the option_parse_type() callback,
passing it the address of "cmdmode" as the value to write to. But the
callback doesn't look at opt->value at all, and instead writes to a
global variable.

This works out because that's the same global variable we happen to pass
in, but it's rather confusing.  Let's use the passed-in value instead.
We'll also make "cmdmode" a local variable of the main function,
ensuring we can't make the same mistake again.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/env--helper.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/builtin/env--helper.c b/builtin/env--helper.c
index 23c214fff6..3aa4282114 100644
--- a/builtin/env--helper.c
+++ b/builtin/env--helper.c
@@ -7,18 +7,20 @@ static char const * const env__helper_usage[] = {
 	NULL
 };
 
-static enum {
+enum cmdmode {
 	ENV_HELPER_TYPE_BOOL = 1,
 	ENV_HELPER_TYPE_ULONG
-} cmdmode = 0;
+};
 
 static int option_parse_type(const struct option *opt, const char *arg,
 			     int unset)
 {
+	enum cmdmode *cmdmode = opt->value;
+
 	if (!strcmp(arg, "bool"))
-		cmdmode = ENV_HELPER_TYPE_BOOL;
+		*cmdmode = ENV_HELPER_TYPE_BOOL;
 	else if (!strcmp(arg, "ulong"))
-		cmdmode = ENV_HELPER_TYPE_ULONG;
+		*cmdmode = ENV_HELPER_TYPE_ULONG;
 	else
 		die(_("unrecognized --type argument, %s"), arg);
 
@@ -33,6 +35,7 @@ int cmd_env__helper(int argc, const char **argv, const char *prefix)
 	int ret;
 	int ret_int, default_int;
 	unsigned long ret_ulong, default_ulong;
+	enum cmdmode cmdmode = 0;
 	struct option opts[] = {
 		OPT_CALLBACK_F(0, "type", &cmdmode, N_("type"),
 			       N_("value is given this type"), PARSE_OPT_NONEG,
-- 
2.28.0.1173.gad90222cf0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 04/10] assert PARSE_OPT_NONEG in parse-options callbacks
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
                   ` (2 preceding siblings ...)
  2020-09-30 12:28 ` [PATCH 03/10] env--helper: write to opt->value in parseopt helper Jeff King
@ 2020-09-30 12:29 ` Jeff King
  2020-09-30 12:29 ` [PATCH 05/10] push: drop unused repo argument to do_push() Jeff King
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:29 UTC (permalink / raw)
  To: git

In the spirit of 517fe807d6 (assert NOARG/NONEG behavior of
parse-options callbacks, 2018-11-05), let's cover some parse-options
callbacks which expect to be used with PARSE_OPT_NONEG but don't
explicitly assert that this is the case. These callbacks are all used
correctly in the current code, but this will help document their
expectations and future-proof the code.

As a bonus, it also silences -Wunused-parameters (these were added since
the initial sweep of 517fe807d6, and we can't yet turn on
-Wunused-parameters to remind people because it has too many existing
false positives).

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/am.c           | 2 ++
 builtin/commit-graph.c | 2 ++
 builtin/env--helper.c  | 2 ++
 parse-options-cb.c     | 2 ++
 4 files changed, 8 insertions(+)

diff --git a/builtin/am.c b/builtin/am.c
index 7259186408..2c7673f74e 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -2180,6 +2180,8 @@ static int parse_opt_show_current_patch(const struct option *opt, const char *ar
 	};
 	int new_value = SHOW_PATCH_RAW;
 
+	BUG_ON_OPT_NEG(unset);
+
 	if (arg) {
 		for (new_value = 0; new_value < ARRAY_SIZE(valid_modes); new_value++) {
 			if (!strcmp(arg, valid_modes[new_value]))
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 988445abdf..78fa08f43a 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -128,6 +128,8 @@ static int write_option_parse_split(const struct option *opt, const char *arg,
 {
 	enum commit_graph_split_flags *flags = opt->value;
 
+	BUG_ON_OPT_NEG(unset);
+
 	opts.split = 1;
 	if (!arg)
 		return 0;
diff --git a/builtin/env--helper.c b/builtin/env--helper.c
index 3aa4282114..27349098b0 100644
--- a/builtin/env--helper.c
+++ b/builtin/env--helper.c
@@ -17,6 +17,8 @@ static int option_parse_type(const struct option *opt, const char *arg,
 {
 	enum cmdmode *cmdmode = opt->value;
 
+	BUG_ON_OPT_NEG(unset);
+
 	if (!strcmp(arg, "bool"))
 		*cmdmode = ENV_HELPER_TYPE_BOOL;
 	else if (!strcmp(arg, "ulong"))
diff --git a/parse-options-cb.c b/parse-options-cb.c
index d9d3b0819f..4542d4d3f9 100644
--- a/parse-options-cb.c
+++ b/parse-options-cb.c
@@ -105,6 +105,8 @@ int parse_opt_commit(const struct option *opt, const char *arg, int unset)
 	struct commit *commit;
 	struct commit **target = opt->value;
 
+	BUG_ON_OPT_NEG(unset);
+
 	if (!arg)
 		return -1;
 	if (get_oid(arg, &oid))
-- 
2.28.0.1173.gad90222cf0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 05/10] push: drop unused repo argument to do_push()
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
                   ` (3 preceding siblings ...)
  2020-09-30 12:29 ` [PATCH 04/10] assert PARSE_OPT_NONEG in parse-options callbacks Jeff King
@ 2020-09-30 12:29 ` Jeff King
  2020-09-30 12:29 ` [PATCH 06/10] sequencer: drop repository argument from run_git_commit() Jeff King
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:29 UTC (permalink / raw)
  To: git

We stopped using the "repo" argument in 8e4c8af058 (push: disallow --all
and refspecs when remote.<name>.mirror is set, 2019-09-02), which moved
the pushremote handling to its caller.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/push.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/builtin/push.c b/builtin/push.c
index 0eeb2c8dd5..6da3a8e5d3 100644
--- a/builtin/push.c
+++ b/builtin/push.c
@@ -379,7 +379,7 @@ static int push_with_options(struct transport *transport, struct refspec *rs,
 	return 1;
 }
 
-static int do_push(const char *repo, int flags,
+static int do_push(int flags,
 		   const struct string_list *push_options,
 		   struct remote *remote)
 {
@@ -629,7 +629,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
 		if (strchr(item->string, '\n'))
 			die(_("push options must not have new line characters"));
 
-	rc = do_push(repo, flags, push_options, remote);
+	rc = do_push(flags, push_options, remote);
 	string_list_clear(&push_options_cmdline, 0);
 	string_list_clear(&push_options_config, 0);
 	if (rc == -1)
-- 
2.28.0.1173.gad90222cf0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 06/10] sequencer: drop repository argument from run_git_commit()
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
                   ` (4 preceding siblings ...)
  2020-09-30 12:29 ` [PATCH 05/10] push: drop unused repo argument to do_push() Jeff King
@ 2020-09-30 12:29 ` Jeff King
  2020-09-30 12:30 ` [PATCH 07/10] sparse-checkout: fill in some options boilerplate Jeff King
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:29 UTC (permalink / raw)
  To: git

When we switched to using an external git-commit call in b0a3186140
(sequencer: simplify root commit creation, 2019-08-19), this function
didn't need to care about the repository object any more.

Arguably we could be passing along the repository path to the external
git-commit by using "--git-dir=r->path" here. But for the most part the
sequencer code relies on sub-process finding the same repository we're
already in (using the same environment variables or discovery process we
did). But we don't have a convenient interface for doing so, and there's
no indication that we need to. Let's just drop the unused parameter for
now.

Signed-off-by: Jeff King <peff@peff.net>
---
 sequencer.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/sequencer.c b/sequencer.c
index e8676e965f..6e9aabaac1 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -934,8 +934,7 @@ static int run_command_silent_on_success(struct child_process *cmd)
  * interactive rebase: in that case, we will want to retain the
  * author metadata.
  */
-static int run_git_commit(struct repository *r,
-			  const char *defmsg,
+static int run_git_commit(const char *defmsg,
 			  struct replay_opts *opts,
 			  unsigned int flags)
 {
@@ -1545,7 +1544,7 @@ static int do_commit(struct repository *r,
 		if (is_rebase_i(opts) && oid)
 			if (write_rebase_head(oid))
 			    return -1;
-		return run_git_commit(r, msg_file, opts, flags);
+		return run_git_commit(msg_file, opts, flags);
 	}
 
 	return res;
@@ -2060,7 +2059,7 @@ static int do_pick_commit(struct repository *r,
 		*check_todo = !!(flags & EDIT_MSG);
 		if (!res && reword) {
 fast_forward_edit:
-			res = run_git_commit(r, NULL, opts, EDIT_MSG |
+			res = run_git_commit(NULL, opts, EDIT_MSG |
 					     VERIFY_MSG | AMEND_MSG |
 					     (flags & ALLOW_EMPTY));
 			*check_todo = 1;
@@ -3748,7 +3747,7 @@ static int do_merge(struct repository *r,
 		 * command needs to be rescheduled).
 		 */
 	fast_forward_edit:
-		ret = !!run_git_commit(r, git_path_merge_msg(r), opts,
+		ret = !!run_git_commit(git_path_merge_msg(r), opts,
 				       run_commit_flags);
 
 leave_merge:
@@ -4437,7 +4436,7 @@ static int commit_staged_changes(struct repository *r,
 			return 0;
 	}
 
-	if (run_git_commit(r, final_fixup ? NULL : rebase_path_message(),
+	if (run_git_commit(final_fixup ? NULL : rebase_path_message(),
 			   opts, flags))
 		return error(_("could not commit staged changes."));
 	unlink(rebase_path_amend());
-- 
2.28.0.1173.gad90222cf0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 07/10] sparse-checkout: fill in some options boilerplate
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
                   ` (5 preceding siblings ...)
  2020-09-30 12:29 ` [PATCH 06/10] sequencer: drop repository argument from run_git_commit() Jeff King
@ 2020-09-30 12:30 ` Jeff King
  2020-09-30 12:30 ` [PATCH 08/10] test-advise: check argument count with argc instead of argv Jeff King
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:30 UTC (permalink / raw)
  To: git

The sparse-checkout passes along argv and argc to its sub-command helper
functions. Many of these sub-commands do not yet take any command-line
options, and ignore those parameters.

Let's instead add empty option lists and make sure we call
parse_options(). That will give a useful error message for something
like:

  git sparse-checkout list --nonsense

which currently just silently ignores the unknown option.

As a bonus, it also silences some -Wunused-parameter warnings.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/sparse-checkout.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index 4003f4d13a..e3140db2a0 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -46,12 +46,24 @@ static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
 	}
 }
 
+static char const * const builtin_sparse_checkout_list_usage[] = {
+	N_("git sparse-checkout list"),
+	NULL
+};
+
 static int sparse_checkout_list(int argc, const char **argv)
 {
+	static struct option builtin_sparse_checkout_list_options[] = {
+		OPT_END(),
+	};
 	struct pattern_list pl;
 	char *sparse_filename;
 	int res;
 
+	argc = parse_options(argc, argv, NULL,
+			     builtin_sparse_checkout_list_options,
+			     builtin_sparse_checkout_list_usage, 0);
+
 	memset(&pl, 0, sizeof(pl));
 
 	pl.use_cone_patterns = core_sparse_checkout_cone;
@@ -560,17 +572,42 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
 	return modify_pattern_list(argc, argv, m);
 }
 
+static char const * const builtin_sparse_checkout_reapply_usage[] = {
+	N_("git sparse-checkout reapply"),
+	NULL
+};
+
 static int sparse_checkout_reapply(int argc, const char **argv)
 {
+	static struct option builtin_sparse_checkout_reapply_options[] = {
+		OPT_END(),
+	};
+
+	argc = parse_options(argc, argv, NULL,
+			     builtin_sparse_checkout_reapply_options,
+			     builtin_sparse_checkout_reapply_usage, 0);
+
 	repo_read_index(the_repository);
 	return update_working_directory(NULL);
 }
 
+static char const * const builtin_sparse_checkout_disable_usage[] = {
+	N_("git sparse-checkout disable"),
+	NULL
+};
+
 static int sparse_checkout_disable(int argc, const char **argv)
 {
+	static struct option builtin_sparse_checkout_disable_options[] = {
+		OPT_END(),
+	};
 	struct pattern_list pl;
 	struct strbuf match_all = STRBUF_INIT;
 
+	argc = parse_options(argc, argv, NULL,
+			     builtin_sparse_checkout_disable_options,
+			     builtin_sparse_checkout_disable_usage, 0);
+
 	repo_read_index(the_repository);
 
 	memset(&pl, 0, sizeof(pl));
-- 
2.28.0.1173.gad90222cf0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 08/10] test-advise: check argument count with argc instead of argv
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
                   ` (6 preceding siblings ...)
  2020-09-30 12:30 ` [PATCH 07/10] sparse-checkout: fill in some options boilerplate Jeff King
@ 2020-09-30 12:30 ` Jeff King
  2020-09-30 12:34 ` [PATCH 09/10] sequencer: handle ignore_footer when parsing trailers Jeff King
  2020-09-30 12:35 ` [PATCH 10/10] dir.c: drop unused "untracked" from treat_path_fast() Jeff King
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:30 UTC (permalink / raw)
  To: git

We complain if "test-tool advise" is not given an argument, but we
quietly ignore any additional arguments it receives. Let's instead check
that we got the expected number. As a bonus, this silences
-Wunused-parameter, which notes that we don't ever look at argc.

While we're here, we can also fix the indentation in the conditional.

Signed-off-by: Jeff King <peff@peff.net>
---
 t/helper/test-advise.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
index 38cdc2884e..a7043df1d3 100644
--- a/t/helper/test-advise.c
+++ b/t/helper/test-advise.c
@@ -5,8 +5,8 @@
 
 int cmd__advise_if_enabled(int argc, const char **argv)
 {
-	if (!argv[1])
-	die("usage: %s <advice>", argv[0]);
+	if (argc != 2)
+		die("usage: %s <advice>", argv[0]);
 
 	setup_git_directory();
 	git_config(git_default_config, NULL);
-- 
2.28.0.1173.gad90222cf0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 09/10] sequencer: handle ignore_footer when parsing trailers
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
                   ` (7 preceding siblings ...)
  2020-09-30 12:30 ` [PATCH 08/10] test-advise: check argument count with argc instead of argv Jeff King
@ 2020-09-30 12:34 ` Jeff King
  2020-09-30 12:35 ` [PATCH 10/10] dir.c: drop unused "untracked" from treat_path_fast() Jeff King
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:34 UTC (permalink / raw)
  To: git

The append_signoff() function takes an "ignore_footer"
argument, which specifies a number of bytes at the end of
the message buffer which should not be considered (they
cannot contain trailers, and the trailer is spliced in
before them).

But to find the existing trailers, it calls into
has_conforming_trailer(). That function takes an
ignore_footer parameter, but since 967dfd4d56 (sequencer:
use trailer's trailer layout, 2016-11-02) the parameter is
completely ignored.

The trailer interface we're using takes a single string,
with no option to tell it to use part of the string.
However, since we have a mutable strbuf, we can work around
this by simply overwriting (and later restoring) the
boundary with a NUL.

I'm not sure if this can actually trigger a bug in practice.
It's easy to get a non-zero ignore_footer by doing something
like this:

  git commit -F - --cleanup=verbatim <<-EOF
  subject

  body

  Signed-off-by: me

  # this looks like a comment, but is actually in the
  # message! That makes the earlier s-o-b fake.
  EOF

  git commit --amend -s

There git-commit calls ignore_non_trailer() to count up the
"#" cruft, which becomes the ignore_footer header. But it
works even without this patch! That's because the trailer
code _also_ calls ignore_non_trailer() and skips the cruft,
too. So it happens to work because the only callers with a
non-zero ignore_footer are using the exact same function
that the trailer parser uses internally.

And that seems true for all of the current callers, but
there's nothing guaranteeing it. We're better off only
feeding the correct buffer to the trailer code in the first
place.

Signed-off-by: Jeff King <peff@peff.net>
---
This was actually posted as part of an earlier series:

  https://lore.kernel.org/git/20180824072629.GA11977@sigill.intra.peff.net/

It ended as:

  I think patch 9 is not hurting anything and may later help us, but I
  could take or leave it.

and we left it. But now I have the ulterior motive of wanting to get rid
of the unused parameter warning. :) Another option would be to leave the
code as-is and just drop the ignored parameter. But I think this is
probably a safe and reasonable patch (I've been running with it in my
personal fork for 2 years, though perhaps I don't really exercise the
trailer code's corner cases myself).

 sequencer.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/sequencer.c b/sequencer.c
index 6e9aabaac1..e454264fbc 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -249,11 +249,20 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
 	struct trailer_info info;
 	size_t i;
 	int found_sob = 0, found_sob_last = 0;
+	char saved_char;
 
 	opts.no_divider = 1;
 
+	if (ignore_footer) {
+		saved_char = sb->buf[sb->len - ignore_footer];
+		sb->buf[sb->len - ignore_footer] = '\0';
+	}
+
 	trailer_info_get(&info, sb->buf, &opts);
 
+	if (ignore_footer)
+		sb->buf[sb->len - ignore_footer] = saved_char;
+
 	if (info.trailer_start == info.trailer_end)
 		return 0;
 
-- 
2.28.0.1173.gad90222cf0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 10/10] dir.c: drop unused "untracked" from treat_path_fast()
  2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
                   ` (8 preceding siblings ...)
  2020-09-30 12:34 ` [PATCH 09/10] sequencer: handle ignore_footer when parsing trailers Jeff King
@ 2020-09-30 12:35 ` Jeff King
  9 siblings, 0 replies; 11+ messages in thread
From: Jeff King @ 2020-09-30 12:35 UTC (permalink / raw)
  To: git

We don't use the untracked_cache_dir parameter that is passed in, but
instead look at the untracked_cache_dir inside the cached_dir struct we
are passed. It's been this way since the introduction of
treat_path_fast() in 91a2288b5f (untracked cache: record/validate dir
mtime and reuse cached output, 2015-03-08).

Signed-off-by: Jeff King <peff@peff.net>
---
This is the one I'm least sure of (not that it makes anything worse, but
that it might be hiding a bug; I'm pretty sure I just don't understand
how the untracked-cache code works, though).

 dir.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/dir.c b/dir.c
index 3018a657b0..78387110e6 100644
--- a/dir.c
+++ b/dir.c
@@ -2105,7 +2105,6 @@ static int resolve_dtype(int dtype, struct index_state *istate,
 }
 
 static enum path_treatment treat_path_fast(struct dir_struct *dir,
-					   struct untracked_cache_dir *untracked,
 					   struct cached_dir *cdir,
 					   struct index_state *istate,
 					   struct strbuf *path,
@@ -2153,7 +2152,7 @@ static enum path_treatment treat_path(struct dir_struct *dir,
 	int has_path_in_index, dtype, excluded;
 
 	if (!cdir->d_name)
-		return treat_path_fast(dir, untracked, cdir, istate, path,
+		return treat_path_fast(dir, cdir, istate, path,
 				       baselen, pathspec);
 	if (is_dot_or_dotdot(cdir->d_name) || !fspathcmp(cdir->d_name, ".git"))
 		return path_none;
-- 
2.28.0.1173.gad90222cf0

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-09-30 12:35 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-30 12:27 [PATCH 0/10] dropping more unused parameters Jeff King
2020-09-30 12:27 ` [PATCH 01/10] convert: drop unused crlf_action from check_global_conv_flags_eol() Jeff King
2020-09-30 12:28 ` [PATCH 02/10] drop unused argc parameters Jeff King
2020-09-30 12:28 ` [PATCH 03/10] env--helper: write to opt->value in parseopt helper Jeff King
2020-09-30 12:29 ` [PATCH 04/10] assert PARSE_OPT_NONEG in parse-options callbacks Jeff King
2020-09-30 12:29 ` [PATCH 05/10] push: drop unused repo argument to do_push() Jeff King
2020-09-30 12:29 ` [PATCH 06/10] sequencer: drop repository argument from run_git_commit() Jeff King
2020-09-30 12:30 ` [PATCH 07/10] sparse-checkout: fill in some options boilerplate Jeff King
2020-09-30 12:30 ` [PATCH 08/10] test-advise: check argument count with argc instead of argv Jeff King
2020-09-30 12:34 ` [PATCH 09/10] sequencer: handle ignore_footer when parsing trailers Jeff King
2020-09-30 12:35 ` [PATCH 10/10] dir.c: drop unused "untracked" from treat_path_fast() Jeff King

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.