Skip to content

Commit 6b090e1

Browse files
committed
Merge branch 'tc/http-urls-ends-with-slash' into maint
* tc/http-urls-ends-with-slash: http-fetch: rework url handling http-push: add trailing slash at arg-parse time, instead of later on http-push: check path length before using it http-push: Normalise directory names when pushing to some WebDAV servers http-backend: use end_url_with_slash() url: add str wrapper for end_url_with_slash() shift end_url_with_slash() from http.[ch] to url.[ch] t5550-http-fetch: add test for http-fetch t5550-http-fetch: add missing '&&'
2 parents 5b57076 + 6f5185b commit 6b090e1

File tree

9 files changed

+55
-40
lines changed

9 files changed

+55
-40
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ builtin/tar-tree.o archive-tar.o: tar.h
18791879
builtin/pack-objects.o: thread-utils.h
18801880
connect.o transport.o http-backend.o: url.h
18811881
http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h
1882-
http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h
1882+
http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h
18831883

18841884
xdiff-interface.o $(XDIFF_OBJS): \
18851885
xdiff/xinclude.h xdiff/xmacros.h xdiff/xdiff.h xdiff/xtypes.h \

http-backend.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,7 @@ static char* getdir(void)
510510
die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
511511
if (daemon_avoid_alias(pathinfo))
512512
die("'%s': aliased", pathinfo);
513-
strbuf_addstr(&buf, root);
514-
if (buf.buf[buf.len - 1] != '/')
515-
strbuf_addch(&buf, '/');
513+
end_url_with_slash(&buf, root);
516514
if (pathinfo[0] == '/')
517515
pathinfo++;
518516
strbuf_addstr(&buf, pathinfo);

http-fetch.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ int main(int argc, const char **argv)
1414
int commits;
1515
const char **write_ref = NULL;
1616
char **commit_id;
17-
const char *url;
18-
char *rewritten_url = NULL;
17+
char *url = NULL;
1918
int arg = 1;
2019
int rc = 0;
2120
int get_tree = 0;
@@ -57,19 +56,14 @@ int main(int argc, const char **argv)
5756
commit_id = (char **) &argv[arg++];
5857
commits = 1;
5958
}
60-
url = argv[arg];
59+
60+
if (argv[arg])
61+
str_end_url_with_slash(argv[arg], &url);
6162

6263
prefix = setup_git_directory();
6364

6465
git_config(git_default_config, NULL);
6566

66-
if (url && url[strlen(url)-1] != '/') {
67-
rewritten_url = xmalloc(strlen(url)+2);
68-
strcpy(rewritten_url, url);
69-
strcat(rewritten_url, "/");
70-
url = rewritten_url;
71-
}
72-
7367
http_init(NULL);
7468
walker = get_http_walker(url);
7569
walker->get_tree = get_tree;
@@ -93,7 +87,7 @@ int main(int argc, const char **argv)
9387
walker_free(walker);
9488
http_cleanup();
9589

96-
free(rewritten_url);
90+
free(url);
9791

9892
return rc;
9993
}

http-push.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,10 @@ static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
10901090
if (tag_closed) {
10911091
if (!strcmp(ctx->name, DAV_PROPFIND_RESP) && ls->dentry_name) {
10921092
if (ls->dentry_flags & IS_DIR) {
1093+
1094+
/* ensure collection names end with slash */
1095+
str_end_url_with_slash(ls->dentry_name, &ls->dentry_name);
1096+
10931097
if (ls->flags & PROCESS_DIRS) {
10941098
ls->userFunc(ls);
10951099
}
@@ -1112,8 +1116,16 @@ static void handle_remote_ls_ctx(struct xml_ctx *ctx, int tag_closed)
11121116
}
11131117
}
11141118
if (path) {
1115-
path += repo->path_len;
1116-
ls->dentry_name = xstrdup(path);
1119+
const char *url = repo->url;
1120+
if (repo->path)
1121+
url = repo->path;
1122+
if (strncmp(path, url, repo->path_len))
1123+
error("Parsed path '%s' does not match url: '%s'\n",
1124+
path, url);
1125+
else {
1126+
path += repo->path_len;
1127+
ls->dentry_name = xstrdup(path);
1128+
}
11171129
}
11181130
} else if (!strcmp(ctx->name, DAV_PROPFIND_COLLECTION)) {
11191131
ls->dentry_flags |= IS_DIR;
@@ -1789,7 +1801,6 @@ int main(int argc, char **argv)
17891801
int new_refs;
17901802
struct ref *ref, *local_refs;
17911803
struct remote *remote;
1792-
char *rewritten_url = NULL;
17931804

17941805
git_extract_argv0_path(argv[0]);
17951806

@@ -1835,8 +1846,8 @@ int main(int argc, char **argv)
18351846
}
18361847
if (!repo->url) {
18371848
char *path = strstr(arg, "//");
1838-
repo->url = arg;
1839-
repo->path_len = strlen(arg);
1849+
str_end_url_with_slash(arg, &repo->url);
1850+
repo->path_len = strlen(repo->url);
18401851
if (path) {
18411852
repo->path = strchr(path+2, '/');
18421853
if (repo->path)
@@ -1872,15 +1883,6 @@ int main(int argc, char **argv)
18721883
remote->url[remote->url_nr++] = repo->url;
18731884
http_init(remote);
18741885

1875-
if (repo->url && repo->url[strlen(repo->url)-1] != '/') {
1876-
rewritten_url = xmalloc(strlen(repo->url)+2);
1877-
strcpy(rewritten_url, repo->url);
1878-
strcat(rewritten_url, "/");
1879-
repo->path = rewritten_url + (repo->path - repo->url);
1880-
repo->path_len++;
1881-
repo->url = rewritten_url;
1882-
}
1883-
18841886
#ifdef USE_CURL_MULTI
18851887
is_running_queue = 0;
18861888
#endif
@@ -2088,7 +2090,6 @@ int main(int argc, char **argv)
20882090
}
20892091

20902092
cleanup:
2091-
free(rewritten_url);
20922093
if (info_ref_lock)
20932094
unlock_remote(info_ref_lock);
20942095
free(repo);

http.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -743,13 +743,6 @@ static inline int hex(int v)
743743
return 'A' + v - 10;
744744
}
745745

