static int try_difference(const char *arg) { char *dotdot; unsigned char sha1[20]; unsigned char end[20]; const char *next; const char *this; int symmetric; static const char head_by_default[] = "HEAD"; if (!(dotdot = strstr(arg, ".."))) return 0; next = dotdot + 2; this = arg; symmetric = (*next == '.'); *dotdot = 0; next += symmetric; if (!*next) next = head_by_default; if (dotdot == arg) this = head_by_default; if (this == head_by_default && next == head_by_default && !symmetric) { /* * Just ".."? That is not a range but the * pathspec for the parent directory. */ *dotdot = '.'; return 0; } if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) { show_rev(NORMAL, end, next); show_rev(symmetric ? NORMAL : REVERSED, sha1, this); if (symmetric) { struct commit_list *exclude; struct commit *a, *b; a = lookup_commit_reference(sha1); b = lookup_commit_reference(end); exclude = get_merge_bases(a, b, 1); while (exclude) { struct commit_list *n = exclude->next; show_rev(REVERSED, exclude->item->object.sha1,NULL); free(exclude); exclude = n; } } *dotdot = '.'; return 1; } *dotdot = '.'; return 0; }
static int try_parent_shorthands(const char *arg) { char *dotdot; unsigned char sha1[20]; struct commit *commit; struct commit_list *parents; int parents_only; if ((dotdot = strstr(arg, "^!"))) parents_only = 0; else if ((dotdot = strstr(arg, "^@"))) parents_only = 1; if (!dotdot || dotdot[2]) return 0; *dotdot = 0; if (get_sha1_committish(arg, sha1)) return 0; if (!parents_only) show_rev(NORMAL, sha1, arg); commit = lookup_commit_reference(sha1); for (parents = commit->parents; parents; parents = parents->next) show_rev(parents_only ? NORMAL : REVERSED, parents->item->object.sha1, arg); return 1; }
static int try_parent_shorthands(const char *arg) { char *dotdot; unsigned char sha1[20]; struct commit *commit; struct commit_list *parents; int parent_number; int include_rev = 0; int include_parents = 0; int exclude_parent = 0; if ((dotdot = strstr(arg, "^!"))) { include_rev = 1; if (dotdot[2]) return 0; } else if ((dotdot = strstr(arg, "^@"))) { include_parents = 1; if (dotdot[2]) return 0; } else if ((dotdot = strstr(arg, "^-"))) { include_rev = 1; exclude_parent = 1; if (dotdot[2]) { char *end; exclude_parent = strtoul(dotdot + 2, &end, 10); if (*end != '\0' || !exclude_parent) return 0; } } else return 0; *dotdot = 0; if (get_sha1_committish(arg, sha1)) { *dotdot = '^'; return 0; } commit = lookup_commit_reference(sha1); if (exclude_parent && exclude_parent > commit_list_count(commit->parents)) { *dotdot = '^'; return 0; } if (include_rev) show_rev(NORMAL, sha1, arg); for (parents = commit->parents, parent_number = 1; parents; parents = parents->next, parent_number++) { if (exclude_parent && parent_number != exclude_parent) continue; show_rev(include_parents ? NORMAL : REVERSED, parents->item->object.oid.hash, arg); } *dotdot = '^'; return 1; }
static void parse_args(struct pathspec *pathspec, const char **argv, const char *prefix, int patch_mode, const char **rev_ret) { const char *rev = "HEAD"; unsigned char unused[20]; /* * Possible arguments are: * * git reset [-opts] [<rev>] * git reset [-opts] <tree> [<paths>...] * git reset [-opts] <tree> -- [<paths>...] * git reset [-opts] -- [<paths>...] * git reset [-opts] <paths>... * * At this point, argv points immediately after [-opts]. */ if (argv[0]) { if (!strcmp(argv[0], "--")) { argv++; /* reset to HEAD, possibly with paths */ } else if (argv[1] && !strcmp(argv[1], "--")) { rev = argv[0]; argv += 2; } /* * Otherwise, argv[0] could be either <rev> or <paths> and * has to be unambiguous. If there is a single argument, it * can not be a tree */ else if ((!argv[1] && !get_sha1_committish(argv[0], unused)) || (argv[1] && !get_sha1_treeish(argv[0], unused))) { /* * Ok, argv[0] looks like a commit/tree; it should not * be a filename. */ verify_non_filename(prefix, argv[0]); rev = *argv++; } else { /* Otherwise we treat this as a filename */ verify_filename(prefix, argv[0], 1); } } *rev_ret = rev; if (read_cache() < 0) die(_("index file corrupt")); parse_pathspec(pathspec, 0, PATHSPEC_PREFER_FULL | PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP | (patch_mode ? PATHSPEC_PREFIX_ORIGIN : 0), prefix, argv); }
int cmd_reset(int argc, const char **argv, const char *prefix) { int reset_type = NONE, update_ref_status = 0, quiet = 0; int patch_mode = 0, unborn; const char *rev; unsigned char sha1[20]; const char **pathspec = NULL; const struct option options[] = { OPT__QUIET(&quiet, N_("be quiet, only report errors")), OPT_SET_INT(0, "mixed", &reset_type, N_("reset HEAD and index"), MIXED), OPT_SET_INT(0, "soft", &reset_type, N_("reset only HEAD"), SOFT), OPT_SET_INT(0, "hard", &reset_type, N_("reset HEAD, index and working tree"), HARD), OPT_SET_INT(0, "merge", &reset_type, N_("reset HEAD, index and working tree"), MERGE), OPT_SET_INT(0, "keep", &reset_type, N_("reset HEAD but keep local changes"), KEEP), OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")), OPT_END() }; git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, options, git_reset_usage, PARSE_OPT_KEEP_DASHDASH); pathspec = parse_args(argv, prefix, &rev); unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", sha1); if (unborn) { /* reset on unborn branch: treat as reset to empty tree */ hashcpy(sha1, EMPTY_TREE_SHA1_BIN); } else if (!pathspec) { struct commit *commit; if (get_sha1_committish(rev, sha1)) die(_("Failed to resolve '%s' as a valid revision."), rev); commit = lookup_commit_reference(sha1); if (!commit) die(_("Could not parse object '%s'."), rev); hashcpy(sha1, commit->object.sha1); } else { struct tree *tree; if (get_sha1_treeish(rev, sha1)) die(_("Failed to resolve '%s' as a valid tree."), rev); tree = parse_tree_indirect(sha1); if (!tree) die(_("Could not parse object '%s'."), rev); hashcpy(sha1, tree->object.sha1); } if (patch_mode) { if (reset_type != NONE) die(_("--patch is incompatible with --{hard,mixed,soft}")); return run_add_interactive(sha1_to_hex(sha1), "--patch=reset", pathspec); } /* git reset tree [--] paths... can be used to * load chosen paths from the tree into the index without * affecting the working tree nor HEAD. */ if (pathspec) { if (reset_type == MIXED) warning(_("--mixed with paths is deprecated; use 'git reset -- <paths>' instead.")); else if (reset_type != NONE) die(_("Cannot do %s reset with paths."), _(reset_type_names[reset_type])); } if (reset_type == NONE) reset_type = MIXED; /* by default */ if (reset_type != SOFT && reset_type != MIXED) setup_work_tree(); if (reset_type == MIXED && is_bare_repository()) die(_("%s reset is not allowed in a bare repository"), _(reset_type_names[reset_type])); /* Soft reset does not touch the index file nor the working tree * at all, but requires them in a good order. Other resets reset * the index file to the tree object we are switching to. */ if (reset_type == SOFT || reset_type == KEEP) die_if_unmerged_cache(reset_type); if (reset_type != SOFT) { struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); int newfd = hold_locked_index(lock, 1); if (reset_type == MIXED) { if (read_from_tree(pathspec, sha1)) return 1; } else { int err = reset_index(sha1, reset_type, quiet); if (reset_type == KEEP && !err) err = reset_index(sha1, MIXED, quiet); if (err) die(_("Could not reset index file to revision '%s'."), rev); } if (reset_type == MIXED) { /* Report what has not been updated. */ int flags = quiet ? REFRESH_QUIET : REFRESH_IN_PORCELAIN; refresh_index(&the_index, flags, NULL, NULL, _("Unstaged changes after reset:")); } if (write_cache(newfd, active_cache, active_nr) || commit_locked_index(lock)) die(_("Could not write new index file.")); } if (!pathspec && !unborn) { /* Any resets without paths update HEAD to the head being * switched to, saving the previous head in ORIG_HEAD before. */ update_ref_status = update_refs(rev, sha1); if (reset_type == HARD && !update_ref_status && !quiet) print_new_head_line(lookup_commit_reference(sha1)); } if (!pathspec) remove_branch_state(); return update_ref_status; }