Skip to content

Commit 4bb003d

Browse files
committed
Merge branch 'rs/apply-symlinks-use-strset'
"git apply" (ab)used the util pointer of the string-list to keep track of how each symbolic link needs to be handled, which has been simplified by using strset. * rs/apply-symlinks-use-strset: apply: use strsets to track symlinks
2 parents d0bb19c + 4e9a325 commit 4bb003d

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() */
@@ -3814,59 +3816,31 @@ static int check_to_create(struct apply_state *state,
38143816
return 0;
38153817
}
38163818

3817-
static uintptr_t register_symlink_changes(struct apply_state *state,
3818-
const char *path,
3819-
uintptr_t what)
3820-
{
3821-
struct string_list_item *ent;
3822-
3823-
ent = string_list_lookup(&state->symlink_changes, path);
3824-
if (!ent) {
3825-
ent = string_list_insert(&state->symlink_changes, path);
3826-
ent->util = (void *)0;
3827-
}
3828-
ent->util = (void *)(what | ((uintptr_t)ent->util));
3829-
return (uintptr_t)ent->util;
3830-
}
3831-
3832-
static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)
3833-
{
3834-
struct string_list_item *ent;
3835-
3836-
ent = string_list_lookup(&state->symlink_changes, path);
3837-
if (!ent)
3838-
return 0;
3839-
return (uintptr_t)ent->util;
3840-
}
3841-
38423819
static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)
38433820
{
38443821
for ( ; patch; patch = patch->next) {
38453822
if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
38463823
(patch->is_rename || patch->is_delete))
38473824
/* the symlink at patch->old_name is removed */
3848-
register_symlink_changes(state, patch->old_name, APPLY_SYMLINK_GOES_AWAY);
3825+
strset_add(&state->removed_symlinks, patch->old_name);
38493826

38503827
if (patch->new_name && S_ISLNK(patch->new_mode))
38513828
/* the symlink at patch->new_name is created or remains */
3852-
register_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);
3829+
strset_add(&state->kept_symlinks, patch->new_name);
38533830
}
38543831
}
38553832

38563833
static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
38573834
{
38583835
do {
3859-
unsigned int change;
3860-
38613836
while (--name->len && name->buf[name->len] != '/')
38623837
; /* scan backwards */
38633838
if (!name->len)
38643839
break;
38653840
name->buf[name->len] = '\0';
3866-
change = check_symlink_changes(state, name->buf);
3867-
if (change & APPLY_SYMLINK_IN_RESULT)
3841+
if (strset_contains(&state->kept_symlinks, name->buf))
38683842
return 1;
3869-
if (change & APPLY_SYMLINK_GOES_AWAY)
3843+
if (strset_contains(&state->removed_symlinks, name->buf))
38703844
/*
38713845
* This cannot be "return 0", because we may
38723846
* 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

@@ -86,7 +73,16 @@ struct apply_state {
8673

8774
/* Various "current state" */
8875
int linenr; /* current line number */
89-
struct string_list symlink_changes; /* we have to track symlinks */
76+
/*
77+
* We need to keep track of how symlinks in the preimage are
78+
* manipulated by the patches. A patch to add a/b/c where a/b
79+
* is a symlink should not be allowed to affect the directory
80+
* the symlink points at, but if the same patch removes a/b,
81+
* it is perfectly fine, as the patch removes a/b to make room
82+
* to create a directory a/b so that a/b/c can be created.
83+
*/
84+
struct strset removed_symlinks;
85+
struct strset kept_symlinks;
9086

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

0 commit comments

Comments
 (0)