[v2,14/18] unpack-trees: split display_error_msgs() into two
diff mbox series

Message ID 57679c8e292ceb58cea2b5b7d893d5f47e1e2de0.1584813609.git.gitgitgadget@gmail.com
State New
Headers show
Series
  • Sparse checkout improvements -- improved sparsity updating
Related show

Commit Message

Elijah Newren via GitGitGadget March 21, 2020, 6 p.m. UTC
From: Elijah Newren <newren@gmail.com>

display_error_msgs() is never called to show messages of both ERROR_*
and WARNING_* types at the same time; it is instead called multiple
times, separately for each type.  Since we want to display these types
differently, make two slightly different versions of this function.

A subsequent commit will further modify unpack_trees() and how it calls
the new display_warning_msgs().

Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t1091-sparse-checkout-builtin.sh |  6 ++--
 unpack-trees.c                     | 50 +++++++++++++++++++++++++-----
 unpack-trees.h                     |  8 +++--
 3 files changed, 50 insertions(+), 14 deletions(-)

Comments

Derrick Stolee March 23, 2020, 6:32 p.m. UTC | #1
On 3/21/2020 2:00 PM, Elijah Newren via GitGitGadget wrote:
> From: Elijah Newren <newren@gmail.com>
> 
> display_error_msgs() is never called to show messages of both ERROR_*
> and WARNING_* types at the same time; it is instead called multiple
> times, separately for each type.  Since we want to display these types
> differently, make two slightly different versions of this function.
> 
> A subsequent commit will further modify unpack_trees() and how it calls
> the new display_warning_msgs().
> 
> Signed-off-by: Elijah Newren <newren@gmail.com>
> ---
>  t/t1091-sparse-checkout-builtin.sh |  6 ++--
>  unpack-trees.c                     | 50 +++++++++++++++++++++++++-----
>  unpack-trees.h                     |  8 +++--
>  3 files changed, 50 insertions(+), 14 deletions(-)
> 
> diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
> index 93c650ac038..0d93d3983e0 100755
> --- a/t/t1091-sparse-checkout-builtin.sh
> +++ b/t/t1091-sparse-checkout-builtin.sh
> @@ -328,13 +328,13 @@ test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status'
>  	echo dirty >dirty/folder1/a &&
>  
>  	git -C dirty sparse-checkout init 2>err &&
> -	test_i18ngrep "error.*Cannot update sparse checkout" err &&
> +	test_i18ngrep "warning.*Cannot update sparse checkout" err &&
>  
>  	git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
> -	test_i18ngrep "error.*Cannot update sparse checkout" err &&
> +	test_i18ngrep "warning.*Cannot update sparse checkout" err &&
>  
>  	git -C dirty sparse-checkout disable &&
> -	test_i18ngrep "error.*Cannot update sparse checkout" err &&
> +	test_i18ngrep "warning.*Cannot update sparse checkout" err &&

I'm very happy this change is so clearly demonstrated by
a simple test change like this.

