Code

Fix drawing loading views that are not displayed.
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index 5c456e723bf1fea3b55382d3e5fd1b10431159a7..d2bc2dd46555008a03b6aa903fea78ea304ecfbf 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -164,7 +164,7 @@ struct ref {
        unsigned int next:1;    /* For ref lists: are there more refs? */
 };
 
-static struct ref **get_refs(char *id);
+static struct ref **get_refs(const char *id);
 
 struct int_map {
        const char *name;
@@ -277,6 +277,18 @@ string_enum_compare(const char *str1, const char *str2, int len)
        return 0;
 }
 
+#define prefixcmp(str1, str2) \
+       strncmp(str1, str2, STRING_SIZE(str2))
+
+static inline int
+suffixcmp(const char *str, int slen, const char *suffix)
+{
+       size_t len = slen >= 0 ? slen : strlen(str);
+       size_t suffixlen = strlen(suffix);
+
+       return suffixlen < len ? strcmp(str + len - suffixlen, suffix) : -1;
+}
+
 /* Shell quoting
  *
  * NOTE: The following is a slightly modified copy of the git project's shell
@@ -408,9 +420,9 @@ enum request {
 
 struct request_info {
        enum request request;
-       char *name;
+       const char *name;
        int namelen;
-       char *help;
+       const char *help;
 };
 
 static struct request_info req_info[] = {
@@ -468,8 +480,8 @@ static char opt_path[SIZEOF_STR]    = "";
 static char opt_file[SIZEOF_STR]       = "";
 static char opt_ref[SIZEOF_REF]                = "";
 static char opt_head[SIZEOF_REF]       = "";
+static char opt_head_rev[SIZEOF_REV]   = "";
 static char opt_remote[SIZEOF_REF]     = "";
-static bool opt_no_head                        = TRUE;
 static FILE *opt_pipe                  = NULL;
 static char opt_encoding[20]           = "UTF-8";
 static bool opt_utf8                   = TRUE;
@@ -480,13 +492,17 @@ static char opt_cdup[SIZEOF_STR]  = "";
 static char opt_git_dir[SIZEOF_STR]    = "";
 static signed char opt_is_inside_work_tree     = -1; /* set to TRUE or FALSE */
 static char opt_editor[SIZEOF_STR]     = "";
+static FILE *opt_tty                   = NULL;
+
+#define is_initial_commit()    (!*opt_head_rev)
+#define is_head_commit(rev)    (!strcmp((rev), "HEAD") || !strcmp(opt_head_rev, (rev)))
 
 static enum request
