int main(int argc, char *argv[]) { int ret = EXIT_SUCCESS; struct credential_operation const *try_op = credential_helper_ops; struct credential cred = CREDENTIAL_INIT; if (!argv[1]) { usage(argv[0]); exit(EXIT_FAILURE); } g_set_application_name("Git Credential Helper"); /* lookup operation callback */ while (try_op->name && strcmp(argv[1], try_op->name)) try_op++; /* unsupported operation given -- ignore silently */ if (!try_op->name || !try_op->op) goto out; ret = credential_read(&cred); if (ret) goto out; /* perform credential operation */ ret = (*try_op->op)(&cred); credential_write(&cred); out: credential_clear(&cred); return ret; }
static int run_credential_helper(struct credential *c, const char *cmd, int want_output) { struct child_process helper = CHILD_PROCESS_INIT; const char *argv[] = { NULL, NULL }; FILE *fp; argv[0] = cmd; helper.argv = argv; helper.use_shell = 1; helper.in = -1; if (want_output) helper.out = -1; else helper.no_stdout = 1; if (start_command(&helper) < 0) return -1; fp = xfdopen(helper.in, "w"); sigchain_push(SIGPIPE, SIG_IGN); credential_write(c, fp); fclose(fp); sigchain_pop(SIGPIPE); if (want_output) { int r; fp = xfdopen(helper.out, "r"); r = credential_read(c, fp); fclose(fp); if (r < 0) { finish_command(&helper); return -1; } } if (finish_command(&helper)) return -1; return 0; }
static int run_credential_helper(struct credential *c, const char *cmd, int want_output) { struct child_process helper; const char *argv[] = { NULL, NULL }; FILE *fp; memset(&helper, 0, sizeof(helper)); argv[0] = cmd; helper.argv = argv; helper.use_shell = 1; helper.in = -1; if (want_output) helper.out = -1; else helper.no_stdout = 1; if (start_command(&helper) < 0) return -1; fp = xfdopen(helper.in, "w"); credential_write(c, fp); fclose(fp); if (want_output) { int r; fp = xfdopen(helper.out, "r"); r = credential_read(c, fp); fclose(fp); if (r < 0) { finish_command(&helper); return -1; } } if (finish_command(&helper)) return -1; return 0; }
int cmd_credential(int argc, const char **argv, const char *prefix) { const char *op; struct credential c = CREDENTIAL_INIT; op = argv[1]; if (!op) usage(usage_msg); if (credential_read(&c, stdin) < 0) die("unable to read credential from stdin"); if (!strcmp(op, "fill")) { credential_fill(&c); credential_write(&c, stdout); } else if (!strcmp(op, "approve")) { credential_approve(&c); } else if (!strcmp(op, "reject")) { credential_reject(&c); } else { usage(usage_msg); } return 0; }