Skip to content

Commit 78d909a

Browse files
committed
Merge branch 'tc/http-cleanup'
* tc/http-cleanup: remote-curl: init walker only when needed remote-curl: use http_fetch_ref() instead of walker wrapper http: init and cleanup separately from http-walker http-walker: cleanup more thoroughly http-push: remove "|| 1" to enable verbose check t554[01]-http-push: refactor, add non-ff tests t5541-http-push: check that ref is unchanged for non-ff test
2 parents 53997a3 + 26e1e0b commit 78d909a

File tree

8 files changed

+66
-39
lines changed

8 files changed

+66
-39
lines changed

http-fetch.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "exec_cmd.h"
3+
#include "http.h"
34
#include "walker.h"
45

56
static const char http_fetch_usage[] = "git http-fetch "
@@ -69,7 +70,8 @@ int main(int argc, const char **argv)
6970
url = rewritten_url;
7071
}
7172

72-
walker = get_http_walker(url, NULL);
73+
http_init(NULL);
74+
walker = get_http_walker(url);
7375
walker->get_tree = get_tree;
7476
walker->get_history = get_history;
7577
walker->get_all = get_all;
@@ -89,6 +91,7 @@ int main(int argc, const char **argv)
8991
}
9092

9193
walker_free(walker);
94+
http_cleanup();
9295

9396
free(rewritten_url);
9497

http-push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,7 @@ int main(int argc, char **argv)
19651965
}
19661966

19671967
if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
1968-
if (push_verbosely || 1)
1968+
if (push_verbosely)
19691969
fprintf(stderr, "'%s': up-to-date\n", ref->name);
19701970
if (helper_status)
19711971
printf("ok %s up to date\n", ref->name);

http-walker.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,17 +543,30 @@ static int fetch_ref(struct walker *walker, struct ref *ref)
543543

544544
static void cleanup(struct walker *walker)
545545
{
546-
http_cleanup();
546+
struct walker_data *data = walker->data;
547+
struct alt_base *alt, *alt_next;
548+
549+
if (data) {
550+
alt = data->alt;
551+
while (alt) {
552+
alt_next = alt->next;
553+
554+
free(alt->base);
555+
free(alt);
556+
557+
alt = alt_next;
558+
}
559+
free(data);
560+
walker->data = NULL;
561+
}
547562
}
548563

