Code

Oops, fix enum_equals
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index f43ec7f7db4074fb0d1d5966d2590579be6b639e..d9f26dd0f850c345093bc02a386e25421734221d 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -114,10 +114,6 @@ static size_t utf8_length(const char **string, size_t col, int *width, size_t ma
 
 #define S_ISGITLINK(mode) (((mode) & S_IFMT) == 0160000)
 
-#ifndef GIT_CONFIG
-#define GIT_CONFIG "config"
-#endif
-
 /* Some ASCII-shorthands fitted into the ncurses namespace. */
 #define KEY_TAB                '\t'
 #define KEY_RETURN     '\r'
@@ -141,7 +137,7 @@ struct ref_list {
 };
 
 static struct ref_list *get_ref_list(const char *id);
-static void foreach_ref(bool (*visitor)(void *data, struct ref *ref), void *data);
+static void foreach_ref(bool (*visitor)(void *data, const struct ref *ref), void *data);
 static int load_refs(void);
 
 enum format_flags {
@@ -301,6 +297,9 @@ string_enum_compare(const char *str1, const char *str2, int len)
        return 0;
 }
 
+#define enum_equals(entry, str, len) \
+       ((entry).namelen == (len) && !string_enum_compare((entry).name, str, len))
+
 struct enum_map {
        const char *name;
        int namelen;
@@ -309,6 +308,24 @@ struct enum_map {
 
 #define ENUM_MAP(name, value) { name, STRING_SIZE(name), value }
 
+static char *
+enum_map_name(const char *name, size_t namelen)
+{
+       static char buf[SIZEOF_STR];
+       int bufpos;
+
+       for (bufpos = 0; bufpos <= namelen; bufpos++) {
+               buf[bufpos] = tolower(name[bufpos]);
+               if (buf[bufpos] == '_')
+                       buf[bufpos] = '-';
+       }
+
+       buf[bufpos] = 0;
+       return buf;
+}
+
+#define enum_name(entry) enum_map_name((entry).name, (entry).namelen)
+
 static bool
 map_enum_do(const struct enum_map *map, size_t map_size, int *value, const char *name)
 {
@@ -316,8 +333,7 @@ map_enum_do(const struct enum_map *map, size_t map_size, int *value, const char
        int i;
 
        for (i = 0; i < map_size; i++)
-               if (namelen == map[i].namelen &&
-                   !string_enum_compare(name, map[i].name, namelen)) {
+               if (enum_equals(map[i], name, namelen)) {
                        *value = map[i].value;
                        return TRUE;
                }
@@ -367,14 +383,25 @@ static int local_tzoffset(time_t time)
        return offset * eastwest;
 }
 
+#define DATE_INFO \
+       DATE_(NO), \
+       DATE_(DEFAULT), \
+       DATE_(RELATIVE), \
+       DATE_(SHORT)
+
 enum date {
-       DATE_NONE = 0,
-       DATE_DEFAULT,
-       DATE_RELATIVE,
-       DATE_SHORT
+#define DATE_(name) DATE_##name
+       DATE_INFO
+#undef DATE_
 };
 
-static char *
+static const struct enum_map date_map[] = {
+#define DATE_(name) ENUM_MAP(#name, DATE_##name)
+       DATE_INFO
+#undef DATE_
+};
+
+static const char *
 string_date(const time_t *time, enum date date)
 {
        static char buf[DATE_COLS + 1];
@@ -501,9 +528,22 @@ init_io_rd(struct io *io, const char *argv[], const char *dir,
 }
 
 static bool
-io_open(struct io *io, const char *name)
+io_open(struct io *io, const char *fmt, ...)
 {
+       char name[SIZEOF_STR] = "";
+       bool fits;
+       va_list args;
+
        init_io(io, NULL, IO_FD);
+
+       va_start(args, fmt);
+       fits = vsnprintf(name, sizeof(name), fmt, args) < sizeof(name);
+       va_end(args);
+
+       if (!fits) {
+               io->error = ENAMETOOLONG;
+               return FALSE;
+       }
        io->pipe = *name ? open(name, O_RDONLY) : STDIN_FILENO;
        if (io->pipe == -1)
                io->error = errno;
@@ -649,9 +689,9 @@ run_io_append(const char **argv, enum format_flags flags, int fd)
 }
 
 static bool
-run_io_rd(struct io *io, const char **argv, enum format_flags flags)
+run_io_rd(struct io *io, const char **argv, const char *dir, enum format_flags flags)
 {
-       return init_io_rd(io, argv, NULL, flags) && start_io(io);
+       return init_io_rd(io, argv, dir, flags) && start_io(io);
 }
 
 static bool
@@ -788,7 +828,8 @@ run_io_buf(const char **argv, char buf[], size_t bufsize)
 {
        struct io io = {};
 
-       return run_io_rd(&io, argv, FORMAT_NONE) && io_read_buf(&io, buf, bufsize);
+       return run_io_rd(&io, argv, NULL, FORMAT_NONE)
+           && io_read_buf(&io, buf, bufsize);
 }
 
 static int
@@ -953,8 +994,7 @@ get_request(const char *name)
        int i;
 
        for (i = 0; i < ARRAY_SIZE(req_info); i++)
-               if (req_info[i].namelen == namelen &&
-                   !string_enum_compare(req_info[i].name, name, namelen))
+               if (enum_equals(req_info[i], name, namelen))
                        return req_info[i].request;
 
        return REQ_NONE;
@@ -984,9 +1024,9 @@ static char opt_head[SIZEOF_REF]   = "";
 static char opt_head_rev[SIZEOF_REV]   = "";
 static char opt_remote[SIZEOF_REF]     = "";
 static char opt_encoding[20]           = "UTF-8";
-static bool opt_utf8                   = TRUE;
 static char opt_codeset[20]            = "UTF-8";
-static iconv_t opt_iconv               = ICONV_NONE;
+static iconv_t opt_iconv_in            = ICONV_NONE;
+static iconv_t opt_iconv_out           = ICONV_NONE;
 static char opt_search[SIZEOF_STR]     = "";
 static char opt_cdup[SIZEOF_STR]       = "";
 static char opt_prefix[SIZEOF_STR]     = "";
@@ -1115,8 +1155,7 @@ get_line_info(const char *name)
        enum line_type type;
 
        for (type = 0; type < ARRAY_SIZE(line_info); type++)
-               if (namelen == line_info[type].namelen &&
-                   !string_enum_compare(line_info[type].name, name, namelen))
+               if (enum_equals(line_info[type], name, namelen))
                        return &line_info[type];
 
        return NULL;
@@ -1648,6 +1687,26 @@ static int parse_bool(bool *opt, const char *arg)
        return OK;
 }
 
+static int parse_enum_do(unsigned int *opt, const char *arg,
+                        const struct enum_map *map, size_t map_size)
+{
+       bool is_true;
+
+       assert(map_size > 1);
+
+       if (map_enum_do(map, map_size, (int *) opt, arg))
+               return OK;
+
+       if (parse_bool(&is_true, arg) != OK)
+               return ERR;
+
+       *opt = is_true ? map[1].value : map[0].value;
+       return OK;
+}
+
+#define parse_enum(opt, arg, map) \
+       parse_enum_do(opt, arg, map, ARRAY_SIZE(map))
+
 static int
 parse_string(char *opt, const char *arg, size_t optsize)
 {
@@ -1684,20 +1743,8 @@ option_set_command(int argc, const char *argv[])
        if (!strcmp(argv[0], "show-author"))
                return parse_bool(&opt_author, argv[2]);
 
-       if (!strcmp(argv[0], "show-date")) {
-               bool show_date;
-
-               if (!strcmp(argv[2], "relative")) {
-                       opt_date = DATE_RELATIVE;
-                       return OK;
-               } else if (!strcmp(argv[2], "short")) {
-                       opt_date = DATE_SHORT;
-                       return OK;
-               } else if (parse_bool(&show_date, argv[2])) {
-                       opt_date = show_date ? DATE_DEFAULT : DATE_NONE;
-               }
-               return ERR;
-       }
+       if (!strcmp(argv[0], "show-date"))
+               return parse_enum(&opt_date, argv[2], date_map);
 
        if (!strcmp(argv[0], "show-rev-graph"))
                return parse_bool(&opt_rev_graph, argv[2]);
@@ -1855,7 +1902,7 @@ load_option_file(const char *path)
        struct io io = {};
 
        /* It's OK that the file doesn't exist. */
-       if (!io_open(&io, path))
+       if (!io_open(&io, "%s", path))
                return;
 
        config_lineno = 0;
@@ -1981,6 +2028,8 @@ struct view_ops {
        bool (*grep)(struct view *view, struct line *line);
        /* Select line */
        void (*select)(struct view *view, struct line *line);
+       /* Prepare view for loading */
+       bool (*prepare)(struct view *view);
 };
 
 static struct view_ops blame_ops;
@@ -2048,6 +2097,7 @@ static int
 draw_chars(struct view *view, enum line_type type, const char *string,
           int max_len, bool use_tilde)
 {
+       static char out_buffer[BUFSIZ * 2];
        int len = 0;
        int col = 0;
        int trimmed = FALSE;
@@ -2056,22 +2106,28 @@ draw_chars(struct view *view, enum line_type type, const char *string,
        if (max_len <= 0)
                return 0;
 
-       if (opt_utf8) {
-               len = utf8_length(&string, skip, &col, max_len, &trimmed, use_tilde);
-       } else {
-               col = len = strlen(string);
-               if (len > max_len) {
-                       if (use_tilde) {
-                               max_len -= 1;
+       len = utf8_length(&string, skip, &col, max_len, &trimmed, use_tilde);
+
+       set_view_attr(view, type);
+       if (len > 0) {
+               if (opt_iconv_out != ICONV_NONE) {
+                       ICONV_CONST char *inbuf = (ICONV_CONST char *) string;
+                       size_t inlen = len + 1;
+
+                       char *outbuf = out_buffer;
+                       size_t outlen = sizeof(out_buffer);
+
+                       size_t ret;
+
+                       ret = iconv(opt_iconv_out, &inbuf, &inlen, &outbuf, &outlen);
+                       if (ret != (size_t) -1) {
+                               string = out_buffer;
+                               len = sizeof(out_buffer) - outlen;
                        }
-                       col = len = max_len;
-                       trimmed = TRUE;
                }
-       }
 
-       set_view_attr(view, type);
-       if (len > 0)
                waddnstr(view->win, string, len);
+       }
        if (trimmed && use_tilde) {
                set_view_attr(view, LINE_DELIMITER);
                waddch(view->win, '~');
@@ -2149,7 +2205,7 @@ draw_field(struct view *view, enum line_type type, const char *text, int len, bo
 static bool
 draw_date(struct view *view, time_t *time)
 {
-       const char *date = mkdate(time);
+       const char *date = time ? mkdate(time) : "";
        int cols = opt_date == DATE_SHORT ? DATE_SHORT_COLS : DATE_COLS;
 
        return draw_field(view, LINE_DATE, date, cols, FALSE);
@@ -2430,20 +2486,19 @@ redraw_display(bool clear)
 }
 
 static void
-toggle_date_option(enum date *date)
+toggle_enum_option_do(unsigned int *opt, const char *help,
+                     const struct enum_map *map, size_t size)
 {
-       static const char *help[] = {
-               "no",
-               "default",
-               "relative",
-               "short"
-       };
-
-       opt_date = (opt_date + 1) % ARRAY_SIZE(help);
+       *opt = (*opt + 1) % size;
        redraw_display(FALSE);
-       report("Displaying %s dates", help[opt_date]);
+       report("Displaying %s %s", enum_name(map[*opt]), help);
 }
 
+#define toggle_enum_option(opt, help, map) \
+       toggle_enum_option_do(opt, help, map, ARRAY_SIZE(map))
+
+#define toggle_date() toggle_enum_option(&opt_date, "dates", date_map)
+
 static void
 toggle_view_option(bool *option, const char *help)
 {
@@ -2467,7 +2522,7 @@ open_option_menu(void)
 
        if (prompt_menu("Toggle option", menu, &selected)) {
                if (menu[selected].data == &opt_date)
-                       toggle_date_option(menu[selected].data);
+                       toggle_date();
                else
                        toggle_view_option(menu[selected].data, menu[selected].text);
        }
@@ -2984,7 +3039,7 @@ prepare_update_file(struct view *view, const char *name)
 {
        if (view->pipe)
                end_update(view, TRUE);
-       return io_open(&view->io, name);
+       return io_open(&view->io, "%s", name);
 }
 
 static bool
@@ -2993,16 +3048,13 @@ begin_update(struct view *view, bool refresh)
        if (view->pipe)
                end_update(view, TRUE);
 
-       if (refresh) {
-               if (!start_io(&view->io))
-                       return FALSE;
-
-       } else {
-               if (view == VIEW(REQ_VIEW_TREE) && strcmp(view->vid, view->id))
-                       opt_path[0] = 0;
-
-               if (!run_io_rd(&view->io, view->ops->argv, FORMAT_ALL))
+       if (!refresh) {
+               if (view->ops->prepare) {
+                       if (!view->ops->prepare(view))
+                               return FALSE;
+               } else if (!init_io_rd(&view->io, view->ops->argv, NULL, FORMAT_ALL)) {
                        return FALSE;
+               }
 
                /* Put the current ref_* value to the view title ref
                 * member. This is needed by the blob view. Most other
@@ -3011,6 +3063,9 @@ begin_update(struct view *view, bool refresh)
                string_copy_rev(view->ref, view->id);
        }
 
+       if (!start_io(&view->io))
+               return FALSE;
+
        setup_update(view, view->id);
 
        return TRUE;
@@ -3044,7 +3099,7 @@ update_view(struct view *view)
        }
 
        for (; (line = io_get(view->pipe, '\n', can_read)); can_read = FALSE) {
-               if (opt_iconv != ICONV_NONE) {
+               if (opt_iconv_in != ICONV_NONE) {
                        ICONV_CONST char *inbuf = line;
                        size_t inlen = strlen(line) + 1;
 
@@ -3053,7 +3108,7 @@ update_view(struct view *view)
 
                        size_t ret;
 
-                       ret = iconv(opt_iconv, &inbuf, &inlen, &outbuf, &outlen);
+                       ret = iconv(opt_iconv_in, &inbuf, &inlen, &outbuf, &outlen);
                        if (ret != (size_t) -1)
                                line = out_buffer;
                }
@@ -3189,6 +3244,11 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
                display[current_view] = view;
        }
 
+       /* No parent signals that this is the first loaded view. */
+       if (prev && view != prev) {
+               view->parent = prev;
+       }
+
        /* Resize the view when switching between split- and full-screen,
         * or when switching between two different full-screen views. */
        if (nviews != displayed_views() ||
@@ -3219,13 +3279,9 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
                do_scroll_view(prev, lines);
        }
 
-       if (prev && view != prev) {
-               if (split) {
-                       /* "Blur" the previous view. */
-                       update_view_title(prev);
-               }
-
-               view->parent = prev;
+       if (prev && view != prev && split && view_is_displayed(prev)) {
+               /* "Blur" the previous view. */
+               update_view_title(prev);
        }
 
        if (view->pipe && view->lines == 0) {
@@ -3463,7 +3519,7 @@ view_driver(struct view *view, enum request request)
                break;
 
        case REQ_TOGGLE_DATE:
-               toggle_date_option(&opt_date);
+               toggle_date();
                break;
 
        case REQ_TOGGLE_AUTHOR:
@@ -3941,33 +3997,14 @@ static struct view_ops diff_ops = {
 
 static bool help_keymap_hidden[ARRAY_SIZE(keymap_table)];
 
-static char *
-help_name(char buf[SIZEOF_STR], const char *name, size_t namelen)
-{
-       int bufpos;
-
-       for (bufpos = 0; bufpos <= namelen; bufpos++) {
-               buf[bufpos] = tolower(name[bufpos]);
-               if (buf[bufpos] == '_')
-                       buf[bufpos] = '-';
-       }
-
-       buf[bufpos] = 0;
-       return buf;
-}
-
-#define help_keymap_name(buf, keymap) \
-       help_name(buf, keymap_table[keymap].name, keymap_table[keymap].namelen)
-
 static bool
 help_open_keymap_title(struct view *view, enum keymap keymap)
 {
-       char buf[SIZEOF_STR];
        struct line *line;
 
        line = add_line_format(view, LINE_HELP_KEYMAP, "[%c] %s bindings",
                               help_keymap_hidden[keymap] ? '+' : '-',
-                              help_keymap_name(buf, keymap));
+                              enum_name(keymap_table[keymap]));
        if (line)
                line->other = keymap;
 
@@ -4008,8 +4045,7 @@ help_open_keymap(struct view *view, enum keymap keymap)
                }
 
                add_line_format(view, LINE_DEFAULT, "    %-25s %-20s %s", key,
-                               help_name(buf, req_info[i].name, req_info[i].namelen),
-                               req_info[i].help);
+                               enum_name(req_info[i]), req_info[i].help);
        }
 
        group = "External commands:";
@@ -4249,7 +4285,7 @@ tree_read_date(struct view *view, char *text, bool *read_date)
                        return TRUE;
                }
 
-               if (!run_io_rd(&io, log_file, FORMAT_NONE)) {
+               if (!run_io_rd(&io, log_file, opt_cdup, FORMAT_NONE)) {
                        report("Failed to load tree data");
                        return TRUE;
                }
@@ -4272,8 +4308,6 @@ tree_read_date(struct view *view, char *text, bool *read_date)
                if (!pos)
                        return TRUE;
                text = pos + 1;
-               if (*opt_prefix && !strncmp(text, opt_prefix, strlen(opt_prefix)))
-                       text += strlen(opt_prefix);
                if (*opt_path && !strncmp(text, opt_path, strlen(opt_path)))
                        text += strlen(opt_path);
                pos = strchr(text, '/');
@@ -4516,6 +4550,32 @@ tree_select(struct view *view, struct line *line)
        string_copy_rev(view->ref, entry->id);
 }
 
+static bool
+tree_prepare(struct view *view)
+{
+       if (view->lines == 0 && opt_prefix[0]) {
+               char *pos = opt_prefix;
+
+               while (pos && *pos) {
+                       char *end = strchr(pos, '/');
+
+                       if (end)
+                               *end = 0;
+                       push_tree_stack_entry(pos, 0);
+                       pos = end;
+                       if (end) {
+                               *end = '/';
+                               pos++;
+                       }
+               }
+
+       } else if (strcmp(view->vid, view->id)) {
+               opt_path[0] = 0;
+       }
+
+       return init_io_rd(&view->io, view->ops->argv, opt_cdup, FORMAT_ALL);
+}
+
 static const char *tree_argv[SIZEOF_ARG] = {
        "git", "ls-tree", "%(commit)", "%(directory)", NULL
 };
@@ -4529,6 +4589,7 @@ static struct view_ops tree_ops = {
        tree_request,
        tree_grep,
        tree_select,
+       tree_prepare,
 };
 
 static bool
@@ -4607,8 +4668,16 @@ struct blame {
 static bool
 blame_open(struct view *view)
 {
-       if (*opt_ref || !io_open(&view->io, opt_file)) {
-               if (!run_io_rd(&view->io, blame_cat_file_argv, FORMAT_ALL))
+       char path[SIZEOF_STR];
+
+       if (!view->parent && *opt_prefix) {
+               string_copy(path, opt_file);
+               if (!string_format(opt_file, "%s%s", opt_prefix, path))
+                       return FALSE;
+       }
+
+       if (*opt_ref || !io_open(&view->io, "%s%s", opt_cdup, opt_file)) {
+               if (!run_io_rd(&view->io, blame_cat_file_argv, opt_cdup, FORMAT_ALL))
                        return FALSE;
        }
 
@@ -4704,7 +4773,7 @@ blame_read_file(struct view *view, const char *line, bool *read_file)
                if (view->lines == 0 && !view->parent)
                        die("No blame exist for %s", view->vid);
 
-               if (view->lines == 0 || !run_io_rd(&io, argv, FORMAT_ALL)) {
+               if (view->lines == 0 || !run_io_rd(&io, argv, opt_cdup, FORMAT_ALL)) {
                        report("Failed to load blame data");
                        return TRUE;
                }
@@ -4988,9 +5057,11 @@ static struct view_ops blame_ops = {
 struct branch {
        const char *author;             /* Author of the last commit. */
        time_t time;                    /* Date of the last activity. */
-       struct ref *ref;                /* Name and commit ID information. */
+       const struct ref *ref;          /* Name and commit ID information. */
 };
 
+static const struct ref branch_all;
+
 static const enum sort_field branch_sort_fields[] = {
        ORDERBY_NAME, ORDERBY_DATE, ORDERBY_AUTHOR
 };
@@ -5027,13 +5098,15 @@ branch_draw(struct view *view, struct line *line, unsigned int lineno)
        if (opt_author && draw_author(view, branch->author))
                return TRUE;
 
-       draw_text(view, type, branch->ref->name, TRUE);
+       draw_text(view, type, branch->ref == &branch_all ? "All branches" : branch->ref->name, TRUE);
        return TRUE;
 }
 
 static enum request
 branch_request(struct view *view, enum request request, struct line *line)
 {
+       struct branch *branch = line->data;
+
        switch (request) {
        case REQ_REFRESH:
                load_refs();
@@ -5046,7 +5119,21 @@ branch_request(struct view *view, enum request request, struct line *line)
                return REQ_NONE;
 
        case REQ_ENTER:
-               open_view(view, REQ_VIEW_MAIN, OPEN_SPLIT);
+               if (branch->ref == &branch_all) {
+                       const char *all_branches_argv[] = {
+                               "git", "log", "--no-color", "--pretty=raw", "--parents",
+                                     "--topo-order", "--all", NULL
+                       };
+                       struct view *main_view = VIEW(REQ_VIEW_MAIN);
+
+                       if (!prepare_update(main_view, all_branches_argv, NULL, FORMAT_NONE)) {
+                               report("Failed to load view of all branches");
+                               return REQ_NONE;
+                       }
+                       open_view(view, REQ_VIEW_MAIN, OPEN_PREPARED | OPEN_SPLIT);
+               } else {
+                       open_view(view, REQ_VIEW_MAIN, OPEN_SPLIT);
+               }
                return REQ_NONE;
 
        default:
@@ -5096,7 +5183,7 @@ branch_read(struct view *view, char *line)
 }
 
 static bool
-branch_open_visitor(void *data, struct ref *ref)
+branch_open_visitor(void *data, const struct ref *ref)
 {
        struct view *view = data;
        struct branch *branch;
@@ -5120,12 +5207,13 @@ branch_open(struct view *view)
                        "--simplify-by-decoration", "--all", NULL
        };
 
-       if (!run_io_rd(&view->io, branch_log, FORMAT_NONE)) {
+       if (!run_io_rd(&view->io, branch_log, NULL, FORMAT_NONE)) {
                report("Failed to load branch data");
                return TRUE;
        }
 
        setup_update(view, view->id);
+       branch_open_visitor(view, &branch_all);
        foreach_ref(branch_open_visitor, view);
        view->p_restore = TRUE;
 
@@ -5239,7 +5327,7 @@ status_run(struct view *view, const char *argv[], char status, enum line_type ty
        char *buf;
        struct io io = {};
 
-       if (!run_io(&io, argv, NULL, IO_RD))
+       if (!run_io(&io, argv, opt_cdup, IO_RD))
                return FALSE;
 
        add_line_data(view, NULL, type);
@@ -5389,8 +5477,7 @@ status_update_onbranch(void)
                if (!*opt_head) {
                        struct io io = {};
 
-                       if (string_format(buf, "%s/rebase-merge/head-name", opt_git_dir) &&
-                           io_open(&io, buf) &&
+                       if (io_open(&io, "%s/rebase-merge/head-name", opt_git_dir) &&
                            io_read_buf(&io, buf, sizeof(buf))) {
                                head = buf;
                                if (!prefixcmp(head, "refs/heads/"))
@@ -5638,10 +5725,8 @@ status_update_prepare(struct io *io, enum line_type type)
                return run_io(io, staged_argv, opt_cdup, IO_WR);
 
        case LINE_STAT_UNSTAGED:
-               return run_io(io, others_argv, opt_cdup, IO_WR);
-
        case LINE_STAT_UNTRACKED:
-               return run_io(io, others_argv, NULL, IO_WR);
+               return run_io(io, others_argv, opt_cdup, IO_WR);
 
        default:
                die("line type %d not handled in switch", type);
@@ -5767,9 +5852,8 @@ status_revert(struct status *status, enum line_type type, bool has_none)
                } else {
                        report("Cannot revert changes to multiple files");
                }
-               return FALSE;
 
-       } else {
+       } else if (prompt_yesno("Are you sure you want to revert changes?")) {
                char mode[10] = "100644";
                const char *reset_argv[] = {
                        "git", "update-index", "--cacheinfo", mode,
@@ -5779,12 +5863,25 @@ status_revert(struct status *status, enum line_type type, bool has_none)
                        "git", "checkout", "--", status->old.name, NULL
                };
 
-               if (!prompt_yesno("Are you sure you want to overwrite any changes?"))
-                       return FALSE;
-               string_format(mode, "%o", status->old.mode);
-               return (status->status != 'U' || run_io_fg(reset_argv, opt_cdup)) &&
-                       run_io_fg(checkout_argv, opt_cdup);
+               if (status->status == 'U') {
+                       string_format(mode, "%5o", status->old.mode);
+
+                       if (status->old.mode == 0 && status->new.mode == 0) {
+                               reset_argv[2] = "--force-remove";
+                               reset_argv[3] = status->old.name;
+                               reset_argv[4] = NULL;
+                       }
+
+                       if (!run_io_fg(reset_argv, opt_cdup))
+                               return FALSE;
+                       if (status->old.mode == 0 && status->new.mode == 0)
+                               return TRUE;
+               }
+
+               return run_io_fg(checkout_argv, opt_cdup);
        }
+
+       return FALSE;
 }
 
 static enum request
@@ -5823,10 +5920,8 @@ status_request(struct view *view, enum request request, struct line *line)
                break;
 
        case REQ_VIEW_BLAME:
-               if (status) {
-                       string_copy(opt_file, status->new.name);
+               if (status)
                        opt_ref[0] = 0;
-               }
                return request;
 
        case REQ_ENTER:
@@ -5893,6 +5988,8 @@ status_select(struct view *view, struct line *line)
        }
 
        string_format(view->ref, text, key, file);
+       if (status)
+               string_copy(opt_file, status->new.name);
 }
 
 static bool
@@ -7148,7 +7245,7 @@ compare_refs(const void *ref1_, const void *ref2_)
 }
 
 static void
-foreach_ref(bool (*visitor)(void *data, struct ref *ref), void *data)
+foreach_ref(bool (*visitor)(void *data, const struct ref *ref), void *data)
 {
        size_t i;
 
@@ -7419,7 +7516,7 @@ read_repo_config_option(char *name, size_t namelen, char *value, size_t valuelen
 static int
 load_git_config(void)
 {
-       const char *config_list_argv[] = { "git", GIT_CONFIG, "--list", NULL };
+       const char *config_list_argv[] = { "git", "config", "--list", NULL };
 
        return run_io_load(config_list_argv, "=", read_repo_config_option);
 }
@@ -7587,8 +7684,8 @@ parse_options(int argc, const char *argv[])
                        die("command too long");
        }
 
-       if (!prepare_update(VIEW(request), custom_argv, NULL, FORMAT_NONE))                                                                        
-               die("Failed to format arguments"); 
+       if (!prepare_update(VIEW(request), custom_argv, NULL, FORMAT_NONE))
+               die("Failed to format arguments");
 
        return request;
 }
@@ -7622,12 +7719,15 @@ main(int argc, const char *argv[])
        if (!opt_git_dir[0] && request != REQ_VIEW_PAGER)
                die("Not a git repository");
 
-       if (*opt_encoding && strcasecmp(opt_encoding, "UTF-8"))
-               opt_utf8 = FALSE;
+       if (*opt_encoding && strcmp(opt_codeset, "UTF-8")) {
+               opt_iconv_in = iconv_open("UTF-8", opt_encoding);
+               if (opt_iconv_in == ICONV_NONE)
+                       die("Failed to initialize character set conversion");
+       }
 
-       if (*opt_codeset && strcmp(opt_codeset, opt_encoding)) {
-               opt_iconv = iconv_open(opt_codeset, opt_encoding);
-               if (opt_iconv == ICONV_NONE)
+       if (*opt_codeset && strcmp(opt_codeset, "UTF-8")) {
+               opt_iconv_out = iconv_open(opt_codeset, "UTF-8");
+               if (opt_iconv_out == ICONV_NONE)
                        die("Failed to initialize character set conversion");
        }