summaryrefslogtreecommitdiff
path: root/upload-pack.c
diff options
context:
space:
mode:
Diffstat (limited to 'upload-pack.c')
-rw-r--r--upload-pack.c226
1 files changed, 181 insertions, 45 deletions
diff --git a/upload-pack.c b/upload-pack.c
index 951a2b23aa..3b66bf92ba 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -18,7 +18,7 @@
#include "sigchain.h"
#include "version.h"
#include "string-list.h"
-#include "argv-array.h"
+#include "strvec.h"
#include "prio-queue.h"
#include "protocol.h"
#include "quote.h"
@@ -88,6 +88,7 @@ struct upload_pack_data {
enum allow_uor allow_uor;
struct list_objects_filter_options filter_options;
+ struct string_list allowed_filters;
struct packet_writer writer;
@@ -103,10 +104,13 @@ struct upload_pack_data {
unsigned no_progress : 1;
unsigned use_include_tag : 1;
unsigned allow_filter : 1;
+ unsigned allow_filter_fallback : 1;
+ unsigned long tree_filter_max_depth;
unsigned done : 1; /* v2 only */
unsigned allow_ref_in_want : 1; /* v2 only */
unsigned allow_sideband_all : 1; /* v2 only */
+ unsigned advertise_sid : 1;
};
static void upload_pack_data_init(struct upload_pack_data *data)
@@ -120,6 +124,7 @@ static void upload_pack_data_init(struct upload_pack_data *data)
struct string_list deepen_not = STRING_LIST_INIT_DUP;
struct string_list uri_protocols = STRING_LIST_INIT_DUP;
struct object_array extra_edge_obj = OBJECT_ARRAY_INIT;
+ struct string_list allowed_filters = STRING_LIST_INIT_DUP;
memset(data, 0, sizeof(*data));
data->symref = symref;
@@ -131,9 +136,13 @@ static void upload_pack_data_init(struct upload_pack_data *data)
data->deepen_not = deepen_not;
data->uri_protocols = uri_protocols;
data->extra_edge_obj = extra_edge_obj;
+ data->allowed_filters = allowed_filters;
+ data->allow_filter_fallback = 1;
+ data->tree_filter_max_depth = ULONG_MAX;
packet_writer_init(&data->writer, 1);
data->keepalive = 5;
+ data->advertise_sid = 0;
}
static void upload_pack_data_clear(struct upload_pack_data *data)
@@ -147,6 +156,7 @@ static void upload_pack_data_clear(struct upload_pack_data *data)
string_list_clear(&data->deepen_not, 0);
object_array_clear(&data->extra_edge_obj);
list_objects_filter_release(&data->filter_options);
+ string_list_clear(&data->allowed_filters, 0);
free((char *)data->pack_objects_hook);
}
@@ -269,51 +279,51 @@ static void create_pack_file(struct upload_pack_data *pack_data,
if (!pack_data->pack_objects_hook)
pack_objects.git_cmd = 1;
else {
- argv_array_push(&pack_objects.args, pack_data->pack_objects_hook);
- argv_array_push(&pack_objects.args, "git");
+ strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
+ strvec_push(&pack_objects.args, "git");
pack_objects.use_shell = 1;
}
if (pack_data->shallow_nr) {
- argv_array_push(&pack_objects.args, "--shallow-file");
- argv_array_push(&pack_objects.args, "");
+ strvec_push(&pack_objects.args, "--shallow-file");
+ strvec_push(&pack_objects.args, "");
}
- argv_array_push(&pack_objects.args, "pack-objects");
- argv_array_push(&pack_objects.args, "--revs");
+ strvec_push(&pack_objects.args, "pack-objects");
+ strvec_push(&pack_objects.args, "--revs");
if (pack_data->use_thin_pack)
- argv_array_push(&pack_objects.args, "--thin");
+ strvec_push(&pack_objects.args, "--thin");
- argv_array_push(&pack_objects.args, "--stdout");
+ strvec_push(&pack_objects.args, "--stdout");
if (pack_data->shallow_nr)
- argv_array_push(&pack_objects.args, "--shallow");
+ strvec_push(&pack_objects.args, "--shallow");
if (!pack_data->no_progress)
- argv_array_push(&pack_objects.args, "--progress");
+ strvec_push(&pack_objects.args, "--progress");
if (pack_data->use_ofs_delta)
- argv_array_push(&pack_objects.args, "--delta-base-offset");
+ strvec_push(&pack_objects.args, "--delta-base-offset");
if (pack_data->use_include_tag)
- argv_array_push(&pack_objects.args, "--include-tag");
+ strvec_push(&pack_objects.args, "--include-tag");
if (pack_data->filter_options.choice) {
const char *spec =
expand_list_objects_filter_spec(&pack_data->filter_options);
if (pack_objects.use_shell) {
struct strbuf buf = STRBUF_INIT;
sq_quote_buf(&buf, spec);
- argv_array_pushf(&pack_objects.args, "--filter=%s", buf.buf);
+ strvec_pushf(&pack_objects.args, "--filter=%s", buf.buf);
strbuf_release(&buf);
} else {
- argv_array_pushf(&pack_objects.args, "--filter=%s",
- spec);
+ strvec_pushf(&pack_objects.args, "--filter=%s", spec);
}
}
if (uri_protocols) {
for (i = 0; i < uri_protocols->nr; i++)
- argv_array_pushf(&pack_objects.args, "--uri-protocol=%s",
+ strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
uri_protocols->items[i].string);
}
pack_objects.in = -1;
pack_objects.out = -1;
pack_objects.err = -1;
+ pack_objects.clean_on_exit = 1;
if (start_command(&pack_objects))
die("git upload-pack: unable to fork git-pack-objects");
@@ -482,7 +492,8 @@ static int got_oid(struct upload_pack_data *data,
{
if (get_oid_hex(hex, oid))
die("git upload-pack: expected SHA1 object, got '%s'", hex);
- if (!has_object_file(oid))
+ if (!has_object_file_with_flags(oid,
+ OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
return -1;
return do_got_oid(data, oid);
}
@@ -595,9 +606,8 @@ static int do_reachable_revlist(struct child_process *cmd,
"rev-list", "--stdin", NULL,
};
struct object *o;
- char namebuf[GIT_MAX_HEXSZ + 2]; /* ^ + hash + LF */
+ FILE *cmd_in = NULL;
int i;
- const unsigned hexsz = the_hash_algo->hexsz;
cmd->argv = argv;
cmd->git_cmd = 1;
@@ -615,8 +625,8 @@ static int do_reachable_revlist(struct child_process *cmd,
if (start_command(cmd))
goto error;
- namebuf[0] = '^';
- namebuf[hexsz + 1] = '\n';
+ cmd_in = xfdopen(cmd->in, "w");
+
for (i = get_max_object_index(); 0 < i; ) {
o = get_indexed_object(--i);
if (!o)
@@ -625,11 +635,9 @@ static int do_reachable_revlist(struct child_process *cmd,
o->flags &= ~TMP_MARK;
if (!is_our_ref(o, allow_uor))
continue;
- memcpy(namebuf + 1, oid_to_hex(&o->oid), hexsz);
- if (write_in_full(cmd->in, namebuf, hexsz + 2) < 0)
+ if (fprintf(cmd_in, "^%s\n", oid_to_hex(&o->oid)) < 0)
goto error;
}
- namebuf[hexsz] = '\n';
for (i = 0; i < src->nr; i++) {
o = src->objects[i].item;
if (is_our_ref(o, allow_uor)) {
@@ -639,11 +647,12 @@ static int do_reachable_revlist(struct child_process *cmd,
}
if (reachable && o->type == OBJ_COMMIT)
o->flags |= TMP_MARK;
- memcpy(namebuf, oid_to_hex(&o->oid), hexsz);
- if (write_in_full(cmd->in, namebuf, hexsz + 1) < 0)
+ if (fprintf(cmd_in, "%s\n", oid_to_hex(&o->oid)) < 0)
goto error;
}
- close(cmd->in);
+ if (ferror(cmd_in) || fflush(cmd_in))
+ goto error;
+ fclose(cmd_in);
cmd->in = -1;
sigchain_pop(SIGPIPE);
@@ -652,8 +661,8 @@ static int do_reachable_revlist(struct child_process *cmd,
error:
sigchain_pop(SIGPIPE);
- if (cmd->in >= 0)
- close(cmd->in);
+ if (cmd_in)
+ fclose(cmd_in);
if (cmd->out >= 0)
close(cmd->out);
return -1;
@@ -731,7 +740,6 @@ static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
return 0;
error:
- sigchain_pop(SIGPIPE);
if (cmd.out >= 0)
close(cmd.out);
return 1;
@@ -880,26 +888,26 @@ static int send_shallow_list(struct upload_pack_data *data)
deepen(data, data->depth);
ret = 1;
} else if (data->deepen_rev_list) {
- struct argv_array av = ARGV_ARRAY_INIT;
+ struct strvec av = STRVEC_INIT;
int i;
- argv_array_push(&av, "rev-list");
+ strvec_push(&av, "rev-list");
if (data->deepen_since)
- argv_array_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
+ strvec_pushf(&av, "--max-age=%"PRItime, data->deepen_since);
if (data->deepen_not.nr) {
- argv_array_push(&av, "--not");
+ strvec_push(&av, "--not");
for (i = 0; i < data->deepen_not.nr; i++) {
struct string_list_item *s = data->deepen_not.items + i;
- argv_array_push(&av, s->string);
+ strvec_push(&av, s->string);
}
- argv_array_push(&av, "--not");
+ strvec_push(&av, "--not");
}
for (i = 0; i < data->want_obj.nr; i++) {
struct object *o = data->want_obj.objects[i].item;
- argv_array_push(&av, oid_to_hex(&o->oid));
+ strvec_push(&av, oid_to_hex(&o->oid));
}
- deepen_by_rev_list(data, av.argc, av.argv);
- argv_array_clear(&av);
+ deepen_by_rev_list(data, av.nr, av.v);
+ strvec_clear(&av);
ret = 1;
} else {
if (data->shallows.nr > 0) {
@@ -983,6 +991,63 @@ static int process_deepen_not(const char *line, struct string_list *deepen_not,
return 0;
}
+NORETURN __attribute__((format(printf,2,3)))
+static void send_err_and_die(struct upload_pack_data *data,
+ const char *fmt, ...)
+{
+ struct strbuf buf = STRBUF_INIT;
+ va_list ap;
+
+ va_start(ap, fmt);
+ strbuf_vaddf(&buf, fmt, ap);
+ va_end(ap);
+
+ packet_writer_error(&data->writer, "%s", buf.buf);
+ die("%s", buf.buf);
+}
+
+static void check_one_filter(struct upload_pack_data *data,
+ struct list_objects_filter_options *opts)
+{
+ const char *key = list_object_filter_config_name(opts->choice);
+ struct string_list_item *item = string_list_lookup(&data->allowed_filters,
+ key);
+ int allowed;
+
+ if (item)
+ allowed = (intptr_t)item->util;
+ else
+ allowed = data->allow_filter_fallback;
+
+ if (!allowed)
+ send_err_and_die(data, "filter '%s' not supported", key);
+
+ if (opts->choice == LOFC_TREE_DEPTH &&
+ opts->tree_exclude_depth > data->tree_filter_max_depth)
+ send_err_and_die(data,
+ "tree filter allows max depth %lu, but got %lu",
+ data->tree_filter_max_depth,
+ opts->tree_exclude_depth);
+}
+
+static void check_filter_recurse(struct upload_pack_data *data,
+ struct list_objects_filter_options *opts)
+{
+ size_t i;
+
+ check_one_filter(data, opts);
+ if (opts->choice != LOFC_COMBINE)
+ return;
+
+ for (i = 0; i < opts->sub_nr; i++)
+ check_filter_recurse(data, &opts->sub[i]);
+}
+
+static void die_if_using_banned_filter(struct upload_pack_data *data)
+{
+ check_filter_recurse(data, &data->filter_options);
+}
+
static void receive_needs(struct upload_pack_data *data,
struct packet_reader *reader)
{
@@ -994,6 +1059,7 @@ static void receive_needs(struct upload_pack_data *data,
const char *features;
struct object_id oid_buf;
const char *arg;
+ int feature_len;
reset_timeout(data->timeout);
if (packet_reader_read(reader) != PACKET_READ_NORMAL)
@@ -1013,6 +1079,7 @@ static void receive_needs(struct upload_pack_data *data,
die("git upload-pack: filtering capability not negotiated");
list_objects_filter_die_if_populated(&data->filter_options);
parse_list_objects_filter(&data->filter_options, arg);
+ die_if_using_banned_filter(data);
continue;
}
@@ -1045,6 +1112,13 @@ static void receive_needs(struct upload_pack_data *data,
parse_feature_request(features, "filter"))
data->filter_capability_requested = 1;
+ arg = parse_feature_value(features, "session-id", &feature_len, NULL);
+ if (arg) {
+ char *client_sid = xstrndup(arg, feature_len);
+ trace2_data_string("transfer", NULL, "client-sid", client_sid);
+ free(client_sid);
+ }
+
o = parse_object(the_repository, &oid_buf);
if (!o) {
packet_writer_error(&data->writer,
@@ -1115,6 +1189,11 @@ static void format_symref_info(struct strbuf *buf, struct string_list *symref)
strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util);
}
+static void format_session_id(struct strbuf *buf, struct upload_pack_data *d) {
+ if (d->advertise_sid)
+ strbuf_addf(buf, " session-id=%s", trace2_session_id());
+}
+
static int send_ref(const char *refname, const struct object_id *oid,
int flag, void *cb_data)
{
@@ -1130,9 +1209,11 @@ static int send_ref(const char *refname, const struct object_id *oid,
if (capabilities) {
struct strbuf symref_info = STRBUF_INIT;
+ struct strbuf session_id = STRBUF_INIT;
format_symref_info(&symref_info, &data->symref);
- packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s object-format=%s agent=%s\n",
+ format_session_id(&session_id, data);
+ packet_write_fmt(1, "%s %s%c%s%s%s%s%s%s%s object-format=%s agent=%s\n",
oid_to_hex(oid), refname_nons,
0, capabilities,
(data->allow_uor & ALLOW_TIP_SHA1) ?
@@ -1142,9 +1223,11 @@ static int send_ref(const char *refname, const struct object_id *oid,
data->stateless_rpc ? " no-done" : "",
symref_info.buf,
data->allow_filter ? " filter" : "",
+ session_id.buf,
the_hash_algo->name,
git_user_agent_sanitized());
strbuf_release(&symref_info);
+ strbuf_release(&session_id);
} else {
packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), refname_nons);
}
@@ -1170,6 +1253,41 @@ static int find_symref(const char *refname, const struct object_id *oid,
return 0;
}
+static int parse_object_filter_config(const char *var, const char *value,
+ struct upload_pack_data *data)
+{
+ struct strbuf buf = STRBUF_INIT;
+ const char *sub, *key;
+ size_t sub_len;
+
+ if (parse_config_key(var, "uploadpackfilter", &sub, &sub_len, &key))
+ return 0;
+
+ if (!sub) {
+ if (!strcmp(key, "allow"))
+ data->allow_filter_fallback = git_config_bool(var, value);
+ return 0;
+ }
+
+ strbuf_add(&buf, sub, sub_len);
+
+ if (!strcmp(key, "allow"))
+ string_list_insert(&data->allowed_filters, buf.buf)->util =
+ (void *)(intptr_t)git_config_bool(var, value);
+ else if (!strcmp(buf.buf, "tree") && !strcmp(key, "maxdepth")) {
+ if (!value) {
+ strbuf_release(&buf);
+ return config_error_nonbool(var);
+ }
+ string_list_insert(&data->allowed_filters, buf.buf)->util =
+ (void *)(intptr_t)1;
+ data->tree_filter_max_depth = git_config_ulong(var, value);
+ }
+
+ strbuf_release(&buf);
+ return 0;
+}
+
static int upload_pack_config(const char *var, const char *value, void *cb_data)
{
struct upload_pack_data *data = cb_data;
@@ -1201,14 +1319,19 @@ static int upload_pack_config(const char *var, const char *value, void *cb_data)
data->allow_sideband_all = git_config_bool(var, value);
} else if (!strcmp("core.precomposeunicode", var)) {
precomposed_unicode = git_config_bool(var, value);
+ } else if (!strcmp("transfer.advertisesid", var)) {
+ data->advertise_sid = git_config_bool(var, value);
}
if (current_config_scope() != CONFIG_SCOPE_LOCAL &&
- current_config_scope() != CONFIG_SCOPE_WORKTREE) {
+ current_config_scope() != CONFIG_SCOPE_WORKTREE) {
if (!strcmp("uploadpack.packobjectshook", var))
return git_config_string(&data->pack_objects_hook, var, value);
}
+ if (parse_object_filter_config(var, value, data) < 0)
+ return -1;
+
return parse_hide_refs_config(var, value, "uploadpack");
}
@@ -1244,7 +1367,18 @@ void upload_pack(struct upload_pack_options *options)
PACKET_READ_DIE_ON_ERR_PACKET);
receive_needs(&data, &reader);
- if (data.want_obj.nr) {
+
+ /*
+ * An EOF at this exact point in negotiation should be
+ * acceptable from stateless clients as they will consume the
+ * shallow list before doing subsequent rpc with haves/etc.
+ */
+ if (data.stateless_rpc)
+ reader.options |= PACKET_READ_GENTLE_ON_EOF;
+
+ if (data.want_obj.nr &&
+ packet_reader_peek(&reader) != PACKET_READ_EOF) {
+ reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
get_common_commits(&data, &reader);
create_pack_file(&data, NULL);
}
@@ -1389,6 +1523,7 @@ static void process_args(struct packet_reader *request,
if (data->allow_filter && skip_prefix(arg, "filter ", &p)) {
list_objects_filter_die_if_populated(&data->filter_options);
parse_list_objects_filter(&data->filter_options, p);
+ die_if_using_banned_filter(data);
continue;
}
@@ -1423,7 +1558,8 @@ static int process_haves(struct upload_pack_data *data, struct oid_array *common
for (i = 0; i < data->haves.nr; i++) {
const struct object_id *oid = &data->haves.oid[i];
- if (!has_object_file(oid))
+ if (!has_object_file_with_flags(oid,
+ OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT))
continue;
oid_array_append(common, oid);
@@ -1521,7 +1657,7 @@ enum fetch_state {
FETCH_DONE,
};
-int upload_pack_v2(struct repository *r, struct argv_array *keys,
+int upload_pack_v2(struct repository *r, struct strvec *keys,
struct packet_reader *request)
{
enum fetch_state state = FETCH_PROCESS_ARGS;