void trace_argv_printf(const char **argv, const char *fmt, ...) { struct strbuf buf; va_list ap; int fd, len, need_close = 0; fd = get_trace_fd(&need_close); if (!fd) return; set_try_to_free_routine(do_nothing); /* is never reset */ strbuf_init(&buf, 64); va_start(ap, fmt); len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); va_end(ap); if (len >= strbuf_avail(&buf)) { strbuf_grow(&buf, len - strbuf_avail(&buf) + 128); va_start(ap, fmt); len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); va_end(ap); if (len >= strbuf_avail(&buf)) die("broken vsnprintf"); } strbuf_setlen(&buf, len); sq_quote_argv(&buf, argv, 0); strbuf_addch(&buf, '\n'); write_or_whine_pipe(fd, buf.buf, buf.len, err_msg); strbuf_release(&buf); if (need_close) close(fd); }
static int cmd_sq_quote(int argc, const char **argv) { struct strbuf buf = STRBUF_INIT; if (argc) sq_quote_argv(&buf, argv, 0); printf("%s\n", buf.buf); strbuf_release(&buf); return 0; }
static int bisect_append_log_quoted(const char **argv) { int retval = 0; FILE *fp = fopen(git_path_bisect_log(), "a"); struct strbuf orig_args = STRBUF_INIT; if (!fp) return -1; if (fprintf(fp, "git bisect start") < 1) { retval = -1; goto finish; } sq_quote_argv(&orig_args, argv); if (fprintf(fp, "%s\n", orig_args.buf) < 1) retval = -1; finish: fclose(fp); strbuf_release(&orig_args); return retval; }
void trace_argv_printf(const char **argv, const char *fmt, ...) { struct strbuf buf = STRBUF_INIT; va_list ap; int fd, need_close = 0; fd = get_trace_fd(&need_close); if (!fd) return; set_try_to_free_routine(NULL); /* is never reset */ va_start(ap, fmt); strbuf_vaddf(&buf, fmt, ap); va_end(ap); sq_quote_argv(&buf, argv, 0); strbuf_addch(&buf, '\n'); write_or_whine_pipe(fd, buf.buf, buf.len, err_msg); strbuf_release(&buf); if (need_close) close(fd); }
static int cmd_parseopt(int argc, const char **argv, const char *prefix) { static int keep_dashdash = 0, stop_at_non_option = 0; static char const * const parseopt_usage[] = { N_("git rev-parse --parseopt [options] -- [<args>...]"), NULL }; static struct option parseopt_opts[] = { OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash, N_("keep the `--` passed as an arg")), OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option, N_("stop parsing after the " "first non-option argument")), OPT_END(), }; struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT; const char **usage = NULL; struct option *opts = NULL; int onb = 0, osz = 0, unb = 0, usz = 0; strbuf_addstr(&parsed, "set --"); argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, PARSE_OPT_KEEP_DASHDASH); if (argc < 1 || strcmp(argv[0], "--")) usage_with_options(parseopt_usage, parseopt_opts); /* get the usage up to the first line with a -- on it */ for (;;) { if (strbuf_getline(&sb, stdin, '\n') == EOF) die("premature end of input"); ALLOC_GROW(usage, unb + 1, usz); if (!strcmp("--", sb.buf)) { if (unb < 1) die("no usage string given before the `--' separator"); usage[unb] = NULL; break; } usage[unb++] = strbuf_detach(&sb, NULL); } /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */ while (strbuf_getline(&sb, stdin, '\n') != EOF) { const char *s; struct option *o; if (!sb.len) continue; ALLOC_GROW(opts, onb + 1, osz); memset(opts + onb, 0, sizeof(opts[onb])); o = &opts[onb++]; s = strchr(sb.buf, ' '); if (!s || *sb.buf == ' ') { o->type = OPTION_GROUP; o->help = xstrdup(skipspaces(sb.buf)); continue; } o->type = OPTION_CALLBACK; o->help = xstrdup(skipspaces(s)); o->value = &parsed; o->flags = PARSE_OPT_NOARG; o->callback = &parseopt_dump; while (s > sb.buf && strchr("*=?!", s[-1])) { switch (*--s) { case '=': o->flags &= ~PARSE_OPT_NOARG; break; case '?': o->flags &= ~PARSE_OPT_NOARG; o->flags |= PARSE_OPT_OPTARG; break; case '!': o->flags |= PARSE_OPT_NONEG; break; case '*': o->flags |= PARSE_OPT_HIDDEN; break; } } if (s - sb.buf == 1) /* short option only */ o->short_name = *sb.buf; else if (sb.buf[1] != ',') /* long option only */ o->long_name = xmemdupz(sb.buf, s - sb.buf); else { o->short_name = *sb.buf; o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2); } } strbuf_release(&sb); /* put an OPT_END() */ ALLOC_GROW(opts, onb + 1, osz); memset(opts + onb, 0, sizeof(opts[onb])); argc = parse_options(argc, argv, prefix, opts, usage, (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) | (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) | PARSE_OPT_SHELL_EVAL); strbuf_addf(&parsed, " --"); sq_quote_argv(&parsed, argv, 0); puts(parsed.buf); return 0; }
static int handle_alias(int *argcp, const char ***argv) { int envchanged = 0, ret = 0, saved_errno = errno; int count, option_count; const char **new_argv; const char *alias_command; char *alias_string; alias_command = (*argv)[0]; alias_string = alias_lookup(alias_command); if (alias_string) { if (alias_string[0] == '!') { if (*argcp > 1) { struct strbuf buf; strbuf_init(&buf, PATH_MAX); strbuf_addstr(&buf, alias_string); sq_quote_argv(&buf, (*argv) + 1, PATH_MAX); free(alias_string); alias_string = buf.buf; } ret = system(alias_string + 1); if (ret >= 0 && WIFEXITED(ret) && WEXITSTATUS(ret) != 127) exit(WEXITSTATUS(ret)); die("Failed to run '%s' when expanding alias '%s'", alias_string + 1, alias_command); } count = split_cmdline(alias_string, &new_argv); if (count < 0) die("Bad alias.%s string", alias_command); option_count = handle_options(&new_argv, &count, &envchanged); if (envchanged) die("alias '%s' changes environment variables\n" "You can use '!perf' in the alias to do this.", alias_command); memmove(new_argv - option_count, new_argv, count * sizeof(char *)); new_argv -= option_count; if (count < 1) die("empty alias for %s", alias_command); if (!strcmp(alias_command, new_argv[0])) die("recursive alias: %s", alias_command); new_argv = realloc(new_argv, sizeof(char *) * (count + *argcp + 1)); memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp); new_argv[count + *argcp] = NULL; *argv = new_argv; *argcp += count - 1; ret = 1; } errno = saved_errno; return ret; }
static int handle_alias(int *argcp, const char ***argv) { int envchanged = 0, ret = 0, saved_errno = errno; const char *subdir; int count, option_count; const char **new_argv; const char *alias_command; char *alias_string; int unused_nongit; subdir = setup_git_directory_gently(&unused_nongit); alias_command = (*argv)[0]; alias_string = alias_lookup(alias_command); if (alias_string) { if (alias_string[0] == '!') { commit_pager_choice(); if (*argcp > 1) { struct strbuf buf; strbuf_init(&buf, PATH_MAX); strbuf_addstr(&buf, alias_string); sq_quote_argv(&buf, (*argv) + 1, PATH_MAX); free(alias_string); alias_string = buf.buf; } trace_printf("trace: alias to shell cmd: %s => %s\n", alias_command, alias_string + 1); ret = system(alias_string + 1); if (ret >= 0 && WIFEXITED(ret) && WEXITSTATUS(ret) != 127) exit(WEXITSTATUS(ret)); die("Failed to run '%s' when expanding alias '%s'", alias_string + 1, alias_command); } count = split_cmdline(alias_string, &new_argv); if (count < 0) die("Bad alias.%s string: %s", alias_command, split_cmdline_strerror(count)); option_count = handle_options(&new_argv, &count, &envchanged); if (envchanged) die("alias '%s' changes environment variables\n" "You can use '!git' in the alias to do this.", alias_command); memmove(new_argv - option_count, new_argv, count * sizeof(char *)); new_argv -= option_count; if (count < 1) die("empty alias for %s", alias_command); if (!strcmp(alias_command, new_argv[0])) die("recursive alias: %s", alias_command); trace_argv_printf(new_argv, "trace: alias expansion: %s =>", alias_command); new_argv = xrealloc(new_argv, sizeof(char *) * (count + *argcp)); /* insert after command name */ memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp); *argv = new_argv; *argcp += count - 1; ret = 1; } if (subdir && chdir(subdir)) die_errno("Cannot change to '%s'", subdir); errno = saved_errno; return ret; }
static int bisect_start(struct bisect_terms *terms, int no_checkout, const char **argv, int argc) { int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0; int flags, pathspec_pos, retval = 0; struct string_list revs = STRING_LIST_INIT_DUP; struct string_list states = STRING_LIST_INIT_DUP; struct strbuf start_head = STRBUF_INIT; struct strbuf bisect_names = STRBUF_INIT; struct object_id head_oid; struct object_id oid; const char *head; if (is_bare_repository()) no_checkout = 1; /* * Check for one bad and then some good revisions */ for (i = 0; i < argc; i++) { if (!strcmp(argv[i], "--")) { has_double_dash = 1; break; } } for (i = 0; i < argc; i++) { const char *arg = argv[i]; if (!strcmp(argv[i], "--")) { break; } else if (!strcmp(arg, "--no-checkout")) { no_checkout = 1; } else if (!strcmp(arg, "--term-good") || !strcmp(arg, "--term-old")) { must_write_terms = 1; free((void *) terms->term_good); terms->term_good = xstrdup(argv[++i]); } else if (skip_prefix(arg, "--term-good=", &arg) || skip_prefix(arg, "--term-old=", &arg)) { must_write_terms = 1; free((void *) terms->term_good); terms->term_good = xstrdup(arg); } else if (!strcmp(arg, "--term-bad") || !strcmp(arg, "--term-new")) { must_write_terms = 1; free((void *) terms->term_bad); terms->term_bad = xstrdup(argv[++i]); } else if (skip_prefix(arg, "--term-bad=", &arg) || skip_prefix(arg, "--term-new=", &arg)) { must_write_terms = 1; free((void *) terms->term_bad); terms->term_bad = xstrdup(arg); } else if (starts_with(arg, "--") && !one_of(arg, "--term-good", "--term-bad", NULL)) { return error(_("unrecognized option: '%s'"), arg); } else { char *commit_id = xstrfmt("%s^{commit}", arg); if (get_oid(commit_id, &oid) && has_double_dash) die(_("'%s' does not appear to be a valid " "revision"), arg); string_list_append(&revs, oid_to_hex(&oid)); free(commit_id); } } pathspec_pos = i; /* * The user ran "git bisect start <sha1> <sha1>", hence did not * explicitly specify the terms, but we are already starting to * set references named with the default terms, and won't be able * to change afterwards. */ if (revs.nr) must_write_terms = 1; for (i = 0; i < revs.nr; i++) { if (bad_seen) { string_list_append(&states, terms->term_good); } else { bad_seen = 1; string_list_append(&states, terms->term_bad); } } /* * Verify HEAD */ head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags); if (!head) if (get_oid("HEAD", &head_oid)) return error(_("bad HEAD - I need a HEAD")); /* * Check if we are bisecting */ if (!is_empty_or_missing_file(git_path_bisect_start())) { /* Reset to the rev from where we started */ strbuf_read_file(&start_head, git_path_bisect_start(), 0); strbuf_trim(&start_head); if (!no_checkout) { struct argv_array argv = ARGV_ARRAY_INIT; argv_array_pushl(&argv, "checkout", start_head.buf, "--", NULL); if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) { retval = error(_("checking out '%s' failed." " Try 'git bisect start " "<valid-branch>'."), start_head.buf); goto finish; } } } else { /* Get the rev from where we start. */ if (!get_oid(head, &head_oid) && !starts_with(head, "refs/heads/")) { strbuf_reset(&start_head); strbuf_addstr(&start_head, oid_to_hex(&head_oid)); } else if (!get_oid(head, &head_oid) && skip_prefix(head, "refs/heads/", &head)) { /* * This error message should only be triggered by * cogito usage, and cogito users should understand * it relates to cg-seek. */ if (!is_empty_or_missing_file(git_path_head_name())) return error(_("won't bisect on cg-seek'ed tree")); strbuf_addstr(&start_head, head); } else { return error(_("bad HEAD - strange symbolic ref")); } } /* * Get rid of any old bisect state. */ if (bisect_clean_state()) return -1; /* * In case of mistaken revs or checkout error, or signals received, * "bisect_auto_next" below may exit or misbehave. * We have to trap this to be able to clean up using * "bisect_clean_state". */ /* * Write new start state */ write_file(git_path_bisect_start(), "%s\n", start_head.buf); if (no_checkout) { get_oid(start_head.buf, &oid); if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR)) { retval = -1; goto finish; } } if (pathspec_pos < argc - 1) sq_quote_argv(&bisect_names, argv + pathspec_pos); write_file(git_path_bisect_names(), "%s\n", bisect_names.buf); for (i = 0; i < states.nr; i++) if (bisect_write(states.items[i].string, revs.items[i].string, terms, 1)) { retval = -1; goto finish; } if (must_write_terms && write_terms(terms->term_bad, terms->term_good)) { retval = -1; goto finish; } retval = bisect_append_log_quoted(argv); if (retval) retval = -1; finish: string_list_clear(&revs, 0); string_list_clear(&states, 0); strbuf_release(&start_head); strbuf_release(&bisect_names); return retval; }