746-
void end_url_with_slash(struct strbuf *buf, const char *url)
747-
{
748-
strbuf_addstr(buf, url);
749-
if (buf->len && buf->buf[buf->len - 1] != '/')
750-
strbuf_addstr(buf, "/");
751-
}
752-
753746
static char *quote_ref_url(const char *base, const char *ref)
754747
{
755748
struct strbuf buf = STRBUF_INIT;

http.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "strbuf.h"
1010
#include "remote.h"
11+
#include "url.h"
1112

1213
/*
1314
* We detect based on the cURL version if multi-transfer is
@@ -117,7 +118,6 @@ extern void append_remote_object_url(struct strbuf *buf, const char *url,
117118
int only_two_digit_prefix);
118119
extern char *get_remote_object_url(const char *url, const char *hex,
119120
int only_two_digit_prefix);
120-
extern void end_url_with_slash(struct strbuf *buf, const char *url);
121121

122122
/* Options for http_request_*() */
123123
#define HTTP_NO_CACHE 1

t/t5550-http-fetch.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ test_expect_success 'create http-accessible bare repository' '
3030
'
3131

3232
test_expect_success 'clone http repository' '
33-
git clone $HTTPD_URL/dumb/repo.git clone &&
33+
git clone $HTTPD_URL/dumb/repo.git clone-tmpl &&
34+
cp -R clone-tmpl clone &&
3435
test_cmp file clone/file
3536
'
3637

@@ -44,11 +45,22 @@ test_expect_success 'clone http repository with authentication' '
4445
test_expect_success 'fetch changes via http' '
4546
echo content >>file &&
4647
git commit -a -m two &&
47-
git push public
48+
git push public &&
4849
(cd clone && git pull) &&
4950
test_cmp file clone/file
5051
'
5152

53+
test_expect_success 'fetch changes via manual http-fetch' '
54+
cp -R clone-tmpl clone2 &&
55+
56+
HEAD=$(git rev-parse --verify HEAD) &&
57+
(cd clone2 &&
58+
git http-fetch -a -w heads/master-new $HEAD $(git config remote.origin.url) &&
59+
git checkout master-new &&
60+
test $HEAD = $(git rev-parse --verify HEAD)) &&
61+
test_cmp file clone2/file
62+
'
63+
5264
test_expect_success 'http remote detects correct HEAD' '
5365
git push public master:other &&
5466
(cd clone &&

url.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,17 @@ char *url_decode_parameter_value(const char **query)
125125
struct strbuf out = STRBUF_INIT;
126126
return url_decode_internal(query, "&", &out, 1);
127127
}
128+
129+
void end_url_with_slash(struct strbuf *buf, const char *url)
130+
{
131+
strbuf_addstr(buf, url);
132+
if (buf->len && buf->buf[buf->len - 1] != '/')
133+
strbuf_addstr(buf, "/");
134+
}
135+
136+
void str_end_url_with_slash(const char *url, char **dest) {
137+
struct strbuf buf = STRBUF_INIT;
138+
end_url_with_slash(&buf, url);
139+
free(*dest);
140+
*dest = strbuf_detach(&buf, NULL);
141+
}

url.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ extern char *url_decode(const char *url);
77
extern char *url_decode_parameter_name(const char **query);
88
extern char *url_decode_parameter_value(const char **query);
99

10+
extern void end_url_with_slash(struct strbuf *buf, const char *url);
11+
extern void str_end_url_with_slash(const char *url, char **dest);
12+
1013
#endif /* URL_H */

0 commit comments

Comments
 (0)