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

Re: [PATCH v2 09/18] unpack-trees: add a new update_sparsity() function

From
Derrick Stolee <stolee@gmail.com>
Date
Mar 23, 2020, 18:02 UTC
Message-ID
<9501e018-fe87-16b8-b0d8-913e4331778d@gmail.com>
In-Reply-To
<a46439c8536f912ad4a1e1751852cf477d3d7dc7.1584813609.git.gitgitgadget@gmail.com>
On 3/21/2020 2:00 PM, Elijah Newren via GitGitGadget wrote:
Show 109 quoted lines
> From: Elijah Newren <newren@gmail.com>
> 
> Previously, the only way to update the SKIP_WORKTREE bits for various
> paths was invoking `git read-tree -mu HEAD` or calling the same code
> that this codepath invoked.  This however had a number of problems if
> the index or working directory were not clean.  First, let's consider
> the case:
> 
>   Flipping SKIP_WORKTREE -> !SKIP_WORKTREE (materializing files)
> 
> If the working tree was clean this was fine, but if there were files or
> directories or symlinks or whatever already present at the given path
> then the operation would abort with an error.  Let's label this case
> for later discussion:
> 
>     A) There is an untracked path in the way
> 
> Now let's consider the opposite case:
> 
>   Flipping !SKIP_WORKTREE -> SKIP_WORKTREE (removing files)
> 
> If the index and working tree was clean this was fine, but if there were
> any unclean paths we would run into problems.  There are three different
> cases to consider:
> 
>     B) The path is unmerged
>     C) The path has unstaged changes
>     D) The path has staged changes (differs from HEAD)
> 
> If any path fell into case B or C, then the whole operation would be
> aborted with an error.  With sparse-checkout, the whole operation would
> be aborted for case D as well, but for its predecessor of using `git
> read-tree -mu HEAD` directly, any paths that fell into case D would be
> removed from the working copy and the index entry for that path would be
> reset to match HEAD -- which looks and feels like data loss to users
> (only a few are even aware to ask whether it can be recovered, and even
> then it requires walking through loose objects trying to match up the
> right ones).
> 
> Refusing to remove files that have unsaved user changes is good, but
> refusing to work on any other paths is very problematic for users.  If
> the user is in the middle of a rebase or has made modifications to files
> that bring in more dependencies, then for their build to work they need
> to update the sparse paths.  This logic has been preventing them from
> doing so.  Sometimes in response, the user will stage the files and
> re-try, to no avail with sparse-checkout or to the horror of losing
> their changes if they are using its predecessor of `git read-tree -mu
> HEAD`.
> 
> Add a new update_sparsity() function which will not error out in any of
> these cases but behaves as follows for the special cases:
>     A) Leave the file in the working copy alone, clear the SKIP_WORKTREE
>        bit, and print a warning (thus leaving the path in a state where
>        status will report the file as modified, which seems logical).
>     B) Do NOT mark this path as SKIP_WORKTREE, and leave it as unmerged.
>     C) Do NOT mark this path as SKIP_WORKTREE and print a warning about
>        the dirty path.
>     D) Mark the path as SKIP_WORKTREE, but do not revert the version
>        stored in the index to match HEAD; leave the contents alone.
> 
> I tried a different behavior for A (leave the SKIP_WORKTREE bit set),
> but found it very surprising and counter-intuitive (e.g. the user sees
> it is present along with all the other files in that directory, tries to
> stage it, but git add ignores it since the SKIP_WORKTREE bit is set).  A
> & C seem like optimal behavior to me.  B may be as well, though I wonder
> if printing a warning would be an improvement.  Some might be slightly
> surprised by D at first, but given that it does the right thing with
> `git commit` and even `git commit -a` (`git add` ignores entries that
> are marked SKIP_WORKTREE and thus doesn't delete them, and `commit -a`
> is similar), it seems logical to me.
> 
> Signed-off-by: Elijah Newren <newren@gmail.com>
> ---
>  unpack-trees.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  unpack-trees.h |  9 ++++++
>  2 files changed, 87 insertions(+)
> 
> diff --git a/unpack-trees.c b/unpack-trees.c
> index 4733e7eaf89..6abea555929 100644
> --- a/unpack-trees.c
> +++ b/unpack-trees.c
> @@ -1714,6 +1714,84 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
>  	goto done;
>  }
>  
> +/*
> + * Update SKIP_WORKTREE bits according to sparsity patterns, and update
> + * working directory to match.
> + *
> + * CE_NEW_SKIP_WORKTREE is used internally.
> + */
> +enum update_sparsity_result update_sparsity(struct unpack_trees_options *o)
> +{
> +	enum update_sparsity_result ret = UPDATE_SPARSITY_SUCCESS;
> +	struct pattern_list pl;
> +	int i, empty_worktree;
> +	unsigned old_show_all_errors;
> +	int free_pattern_list = 0;
> +
> +	old_show_all_errors = o->show_all_errors;
> +	o->show_all_errors = 1;
> +
> +	/* Sanity checks */
> +	if (!o->update || o->index_only || o->skip_sparse_checkout)
> +		BUG("update_sparsity() is for reflecting sparsity patterns in working directory");
> +	if (o->src_index != o->dst_index || o->fn)
> +		BUG("update_sparsity() called wrong");
> +
> +	trace_performance_enter();

I was about to say "why didn't you use the trace2 regions like in unpack_trees()?" when I discovered that we haven't sent them [1] upstream yet. I'll put that on my TODO list.

[1] https://github.com/microsoft/git/commit/9a04644e14fe4aeb556dfc30cb2220b799f53448
> +	/* If we weren't given patterns, use the recorded ones */
> +	if (!o->pl) {
> +		memset(&pl, 0, sizeof(pl));
> +		free_pattern_list = 1;

I notice you are using the same free_pattern_list pattern as your earlier commit. Good.

Show 22 quoted lines
> +		populate_from_existing_patterns(o, &pl);
> +		if (o->skip_sparse_checkout)
> +			goto skip_sparse_checkout;
> +	}
> +
> +	/* Set NEW_SKIP_WORKTREE on existing entries. */
> +	mark_all_ce_unused(o->src_index);
> +	mark_new_skip_worktree(o->pl, o->src_index, 0,
> +			       CE_NEW_SKIP_WORKTREE, o->verbose_update);
> +
> +	/* Then loop over entries and update/remove as needed */
> +	ret = UPDATE_SPARSITY_SUCCESS;
> +	empty_worktree = 1;
> +	for (i = 0; i < o->src_index->cache_nr; i++) {
> +		struct cache_entry *ce = o->src_index->cache[i];
> +
> +		if (apply_sparse_checkout(o->src_index, ce, o))
> +			ret = UPDATE_SPARSITY_WARNINGS;
> +
> +		if (!ce_skip_worktree(ce))
> +			empty_worktree = 0;
> +
nit: extra whitespace-only line
Show 45 quoted lines
> +	}
> +
> +	/*
> +	 * Sparse checkout is meant to narrow down checkout area
> +	 * but it does not make sense to narrow down to empty working
> +	 * tree. This is usually a mistake in sparse checkout rules.
> +	 * Do not allow users to do that.
> +	 */
> +	if (o->src_index->cache_nr && empty_worktree) {
> +		unpack_failed(o, "Sparse checkout leaves no entry on working directory");
> +		ret = UPDATE_SPARSITY_INDEX_UPDATE_FAILURES;
> +		goto done;
> +	}
> +
> +skip_sparse_checkout:
> +	if (check_updates(o, o->src_index))
> +		ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES;
> +
> +done:
> +	display_error_msgs(o);
> +	o->show_all_errors = old_show_all_errors;
> +	if (free_pattern_list)
> +		clear_pattern_list(&pl);
> +	trace_performance_leave("update_sparsity");
> +	return ret;
> +}
> +
>  /* Here come the merge functions */
>  
>  static int reject_merge(const struct cache_entry *ce,
> diff --git a/unpack-trees.h b/unpack-trees.h
> index d3516267f36..2c5d54cae9f 100644
> --- a/unpack-trees.h
> +++ b/unpack-trees.h
> @@ -28,6 +28,13 @@ enum unpack_trees_error_types {
>  	NB_UNPACK_TREES_ERROR_TYPES
>  };
>  
> +enum update_sparsity_result {
> +	UPDATE_SPARSITY_SUCCESS = 0,
> +	UPDATE_SPARSITY_WARNINGS = 1,
> +	UPDATE_SPARSITY_INDEX_UPDATE_FAILURES = -1,
> +	UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES = -2
> +};
> +

Is there a reason this isn't located just before update_sparsity()?

Show 9 quoted lines
>  /*
>   * Sets the list of user-friendly error messages to be used by the
>   * command "cmd" (either merge or checkout), and show_all_errors to 1.
> @@ -88,6 +95,8 @@ struct unpack_trees_options {
>  int unpack_trees(unsigned n, struct tree_desc *t,
>  		 struct unpack_trees_options *options);
>  
> +int update_sparsity(struct unpack_trees_options *options);
> +
This appears to not use the enum as it should.

Thanks, -Stolee

Previous: Elijah Newren via GitGitGadgetNext: Elijah Newren
Message 29 of 69 in “Sparse checkout improvements -- improved sparsity updating”
  1. 0/7 Sparse checkout improvements -- improved sparsity updatingElijah Newren via GitGitGadget, Mar 14, 2020
  2. 7/7 sparse-checkout: provide a new update subcommandElijah Newren via GitGitGadget, Mar 14, 2020
  3. Derrick StoleeMar 15, 2020
  4. Elijah NewrenMar 16, 2020
  5. Derrick StoleeMar 16, 2020
  6. Elijah NewrenMar 16, 2020
  7. 1/7 unpack-trees: fix minor typo in commentElijah Newren via GitGitGadget, Mar 14, 2020
  8. 3/7 unpack-trees: allow check_updates() to work on a different indexElijah Newren via GitGitGadget, Mar 14, 2020
  9. 4/7 unpack-trees: do not mark a dirty path with SKIP_WORKTREEElijah Newren via GitGitGadget, Mar 14, 2020
  10. Derrick StoleeMar 15, 2020
  11. 5/7 unpack-trees: add a new update_sparsity() functionElijah Newren via GitGitGadget, Mar 14, 2020
  12. Derrick StoleeMar 15, 2020
  13. Elijah NewrenMar 16, 2020
  14. 2/7 t1091: make some tests a little more defensive against failuresElijah Newren via GitGitGadget, Mar 14, 2020
  15. 6/7 sparse-checkout: use new update_sparsity() functionElijah Newren via GitGitGadget, Mar 14, 2020
  16. Derrick StoleeMar 15, 2020
  17. Elijah NewrenMar 16, 2020
  18. Derrick StoleeMar 15, 2020
  19. 00/18 Sparse checkout improvements -- improved sparsity updatingElijah Newren via GitGitGadget, Mar 21, 2020
  20. 01/18 unpack-trees: fix minor typo in commentElijah Newren via GitGitGadget, Mar 21, 2020
  21. 03/18 unpack-trees: simplify verify_absent_sparse()Elijah Newren via GitGitGadget, Mar 21, 2020
  22. 02/18 unpack-trees: remove unused error typeElijah Newren via GitGitGadget, Mar 21, 2020
  23. 04/18 unpack-trees: simplify pattern_list freeingElijah Newren via GitGitGadget, Mar 21, 2020
  24. Derrick StoleeMar 23, 2020
  25. 06/18 unpack-trees: allow check_updates() to work on a different indexElijah Newren via GitGitGadget, Mar 21, 2020
  26. 05/18 t1091: make some tests a little more defensive against failuresElijah Newren via GitGitGadget, Mar 21, 2020
  27. 08/18 unpack-trees: pull sparse-checkout pattern reading into a new functionElijah Newren via GitGitGadget, Mar 21, 2020
  28. 09/18 unpack-trees: add a new update_sparsity() functionElijah Newren via GitGitGadget, Mar 21, 2020
  29. Derrick StoleeMar 23, 2020
  30. Elijah NewrenMar 23, 2020
  31. Derrick StoleeMar 23, 2020
  32. Junio C HamanoMar 23, 2020
  33. 11/18 sparse-checkout: use improved unpack_trees porcelain messagesElijah Newren via GitGitGadget, Mar 21, 2020
  34. 13/18 unpack-trees: rename ERROR_* fields meant for warnings to WARNING_*Elijah Newren via GitGitGadget, Mar 21, 2020
  35. 10/18 sparse-checkout: use new update_sparsity() functionElijah Newren via GitGitGadget, Mar 21, 2020
  36. Derrick StoleeMar 23, 2020
  37. Elijah NewrenMar 23, 2020
  38. Derrick StoleeMar 23, 2020
  39. 15/18 unpack-trees: make sparse path messages sound like warningsElijah Newren via GitGitGadget, Mar 21, 2020
  40. 18/18 sparse-checkout: provide a new reapply subcommandElijah Newren via GitGitGadget, Mar 21, 2020
  41. Derrick StoleeMar 23, 2020
  42. 17/18 unpack-trees: failure to set SKIP_WORKTREE bits always just a warningElijah Newren via GitGitGadget, Mar 21, 2020
  43. 14/18 unpack-trees: split display_error_msgs() into twoElijah Newren via GitGitGadget, Mar 21, 2020
  44. Derrick StoleeMar 23, 2020
  45. 16/18 unpack-trees: provide warnings on sparse updates for unmerged paths tooElijah Newren via GitGitGadget, Mar 21, 2020
  46. 07/18 unpack-trees: do not mark a dirty path with SKIP_WORKTREEElijah Newren via GitGitGadget, Mar 21, 2020
  47. 12/18 unpack-trees: move ERROR_WOULD_LOSE_SUBMODULE earlierElijah Newren via GitGitGadget, Mar 21, 2020
  48. Derrick StoleeMar 23, 2020
  49. Junio C HamanoMar 23, 2020
  50. 00/18 Sparse checkout improvements -- improved sparsity updatingElijah Newren via GitGitGadget, Mar 27, 2020
  51. 01/18 unpack-trees: fix minor typo in commentElijah Newren via GitGitGadget, Mar 27, 2020
  52. 02/18 unpack-trees: remove unused error typeElijah Newren via GitGitGadget, Mar 27, 2020
  53. 06/18 unpack-trees: allow check_updates() to work on a different indexElijah Newren via GitGitGadget, Mar 27, 2020
  54. 04/18 unpack-trees: simplify pattern_list freeingElijah Newren via GitGitGadget, Mar 27, 2020
  55. 05/18 t1091: make some tests a little more defensive against failuresElijah Newren via GitGitGadget, Mar 27, 2020
  56. 03/18 unpack-trees: simplify verify_absent_sparse()Elijah Newren via GitGitGadget, Mar 27, 2020
  57. 07/18 unpack-trees: do not mark a dirty path with SKIP_WORKTREEElijah Newren via GitGitGadget, Mar 27, 2020
  58. 08/18 unpack-trees: pull sparse-checkout pattern reading into a new functionElijah Newren via GitGitGadget, Mar 27, 2020
  59. 09/18 unpack-trees: add a new update_sparsity() functionElijah Newren via GitGitGadget, Mar 27, 2020
  60. 10/18 sparse-checkout: use new update_sparsity() functionElijah Newren via GitGitGadget, Mar 27, 2020
  61. 11/18 sparse-checkout: use improved unpack_trees porcelain messagesElijah Newren via GitGitGadget, Mar 27, 2020
  62. 13/18 unpack-trees: rename ERROR_* fields meant for warnings to WARNING_*Elijah Newren via GitGitGadget, Mar 27, 2020
  63. 16/18 unpack-trees: provide warnings on sparse updates for unmerged paths tooElijah Newren via GitGitGadget, Mar 27, 2020
  64. 18/18 sparse-checkout: provide a new reapply subcommandElijah Newren via GitGitGadget, Mar 27, 2020
  65. 15/18 unpack-trees: make sparse path messages sound like warningsElijah Newren via GitGitGadget, Mar 27, 2020
  66. 14/18 unpack-trees: split display_error_msgs() into twoElijah Newren via GitGitGadget, Mar 27, 2020
  67. 17/18 unpack-trees: failure to set SKIP_WORKTREE bits always just a warningElijah Newren via GitGitGadget, Mar 27, 2020
  68. 12/18 unpack-trees: move ERROR_WOULD_LOSE_SUBMODULE earlierElijah Newren via GitGitGadget, Mar 27, 2020
  69. Derrick StoleeMar 27, 2020

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.