static void prepare_cmd(struct argv_array *out, const struct child_process *cmd) { if (!cmd->argv[0]) die("BUG: command is empty"); /* * Add SHELL_PATH so in the event exec fails with ENOEXEC we can * attempt to interpret the command with 'sh'. */ argv_array_push(out, SHELL_PATH); if (cmd->git_cmd) { argv_array_push(out, "git"); argv_array_pushv(out, cmd->argv); } else if (cmd->use_shell) { prepare_shell_cmd(out, cmd->argv); } else { argv_array_pushv(out, cmd->argv); } /* * If there are no '/' characters in the command then perform a path * lookup and use the resolved path as the command to exec. If there * are no '/' characters or if the command wasn't found in the path, * have exec attempt to invoke the command directly. */ if (!strchr(out->argv[1], '/')) { char *program = locate_in_PATH(out->argv[1]); if (program) { free((char *)out->argv[1]); out->argv[1] = program; } } }
/** * Given the current HEAD SHA1, the merge head returned from git-fetch and the * fork point calculated by get_rebase_fork_point(), runs git-rebase with the * appropriate arguments and returns its exit status. */ static int run_rebase(const struct object_id *curr_head, const struct object_id *merge_head, const struct object_id *fork_point) { int ret; struct object_id oct_merge_base; struct argv_array args = ARGV_ARRAY_INIT; if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point)) if (!is_null_oid(fork_point) && oideq(&oct_merge_base, fork_point)) fork_point = NULL; argv_array_push(&args, "rebase"); /* Shared options */ argv_push_verbosity(&args); /* Options passed to git-rebase */ if (opt_rebase == REBASE_MERGES) argv_array_push(&args, "--rebase-merges"); else if (opt_rebase == REBASE_PRESERVE) argv_array_push(&args, "--preserve-merges"); else if (opt_rebase == REBASE_INTERACTIVE) argv_array_push(&args, "--interactive"); if (opt_diffstat) argv_array_push(&args, opt_diffstat); argv_array_pushv(&args, opt_strategies.argv); argv_array_pushv(&args, opt_strategy_opts.argv); if (opt_gpg_sign) argv_array_push(&args, opt_gpg_sign); if (opt_autostash == 0) argv_array_push(&args, "--no-autostash"); else if (opt_autostash == 1) argv_array_push(&args, "--autostash"); if (opt_verify_signatures && !strcmp(opt_verify_signatures, "--verify-signatures")) warning(_("ignoring --verify-signatures for rebase")); argv_array_push(&args, "--onto"); argv_array_push(&args, oid_to_hex(merge_head)); if (fork_point && !is_null_oid(fork_point)) argv_array_push(&args, oid_to_hex(fork_point)); else argv_array_push(&args, oid_to_hex(merge_head)); ret = run_command_v_opt(args.argv, RUN_GIT_CMD); argv_array_clear(&args); return ret; }
static void execv_dashed_external(const char **argv) { struct child_process cmd = CHILD_PROCESS_INIT; int status; if (get_super_prefix()) die("%s doesn't support --super-prefix", argv[0]); if (use_pager == -1 && !is_builtin(argv[0])) use_pager = check_pager_config(argv[0]); commit_pager_choice(); argv_array_pushf(&cmd.args, "git-%s", argv[0]); argv_array_pushv(&cmd.args, argv + 1); cmd.clean_on_exit = 1; cmd.wait_after_clean = 1; cmd.silent_exec_failure = 1; trace_argv_printf(cmd.args.argv, "trace: exec:"); /* * If we fail because the command is not found, it is * OK to return. Otherwise, we just pass along the status code, * or our usual generic code if we were not even able to exec * the program. */ status = run_command(&cmd); if (status >= 0) exit(status); else if (errno != ENOENT) exit(128); }
/** * Given the current HEAD SHA1, the merge head returned from git-fetch and the * fork point calculated by get_rebase_fork_point(), runs git-rebase with the * appropriate arguments and returns its exit status. */ static int run_rebase(const unsigned char *curr_head, const unsigned char *merge_head, const unsigned char *fork_point) { int ret; unsigned char oct_merge_base[GIT_SHA1_RAWSZ]; struct argv_array args = ARGV_ARRAY_INIT; if (!get_octopus_merge_base(oct_merge_base, curr_head, merge_head, fork_point)) if (!is_null_sha1(fork_point) && !hashcmp(oct_merge_base, fork_point)) fork_point = NULL; argv_array_push(&args, "rebase"); /* Shared options */ argv_push_verbosity(&args); /* Options passed to git-rebase */ if (opt_rebase == REBASE_PRESERVE) argv_array_push(&args, "--preserve-merges"); else if (opt_rebase == REBASE_INTERACTIVE) argv_array_push(&args, "--interactive"); if (opt_diffstat) argv_array_push(&args, opt_diffstat); argv_array_pushv(&args, opt_strategies.argv); argv_array_pushv(&args, opt_strategy_opts.argv); if (opt_gpg_sign) argv_array_push(&args, opt_gpg_sign); if (opt_autostash == 0) argv_array_push(&args, "--no-autostash"); else if (opt_autostash == 1) argv_array_push(&args, "--autostash"); argv_array_push(&args, "--onto"); argv_array_push(&args, sha1_to_hex(merge_head)); if (fork_point && !is_null_sha1(fork_point)) argv_array_push(&args, sha1_to_hex(fork_point)); else argv_array_push(&args, sha1_to_hex(merge_head)); ret = run_command_v_opt(args.argv, RUN_GIT_CMD); argv_array_clear(&args); return ret; }
/** * Runs git-merge, returning its exit status. */ static int run_merge(void) { int ret; struct argv_array args = ARGV_ARRAY_INIT; argv_array_pushl(&args, "merge", NULL); /* Shared options */ argv_push_verbosity(&args); if (opt_progress) argv_array_push(&args, opt_progress); /* Options passed to git-merge */ if (opt_diffstat) argv_array_push(&args, opt_diffstat); if (opt_log) argv_array_push(&args, opt_log); if (opt_signoff) argv_array_push(&args, opt_signoff); if (opt_squash) argv_array_push(&args, opt_squash); if (opt_commit) argv_array_push(&args, opt_commit); if (opt_edit) argv_array_push(&args, opt_edit); if (opt_ff) argv_array_push(&args, opt_ff); if (opt_verify_signatures) argv_array_push(&args, opt_verify_signatures); argv_array_pushv(&args, opt_strategies.argv); argv_array_pushv(&args, opt_strategy_opts.argv); if (opt_gpg_sign) argv_array_push(&args, opt_gpg_sign); if (opt_allow_unrelated_histories > 0) argv_array_push(&args, "--allow-unrelated-histories"); argv_array_push(&args, "FETCH_HEAD"); ret = run_command_v_opt(args.argv, RUN_GIT_CMD); argv_array_clear(&args); return ret; }
/** * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the * repository and refspecs to fetch, or NULL if they are not provided. */ static int run_fetch(const char *repo, const char **refspecs) { struct argv_array args = ARGV_ARRAY_INIT; int ret; argv_array_pushl(&args, "fetch", "--update-head-ok", NULL); /* Shared options */ argv_push_verbosity(&args); if (opt_progress) argv_array_push(&args, opt_progress); /* Options passed to git-fetch */ if (opt_all) argv_array_push(&args, opt_all); if (opt_append) argv_array_push(&args, opt_append); if (opt_upload_pack) argv_array_push(&args, opt_upload_pack); argv_push_force(&args); if (opt_tags) argv_array_push(&args, opt_tags); if (opt_prune) argv_array_push(&args, opt_prune); if (opt_recurse_submodules) argv_array_push(&args, opt_recurse_submodules); if (max_children) argv_array_push(&args, max_children); if (opt_dry_run) argv_array_push(&args, "--dry-run"); if (opt_keep) argv_array_push(&args, opt_keep); if (opt_depth) argv_array_push(&args, opt_depth); if (opt_unshallow) argv_array_push(&args, opt_unshallow); if (opt_update_shallow) argv_array_push(&args, opt_update_shallow); if (opt_refmap) argv_array_push(&args, opt_refmap); if (repo) { argv_array_push(&args, repo); argv_array_pushv(&args, refspecs); } else if (*refspecs) die("BUG: refspecs without repo?"); ret = run_command_v_opt(args.argv, RUN_GIT_CMD); argv_array_clear(&args); return ret; }
static int list_stash(int argc, const char **argv, const char *prefix) { struct child_process cp = CHILD_PROCESS_INIT; struct option options[] = { OPT_END() }; argc = parse_options(argc, argv, prefix, options, git_stash_list_usage, PARSE_OPT_KEEP_UNKNOWN); if (!ref_exists(ref_stash)) return 0; cp.git_cmd = 1; argv_array_pushl(&cp.args, "log", "--format=%gd: %gs", "-g", "--first-parent", "-m", NULL); argv_array_pushv(&cp.args, argv); argv_array_push(&cp.args, ref_stash); argv_array_push(&cp.args, "--"); return run_command(&cp); }
static int run_pre_command_hook(const char **argv) { char *lock; int ret = 0; /* * Ensure the global pre/post command hook is only called for * the outer command and not when git is called recursively * or spawns multiple commands (like with the alias command) */ lock = getenv("COMMAND_HOOK_LOCK"); if (lock && !strcmp(lock, "true")) return 0; setenv("COMMAND_HOOK_LOCK", "true", 1); /* call the hook proc */ argv_array_pushv(&sargv, argv); argv_array_pushf(&sargv, "--git-pid=%"PRIuMAX, (uintmax_t)getpid()); ret = run_hook_argv(NULL, "pre-command", sargv.argv); if (!ret) run_post_hook = 1; return ret; }
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] == '!') { struct child_process child = CHILD_PROCESS_INIT; int nongit_ok; /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */ setup_git_directory_gently(&nongit_ok); commit_pager_choice(); child.use_shell = 1; argv_array_push(&child.args, alias_string + 1); argv_array_pushv(&child.args, (*argv) + 1); ret = run_command(&child); if (ret >= 0) /* normal exit */ exit(ret); die_errno("while expanding alias '%s': '%s'", alias_command, alias_string + 1); } 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); REALLOC_ARRAY(new_argv, count + *argcp); /* insert after command name */ memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp); *argv = new_argv; *argcp += count - 1; ret = 1; } errno = saved_errno; return ret; }
/** * Runs git-fetch, returning its exit status. `repo` and `refspecs` are the * repository and refspecs to fetch, or NULL if they are not provided. */ static int run_fetch(const char *repo, const char **refspecs) { struct argv_array args = ARGV_ARRAY_INIT; int ret; argv_array_pushl(&args, "fetch", "--update-head-ok", NULL); /* Shared options */ argv_push_verbosity(&args); if (opt_progress) argv_array_push(&args, opt_progress); /* Options passed to git-fetch */ if (opt_all) argv_array_push(&args, opt_all); if (opt_append) argv_array_push(&args, opt_append); if (opt_upload_pack) argv_array_push(&args, opt_upload_pack); argv_push_force(&args); if (opt_tags) argv_array_push(&args, opt_tags); if (opt_prune) argv_array_push(&args, opt_prune); if (recurse_submodules != RECURSE_SUBMODULES_DEFAULT) switch (recurse_submodules) { case RECURSE_SUBMODULES_ON: argv_array_push(&args, "--recurse-submodules=on"); break; case RECURSE_SUBMODULES_OFF: argv_array_push(&args, "--recurse-submodules=no"); break; case RECURSE_SUBMODULES_ON_DEMAND: argv_array_push(&args, "--recurse-submodules=on-demand"); break; default: BUG("submodule recursion option not understood"); } if (max_children) argv_array_push(&args, max_children); if (opt_dry_run) argv_array_push(&args, "--dry-run"); if (opt_keep) argv_array_push(&args, opt_keep); if (opt_depth) argv_array_push(&args, opt_depth); if (opt_unshallow) argv_array_push(&args, opt_unshallow); if (opt_update_shallow) argv_array_push(&args, opt_update_shallow); if (opt_refmap) argv_array_push(&args, opt_refmap); if (opt_ipv4) argv_array_push(&args, opt_ipv4); if (opt_ipv6) argv_array_push(&args, opt_ipv6); if (repo) { argv_array_push(&args, repo); argv_array_pushv(&args, refspecs); } else if (*refspecs) BUG("refspecs without repo?"); ret = run_command_v_opt(args.argv, RUN_GIT_CMD); argv_array_clear(&args); return ret; }
int cmd_describe(int argc, const char **argv, const char *prefix) { int contains = 0; struct option options[] = { OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")), OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")), OPT_BOOL(0, "all", &all, N_("use any ref")), OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")), OPT_BOOL(0, "long", &longformat, N_("always use long format")), OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")), OPT__ABBREV(&abbrev), OPT_SET_INT(0, "exact-match", &max_candidates, N_("only output exact matches"), 0), OPT_INTEGER(0, "candidates", &max_candidates, N_("consider <n> most recent tags (default: 10)")), OPT_STRING(0, "match", &pattern, N_("pattern"), N_("only consider tags matching <pattern>")), OPT_BOOL(0, "always", &always, N_("show abbreviated commit object as fallback")), {OPTION_STRING, 0, "dirty", &dirty, N_("mark"), N_("append <mark> on dirty working tree (default: \"-dirty\")"), PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"}, OPT_END(), }; git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, describe_usage, 0); if (abbrev < 0) abbrev = DEFAULT_ABBREV; if (max_candidates < 0) max_candidates = 0; else if (max_candidates > MAX_TAGS) max_candidates = MAX_TAGS; save_commit_buffer = 0; if (longformat && abbrev == 0) die(_("--long is incompatible with --abbrev=0")); if (contains) { struct argv_array args; argv_array_init(&args); argv_array_pushl(&args, "name-rev", "--peel-tag", "--name-only", "--no-undefined", NULL); if (always) argv_array_push(&args, "--always"); if (!all) { argv_array_push(&args, "--tags"); if (pattern) argv_array_pushf(&args, "--refs=refs/tags/%s", pattern); } if (argc) argv_array_pushv(&args, argv); else argv_array_push(&args, "HEAD"); return cmd_name_rev(args.argc, args.argv, prefix); } hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0); for_each_rawref(get_name, NULL); if (!names.size && !always) die(_("No names found, cannot describe anything.")); if (argc == 0) { if (dirty) { static struct lock_file index_lock; int fd; read_cache_preload(NULL); refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); fd = hold_locked_index(&index_lock, 0); if (0 <= fd) update_index_if_able(&the_index, &index_lock); if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, diff_index_args, prefix)) dirty = NULL; } describe("HEAD", 1); } else if (dirty) { die(_("--dirty is incompatible with commit-ishes")); } else { while (argc-- > 0) describe(*argv++, argc == 0); } return 0; }
int cmd_stash(int argc, const char **argv, const char *prefix) { int i = -1; pid_t pid = getpid(); const char *index_file; struct argv_array args = ARGV_ARRAY_INIT; struct option options[] = { OPT_END() }; if (!use_builtin_stash()) { const char *path = mkpath("%s/git-legacy-stash", git_exec_path()); if (sane_execvp(path, (char **)argv) < 0) die_errno(_("could not exec %s"), path); else BUG("sane_execvp() returned???"); } prefix = setup_git_directory(); trace_repo_setup(prefix); setup_work_tree(); git_config(git_diff_basic_config, NULL); argc = parse_options(argc, argv, prefix, options, git_stash_usage, PARSE_OPT_KEEP_UNKNOWN | PARSE_OPT_KEEP_DASHDASH); index_file = get_index_file(); strbuf_addf(&stash_index_path, "%s.stash.%" PRIuMAX, index_file, (uintmax_t)pid); if (!argc) return !!push_stash(0, NULL, prefix); else if (!strcmp(argv[0], "apply")) return !!apply_stash(argc, argv, prefix); else if (!strcmp(argv[0], "clear")) return !!clear_stash(argc, argv, prefix); else if (!strcmp(argv[0], "drop")) return !!drop_stash(argc, argv, prefix); else if (!strcmp(argv[0], "pop")) return !!pop_stash(argc, argv, prefix); else if (!strcmp(argv[0], "branch")) return !!branch_stash(argc, argv, prefix); else if (!strcmp(argv[0], "list")) return !!list_stash(argc, argv, prefix); else if (!strcmp(argv[0], "show")) return !!show_stash(argc, argv, prefix); else if (!strcmp(argv[0], "store")) return !!store_stash(argc, argv, prefix); else if (!strcmp(argv[0], "create")) return !!create_stash(argc, argv, prefix); else if (!strcmp(argv[0], "push")) return !!push_stash(argc, argv, prefix); else if (!strcmp(argv[0], "save")) return !!save_stash(argc, argv, prefix); else if (*argv[0] != '-') usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]), git_stash_usage, options); if (strcmp(argv[0], "-p")) { while (++i < argc && strcmp(argv[i], "--")) { /* * `akpqu` is a string which contains all short options, * except `-m` which is verified separately. */ if ((strlen(argv[i]) == 2) && *argv[i] == '-' && strchr("akpqu", argv[i][1])) continue; if (!strcmp(argv[i], "--all") || !strcmp(argv[i], "--keep-index") || !strcmp(argv[i], "--no-keep-index") || !strcmp(argv[i], "--patch") || !strcmp(argv[i], "--quiet") || !strcmp(argv[i], "--include-untracked")) continue; /* * `-m` and `--message=` are verified separately because * they need to be immediately followed by a string * (i.e.`-m"foobar"` or `--message="foobar"`). */ if (starts_with(argv[i], "-m") || starts_with(argv[i], "--message=")) continue; usage_with_options(git_stash_usage, options); } } argv_array_push(&args, "push"); argv_array_pushv(&args, argv); return !!push_stash(args.argc, args.argv, prefix); }
static int get_next_submodule(struct child_process *cp, struct strbuf *err, void *data, void **task_cb) { int ret = 0; struct submodule_parallel_fetch *spf = data; for (; spf->count < active_nr; spf->count++) { struct strbuf submodule_path = STRBUF_INIT; struct strbuf submodule_git_dir = STRBUF_INIT; struct strbuf submodule_prefix = STRBUF_INIT; const struct cache_entry *ce = active_cache[spf->count]; const char *git_dir, *default_argv; const struct submodule *submodule; if (!S_ISGITLINK(ce->ce_mode)) continue; submodule = submodule_from_path(null_sha1, ce->name); if (!submodule) submodule = submodule_from_name(null_sha1, ce->name); default_argv = "yes"; if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) { if (submodule && submodule->fetch_recurse != RECURSE_SUBMODULES_NONE) { if (submodule->fetch_recurse == RECURSE_SUBMODULES_OFF) continue; if (submodule->fetch_recurse == RECURSE_SUBMODULES_ON_DEMAND) { if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) continue; default_argv = "on-demand"; } } else { if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) || gitmodules_is_unmerged) continue; if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) { if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) continue; default_argv = "on-demand"; } } } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) { if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) continue; default_argv = "on-demand"; } strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name); strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf); strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name); git_dir = read_gitfile(submodule_git_dir.buf); if (!git_dir) git_dir = submodule_git_dir.buf; if (is_directory(git_dir)) { child_process_init(cp); cp->dir = strbuf_detach(&submodule_path, NULL); cp->env = local_repo_env; cp->git_cmd = 1; if (!spf->quiet) strbuf_addf(err, "Fetching submodule %s%s\n", spf->prefix, ce->name); argv_array_init(&cp->args); argv_array_pushv(&cp->args, spf->args.argv); argv_array_push(&cp->args, default_argv); argv_array_push(&cp->args, "--submodule-prefix"); argv_array_push(&cp->args, submodule_prefix.buf); ret = 1; } strbuf_release(&submodule_path); strbuf_release(&submodule_git_dir); strbuf_release(&submodule_prefix); if (ret) { spf->count++; return 1; } } return 0; }