mirror of
https://github.com/git/git.git
synced 2026-01-09 01:34:00 +00:00
transport: convert pre-push to hook API
Move the pre-push hook from custom run-command invocations to the new hook API which doesn't require a custom child_process structure and signal toggling. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
05eccff8c7
commit
3e2836a742
103
transport.c
103
transport.c
@@ -1316,65 +1316,66 @@ static void die_with_unpushed_submodules(struct string_list *needs_pushing)
|
|||||||
die(_("Aborting."));
|
die(_("Aborting."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct feed_pre_push_hook_data {
|
||||||
|
struct strbuf buf;
|
||||||
|
const struct ref *refs;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int pre_push_hook_feed_stdin(int hook_stdin_fd, void *pp_cb UNUSED, void *pp_task_cb)
|
||||||
|
{
|
||||||
|
struct feed_pre_push_hook_data *data = pp_task_cb;
|
||||||
|
const struct ref *r = data->refs;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
if (!r)
|
||||||
|
return 1; /* no more refs */
|
||||||
|
|
||||||
|
data->refs = r->next;
|
||||||
|
|
||||||
|
switch (r->status) {
|
||||||
|
case REF_STATUS_REJECT_NONFASTFORWARD:
|
||||||
|
case REF_STATUS_REJECT_REMOTE_UPDATED:
|
||||||
|
case REF_STATUS_REJECT_STALE:
|
||||||
|
case REF_STATUS_UPTODATE:
|
||||||
|
return 0; /* skip refs which won't be pushed */
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!r->peer_ref)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
strbuf_reset(&data->buf);
|
||||||
|
strbuf_addf(&data->buf, "%s %s %s %s\n",
|
||||||
|
r->peer_ref->name, oid_to_hex(&r->new_oid),
|
||||||
|
r->name, oid_to_hex(&r->old_oid));
|
||||||
|
|
||||||
|
ret = write_in_full(hook_stdin_fd, data->buf.buf, data->buf.len);
|
||||||
|
if (ret < 0 && errno != EPIPE)
|
||||||
|
return ret; /* We do not mind if a hook does not read all refs. */
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int run_pre_push_hook(struct transport *transport,
|
static int run_pre_push_hook(struct transport *transport,
|
||||||
struct ref *remote_refs)
|
struct ref *remote_refs)
|
||||||
{
|
{
|
||||||
int ret = 0, x;
|
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
|
||||||
struct ref *r;
|
struct feed_pre_push_hook_data data;
|
||||||
struct child_process proc = CHILD_PROCESS_INIT;
|
int ret = 0;
|
||||||
struct strbuf buf;
|
|
||||||
const char *hook_path = find_hook(the_repository, "pre-push");
|
|
||||||
|
|
||||||
if (!hook_path)
|
strvec_push(&opt.args, transport->remote->name);
|
||||||
return 0;
|
strvec_push(&opt.args, transport->url);
|
||||||
|
|
||||||
strvec_push(&proc.args, hook_path);
|
strbuf_init(&data.buf, 0);
|
||||||
strvec_push(&proc.args, transport->remote->name);
|
data.refs = remote_refs;
|
||||||
strvec_push(&proc.args, transport->url);
|
|
||||||
|
|
||||||
proc.in = -1;
|
opt.feed_pipe = pre_push_hook_feed_stdin;
|
||||||
proc.trace2_hook_name = "pre-push";
|
opt.feed_pipe_cb_data = &data;
|
||||||
|
|
||||||
if (start_command(&proc)) {
|
ret = run_hooks_opt(the_repository, "pre-push", &opt);
|
||||||
finish_command(&proc);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
sigchain_push(SIGPIPE, SIG_IGN);
|
strbuf_release(&data.buf);
|
||||||
|
|
||||||
strbuf_init(&buf, 256);
|
|
||||||
|
|
||||||
for (r = remote_refs; r; r = r->next) {
|
|
||||||
if (!r->peer_ref) continue;
|
|
||||||
if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue;
|
|
||||||
if (r->status == REF_STATUS_REJECT_STALE) continue;
|
|
||||||
if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue;
|
|
||||||
if (r->status == REF_STATUS_UPTODATE) continue;
|
|
||||||
|
|
||||||
strbuf_reset(&buf);
|
|
||||||
strbuf_addf( &buf, "%s %s %s %s\n",
|
|
||||||
r->peer_ref->name, oid_to_hex(&r->new_oid),
|
|
||||||
r->name, oid_to_hex(&r->old_oid));
|
|
||||||
|
|
||||||
if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
|
|
||||||
/* We do not mind if a hook does not read all refs. */
|
|
||||||
if (errno != EPIPE)
|
|
||||||
ret = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
strbuf_release(&buf);
|
|
||||||
|
|
||||||
x = close(proc.in);
|
|
||||||
if (!ret)
|
|
||||||
ret = x;
|
|
||||||
|
|
||||||
sigchain_pop(SIGPIPE);
|
|
||||||
|
|
||||||
x = finish_command(&proc);
|
|
||||||
if (!ret)
|
|
||||||
ret = x;
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user