>  	git -C dirty reset --hard &&
>  	git -C dirty sparse-checkout init &&
> diff --git a/unpack-trees.c b/unpack-trees.c
> index 0554842580b..9ee04992ac6 100644
> --- a/unpack-trees.c
> +++ b/unpack-trees.c
> @@ -24,7 +24,7 @@
>   * situation better.  See how "git checkout" and "git merge" replaces
>   * them using setup_unpack_trees_porcelain(), for example.
>   */
> -static const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
> +static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {
>  	/* ERROR_WOULD_OVERWRITE */
>  	"Entry '%s' would be overwritten by merge. Cannot merge.",
>  
> @@ -46,6 +46,9 @@ static const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
>  	/* ERROR_WOULD_LOSE_SUBMODULE */
>  	"Submodule '%s' cannot checkout new HEAD.",
>  
> +	/* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
> +	"",
> +
>  	/* WARNING_SPARSE_NOT_UPTODATE_FILE */
>  	"Entry '%s' not uptodate. Cannot update sparse checkout.",
>  
> @@ -222,7 +225,7 @@ static int add_rejected_path(struct unpack_trees_options *o,
>  
>  	/*
>  	 * Otherwise, insert in a list for future display by
> -	 * display_error_msgs()
> +	 * display_(error|warning)_msgs()
>  	 */
>  	string_list_append(&o->unpack_rejects[e], path);
>  	return -1;
> @@ -233,13 +236,16 @@ static int add_rejected_path(struct unpack_trees_options *o,
>   */
>  static void display_error_msgs(struct unpack_trees_options *o)
>  {
> -	int e, i;
> -	int something_displayed = 0;
> +	int e;
> +	unsigned error_displayed = 0;
>  	for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
>  		struct string_list *rejects = &o->unpack_rejects[e];
> +
>  		if (rejects->nr > 0) {
> +			int i;
>  			struct strbuf path = STRBUF_INIT;
> -			something_displayed = 1;
> +
> +			error_displayed = 1;
>  			for (i = 0; i < rejects->nr; i++)
>  				strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
>  			error(ERRORMSG(o, e), super_prefixed(path.buf));
> @@ -247,10 +253,36 @@ static void display_error_msgs(struct unpack_trees_options *o)
>  		}
>  		string_list_clear(rejects, 0);
>  	}
> -	if (something_displayed)
> +	if (error_displayed)
>  		fprintf(stderr, _("Aborting\n"));
>  }
>  
> +/*
> + * display all the warning messages stored in a nice way
> + */
> +static void display_warning_msgs(struct unpack_trees_options *o)
> +{
> +	int e;
> +	unsigned warning_displayed = 0;
> +	for (e = NB_UNPACK_TREES_ERROR_TYPES+1;

super-nit: spaces around '+'

> +	     e < NB_UNPACK_TREES_WARNING_TYPES; e++) {
> +		struct string_list *rejects = &o->unpack_rejects[e];
> +
> +		if (rejects->nr > 0) {
> +			int i;
> +			struct strbuf path = STRBUF_INIT;
> +
> +			warning_displayed = 1;
> +			for (i = 0; i < rejects->nr; i++)
> +				strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
> +			warning(ERRORMSG(o, e), super_prefixed(path.buf));
> +			strbuf_release(&path);
> +		}
> +		string_list_clear(rejects, 0);
> +	}
> +	if (warning_displayed)
> +		fprintf(stderr, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
> +}
>  static int check_submodule_move_head(const struct cache_entry *ce,
>  				     const char *old_id,
>  				     const char *new_id,
> @@ -1705,8 +1737,10 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
>  	return ret;
>  
>  return_failed:
> -	if (o->show_all_errors)
> +	if (o->show_all_errors) {
>  		display_error_msgs(o);
> +		display_warning_msgs(o);
> +	}
>  	mark_all_ce_unused(o->src_index);
>  	ret = unpack_failed(o, NULL);
>  	if (o->exiting_early)
> @@ -1784,7 +1818,7 @@ enum update_sparsity_result update_sparsity(struct unpack_trees_options *o)
>  		ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES;
>  
>  done:
> -	display_error_msgs(o);
> +	display_warning_msgs(o);
>  	o->show_all_errors = old_show_all_errors;
>  	if (free_pattern_list)
>  		clear_pattern_list(&pl);
> diff --git a/unpack-trees.h b/unpack-trees.h
> index 3c6452fe9e5..d91c65ae453 100644
> --- a/unpack-trees.h
> +++ b/unpack-trees.h
> @@ -24,10 +24,12 @@ enum unpack_trees_error_types {
>  	ERROR_BIND_OVERLAP,
>  	ERROR_WOULD_LOSE_SUBMODULE,
>  
> +	NB_UNPACK_TREES_ERROR_TYPES,
> +

I think this is a very clever way to partition the enum.

>  	WARNING_SPARSE_NOT_UPTODATE_FILE,
>  	WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN,
>  
> -	NB_UNPACK_TREES_ERROR_TYPES,
> +	NB_UNPACK_TREES_WARNING_TYPES,
>  };
>  
>  enum update_sparsity_result {
> @@ -73,13 +75,13 @@ struct unpack_trees_options {
>  	struct dir_struct *dir;
>  	struct pathspec *pathspec;
>  	merge_fn_t fn;
> -	const char *msgs[NB_UNPACK_TREES_ERROR_TYPES];
> +	const char *msgs[NB_UNPACK_TREES_WARNING_TYPES];
>  	struct argv_array msgs_to_free;
>  	/*
>  	 * Store error messages in an array, each case
>  	 * corresponding to a error message type
>  	 */
> -	struct string_list unpack_rejects[NB_UNPACK_TREES_ERROR_TYPES];
> +	struct string_list unpack_rejects[NB_UNPACK_TREES_WARNING_TYPES];
>  
>  	int head_idx;
>  	int merge_size;

LGTM other than my really nitty nit-pick. Feel free to ignore.

-Stolee

Patch
diff mbox series

diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index 93c650ac038..0d93d3983e0 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -328,13 +328,13 @@  test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status'
 	echo dirty >dirty/folder1/a &&
 
 	git -C dirty sparse-checkout init 2>err &&
-	test_i18ngrep "error.*Cannot update sparse checkout" err &&
+	test_i18ngrep "warning.*Cannot update sparse checkout" err &&
 
 	git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
-	test_i18ngrep "error.*Cannot update sparse checkout" err &&
+	test_i18ngrep "warning.*Cannot update sparse checkout" err &&
 
 	git -C dirty sparse-checkout disable &&
-	test_i18ngrep "error.*Cannot update sparse checkout" err &&
+	test_i18ngrep "warning.*Cannot update sparse checkout" err &&
 
 	git -C dirty reset --hard &&
 	git -C dirty sparse-checkout init &&
diff --git a/unpack-trees.c b/unpack-trees.c
index 0554842580b..9ee04992ac6 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -24,7 +24,7 @@ 
  * situation better.  See how "git checkout" and "git merge" replaces
  * them using setup_unpack_trees_porcelain(), for example.
  */
-static const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
+static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {
 	/* ERROR_WOULD_OVERWRITE */
 	"Entry '%s' would be overwritten by merge. Cannot merge.",
 
@@ -46,6 +46,9 @@  static const char *unpack_plumbing_errors[NB_UNPACK_TREES_ERROR_TYPES] = {
 	/* ERROR_WOULD_LOSE_SUBMODULE */
 	"Submodule '%s' cannot checkout new HEAD.",
 
+	/* NB_UNPACK_TREES_ERROR_TYPES; just a meta value */
+	"",
+
 	/* WARNING_SPARSE_NOT_UPTODATE_FILE */
 	"Entry '%s' not uptodate. Cannot update sparse checkout.",
 
@@ -222,7 +225,7 @@  static int add_rejected_path(struct unpack_trees_options *o,
 
 	/*
 	 * Otherwise, insert in a list for future display by
-	 * display_error_msgs()
+	 * display_(error|warning)_msgs()
 	 */
 	string_list_append(&o->unpack_rejects[e], path);
 	return -1;
@@ -233,13 +236,16 @@  static int add_rejected_path(struct unpack_trees_options *o,
  */
 static void display_error_msgs(struct unpack_trees_options *o)
 {
-	int e, i;
-	int something_displayed = 0;
+	int e;
+	unsigned error_displayed = 0;
 	for (e = 0; e < NB_UNPACK_TREES_ERROR_TYPES; e++) {
 		struct string_list *rejects = &o->unpack_rejects[e];
+
 		if (rejects->nr > 0) {
+			int i;
 			struct strbuf path = STRBUF_INIT;
-			something_displayed = 1;
+
+			error_displayed = 1;
 			for (i = 0; i < rejects->nr; i++)
 				strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
 			error(ERRORMSG(o, e), super_prefixed(path.buf));
@@ -247,10 +253,36 @@  static void display_error_msgs(struct unpack_trees_options *o)
 		}
 		string_list_clear(rejects, 0);
 	}
-	if (something_displayed)
+	if (error_displayed)
 		fprintf(stderr, _("Aborting\n"));
 }
 
+/*
+ * display all the warning messages stored in a nice way
+ */
+static void display_warning_msgs(struct unpack_trees_options *o)
+{
+	int e;
+	unsigned warning_displayed = 0;
+	for (e = NB_UNPACK_TREES_ERROR_TYPES+1;
+	     e < NB_UNPACK_TREES_WARNING_TYPES; e++) {
+		struct string_list *rejects = &o->unpack_rejects[e];
+
+		if (rejects->nr > 0) {
+			int i;
+			struct strbuf path = STRBUF_INIT;
+
+			warning_displayed = 1;
+			for (i = 0; i < rejects->nr; i++)
+				strbuf_addf(&path, "\t%s\n", rejects->items[i].string);
+			warning(ERRORMSG(o, e), super_prefixed(path.buf));
+			strbuf_release(&path);
+		}
+		string_list_clear(rejects, 0);
+	}
+	if (warning_displayed)
+		fprintf(stderr, _("After fixing the above paths, you may want to run `git sparse-checkout reapply`.\n"));
+}
 static int check_submodule_move_head(const struct cache_entry *ce,
 				     const char *old_id,
 				     const char *new_id,
@@ -1705,8 +1737,10 @@  int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
 	return ret;
 
 return_failed:
-	if (o->show_all_errors)
+	if (o->show_all_errors) {
 		display_error_msgs(o);
+		display_warning_msgs(o);
+	}
 	mark_all_ce_unused(o->src_index);
 	ret = unpack_failed(o, NULL);
 	if (o->exiting_early)
@@ -1784,7 +1818,7 @@  enum update_sparsity_result update_sparsity(struct unpack_trees_options *o)
 		ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES;
 
 done:
-	display_error_msgs(o);
+	display_warning_msgs(o);
 	o->show_all_errors = old_show_all_errors;
 	if (free_pattern_list)
 		clear_pattern_list(&pl);
diff --git a/unpack-trees.h b/unpack-trees.h
index 3c6452fe9e5..d91c65ae453 100644
--- a/unpack-trees.h
+++ b/unpack-trees.h
@@ -24,10 +24,12 @@  enum unpack_trees_error_types {
 	ERROR_BIND_OVERLAP,
 	ERROR_WOULD_LOSE_SUBMODULE,
 
+	NB_UNPACK_TREES_ERROR_TYPES,
+
 	WARNING_SPARSE_NOT_UPTODATE_FILE,
 	WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN,
 
-	NB_UNPACK_TREES_ERROR_TYPES,
+	NB_UNPACK_TREES_WARNING_TYPES,
 };
 
 enum update_sparsity_result {
@@ -73,13 +75,13 @@  struct unpack_trees_options {
 	struct dir_struct *dir;
 	struct pathspec *pathspec;
 	merge_fn_t fn;
-	const char *msgs[NB_UNPACK_TREES_ERROR_TYPES];
+	const char *msgs[NB_UNPACK_TREES_WARNING_TYPES];
 	struct argv_array msgs_to_free;
 	/*
 	 * Store error messages in an array, each case
 	 * corresponding to a error message type
 	 */
-	struct string_list unpack_rejects[NB_UNPACK_TREES_ERROR_TYPES];
+	struct string_list unpack_rejects[NB_UNPACK_TREES_WARNING_TYPES];
 
 	int head_idx;
 	int merge_size;