-parse_options(int argc, char *argv[])
+parse_options(int argc, const char *argv[])
 {
        enum request request = REQ_VIEW_MAIN;
        size_t buf_size;
-       char *subcommand;
+       const char *subcommand;
        bool seen_dashdash = FALSE;
        int i;
 
@@ -540,7 +556,7 @@ parse_options(int argc, char *argv[])
        buf_size = strlen(opt_cmd);
 
        for (i = 1 + !!subcommand; i < argc; i++) {
-               char *opt = argv[i];
+               const char *opt = argv[i];
 
                if (seen_dashdash || !strcmp(opt, "--")) {
                        seen_dashdash = TRUE;
@@ -867,7 +883,7 @@ get_keybinding(enum keymap keymap, int key)
 
 
 struct key {
-       char *name;
+       const char *name;
        int value;
 };
 
@@ -917,11 +933,11 @@ get_key_value(const char *name)
        return ERR;
 }
 
-static char *
+static const char *
 get_key_name(int key_value)
 {
        static char key_char[] = "'X'";
-       char *seq = NULL;
+       const char *seq = NULL;
        int key;
 
        for (key = 0; key < ARRAY_SIZE(key_table); key++)
@@ -938,7 +954,7 @@ get_key_name(int key_value)
        return seq ? seq : "(no key)";
 }
 
-static char *
+static const char *
 get_key(enum request request)
 {
        static char buf[BUFSIZ];
@@ -1062,7 +1078,7 @@ static struct int_map attr_map[] = {
 
 static int   config_lineno;
 static bool  config_errors;
-static char *config_msg;
+static const char *config_msg;
 
 /* Wants: object fgcolor bgcolor [attr] */
 static int
@@ -1249,7 +1265,7 @@ option_bind_command(int argc, const char *argv[])
 }
 
 static int
-set_option(char *opt, char *value)
+set_option(const char *opt, char *value)
 {
        const char *argv[SIZEOF_ARG];
        int valuelen;
@@ -1343,9 +1359,9 @@ load_option_file(const char *path)
 static int
 load_options(void)
 {
-       char *home = getenv("HOME");
-       char *tigrc_user = getenv("TIGRC_USER");
-       char *tigrc_system = getenv("TIGRC_SYSTEM");
+       const char *home = getenv("HOME");
+       const char *tigrc_user = getenv("TIGRC_USER");
+       const char *tigrc_system = getenv("TIGRC_SYSTEM");
        char buf[SIZEOF_STR];
 
        add_builtin_run_requests();
@@ -1639,7 +1655,7 @@ draw_graphic(struct view *view, enum line_type type, chtype graphic[], size_t si
 }
 
 static bool
-draw_field(struct view *view, enum line_type type, char *text, int len, bool trim)
+draw_field(struct view *view, enum line_type type, const char *text, int len, bool trim)
 {
        int max = MIN(view->width - view->col, len);
        int col;
@@ -2375,8 +2391,17 @@ update_view(struct view *view)
                }
        }
 
+       if (ferror(view->pipe) && errno != 0) {
+               report("Failed to read: %s", strerror(errno));
+               end_update(view, TRUE);
+
+       } else if (feof(view->pipe)) {
+               report("");
+               end_update(view, FALSE);
+       }
+
        if (!view_is_displayed(view))
-               goto check_pipe;
+               return TRUE;
 
        if (view == VIEW(REQ_VIEW_TREE)) {
                /* Clear the view and redraw everything since the tree sorting
@@ -2406,17 +2431,6 @@ update_view(struct view *view)
        /* Update the title _after_ the redraw so that if the redraw picks up a
         * commit reference in view->ref it'll be available here. */
        update_view_title(view);
-
-check_pipe:
-       if (ferror(view->pipe) && errno != 0) {
-               report("Failed to read: %s", strerror(errno));
-               end_update(view, TRUE);
-
-       } else if (feof(view->pipe)) {
-               report("");
-               end_update(view, FALSE);
-       }
-
        return TRUE;
 
 alloc_error:
@@ -2438,10 +2452,9 @@ add_line_data(struct view *view, void *data, enum line_type type)
 }
 
 static struct line *
-add_line_text(struct view *view, char *data, enum line_type type)
+add_line_text(struct view *view, const char *text, enum line_type type)
 {
-       if (data)
-               data = strdup(data);
+       char *data = text ? strdup(text) : NULL;
 
        return data ? add_line_data(view, data, type) : NULL;
 }
@@ -2565,7 +2578,7 @@ open_external_viewer(const char *cmd)
        endwin();                  /* restore original tty modes */
        system(cmd);
        fprintf(stderr, "Press Enter to continue");
-       getc(stdin);
+       getc(opt_tty);
        reset_prog_mode();
        redraw_display();
 }
@@ -2587,7 +2600,7 @@ open_editor(bool from_root, const char *file)
 {
        char cmd[SIZEOF_STR];
        char file_sq[SIZEOF_STR];
-       char *editor;
+       const char *editor;
        char *prefix = from_root ? opt_cdup : "";
 
        editor = getenv("GIT_EDITOR");
@@ -2893,7 +2906,6 @@ view_driver(struct view *view, enum request request)
                return FALSE;
 
        default:
-               /* An unknown key will show most commonly used commands. */
                report("Unknown key, press 'h' for help");
                return TRUE;
        }
@@ -2919,7 +2931,7 @@ pager_draw(struct view *view, struct line *line, unsigned int lineno)
 }
 
 static bool
-add_describe_ref(char *buf, size_t *bufpos, char *commit_id, const char *sep)
+add_describe_ref(char *buf, size_t *bufpos, const char *commit_id, const char *sep)
 {
        char refbuf[SIZEOF_STR];
        char *ref = NULL;
@@ -2967,8 +2979,8 @@ add_pager_refs(struct view *view, struct line *line)
 
        do {
                struct ref *ref = refs[refpos];
-               char *fmt = ref->tag    ? "%s[%s]" :
-                           ref->remote ? "%s<%s>" : "%s%s";
+               const char *fmt = ref->tag    ? "%s[%s]" :
+                                 ref->remote ? "%s<%s>" : "%s%s";
 
                if (!string_format_from(buf, &bufpos, fmt, sep, ref->name))
                        return;
@@ -3130,7 +3142,7 @@ help_open(struct view *view)
        add_line_text(view, "Quick reference for tig keybindings:", LINE_DEFAULT);
 
        for (i = 0; i < ARRAY_SIZE(req_info); i++) {
-               char *key;
+               const char *key;
 
                if (req_info[i].request == REQ_NONE)
                        continue;
@@ -3158,7 +3170,7 @@ help_open(struct view *view)
 
        for (i = 0; i < run_requests; i++) {
                struct run_request *req = get_run_request(REQ_NONE + i + 1);
-               char *key;
+               const char *key;
 
                if (!req)
                        continue;
@@ -3215,7 +3227,7 @@ pop_tree_stack_entry(void)
 }
 
 static void
-push_tree_stack_entry(char *name, unsigned long lineno)
+push_tree_stack_entry(const char *name, unsigned long lineno)
 {
        struct tree_stack_entry *entry = calloc(1, sizeof(*entry));
        size_t pathlen = strlen(opt_path);
@@ -3251,8 +3263,8 @@ push_tree_stack_entry(char *name, unsigned long lineno)
 #define TREE_UP_FORMAT "040000 tree %s\t.."
 
 static int
-tree_compare_entry(enum line_type type1, char *name1,
-                  enum line_type type2, char *name2)
+tree_compare_entry(enum line_type type1, const char *name1,
+                  enum line_type type2, const char *name2)
 {
        if (type1 != type2) {
                if (type1 == LINE_TREE_DIR)
@@ -3263,10 +3275,10 @@ tree_compare_entry(enum line_type type1, char *name1,
        return strcmp(name1, name2);
 }
 
-static char *
+static const char *
 tree_path(struct line *line)
 {
-       char *path = line->data;
+       const char *path = line->data;
 
        return path + SIZEOF_TREE_ATTR;
 }
@@ -3318,7 +3330,7 @@ tree_read(struct view *view, char *text)
        /* Skip "Directory ..." and ".." line. */
        for (pos = 1 + !!*opt_path; pos < view->lines; pos++) {
                struct line *line = &view->line[pos];
-               char *path1 = tree_path(line);
+               const char *path1 = tree_path(line);
                char *path2 = text + SIZEOF_TREE_ATTR;
                int cmp = tree_compare_entry(line->type, path1, type, path2);
 
@@ -3356,30 +3368,41 @@ tree_request(struct view *view, enum request request, struct line *line)
 {
        enum open_flags flags;
 
-       if (request == REQ_VIEW_BLAME) {
-               char *filename = tree_path(line);
-
-               if (line->type == LINE_TREE_DIR) {
-                       report("Cannot show blame for directory %s", opt_path);
+       switch (request) {
+       case REQ_VIEW_BLAME:
+               if (line->type != LINE_TREE_FILE) {
+                       report("Blame only supported for files");
                        return REQ_NONE;
                }
 
                string_copy(opt_ref, view->vid);
-               string_format(opt_file, "%s%s", opt_path, filename);
                return request;
-       }
-       if (request == REQ_TREE_PARENT) {
-               if (*opt_path) {
-                       /* fake 'cd  ..' */
-                       request = REQ_ENTER;
-                       line = &view->line[1];
+
+       case REQ_EDIT:
+               if (line->type != LINE_TREE_FILE) {
+                       report("Edit only supported for files");
+               } else if (!is_head_commit(view->vid)) {
+                       report("Edit only supported for files in the current work tree");
                } else {
+                       open_editor(TRUE, opt_file);
+               }
+               return REQ_NONE;
+
+       case REQ_TREE_PARENT:
+               if (!*opt_path) {
                        /* quit view if at top of tree */
                        return REQ_VIEW_CLOSE;
                }
-       }
-       if (request != REQ_ENTER)
+               /* fake 'cd  ..' */
+               line = &view->line[1];
+               break;
+
+       case REQ_ENTER:
+               break;
+
+       default:
                return request;
+       }
 
        /* Cleanup the stack if the tree view is at a different tree. */
        while (!*opt_path && tree_stack)
@@ -3393,7 +3416,7 @@ tree_request(struct view *view, enum request request, struct line *line)
                        pop_tree_stack_entry();
 
                } else {
-                       char *basename = tree_path(line);
+                       const char *basename = tree_path(line);
 
                        push_tree_stack_entry(basename, view->lineno);
                }
@@ -3428,6 +3451,7 @@ tree_select(struct view *view, struct line *line)
 
        if (line->type == LINE_TREE_FILE) {
                string_copy_rev(ref_blob, text);
+               string_format(opt_file, "%s%s", opt_path, tree_path(line));
 
        } else if (line->type != LINE_TREE_DIR) {
                return;
@@ -3490,7 +3514,7 @@ struct blame {
 };
 
 #define BLAME_CAT_FILE_CMD "git cat-file blob %s:%s"
-#define BLAME_INCREMENTAL_CMD "git blame --incremental %s %s"
+#define BLAME_INCREMENTAL_CMD "git blame --incremental %s -- %s"
 
 static bool
 blame_open(struct view *view)
@@ -3556,9 +3580,9 @@ get_blame_commit(struct view *view, const char *id)
 }
 
 static bool
-parse_number(char **posref, size_t *number, size_t min, size_t max)
+parse_number(const char **posref, size_t *number, size_t min, size_t max)
 {
-       char *pos = *posref;
+       const char *pos = *posref;
 
        *posref = NULL;
        pos = strchr(pos + 1, ' ');
@@ -3573,11 +3597,11 @@ parse_number(char **posref, size_t *number, size_t min, size_t max)
 }
 
 static struct blame_commit *
-parse_blame_commit(struct view *view, char *text, int *blamed)
+parse_blame_commit(struct view *view, const char *text, int *blamed)
 {
        struct blame_commit *commit;
        struct blame *blame;
-       char *pos = text + SIZEOF_REV - 1;
+       const char *pos = text + SIZEOF_REV - 1;
        size_t lineno;
        size_t group;
 
@@ -3606,7 +3630,7 @@ parse_blame_commit(struct view *view, char *text, int *blamed)
 }
 
 static bool
-blame_read_file(struct view *view, char *line)
+blame_read_file(struct view *view, const char *line)
 {
        if (!line) {
                FILE *pipe = NULL;
@@ -3711,7 +3735,7 @@ blame_draw(struct view *view, struct line *line, unsigned int lineno)
 {
        struct blame *blame = line->data;
        struct tm *time = NULL;
-       char *id = NULL, *author = NULL;
+       const char *id = NULL, *author = NULL;
 
        if (blame->commit && *blame->commit->filename) {
                id = blame->commit->id;
@@ -3855,13 +3879,13 @@ status_has_none(struct view *view, struct line *line)
  * :100644 100644 06a5d6ae9eca55be2e0e585a152e6b1336f2b20e 0000000000000000000000000000000000000000 M
  */
 static inline bool
-status_get_diff(struct status *file, char *buf, size_t bufsize)
+status_get_diff(struct status *file, const char *buf, size_t bufsize)
 {
-       char *old_mode = buf +  1;
-       char *new_mode = buf +  8;
-       char *old_rev  = buf + 15;
-       char *new_rev  = buf + 56;
-       char *status   = buf + 97;
+       const char *old_mode = buf +  1;
+       const char *new_mode = buf +  8;
+       const char *old_rev  = buf + 15;
+       const char *new_rev  = buf + 56;
+       const char *status   = buf + 97;
 
        if (bufsize < 99 ||
            old_mode[-1] != ':' ||
@@ -4029,7 +4053,7 @@ status_open(struct view *view)
                return FALSE;
 
        add_line_data(view, NULL, LINE_STAT_HEAD);
-       if (opt_no_head)
+       if (is_initial_commit())
                string_copy(status_onbranch, "Initial commit");
        else if (!*opt_head)
                string_copy(status_onbranch, "Not currently on any branch");
@@ -4038,7 +4062,7 @@ status_open(struct view *view)
 
        system("git update-index -q --refresh >/dev/null 2>/dev/null");
 
-       if (opt_no_head) {
+       if (is_initial_commit()) {
                if (!status_run(view, STATUS_LIST_NO_HEAD_CMD, 'A', LINE_STAT_STAGED))
                        return FALSE;
        } else if (!status_run(view, STATUS_DIFF_INDEX_CMD, 0, LINE_STAT_STAGED)) {
@@ -4078,7 +4102,7 @@ status_draw(struct view *view, struct line *line, unsigned int lineno)
 {
        struct status *status = line->data;
        enum line_type type;
-       char *text;
+       const char *text;
 
        if (!status) {
                switch (line->type) {
@@ -4130,7 +4154,7 @@ status_enter(struct view *view, struct line *line)
        struct status *status = line->data;
        char oldpath[SIZEOF_STR] = "";
        char newpath[SIZEOF_STR] = "";
-       char *info;
+       const char *info;
        size_t cmdsize = 0;
        enum open_flags split;
 
@@ -4157,7 +4181,7 @@ status_enter(struct view *view, struct line *line)
 
        switch (line->type) {
        case LINE_STAT_STAGED:
-               if (opt_no_head) {
+               if (is_initial_commit()) {
                        if (!string_format_from(opt_cmd, &cmdsize,
                                                STATUS_DIFF_NO_HEAD_SHOW_CMD,
                                                newpath))
@@ -4194,6 +4218,11 @@ status_enter(struct view *view, struct line *line)
                        return REQ_NONE;
                }
 
+               if (!suffixcmp(status->new.name, -1, "/")) {
+                       report("Cannot display a directory");
+                       return REQ_NONE;
+               }
+
                opt_pipe = fopen(status->new.name, "r");
                info = "Untracked file %s";
                break;
@@ -4393,7 +4422,7 @@ status_revert(struct status *status, enum line_type type, bool has_none)
                char file_sq[SIZEOF_STR];
 
                if (sq_quote(file_sq, 0, status->old.name) >= sizeof(file_sq) ||
-                   !string_format(cmd, "git checkout %s%s", opt_cdup, file_sq))
+                   !string_format(cmd, "git checkout -- %s%s", opt_cdup, file_sq))
                        return FALSE;
 
                return run_confirm(cmd, "Are you sure you want to overwrite any changes?");
@@ -4427,6 +4456,10 @@ status_request(struct view *view, enum request request, struct line *line)
        case REQ_EDIT:
                if (!status)
                        return request;
+               if (status->status == 'D') {
+                       report("File has been deleted.");
+                       return REQ_NONE;
+               }
 
                open_editor(status->status != '?', status->new.name);
                break;
@@ -4463,8 +4496,8 @@ status_select(struct view *view, struct line *line)
 {
        struct status *status = line->data;
        char file[SIZEOF_STR] = "all files";
-       char *text;
-       char *key;
+       const char *text;
+       const char *key;
 
        if (status && !string_format(file, "'%s'", status->new.name))
                return;
@@ -4517,7 +4550,7 @@ status_grep(struct view *view, struct line *line)
                return FALSE;
 
        for (state = S_STATUS; state < S_END; state++) {
-               char *text;
+               const char *text;
 
                switch (state) {
                case S_NAME:    text = status->new.name;        break;
@@ -4551,7 +4584,7 @@ static struct view_ops status_ops = {
 static bool
 stage_diff_line(FILE *pipe, struct line *line)
 {
-       char *buf = line->data;
+       const char *buf = line->data;
        size_t bufsize = strlen(buf);
        size_t written = 0;
 
@@ -4629,7 +4662,7 @@ stage_update(struct view *view, struct line *line)
 {
        struct line *chunk = NULL;
 
-       if (!opt_no_head && stage_line_type != LINE_STAT_UNTRACKED)
+       if (!is_initial_commit() && stage_line_type != LINE_STAT_UNTRACKED)
                chunk = stage_diff_find(view, line, LINE_DIFF_CHUNK);
 
        if (chunk) {
@@ -4663,7 +4696,7 @@ stage_revert(struct view *view, struct line *line)
 {
        struct line *chunk = NULL;
 
-       if (!opt_no_head && stage_line_type == LINE_STAT_UNSTAGED)
+       if (!is_initial_commit() && stage_line_type == LINE_STAT_UNSTAGED)
                chunk = stage_diff_find(view, line, LINE_DIFF_CHUNK);
 
        if (chunk) {
@@ -4745,6 +4778,10 @@ stage_request(struct view *view, enum request request, struct line *line)
        case REQ_EDIT:
                if (!stage_status.new.name[0])
                        return request;
+               if (stage_status.status == 'D') {
+                       report("File has been deleted.");
+                       return REQ_NONE;
+               }
 
                open_editor(stage_status.status != '?', stage_status.new.name);
                break;
@@ -4774,8 +4811,14 @@ stage_request(struct view *view, enum request request, struct line *line)
        if (!status_exists(&stage_status, stage_line_type))
                return REQ_VIEW_CLOSE;
 
-       if (stage_line_type == LINE_STAT_UNTRACKED)
+       if (stage_line_type == LINE_STAT_UNTRACKED) {
+               if (!suffixcmp(stage_status.new.name, -1, "/")) {
+                       report("Cannot display a directory");
+                       return REQ_NONE;
+               }
+
                opt_pipe = fopen(stage_status.new.name, "r");
+       }
        open_view(view, REQ_VIEW_STAGE, OPEN_REFRESH);
 
        return REQ_NONE;
@@ -4874,7 +4917,7 @@ done_rev_graph(struct rev_graph *graph)
 }
 
 static void
-push_rev_graph(struct rev_graph *graph, char *parent)
+push_rev_graph(struct rev_graph *graph, const char *parent)
 {
        int i;
 
@@ -5531,13 +5574,13 @@ init_display(void)
        /* Initialize the curses library */
        if (isatty(STDIN_FILENO)) {
                cursed = !!initscr();
+               opt_tty = stdin;
        } else {
                /* Leave stdin and stdout alone when acting as a pager. */
-               FILE *io = fopen("/dev/tty", "r+");
-
-               if (!io)
+               opt_tty = fopen("/dev/tty", "r+");
+               if (!opt_tty)
                        die("Failed to open /dev/tty");
-               cursed = !!newterm(NULL, io, io);
+               cursed = !!newterm(NULL, opt_tty, opt_tty);
        }
 
        if (!cursed)
@@ -5698,8 +5741,27 @@ static struct ref ***id_refs = NULL;
 static size_t id_refs_alloc = 0;
 static size_t id_refs_size = 0;
 
+static int
+compare_refs(const void *ref1_, const void *ref2_)
+{
+       const struct ref *ref1 = *(const struct ref **)ref1_;
+       const struct ref *ref2 = *(const struct ref **)ref2_;
+
+       if (ref1->tag != ref2->tag)
+               return ref2->tag - ref1->tag;
+       if (ref1->ltag != ref2->ltag)
+               return ref2->ltag - ref2->ltag;
+       if (ref1->head != ref2->head)
+               return ref2->head - ref1->head;
+       if (ref1->tracked != ref2->tracked)
+               return ref2->tracked - ref1->tracked;
+       if (ref1->remote != ref2->remote)
+               return ref2->remote - ref1->remote;
+       return strcmp(ref1->name, ref2->name);
+}
+
 static struct ref **
-get_refs(char *id)
+get_refs(const char *id)
 {
        struct ref ***tmp_id_refs;
        struct ref **ref_list = NULL;
@@ -5733,19 +5795,20 @@ get_refs(char *id)
                }
 
                ref_list = tmp;
-               if (ref_list_size > 0)
-                       ref_list[ref_list_size - 1]->next = 1;
                ref_list[ref_list_size] = &refs[i];
-
                /* XXX: The properties of the commit chains ensures that we can
                 * safely modify the shared ref. The repo references will
                 * always be similar for the same id. */
-               ref_list[ref_list_size]->next = 0;
+               ref_list[ref_list_size]->next = 1;
+
                ref_list_size++;
        }
 
-       if (ref_list)
+       if (ref_list) {
+               qsort(ref_list, ref_list_size, sizeof(*ref_list), compare_refs);
+               ref_list[ref_list_size - 1]->next = 0;
                id_refs[id_refs_size++] = ref_list;
+       }
 
        return ref_list;
 }
@@ -5761,8 +5824,8 @@ read_ref(char *id, size_t idlen, char *name, size_t namelen)
        bool check_replace = FALSE;
        bool head = FALSE;
 
-       if (!strncmp(name, "refs/tags/", STRING_SIZE("refs/tags/"))) {
-               if (!strcmp(name + namelen - 3, "^{}")) {
+       if (!prefixcmp(name, "refs/tags/")) {
+               if (!suffixcmp(name, namelen, "^{}")) {
                        namelen -= 3;
                        name[namelen] = 0;
                        if (refs_size > 0 && refs[refs_size - 1].ltag == TRUE)
@@ -5775,26 +5838,26 @@ read_ref(char *id, size_t idlen, char *name, size_t namelen)
                namelen -= STRING_SIZE("refs/tags/");
                name    += STRING_SIZE("refs/tags/");
 
-       } else if (!strncmp(name, "refs/remotes/", STRING_SIZE("refs/remotes/"))) {
+       } else if (!prefixcmp(name, "refs/remotes/")) {
                remote = TRUE;
                namelen -= STRING_SIZE("refs/remotes/");
                name    += STRING_SIZE("refs/remotes/");
                tracked  = !strcmp(opt_remote, name);
 
-       } else if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) {
+       } else if (!prefixcmp(name, "refs/heads/")) {
                namelen -= STRING_SIZE("refs/heads/");
                name    += STRING_SIZE("refs/heads/");
                head     = !strncmp(opt_head, name, namelen);
 
        } else if (!strcmp(name, "HEAD")) {
-               opt_no_head = FALSE;
+               string_ncopy(opt_head_rev, id, idlen);
                return OK;
        }
 
        if (check_replace && !strcmp(name, refs[refs_size - 1].name)) {
                /* it's an annotated tag, replace the previous sha1 with the
                 * resolved commit id; relies on the fact git-ls-remote lists
-                * the commit id of an annotated tag right beofre the commit id
+                * the commit id of an annotated tag right before the commit id
                 * it points to. */
                refs[refs_size - 1].ltag = ltag;
                string_copy_rev(refs[refs_size - 1].id, id);
@@ -5861,7 +5924,7 @@ read_repo_config_option(char *name, size_t namelen, char *value, size_t valuelen
            !strcmp(name + 7 + strlen(opt_head), ".merge")) {
                size_t from = strlen(opt_remote);
 
-               if (!strncmp(value, "refs/heads/", STRING_SIZE("refs/heads/"))) {
+               if (!prefixcmp(value, "refs/heads/")) {
                        value += STRING_SIZE("refs/heads/");
                        valuelen -= STRING_SIZE("refs/heads/");
                }
@@ -5897,7 +5960,7 @@ read_repo_info(char *name, size_t namelen, char *value, size_t valuelen)
        } else if (opt_cdup[0] == ' ') {
                string_ncopy(opt_cdup, name, namelen);
        } else {
-               if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) {
+               if (!prefixcmp(name, "refs/heads/")) {
                        namelen -= STRING_SIZE("refs/heads/");
                        name    += STRING_SIZE("refs/heads/");
                        string_ncopy(opt_head, name, namelen);
@@ -6009,7 +6072,7 @@ warn(const char *msg, ...)
 }
 
 int
-main(int argc, char *argv[])
+main(int argc, const char *argv[])
 {
        struct view *view;
        enum request request;
@@ -6052,7 +6115,7 @@ main(int argc, char *argv[])
        if (load_refs() == ERR)
                die("Failed to load refs.");
 
-       for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++)
+       foreach_view (view, i)
                view->cmd_env = getenv(view->cmd_env);
 
        init_display();
@@ -6063,6 +6126,7 @@ main(int argc, char *argv[])
 
                foreach_view (view, i)
                        update_view(view);
+               view = display[current_view];
 
                /* Refresh, accept single keystroke of input */
                key = wgetch(status_win);
@@ -6074,7 +6138,7 @@ main(int argc, char *argv[])
                        continue;
                }
 
-               request = get_keybinding(display[current_view]->keymap, key);
+               request = get_keybinding(view->keymap, key);
 
                /* Some low-level request handling. This keeps access to
                 * status_win restricted. */
@@ -6100,8 +6164,7 @@ main(int argc, char *argv[])
                case REQ_SEARCH:
                case REQ_SEARCH_BACK:
                {
-                       const char *prompt = request == REQ_SEARCH
-                                          ? "/" : "?";
+                       const char *prompt = request == REQ_SEARCH ? "/" : "?";
                        char *search = read_prompt(prompt);
 
                        if (search)