Skip to content

Commit 05c1eb1

Browse files
committed
push: teach --force-with-lease to smart-http transport
We have been passing enough information to enable the compare-and-swap logic down to the transport layer, but the transport helper was not passing it to smart-http transport. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 77aa934 commit 05c1eb1

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

remote-curl.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "exec_cmd.h"
77
#include "run-command.h"
88
#include "pkt-line.h"
9+
#include "string-list.h"
910
#include "sideband.h"
1011

1112
static struct remote *remote;
@@ -20,6 +21,7 @@ struct options {
2021
thin : 1;
2122
};
2223
static struct options options;
24+
static struct string_list cas_options = STRING_LIST_INIT_DUP;
2325

2426
static int set_option(const char *name, const char *value)
2527
{
@@ -66,6 +68,13 @@ static int set_option(const char *name, const char *value)
6668
return -1;
6769
return 0;
6870
}
71+
else if (!strcmp(name, "cas")) {
72+
struct strbuf val = STRBUF_INIT;
73+
strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
74+
string_list_append(&cas_options, val.buf);
75+
strbuf_release(&val);
76+
return 0;
77+
}
6978
else {
7079
return 1 /* unsupported */;
7180
}
@@ -789,8 +798,9 @@ static int push_git(struct discovery *heads, int nr_spec, char **specs)
789798
struct rpc_state rpc;
790799
const char **argv;
791800
int argc = 0, i, err;
801+
struct string_list_item *cas_option;
792802

793-
argv = xmalloc((10 + nr_spec) * sizeof(char*));
803+
argv = xmalloc((10 + nr_spec + cas_options.nr) * sizeof(char *));
794804
argv[argc++] = "send-pack";
795805
argv[argc++] = "--stateless-rpc";
796806
argv[argc++] = "--helper-status";
@@ -803,6 +813,10 @@ static int push_git(struct discovery *heads, int nr_spec, char **specs)
803813
else if (options.verbosity > 1)
804814
argv[argc++] = "--verbose";
805815
argv[argc++] = options.progress ? "--progress" : "--no-progress";
816+
817+
for_each_string_list_item(cas_option, &cas_options)
818+
argv[argc++] = cas_option->string;
819+
806820
argv[argc++] = url;
807821
for (i = 0; i < nr_spec; i++)
808822
argv[argc++] = specs[i];

t/lib-httpd.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ stop_httpd() {
141141
-f "$TEST_PATH/apache.conf" $HTTPD_PARA -k stop
142142
}
143143

144-
test_http_push_nonff() {
144+
test_http_push_nonff () {
145145
REMOTE_REPO=$1
146146
LOCAL_REPO=$2
147147
BRANCH=$3
148+
EXPECT_CAS_RESULT=${4-failure}
148149

149150
test_expect_success 'non-fast-forward push fails' '
150151
cd "$REMOTE_REPO" &&

t/t5541-http-push.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ test_expect_success 'used receive-pack service' '
153153
'
154154

155155
test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
156-
"$ROOT_PATH"/test_repo_clone master
156+
"$ROOT_PATH"/test_repo_clone master success
157157

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

transport-helper.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,13 +742,15 @@ static void push_update_refs_status(struct helper_data *data,
742742
}
743743

744744
static int push_refs_with_push(struct transport *transport,
745-
struct ref *remote_refs, int flags)
745+
struct ref *remote_refs, int flags)
746746
{
747747
int force_all = flags & TRANSPORT_PUSH_FORCE;
748748
int mirror = flags & TRANSPORT_PUSH_MIRROR;
749749
struct helper_data *data = transport->data;
750750
struct strbuf buf = STRBUF_INIT;
751751
struct ref *ref;
752+
struct string_list cas_options = STRING_LIST_INIT_DUP;
753+
struct string_list_item *cas_option;
752754

753755
get_helper(transport);
754756
if (!data->push)
@@ -784,11 +786,29 @@ static int push_refs_with_push(struct transport *transport,
784786
strbuf_addch(&buf, ':');
785787
strbuf_addstr(&buf, ref->name);
786788
strbuf_addch(&buf, '\n');
789+
790+
/*
791+
* The "--force-with-lease" options without explicit
792+
* values to expect have already been expanded into
793+
* the ref->old_sha1_expect[] field; we can ignore
794+
* transport->smart_options->cas altogether and instead
795+
* can enumerate them from the refs.
796+
*/
797+
if (ref->expect_old_sha1) {
798+
struct strbuf cas = STRBUF_INIT;
799+
strbuf_addf(&cas, "%s:%s",
800+
ref->name, sha1_to_hex(ref->old_sha1_expect));
801+
string_list_append(&cas_options, strbuf_detach(&cas, NULL));
802+
}
787803
}
788-
if (buf.len == 0)
804+
if (buf.len == 0) {
805+
string_list_clear(&cas_options, 0);
789806
return 0;
807+
}
790808

791809
standard_options(transport);
810+
for_each_string_list_item(cas_option, &cas_options)
811+
set_helper_option(transport, "cas", cas_option->string);
792812

793813
if (flags & TRANSPORT_PUSH_DRY_RUN) {
794814
if (set_helper_option(transport, "dry-run", "true") != 0)

0 commit comments

Comments
 (0)