]> git.ipfire.org Git - thirdparty/git.git/commitdiff
apply: use strsets to track symlinks
authorRené Scharfe <l.s.r@web.de>
Fri, 7 Jan 2022 12:16:53 +0000 (13:16 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 7 Jan 2022 19:40:44 +0000 (11:40 -0800)
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>
apply.c
apply.h

diff --git a/apply.c b/apply.c
index 43a0aebf4eec85fa6b0a69f655c0224aa14f29e5..99f76b59378851042ac87edf03b354dfeb6a551c 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -103,7 +103,8 @@ int init_apply_state(struct apply_state *state,
        state->linenr = 1;
        string_list_init_nodup(&state->fn_table);
        string_list_init_nodup(&state->limit_by_name);
-       string_list_init_nodup(&state->symlink_changes);
+       strset_init(&state->removed_symlinks);
+       strset_init(&state->kept_symlinks);
        strbuf_init(&state->root, 0);
 
        git_apply_config();
@@ -117,7 +118,8 @@ int init_apply_state(struct apply_state *state,
 void clear_apply_state(struct apply_state *state)
 {
        string_list_clear(&state->limit_by_name, 0);
-       string_list_clear(&state->symlink_changes, 0);
+       strset_clear(&state->removed_symlinks);
+       strset_clear(&state->kept_symlinks);
        strbuf_release(&state->root);
 
        /* &state->fn_table is cleared at the end of apply_patch() */
@@ -3812,59 +3814,31 @@ static int check_to_create(struct apply_state *state,
        return 0;
 }
 
-static uintptr_t register_symlink_changes(struct apply_state *state,
-                                         const char *path,
-                                         uintptr_t what)
-{
-       struct string_list_item *ent;
-
-       ent = string_list_lookup(&state->symlink_changes, path);
-       if (!ent) {
-               ent = string_list_insert(&state->symlink_changes, path);
-               ent->util = (void *)0;
-       }
-       ent->util = (void *)(what | ((uintptr_t)ent->util));
-       return (uintptr_t)ent->util;
-}
-
-static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
-{
-       struct string_list_item *ent;
-
-       ent = string_list_lookup(&state->symlink_changes, path);
-       if (!ent)
-               return 0;
-       return (uintptr_t)ent->util;
-}
-
 static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
 {
        for ( ; patch; patch = patch->next) {
                if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
                    (patch->is_rename || patch->is_delete))
                        /* the symlink at patch->old_name is removed */
-                       register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);
+                       strset_add(&state->removed_symlinks, patch->old_name);
 
                if (patch->new_name && S_ISLNK(patch->new_mode))
                        /* the symlink at patch->new_name is created or remains */
-                       register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);
+                       strset_add(&state->kept_symlinks, patch->new_name);
        }
 }
 
 static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
 {
        do {
-               unsigned int change;
-
                while (--name->len && name->buf[name->len] != '/')
                        ; /* scan backwards */
                if (!name->len)
                        break;
                name->buf[name->len] = '\0';
-               change = check_symlink_changes(state, name->buf);
-               if (change & APPLY_SYMLINK_IN_RESULT)
+               if (strset_contains(&state->kept_symlinks, name->buf))
                        return 1;
-               if (change & APPLY_SYMLINK_GOES_AWAY)
+               if (strset_contains(&state->removed_symlinks, name->buf))
                        /*
                         * This cannot be "return 0", because we may
                         * see a new one created at a higher level.
diff --git a/apply.h b/apply.h
index da3d95fa50989bdd18289eec3a79b955c0d59443..1f959c064e305a5686a95726f264d147a49c20fe 100644 (file)
--- a/apply.h
+++ b/apply.h
@@ -4,6 +4,7 @@
 #include "hash.h"
 #include "lockfile.h"
 #include "string-list.h"
+#include "strmap.h"
 
 struct repository;
 
@@ -25,20 +26,6 @@ enum apply_verbosity {
        verbosity_verbose = 1
 };
 
-/*
- * We need to keep track of how symlinks in the preimage are
- * manipulated by the patches.  A patch to add a/b/c where a/b
- * is a symlink should not be allowed to affect the directory
- * the symlink points at, but if the same patch removes a/b,
- * it is perfectly fine, as the patch removes a/b to make room
- * to create a directory a/b so that a/b/c can be created.
- *
- * See also "struct string_list symlink_changes" in "struct
- * apply_state".
- */
-#define APPLY_SYMLINK_GOES_AWAY 01
-#define APPLY_SYMLINK_IN_RESULT 02
-
 struct apply_state {
        const char *prefix;
 
@@ -85,7 +72,16 @@ struct apply_state {
 
        /* Various "current state" */
        int linenr; /* current line number */
-       struct string_list symlink_changes; /* we have to track symlinks */
+       /*
+        * We need to keep track of how symlinks in the preimage are
+        * manipulated by the patches.  A patch to add a/b/c where a/b
+        * is a symlink should not be allowed to affect the directory
+        * the symlink points at, but if the same patch removes a/b,
+        * it is perfectly fine, as the patch removes a/b to make room
+        * to create a directory a/b so that a/b/c can be created.
+        */
+       struct strset removed_symlinks;
+       struct strset kept_symlinks;
 
        /*
         * For "diff-stat" like behaviour, we keep track of the biggest change