Code

setenv(GIT_DIR) clean-up
[git.git] / builtin-revert.c
index 3f2614e1bbef009afadefff7d284225533db2a09..8ac86f09434d1dad830c924de8cadcef6705e1e5 100644 (file)
@@ -38,6 +38,7 @@ static const char * const cherry_pick_usage[] = {
 static int edit, no_replay, no_commit, mainline, signoff;
 static enum { REVERT, CHERRY_PICK } action;
 static struct commit *commit;
+static int allow_rerere_auto;
 
 static const char *me;
 
@@ -57,10 +58,11 @@ static void parse_args(int argc, const char **argv)
                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_RERERE_AUTOUPDATE(&allow_rerere_auto),
                OPT_END(),
        };
 
-       if (parse_options(argc, argv, options, usage_str, 0) != 1)
+       if (parse_options(argc, argv, NULL, options, usage_str, 0) != 1)
                usage_with_options(usage_str, options);
        arg = argv[0];
 
@@ -135,7 +137,7 @@ static void add_to_msg(const char *string)
 {
        int len = strlen(string);
        if (write_in_full(msg_fd, string, len) < 0)
-               die ("Could not write to MERGE_MSG");
+               die_errno ("Could not write to MERGE_MSG");
 }
 
 static void add_message_to_msg(const char *message)
@@ -233,6 +235,19 @@ static struct tree *empty_tree(void)
        return tree;
 }
 
+static NORETURN void die_dirty_index(const char *me)
+{
+       if (read_cache_unmerged()) {
+               die_resolve_conflict(me);
+       } else {
+               if (advice_commit_before_merge)
+                       die("Your local changes would be overwritten by %s.\n"
+                           "Please, commit your changes or stash them to proceed.", me);
+               else
+                       die("Your local changes would be overwritten by %s.\n", me);
+       }
+}
+
 static int revert_or_cherry_pick(int argc, const char **argv)
 {
        unsigned char head[20];
@@ -269,7 +284,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
                if (get_sha1("HEAD", head))
                        die ("You do not have a valid HEAD");
                if (index_differs_from("HEAD", 0))
-                       die ("Dirty index: cannot %s", me);
+                       die_dirty_index(me);
        }
        discard_cache();
 
@@ -323,9 +338,9 @@ static int revert_or_cherry_pick(int argc, const char **argv)
 
        encoding = get_encoding(message);
        if (!encoding)
-               encoding = "utf-8";
+               encoding = "UTF-8";
        if (!git_commit_encoding)
-               git_commit_encoding = "utf-8";
+               git_commit_encoding = "UTF-8";
        if ((reencoded_message = reencode_string(message,
                                        git_commit_encoding, encoding)))
                message = reencoded_message;
@@ -395,7 +410,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
                        die ("Error wrapping up %s", defmsg);
                fprintf(stderr, "Automatic %s failed.%s\n",
                        me, help_msg(commit->object.sha1));
-               rerere();
+               rerere(allow_rerere_auto);
                exit(1);
        }
        if (commit_lock_file(&msg_file) < 0)