Skip to content

Commit 4e9a325

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e9d7761 commit 4e9a325

File tree

2 files changed

+19
-49
lines changed

2 files changed

+19
-49
lines changed

apply.c

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ int init_apply_state(struct apply_state *state,
103103
state->linenr = 1;
104104
string_list_init_nodup(&state->fn_table);
105105
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);
107108
strbuf_init(&state->root, 0);
108109

109110
git_apply_config();
@@ -117,7 +118,8 @@ int init_apply_state(struct apply_state *state,
117118
void clear_apply_state(struct apply_state *state)
118119
{
119120
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);
121123
strbuf_release(&state->root);
122124

123125
/* &state->fn_table is cleared at the end of apply_patch() */
@@ -3812,59 +3814,31 @@ static int check_to_create(struct apply_state *state,
38123814
return 0;
38133815
}
38143816

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-
38403817
static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
38413818
{
38423819
for ( ; patch; patch = patch->next) {
38433820
if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
38443821
(patch->is_rename || patch->is_delete))
38453822
/* 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);
38473824

38483825
if (patch->new_name && S_ISLNK(patch->new_mode))
38493826
/* 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);
38513828
}
38523829
}
38533830

38543831
static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
38553832
{
38563833
do {
3857-
unsigned int change;
3858-
38593834
while (--name->len && name->buf[name->len] != '/')
38603835
; /* scan backwards */
38613836
if (!name->len)
38623837
break;
38633838
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))
38663840
return 1;
3867-
if (change & APPLY_SYMLINK_GOES_AWAY)
3841+
if (strset_contains(&state->removed_symlinks, name->buf))
38683842
/*
38693843
* This cannot be "return 0", because we may
38703844
* see a new one created at a higher level.

apply.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "hash.h"
55
#include "lockfile.h"
66
#include "string-list.h"
7+
#include "strmap.h"
78

89
struct repository;
910

@@ -25,20 +26,6 @@ enum apply_verbosity {
2526
verbosity_verbose = 1
2627
};
2728

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-
4229
struct apply_state {
4330
const char *prefix;
4431

@@ -85,7 +72,16 @@ struct apply_state {
8572

8673
/* Various "current state" */
8774
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;
8985

9086
/*
9187
* For "diff-stat" like behaviour, we keep track of the biggest change

0 commit comments

Comments
 (0)