Skip to content

Commit 655ee9e

Browse files
ienorandgitster
authored andcommitted
setup: don't dereference in-tree symlinks for absolute paths
The prefix_path_gently() function currently applies real_path to everything if given an absolute path, dereferencing symlinks both outside and inside the work tree. This causes most high-level functions to misbehave when acting on symlinks given via absolute paths. For example $ git add /dir/repo/symlink attempts to add the target of the symlink rather than the symlink itself, which is usually not what the user intends to do. In order to manipulate symlinks in the work tree using absolute paths, symlinks should only be dereferenced outside the work tree. Modify the prefix_path_gently() to first normalize the path in order to make sure path levels are separated by '/', then pass the result to 'abspath_part_inside_repo' to find the part inside the work tree (without dereferencing any symlinks inside the work tree). For absolute paths, prefix_path_gently() did not, nor does now do, any actual prefixing, hence the result from abspath_part_in_repo() is returned as-is. Fixes t0060-82 and t3004-5. Signed-off-by: Martin Erik Werner <[email protected]> Reviewed-by: Duy Nguyen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ddc2a62 commit 655ee9e

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

setup.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,38 +86,28 @@ char *prefix_path_gently(const char *prefix, int len,
8686
const char *orig = path;
8787
char *sanitized;
8888
if (is_absolute_path(orig)) {
89-
const char *temp = real_path(path);
90-
sanitized = xmalloc(len + strlen(temp) + 1);
91-
strcpy(sanitized, temp);
89+
sanitized = xmalloc(strlen(path) + 1);
9290
if (remaining_prefix)
9391
*remaining_prefix = 0;
92+
if (normalize_path_copy_len(sanitized, path, remaining_prefix)) {
93+
free(sanitized);
94+
return NULL;
95+
}
96+
if (abspath_part_inside_repo(sanitized)) {
97+
free(sanitized);
98+
return NULL;
99+
}
94100
} else {
95101
sanitized = xmalloc(len + strlen(path) + 1);
96102
if (len)
97103
memcpy(sanitized, prefix, len);
98104
strcpy(sanitized + len, path);
99105
if (remaining_prefix)
100106
*remaining_prefix = len;
101-
}
102-
if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix))
103-
goto error_out;
104-
if (is_absolute_path(orig)) {
105-
size_t root_len, len, total;
106-
const char *work_tree = get_git_work_tree();
107-
if (!work_tree)
108-
goto error_out;
109-
len = strlen(work_tree);
110-
root_len = offset_1st_component(work_tree);
111-
total = strlen(sanitized) + 1;
112-
if (strncmp(sanitized, work_tree, len) ||
113-
(len > root_len && sanitized[len] != '\0' && sanitized[len] != '/')) {
114-
error_out:
107+
if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) {
115108
free(sanitized);
116109
return NULL;
117110
}
118-
if (sanitized[len] == '/')
119-
len++;
120-
memmove(sanitized, sanitized + len, total - len);
121111
}
122112
return sanitized;
123113
}

t/t0060-path-utils.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
190190
test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
191191
'
192192

193-
test_expect_failure SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
193+
test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
194194
ln -s target symlink &&
195195
test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
196196
'

t/t3004-ls-files-basic.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ test_expect_success 'ls-files -h in corrupt repository' '
3636
test_i18ngrep "[Uu]sage: git ls-files " broken/usage
3737
'
3838

39-
test_expect_failure SYMLINKS 'ls-files with absolute paths to symlinks' '
39+
test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' '
4040
mkdir subs &&
4141
ln -s nosuch link &&
4242
ln -s ../nosuch subs/link &&

0 commit comments

Comments
 (0)