X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=builtin-revert.c;h=2b57525d7234086bdf2022d13fa405e5007627fb;hb=4b172de81b878cdc8b10803fd0cd79b3ca66a621;hp=e219859f9b04dc94a2444346541e86503085fdea;hpb=2db511fdbdbe1c8aab80f4bc13f0df037bce8a33;p=git.git diff --git a/builtin-revert.c b/builtin-revert.c index e219859f9..2b57525d7 100644 --- a/builtin-revert.c +++ b/builtin-revert.c @@ -9,6 +9,8 @@ #include "utf8.h" #include "parse-options.h" #include "cache-tree.h" +#include "diff.h" +#include "revision.h" /* * This implements the builtins revert and cherry-pick. @@ -31,7 +33,7 @@ static const char * const cherry_pick_usage[] = { NULL }; -static int edit, no_replay, no_commit, mainline; +static int edit, no_replay, no_commit, mainline, signoff; static enum { REVERT, CHERRY_PICK } action; static struct commit *commit; @@ -51,6 +53,7 @@ static void parse_args(int argc, const char **argv) OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"), OPT_BOOLEAN('x', NULL, &no_replay, "append commit name when cherry-picking"), OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"), + OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"), OPT_INTEGER('m', "mainline", &mainline, "parent number"), OPT_END(), }; @@ -246,6 +249,17 @@ static char *help_msg(const unsigned char *sha1) return helpbuf; } +static int index_is_dirty(void) +{ + struct rev_info rev; + init_revisions(&rev, NULL); + setup_revisions(0, NULL, &rev, "HEAD"); + DIFF_OPT_SET(&rev.diffopt, QUIET); + DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS); + run_diff_index(&rev, 1); + return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES); +} + static int revert_or_cherry_pick(int argc, const char **argv) { unsigned char head[20]; @@ -274,12 +288,11 @@ static int revert_or_cherry_pick(int argc, const char **argv) if (write_cache_as_tree(head, 0, NULL)) die ("Your index file is unmerged."); } else { - struct wt_status s; - if (get_sha1("HEAD", head)) die ("You do not have a valid HEAD"); - wt_status_prepare(&s); - if (s.commitable) + if (read_cache() < 0) + die("could not read the index"); + if (index_is_dirty()) die ("Dirty index: cannot %s", me); discard_cache(); } @@ -392,13 +405,21 @@ static int revert_or_cherry_pick(int argc, const char **argv) */ if (!no_commit) { - if (edit) - return execl_git_cmd("commit", "-n", NULL); - else - return execl_git_cmd("commit", "-n", "-F", defmsg, NULL); + /* 6 is max possible length of our args array including NULL */ + const char *args[6]; + int i = 0; + args[i++] = "commit"; + args[i++] = "-n"; + if (signoff) + args[i++] = "-s"; + if (!edit) { + args[i++] = "-F"; + args[i++] = defmsg; + } + args[i] = NULL; + return execv_git_cmd(args); } - if (reencoded_message) - free(reencoded_message); + free(reencoded_message); return 0; }