apply: use strsets to track symlinks

Symlink changes are tracked in a string_list, with the util pointer value indicating whether a symlink is kept or removed. Using fake pointer values requires awkward casts. Use one strset for each type of change instead to simplify and shorten the code. Original-patch-by: Jessica Clarke <jrtc27@jrtc27.com> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jan 7, 2022 at 13:16 UTC 4e9a3252531eab8af5559b25efac02b33f6ca5fe
2 files changed +19 -49
apply.c
+8 -34
@@ -103,7 +103,8 @@ int init_apply_state(struct apply_state *state,
103 state->linenr = 1;
104 string_list_init_nodup(&state->fn_table);
105 string_list_init_nodup(&state->limit_by_name);
106 - string_list_init_nodup(&state->symlink_changes);
106 + strset_init(&state->removed_symlinks);
107 + strset_init(&state->kept_symlinks);
108 strbuf_init(&state->root, 0);
109
110 git_apply_config();
@@ -117,7 +118,8 @@ int init_apply_state(struct apply_state *state,
118 void clear_apply_state(struct apply_state *state)
119 {
120 string_list_clear(&state->limit_by_name, 0);
120 - string_list_clear(&state->symlink_changes, 0);
121 + strset_clear(&state->removed_symlinks);
122 + strset_clear(&state->kept_symlinks);
123 strbuf_release(&state->root);
124
125 /* &state->fn_table is cleared at the end of apply_patch() */
@@ -3812,59 +3814,31 @@ static int check_to_create(struct apply_state *state,
3814 return 0;
3815 }
3816
3815 -static uintptr_t register_symlink_changes(struct apply_state *state,
3816 - const char *path,
3817 - uintptr_t what)
3818 -{
3819 - struct string_list_item *ent;
3820 -
3821 - ent = string_list_lookup(&state->symlink_changes, path);
3822 - if (!ent) {
3823 - ent = string_list_insert(&state->symlink_changes, path);
3824 - ent->util = (void *)0;
3825 - }
3826 - ent->util = (void *)(what | ((uintptr_t)ent->util));
3827 - return (uintptr_t)ent->util;
3828 -}
3829 -
3830 -static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
3831 -{
3832 - struct string_list_item *ent;
3833 -
3834 - ent = string_list_lookup(&state->symlink_changes, path);
3835 - if (!ent)
3836 - return 0;
3837 - return (uintptr_t)ent->util;
3838 -}
3839 -
3817 static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
3818 {
3819 for ( ; patch; patch = patch->next) {
3820 if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
3821 (patch->is_rename || patch->is_delete))
3822 /* the symlink at patch->old_name is removed */
3846 - register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);
3823 + strset_add(&state->removed_symlinks, patch->old_name);
3824
3825 if (patch->new_name && S_ISLNK(patch->new_mode))
3826 /* the symlink at patch->new_name is created or remains */
3850 - register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);
3827 + strset_add(&state->kept_symlinks, patch->new_name);
3828 }
3829 }
3830
3831 static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
3832 {
3833 do {
3857 - unsigned int change;
3858 -
3834 while (--name->len && name->buf[name->len] != '/')
3835 ; /* scan backwards */
3836 if (!name->len)
3837 break;
3838 name->buf[name->len] = '\0';
3864 - change = check_symlink_changes(state, name->buf);
3865 - if (change & APPLY_SYMLINK_IN_RESULT)
3839 + if (strset_contains(&state->kept_symlinks, name->buf))
3840 return 1;
3867 - if (change & APPLY_SYMLINK_GOES_AWAY)
3841 + if (strset_contains(&state->removed_symlinks, name->buf))
3842 /*
3843 * This cannot be "return 0", because we may
3844 * see a new one created at a higher level.
apply.h
+11 -15
@@ -4,6 +4,7 @@
4 #include "hash.h"
5 #include "lockfile.h"
6 #include "string-list.h"
7 +#include "strmap.h"
8
9 struct repository;
10
@@ -25,20 +26,6 @@ enum apply_verbosity {
26 verbosity_verbose = 1
27 };
28
28 -/*
29 - * We need to keep track of how symlinks in the preimage are
30 - * manipulated by the patches. A patch to add a/b/c where a/b
31 - * is a symlink should not be allowed to affect the directory
32 - * the symlink points at, but if the same patch removes a/b,
33 - * it is perfectly fine, as the patch removes a/b to make room
34 - * to create a directory a/b so that a/b/c can be created.
35 - *
36 - * See also "struct string_list symlink_changes" in "struct
37 - * apply_state".
38 - */
39 -#define APPLY_SYMLINK_GOES_AWAY 01
40 -#define APPLY_SYMLINK_IN_RESULT 02
41 -
29 struct apply_state {
30 const char *prefix;
31
@@ -85,7 +72,16 @@ struct apply_state {
72
73 /* Various "current state" */
74 int linenr; /* current line number */
88 - struct string_list symlink_changes; /* we have to track symlinks */
75 + /*
76 + * We need to keep track of how symlinks in the preimage are
77 + * manipulated by the patches. A patch to add a/b/c where a/b
78 + * is a symlink should not be allowed to affect the directory
79 + * the symlink points at, but if the same patch removes a/b,
80 + * it is perfectly fine, as the patch removes a/b to make room
81 + * to create a directory a/b so that a/b/c can be created.
82 + */
83 + struct strset removed_symlinks;
84 + struct strset kept_symlinks;
85
86 /*
87 * For "diff-stat" like behaviour, we keep track of the biggest change