X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=builtin-revert.c;h=64f0d0ee028fd143ea065cad93ce7be03d94839d;hb=d3e54c88291c2d880ff3a72167c5d3159aed8e97;hp=4bf8eb2f58ac5a81a9d6538e31f2da661db4c3e9;hpb=31cbb5d96151891491168d3f813e6898c9ef825b;p=git.git diff --git a/builtin-revert.c b/builtin-revert.c index 4bf8eb2f5..64f0d0ee0 100644 --- a/builtin-revert.c +++ b/builtin-revert.c @@ -8,6 +8,8 @@ #include "exec_cmd.h" #include "utf8.h" #include "parse-options.h" +#include "diff.h" +#include "revision.h" /* * This implements the builtins revert and cherry-pick. @@ -245,6 +247,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]; @@ -273,12 +286,11 @@ static int revert_or_cherry_pick(int argc, const char **argv) if (write_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(); } @@ -371,13 +383,13 @@ static int revert_or_cherry_pick(int argc, const char **argv) i++; } } - if (close(msg_fd) || commit_lock_file(&msg_file) < 0) + if (commit_lock_file(&msg_file) < 0) die ("Error wrapping up %s", defmsg); fprintf(stderr, "Automatic %s failed.%s\n", me, help_msg(commit->object.sha1)); exit(1); } - if (close(msg_fd) || commit_lock_file(&msg_file) < 0) + if (commit_lock_file(&msg_file) < 0) die ("Error wrapping up %s", defmsg); fprintf(stderr, "Finished one %s.\n", me);