549-
struct walker *get_http_walker(const char *url, struct remote *remote)
564+
struct walker *get_http_walker(const char *url)
550565
{
551566
char *s;
552567
struct walker_data *data = xmalloc(sizeof(struct walker_data));
553568
struct walker *walker = xmalloc(sizeof(struct walker));
554569

555-
http_init(remote);
556-
557570
data->alt = xmalloc(sizeof(*data->alt));
558571
data->alt->base = xmalloc(strlen(url) + 1);
559572
strcpy(data->alt->base, url);

remote-curl.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
static struct remote *remote;
1212
static const char *url;
13-
static struct walker *walker;
1413

1514
struct options {
1615
int verbosity;
@@ -22,12 +21,6 @@ struct options {
2221
};
2322
static struct options options;
2423

25-
static void init_walker(void)
26-
{
27-
if (!walker)
28-
walker = get_http_walker(url, remote);
29-
}
30-
3124
static int set_option(const char *name, const char *value)
3225
{
3326
if (!strcmp(name, "verbosity")) {
@@ -119,7 +112,6 @@ static struct discovery* discover_refs(const char *service)
119112
}
120113
refs_url = strbuf_detach(&buffer, NULL);
121114

122-
init_walker();
123115
http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
124116

125117
/* try again with "plain" url (no ? or & appended) */
@@ -250,9 +242,8 @@ static struct ref *parse_info_refs(struct discovery *heads)
250242
i++;
251243
}
252244

253-
init_walker();
254245
ref = alloc_ref("HEAD");
255-
if (!walker->fetch_ref(walker, ref) &&
246+
if (!http_fetch_ref(url, ref) &&
256247
!resolve_remote_symref(ref, refs)) {
257248
ref->next = refs;
258249
refs = ref;
@@ -502,7 +493,6 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
502493
struct child_process client;
503494
int err = 0;
504495

505-
init_walker();
506496
memset(&client, 0, sizeof(client));
507497
client.in = -1;
508498
client.out = -1;
@@ -554,6 +544,7 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
554544

555545
static int fetch_dumb(int nr_heads, struct ref **to_fetch)
556546
{
547+
struct walker *walker;
557548
char **targets = xmalloc(nr_heads * sizeof(char*));
558549
int ret, i;
559550

@@ -562,13 +553,14 @@ static int fetch_dumb(int nr_heads, struct ref **to_fetch)
562553
for (i = 0; i < nr_heads; i++)
563554
targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
564555

565-
init_walker();
556+
walker = get_http_walker(url);
566557
walker->get_all = 1;
567558
walker->get_tree = 1;
568559
walker->get_history = 1;
569560
walker->get_verbosely = options.verbosity >= 3;
570561
walker->get_recover = 0;
571562
ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
563+
walker_free(walker);
572564

573565
for (i = 0; i < nr_heads; i++)
574566
free(targets[i]);
@@ -811,6 +803,8 @@ int main(int argc, const char **argv)
811803
url = remote->url[0];
812804
}
813805

806+
http_init(remote);
807+
814808
do {
815809
if (strbuf_getline(&buf, stdin, '\n') == EOF)
816810
break;
@@ -856,5 +850,8 @@ int main(int argc, const char **argv)
856850
}
857851
strbuf_reset(&buf);
858852
} while (1);
853+
854+
http_cleanup();
855+
859856
return 0;
860857
}

t/lib-httpd.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,32 @@ stop_httpd() {
131131
"$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
132132
-f "$TEST_PATH/apache.conf" $HTTPD_PARA -k stop
133133
}
134+
135+
test_http_push_nonff() {
136+
REMOTE_REPO=$1
137+
LOCAL_REPO=$2
138+
BRANCH=$3
139+
140+
test_expect_success 'non-fast-forward push fails' '
141+
cd "$REMOTE_REPO" &&
142+
HEAD=$(git rev-parse --verify HEAD) &&
143+
144+
cd "$LOCAL_REPO" &&
145+
git checkout $BRANCH &&
146+
echo "changed" > path2 &&
147+
git commit -a -m path2 --amend &&
148+
149+
!(git push -v origin >output 2>&1) &&
150+
(cd "$REMOTE_REPO" &&
151+
test $HEAD = $(git rev-parse --verify HEAD))
152+
'
153+
154+
test_expect_success 'non-fast-forward push show ref status' '
155+
grep "^ ! \[rejected\][ ]*$BRANCH -> $BRANCH (non-fast-forward)$" output
156+
'
157+
158+
test_expect_success 'non-fast-forward push shows help message' '
159+
grep "To prevent you from losing history, non-fast-forward updates were rejected" \
160+
output
161+
'
162+
}

t/t5540-http-push.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ test_expect_success 'PUT and MOVE sends object to URLs with SHA-1 hash suffix' '
137137
138138
'
139139

140+
test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
141+
"$ROOT_PATH"/test_repo_clone master
142+
140143
stop_httpd
141144

142145
test_done

t/t5541-http-push.sh

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,8 @@ test_expect_success 'used receive-pack service' '
8888
test_cmp exp act
8989
'
9090

91-
test_expect_success 'non-fast-forward push fails' '
92-
cd "$ROOT_PATH"/test_repo_clone &&
93-
git checkout master &&
94-
echo "changed" > path2 &&
95-
git commit -a -m path2 --amend &&
96-
97-
HEAD=$(git rev-parse --verify HEAD) &&
98-
!(git push -v origin >output 2>&1) &&
99-
(cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
100-
test $HEAD != $(git rev-parse --verify HEAD))
101-
'
102-
103-
test_expect_success 'non-fast-forward push show ref status' '
104-
grep "^ ! \[rejected\][ ]*master -> master (non-fast-forward)$" output
105-
'
106-
107-
test_expect_success 'non-fast-forward push shows help message' '
108-
grep "To prevent you from losing history, non-fast-forward updates were rejected" \
109-
output
110-
'
91+
test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
92+
"$ROOT_PATH"/test_repo_clone master
11193

11294
test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper' '
11395
# create a dissimilarly-named remote ref so that git is unable to match the

walker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ int walker_fetch(struct walker *impl, int targets, char **target,
3434

3535
void walker_free(struct walker *walker);
3636

37-
struct walker *get_http_walker(const char *url, struct remote *remote);
37+
struct walker *get_http_walker(const char *url);
3838

3939
#endif /* WALKER_H */

0 commit comments

Comments
 (0)