+static void
+pager_select(struct view *view, struct line *line)
+{
+ if (line->type == LINE_COMMIT) {
+ char *text = (char *)line->data + STRING_SIZE("commit ");
+
+ if (view != VIEW(REQ_VIEW_PAGER))
+ string_copy_rev(view->ref, text);
+ string_copy_rev(ref_commit, text);
+ }
+}
+
+static struct view_ops pager_ops = {
+ "line",
+ NULL,
+ pager_read,
+ pager_draw,
+ pager_request,
+ pager_grep,
+ pager_select,
+};
+
+
+/*
+ * Help backend
+ */
+
+static bool
+help_open(struct view *view)
+{
+ char buf[BUFSIZ];
+ int lines = ARRAY_SIZE(req_info) + 2;
+ int i;
+
+ if (view->lines > 0)
+ return TRUE;
+
+ for (i = 0; i < ARRAY_SIZE(req_info); i++)
+ if (!req_info[i].request)
+ lines++;
+
+ lines += run_requests + 1;
+
+ view->line = calloc(lines, sizeof(*view->line));
+ if (!view->line)
+ return FALSE;
+
+ add_line_text(view, "Quick reference for tig keybindings:", LINE_DEFAULT);
+
+ for (i = 0; i < ARRAY_SIZE(req_info); i++) {
+ char *key;
+
+ if (req_info[i].request == REQ_NONE)
+ continue;
+
+ if (!req_info[i].request) {
+ add_line_text(view, "", LINE_DEFAULT);
+ add_line_text(view, req_info[i].help, LINE_DEFAULT);
+ continue;
+ }
+
+ key = get_key(req_info[i].request);
+ if (!*key)
+ key = "(no key defined)";
+
+ if (!string_format(buf, " %-25s %s", key, req_info[i].help))
+ continue;
+
+ add_line_text(view, buf, LINE_DEFAULT);
+ }
+
+ if (run_requests) {
+ add_line_text(view, "", LINE_DEFAULT);
+ add_line_text(view, "External commands:", LINE_DEFAULT);
+ }
+
+ for (i = 0; i < run_requests; i++) {
+ struct run_request *req = get_run_request(REQ_NONE + i + 1);
+ char *key;
+
+ if (!req)
+ continue;
+
+ key = get_key_name(req->key);
+ if (!*key)
+ key = "(no key defined)";
+
+ if (!string_format(buf, " %-10s %-14s `%s`",
+ keymap_table[req->keymap].name,
+ key, req->cmd))
+ continue;
+
+ add_line_text(view, buf, LINE_DEFAULT);
+ }
+
+ return TRUE;
+}
+
+static struct view_ops help_ops = {
+ "line",
+ help_open,
+ NULL,
+ pager_draw,
+ pager_request,
+ pager_grep,
+ pager_select,
+};
+
+
+/*
+ * Tree backend
+ */
+
+struct tree_stack_entry {
+ struct tree_stack_entry *prev; /* Entry below this in the stack */
+ unsigned long lineno; /* Line number to restore */
+ char *name; /* Position of name in opt_path */
+};
+
+/* The top of the path stack. */
+static struct tree_stack_entry *tree_stack = NULL;
+unsigned long tree_lineno = 0;
+
+static void
+pop_tree_stack_entry(void)
+{
+ struct tree_stack_entry *entry = tree_stack;
+
+ tree_lineno = entry->lineno;
+ entry->name[0] = 0;
+ tree_stack = entry->prev;
+ free(entry);
+}
+
+static void
+push_tree_stack_entry(char *name, unsigned long lineno)
+{
+ struct tree_stack_entry *entry = calloc(1, sizeof(*entry));
+ size_t pathlen = strlen(opt_path);
+
+ if (!entry)
+ return;
+
+ entry->prev = tree_stack;
+ entry->name = opt_path + pathlen;
+ tree_stack = entry;
+
+ if (!string_format_from(opt_path, &pathlen, "%s/", name)) {
+ pop_tree_stack_entry();
+ return;
+ }
+
+ /* Move the current line to the first tree entry. */
+ tree_lineno = 1;
+ entry->lineno = lineno;
+}
+
+/* Parse output from git-ls-tree(1):
+ *
+ * 100644 blob fb0e31ea6cc679b7379631188190e975f5789c26 Makefile
+ * 100644 blob 5304ca4260aaddaee6498f9630e7d471b8591ea6 README
+ * 100644 blob f931e1d229c3e185caad4449bf5b66ed72462657 tig.c
+ * 100644 blob ed09fe897f3c7c9af90bcf80cae92558ea88ae38 web.conf
+ */
+
+#define SIZEOF_TREE_ATTR \
+ STRING_SIZE("100644 blob ed09fe897f3c7c9af90bcf80cae92558ea88ae38\t")
+
+#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)
+{
+ if (type1 != type2) {
+ if (type1 == LINE_TREE_DIR)
+ return -1;
+ return 1;
+ }
+
+ return strcmp(name1, name2);
+}
+
+static char *
+tree_path(struct line *line)
+{
+ char *path = line->data;
+
+ return path + SIZEOF_TREE_ATTR;
+}
+
+static bool
+tree_read(struct view *view, char *text)
+{
+ size_t textlen = text ? strlen(text) : 0;
+ char buf[SIZEOF_STR];
+ unsigned long pos;
+ enum line_type type;
+ bool first_read = view->lines == 0;
+
+ if (!text)
+ return TRUE;
+ if (textlen <= SIZEOF_TREE_ATTR)
+ return FALSE;
+
+ type = text[STRING_SIZE("100644 ")] == 't'
+ ? LINE_TREE_DIR : LINE_TREE_FILE;
+
+ if (first_read) {
+ /* Add path info line */
+ if (!string_format(buf, "Directory path /%s", opt_path) ||
+ !realloc_lines(view, view->line_size + 1) ||
+ !add_line_text(view, buf, LINE_DEFAULT))
+ return FALSE;
+
+ /* Insert "link" to parent directory. */
+ if (*opt_path) {
+ if (!string_format(buf, TREE_UP_FORMAT, view->ref) ||
+ !realloc_lines(view, view->line_size + 1) ||
+ !add_line_text(view, buf, LINE_TREE_DIR))
+ return FALSE;
+ }
+ }
+
+ /* Strip the path part ... */
+ if (*opt_path) {
+ size_t pathlen = textlen - SIZEOF_TREE_ATTR;
+ size_t striplen = strlen(opt_path);
+ char *path = text + SIZEOF_TREE_ATTR;
+
+ if (pathlen > striplen)
+ memmove(path, path + striplen,
+ pathlen - striplen + 1);
+ }
+
+ /* Skip "Directory ..." and ".." line. */
+ for (pos = 1 + !!*opt_path; pos < view->lines; pos++) {
+ struct line *line = &view->line[pos];
+ char *path1 = tree_path(line);
+ char *path2 = text + SIZEOF_TREE_ATTR;
+ int cmp = tree_compare_entry(line->type, path1, type, path2);
+
+ if (cmp <= 0)
+ continue;
+
+ text = strdup(text);
+ if (!text)
+ return FALSE;
+
+ if (view->lines > pos)
+ memmove(&view->line[pos + 1], &view->line[pos],
+ (view->lines - pos) * sizeof(*line));
+
+ line = &view->line[pos];
+ line->data = text;
+ line->type = type;
+ view->lines++;
+ return TRUE;
+ }
+
+ if (!add_line_text(view, text, type))
+ return FALSE;
+
+ if (tree_lineno > view->lineno) {
+ view->lineno = tree_lineno;
+ tree_lineno = 0;
+ }
+
+ return TRUE;
+}
+
+static enum request
+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);
+ 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];
+ } else {
+ /* quit view if at top of tree */
+ return REQ_VIEW_CLOSE;
+ }
+ }
+ if (request != REQ_ENTER)
+ return request;
+
+ /* Cleanup the stack if the tree view is at a different tree. */
+ while (!*opt_path && tree_stack)
+ pop_tree_stack_entry();
+
+ switch (line->type) {
+ case LINE_TREE_DIR:
+ /* Depending on whether it is a subdir or parent (updir?) link
+ * mangle the path buffer. */
+ if (line == &view->line[1] && *opt_path) {
+ pop_tree_stack_entry();
+
+ } else {
+ char *basename = tree_path(line);
+
+ push_tree_stack_entry(basename, view->lineno);
+ }
+
+ /* Trees and subtrees share the same ID, so they are not not
+ * unique like blobs. */
+ flags = OPEN_RELOAD;
+ request = REQ_VIEW_TREE;
+ break;
+
+ case LINE_TREE_FILE:
+ flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT;
+ request = REQ_VIEW_BLOB;
+ break;
+
+ default:
+ return TRUE;
+ }
+
+ open_view(view, request, flags);
+ if (request == REQ_VIEW_TREE) {
+ view->lineno = tree_lineno;
+ }
+
+ return REQ_NONE;
+}
+
+static void
+tree_select(struct view *view, struct line *line)
+{
+ char *text = (char *)line->data + STRING_SIZE("100644 blob ");
+
+ if (line->type == LINE_TREE_FILE) {
+ string_copy_rev(ref_blob, text);
+
+ } else if (line->type != LINE_TREE_DIR) {
+ return;
+ }
+
+ string_copy_rev(view->ref, text);
+}
+
+static struct view_ops tree_ops = {
+ "file",
+ NULL,
+ tree_read,
+ pager_draw,
+ tree_request,
+ pager_grep,
+ tree_select,
+};
+
+static bool
+blob_read(struct view *view, char *line)
+{
+ if (!line)
+ return TRUE;
+ return add_line_text(view, line, LINE_DEFAULT) != NULL;
+}
+
+static struct view_ops blob_ops = {
+ "line",
+ NULL,
+ blob_read,
+ pager_draw,
+ pager_request,
+ pager_grep,
+ pager_select,
+};
+
+/*
+ * Blame backend
+ *
+ * Loading the blame view is a two phase job:
+ *
+ * 1. File content is read either using opt_file from the
+ * filesystem or using git-cat-file.
+ * 2. Then blame information is incrementally added by
+ * reading output from git-blame.
+ */
+
+struct blame_commit {
+ char id[SIZEOF_REV]; /* SHA1 ID. */
+ char title[128]; /* First line of the commit message. */
+ char author[75]; /* Author of the commit. */
+ struct tm time; /* Date from the author ident. */
+ char filename[128]; /* Name of file. */
+};
+
+struct blame {
+ struct blame_commit *commit;
+ unsigned int header:1;
+ char text[1];
+};
+
+#define BLAME_CAT_FILE_CMD "git cat-file blob %s:%s"
+#define BLAME_INCREMENTAL_CMD "git blame --incremental %s %s"
+
+static bool
+blame_open(struct view *view)
+{
+ char path[SIZEOF_STR];
+ char ref[SIZEOF_STR] = "";
+
+ if (sq_quote(path, 0, opt_file) >= sizeof(path))
+ return FALSE;
+
+ if (*opt_ref && sq_quote(ref, 0, opt_ref) >= sizeof(ref))
+ return FALSE;
+
+ if (*opt_ref) {
+ if (!string_format(view->cmd, BLAME_CAT_FILE_CMD, ref, path))
+ return FALSE;
+ } else {
+ view->pipe = fopen(opt_file, "r");
+ if (!view->pipe &&
+ !string_format(view->cmd, BLAME_CAT_FILE_CMD, "HEAD", path))
+ return FALSE;
+ }
+
+ if (!view->pipe)
+ view->pipe = popen(view->cmd, "r");
+ if (!view->pipe)
+ return FALSE;
+
+ if (!string_format(view->cmd, BLAME_INCREMENTAL_CMD, ref, path))
+ return FALSE;
+
+ string_format(view->ref, "%s ...", opt_file);
+ string_copy_rev(view->vid, opt_file);
+ set_nonblocking_input(TRUE);
+
+ if (view->line) {
+ int i;
+
+ for (i = 0; i < view->lines; i++)
+ free(view->line[i].data);
+ free(view->line);
+ }
+
+ view->lines = view->line_alloc = view->line_size = view->lineno = 0;
+ view->offset = view->lines = view->lineno = 0;
+ view->line = NULL;
+ view->start_time = time(NULL);
+
+ return TRUE;
+}
+
+static struct blame_commit *
+get_blame_commit(struct view *view, const char *id)
+{
+ size_t i;
+
+ for (i = 0; i < view->lines; i++) {
+ struct blame *blame = view->line[i].data;
+
+ if (!blame->commit)
+ continue;
+
+ if (!strncmp(blame->commit->id, id, SIZEOF_REV - 1))
+ return blame->commit;
+ }
+
+ {
+ struct blame_commit *commit = calloc(1, sizeof(*commit));
+
+ if (commit)
+ string_ncopy(commit->id, id, SIZEOF_REV);
+ return commit;
+ }
+}
+
+static bool
+parse_number(char **posref, size_t *number, size_t min, size_t max)
+{
+ char *pos = *posref;
+
+ *posref = NULL;
+ pos = strchr(pos + 1, ' ');
+ if (!pos || !isdigit(pos[1]))
+ return FALSE;
+ *number = atoi(pos + 1);
+ if (*number < min || *number > max)
+ return FALSE;
+
+ *posref = pos;
+ return TRUE;
+}
+
+static struct blame_commit *
+parse_blame_commit(struct view *view, char *text, int *blamed)
+{
+ struct blame_commit *commit;
+ struct blame *blame;
+ char *pos = text + SIZEOF_REV - 1;
+ size_t lineno;
+ size_t group;
+
+ if (strlen(text) <= SIZEOF_REV || *pos != ' ')
+ return NULL;
+
+ if (!parse_number(&pos, &lineno, 1, view->lines) ||
+ !parse_number(&pos, &group, 1, view->lines - lineno + 1))
+ return NULL;
+
+ commit = get_blame_commit(view, text);
+ if (!commit)
+ return NULL;
+
+ *blamed += group;
+ while (group--) {
+ struct line *line = &view->line[lineno + group - 1];
+
+ blame = line->data;
+ blame->commit = commit;
+ blame->header = !group;
+ line->dirty = 1;
+ }
+
+ return commit;
+}
+
+static bool
+blame_read_file(struct view *view, char *line)
+{
+ if (!line) {
+ FILE *pipe = NULL;
+
+ if (view->lines > 0)
+ pipe = popen(view->cmd, "r");
+ view->cmd[0] = 0;
+ if (!pipe) {
+ report("Failed to load blame data");
+ return TRUE;
+ }
+
+ fclose(view->pipe);
+ view->pipe = pipe;
+ return FALSE;
+
+ } else {
+ size_t linelen = strlen(line);
+ struct blame *blame = malloc(sizeof(*blame) + linelen);
+
+ if (!line)
+ return FALSE;
+
+ blame->commit = NULL;
+ strncpy(blame->text, line, linelen);
+ blame->text[linelen] = 0;
+ return add_line_data(view, blame, LINE_BLAME_COMMIT) != NULL;
+ }
+}
+
+static bool
+match_blame_header(const char *name, char **line)
+{
+ size_t namelen = strlen(name);
+ bool matched = !strncmp(name, *line, namelen);
+
+ if (matched)
+ *line += namelen;
+
+ return matched;
+}
+
+static bool
+blame_read(struct view *view, char *line)
+{
+ static struct blame_commit *commit = NULL;
+ static int blamed = 0;
+ static time_t author_time;
+
+ if (*view->cmd)
+ return blame_read_file(view, line);
+
+ if (!line) {
+ /* Reset all! */
+ commit = NULL;
+ blamed = 0;
+ string_format(view->ref, "%s", view->vid);
+ if (view_is_displayed(view)) {
+ update_view_title(view);
+ redraw_view_from(view, 0);
+ }
+ return TRUE;
+ }
+
+ if (!commit) {
+ commit = parse_blame_commit(view, line, &blamed);
+ string_format(view->ref, "%s %2d%%", view->vid,
+ blamed * 100 / view->lines);
+
+ } else if (match_blame_header("author ", &line)) {
+ string_ncopy(commit->author, line, strlen(line));
+
+ } else if (match_blame_header("author-time ", &line)) {
+ author_time = (time_t) atol(line);
+
+ } else if (match_blame_header("author-tz ", &line)) {
+ long tz;
+
+ tz = ('0' - line[1]) * 60 * 60 * 10;
+ tz += ('0' - line[2]) * 60 * 60;
+ tz += ('0' - line[3]) * 60;
+ tz += ('0' - line[4]) * 60;
+
+ if (line[0] == '-')
+ tz = -tz;
+
+ author_time -= tz;
+ gmtime_r(&author_time, &commit->time);
+
+ } else if (match_blame_header("summary ", &line)) {
+ string_ncopy(commit->title, line, strlen(line));
+
+ } else if (match_blame_header("filename ", &line)) {
+ string_ncopy(commit->filename, line, strlen(line));
+ commit = NULL;
+ }
+
+ return TRUE;
+}
+
+static bool
+blame_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
+{
+ struct blame *blame = line->data;
+ int col = 0;
+
+ wmove(view->win, lineno, 0);
+
+ if (selected) {
+ wattrset(view->win, get_line_attr(LINE_CURSOR));
+ wchgat(view->win, -1, 0, LINE_CURSOR, NULL);
+ } else {
+ wattrset(view->win, A_NORMAL);
+ }
+
+ if (opt_date) {
+ int n;
+
+ if (!selected)
+ wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
+ if (blame->commit) {
+ char buf[DATE_COLS + 1];
+ int timelen;
+
+ timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &blame->commit->time);
+ n = draw_text(view, buf, view->width - col, FALSE, selected);
+ draw_text(view, " ", view->width - col - n, FALSE, selected);
+ }
+
+ col += DATE_COLS;
+ wmove(view->win, lineno, col);
+ if (col >= view->width)
+ return TRUE;
+ }
+
+ if (opt_author) {
+ int max = MIN(AUTHOR_COLS - 1, view->width - col);
+
+ if (!selected)
+ wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
+ if (blame->commit)
+ draw_text(view, blame->commit->author, max, TRUE, selected);
+ col += AUTHOR_COLS;
+ if (col >= view->width)
+ return TRUE;
+ wmove(view->win, lineno, col);
+ }
+
+ {
+ int max = MIN(ID_COLS - 1, view->width - col);
+
+ if (!selected)
+ wattrset(view->win, get_line_attr(LINE_BLAME_ID));
+ if (blame->commit)
+ draw_text(view, blame->commit->id, max, FALSE, -1);
+ col += ID_COLS;
+ if (col >= view->width)
+ return TRUE;
+ wmove(view->win, lineno, col);
+ }
+
+ {
+ if (!selected)
+ wattrset(view->win, get_line_attr(LINE_BLAME_LINENO));
+ col += draw_lineno(view, lineno, view->width - col, selected);
+ if (col >= view->width)
+ return TRUE;
+ }
+
+ col += draw_text(view, blame->text, view->width - col, TRUE, selected);
+
+ return TRUE;
+}
+
+static enum request
+blame_request(struct view *view, enum request request, struct line *line)
+{
+ enum open_flags flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT;
+ struct blame *blame = line->data;
+
+ switch (request) {
+ case REQ_ENTER:
+ if (!blame->commit) {
+ report("No commit loaded yet");
+ break;
+ }
+
+ if (!strcmp(blame->commit->id, NULL_ID)) {
+ char path[SIZEOF_STR];
+
+ if (sq_quote(path, 0, view->vid) >= sizeof(path))
+ break;
+ string_format(opt_cmd, "git diff-index --root --patch-with-stat -C -M --cached HEAD -- %s 2>/dev/null", path);
+ }
+
+ open_view(view, REQ_VIEW_DIFF, flags);
+ break;
+
+ default:
+ return request;
+ }
+
+ return REQ_NONE;
+}
+
+static bool
+blame_grep(struct view *view, struct line *line)
+{
+ struct blame *blame = line->data;
+ struct blame_commit *commit = blame->commit;
+ regmatch_t pmatch;
+
+#define MATCH(text) \
+ (*text && regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH)
+
+ if (commit) {
+ char buf[DATE_COLS + 1];
+
+ if (MATCH(commit->title) ||
+ MATCH(commit->author) ||
+ MATCH(commit->id))
+ return TRUE;
+
+ if (strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time) &&
+ MATCH(buf))
+ return TRUE;
+ }
+
+ return MATCH(blame->text);
+
+#undef MATCH
+}
+
+static void
+blame_select(struct view *view, struct line *line)
+{
+ struct blame *blame = line->data;
+ struct blame_commit *commit = blame->commit;
+
+ if (!commit)
+ return;
+
+ if (!strcmp(commit->id, NULL_ID))
+ string_ncopy(ref_commit, "HEAD", 4);
+ else
+ string_copy_rev(ref_commit, commit->id);
+}
+
+static struct view_ops blame_ops = {
+ "line",
+ blame_open,
+ blame_read,
+ blame_draw,
+ blame_request,
+ blame_grep,
+ blame_select,
+};
+
+/*
+ * Status backend
+ */
+
+struct status {
+ char status;
+ struct {
+ mode_t mode;
+ char rev[SIZEOF_REV];
+ char name[SIZEOF_STR];
+ } old;
+ struct {
+ mode_t mode;
+ char rev[SIZEOF_REV];
+ char name[SIZEOF_STR];
+ } new;
+};
+
+static char status_onbranch[SIZEOF_STR];
+static struct status stage_status;
+static enum line_type stage_line_type;
+
+/* Get fields from the diff line:
+ * :100644 100644 06a5d6ae9eca55be2e0e585a152e6b1336f2b20e 0000000000000000000000000000000000000000 M
+ */
+static inline bool
+status_get_diff(struct status *file, 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;
+
+ if (bufsize < 99 ||
+ old_mode[-1] != ':' ||
+ new_mode[-1] != ' ' ||
+ old_rev[-1] != ' ' ||
+ new_rev[-1] != ' ' ||
+ status[-1] != ' ')
+ return FALSE;
+
+ file->status = *status;
+
+ string_copy_rev(file->old.rev, old_rev);
+ string_copy_rev(file->new.rev, new_rev);
+
+ file->old.mode = strtoul(old_mode, NULL, 8);
+ file->new.mode = strtoul(new_mode, NULL, 8);
+
+ file->old.name[0] = file->new.name[0] = 0;
+
+ return TRUE;
+}
+
+static bool
+status_run(struct view *view, const char cmd[], char status, enum line_type type)
+{
+ struct status *file = NULL;
+ struct status *unmerged = NULL;
+ char buf[SIZEOF_STR * 4];
+ size_t bufsize = 0;
+ FILE *pipe;
+
+ pipe = popen(cmd, "r");
+ if (!pipe)
+ return FALSE;
+
+ add_line_data(view, NULL, type);
+
+ while (!feof(pipe) && !ferror(pipe)) {
+ char *sep;
+ size_t readsize;
+
+ readsize = fread(buf + bufsize, 1, sizeof(buf) - bufsize, pipe);
+ if (!readsize)
+ break;
+ bufsize += readsize;
+
+ /* Process while we have NUL chars. */
+ while ((sep = memchr(buf, 0, bufsize))) {
+ size_t sepsize = sep - buf + 1;
+
+ if (!file) {
+ if (!realloc_lines(view, view->line_size + 1))
+ goto error_out;
+
+ file = calloc(1, sizeof(*file));
+ if (!file)
+ goto error_out;
+
+ add_line_data(view, file, type);
+ }
+
+ /* Parse diff info part. */
+ if (status) {
+ file->status = status;
+ if (status == 'A')
+ string_copy(file->old.rev, NULL_ID);
+
+ } else if (!file->status) {
+ if (!status_get_diff(file, buf, sepsize))
+ goto error_out;
+
+ bufsize -= sepsize;
+ memmove(buf, sep + 1, bufsize);
+
+ sep = memchr(buf, 0, bufsize);
+ if (!sep)
+ break;
+ sepsize = sep - buf + 1;
+
+ /* Collapse all 'M'odified entries that
+ * follow a associated 'U'nmerged entry.
+ */
+ if (file->status == 'U') {
+ unmerged = file;
+
+ } else if (unmerged) {
+ int collapse = !strcmp(buf, unmerged->new.name);
+
+ unmerged = NULL;
+ if (collapse) {
+ free(file);
+ view->lines--;
+ continue;
+ }
+ }
+ }
+
+ /* Grab the old name for rename/copy. */
+ if (!*file->old.name &&
+ (file->status == 'R' || file->status == 'C')) {
+ sepsize = sep - buf + 1;
+ string_ncopy(file->old.name, buf, sepsize);
+ bufsize -= sepsize;
+ memmove(buf, sep + 1, bufsize);
+
+ sep = memchr(buf, 0, bufsize);
+ if (!sep)
+ break;
+ sepsize = sep - buf + 1;
+ }
+
+ /* git-ls-files just delivers a NUL separated
+ * list of file names similar to the second half
+ * of the git-diff-* output. */
+ string_ncopy(file->new.name, buf, sepsize);
+ if (!*file->old.name)
+ string_copy(file->old.name, file->new.name);
+ bufsize -= sepsize;
+ memmove(buf, sep + 1, bufsize);
+ file = NULL;
+ }
+ }
+
+ if (ferror(pipe)) {
+error_out:
+ pclose(pipe);
+ return FALSE;
+ }
+
+ if (!view->line[view->lines - 1].data)
+ add_line_data(view, NULL, LINE_STAT_NONE);
+
+ pclose(pipe);
+ return TRUE;
+}
+
+/* Don't show unmerged entries in the staged section. */
+#define STATUS_DIFF_INDEX_CMD "git diff-index -z --diff-filter=ACDMRTXB --cached -M HEAD"
+#define STATUS_DIFF_FILES_CMD "git diff-files -z"
+#define STATUS_LIST_OTHER_CMD \
+ "git ls-files -z --others --exclude-per-directory=.gitignore"
+#define STATUS_LIST_NO_HEAD_CMD \
+ "git ls-files -z --cached --exclude-per-directory=.gitignore"
+
+#define STATUS_DIFF_INDEX_SHOW_CMD \
+ "git diff-index --root --patch-with-stat -C -M --cached HEAD -- %s %s 2>/dev/null"
+
+#define STATUS_DIFF_FILES_SHOW_CMD \
+ "git diff-files --root --patch-with-stat -C -M -- %s %s 2>/dev/null"
+
+#define STATUS_DIFF_NO_HEAD_SHOW_CMD \
+ "git diff --no-color --patch-with-stat /dev/null %s 2>/dev/null"
+
+/* First parse staged info using git-diff-index(1), then parse unstaged
+ * info using git-diff-files(1), and finally untracked files using
+ * git-ls-files(1). */
+static bool
+status_open(struct view *view)
+{
+ struct stat statbuf;
+ char exclude[SIZEOF_STR];
+ char indexcmd[SIZEOF_STR] = STATUS_DIFF_INDEX_CMD;
+ char othercmd[SIZEOF_STR] = STATUS_LIST_OTHER_CMD;
+ unsigned long prev_lineno = view->lineno;
+ char indexstatus = 0;
+ size_t i;
+
+ for (i = 0; i < view->lines; i++)
+ free(view->line[i].data);
+ free(view->line);
+ view->lines = view->line_alloc = view->line_size = view->lineno = 0;
+ view->line = NULL;
+
+ if (!realloc_lines(view, view->line_size + 7))
+ return FALSE;
+
+ add_line_data(view, NULL, LINE_STAT_HEAD);
+ if (opt_no_head)
+ string_copy(status_onbranch, "Initial commit");
+ else if (!*opt_head)
+ string_copy(status_onbranch, "Not currently on any branch");
+ else if (!string_format(status_onbranch, "On branch %s", opt_head))
+ return FALSE;
+
+ if (opt_no_head) {
+ string_copy(indexcmd, STATUS_LIST_NO_HEAD_CMD);
+ indexstatus = 'A';
+ }
+
+ if (!string_format(exclude, "%s/info/exclude", opt_git_dir))
+ return FALSE;
+
+ if (stat(exclude, &statbuf) >= 0) {
+ size_t cmdsize = strlen(othercmd);
+
+ if (!string_format_from(othercmd, &cmdsize, " %s", "--exclude-from=") ||
+ sq_quote(othercmd, cmdsize, exclude) >= sizeof(othercmd))
+ return FALSE;
+
+ cmdsize = strlen(indexcmd);
+ if (opt_no_head &&
+ (!string_format_from(indexcmd, &cmdsize, " %s", "--exclude-from=") ||
+ sq_quote(indexcmd, cmdsize, exclude) >= sizeof(indexcmd)))
+ return FALSE;
+ }
+
+ system("git update-index -q --refresh");
+
+ if (!status_run(view, indexcmd, indexstatus, LINE_STAT_STAGED) ||
+ !status_run(view, STATUS_DIFF_FILES_CMD, 0, LINE_STAT_UNSTAGED) ||
+ !status_run(view, othercmd, '?', LINE_STAT_UNTRACKED))
+ return FALSE;
+
+ /* If all went well restore the previous line number to stay in
+ * the context or select a line with something that can be
+ * updated. */
+ if (prev_lineno >= view->lines)
+ prev_lineno = view->lines - 1;
+ while (prev_lineno < view->lines && !view->line[prev_lineno].data)
+ prev_lineno++;
+ while (prev_lineno > 0 && !view->line[prev_lineno].data)
+ prev_lineno--;
+
+ /* If the above fails, always skip the "On branch" line. */
+ if (prev_lineno < view->lines)
+ view->lineno = prev_lineno;
+ else
+ view->lineno = 1;
+
+ if (view->lineno < view->offset)
+ view->offset = view->lineno;
+ else if (view->offset + view->height <= view->lineno)
+ view->offset = view->lineno - view->height + 1;
+
+ return TRUE;
+}
+
+static bool
+status_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
+{
+ struct status *status = line->data;
+ char *text;
+ int col = 0;
+
+ wmove(view->win, lineno, 0);
+
+ if (selected) {
+ wattrset(view->win, get_line_attr(LINE_CURSOR));
+ wchgat(view->win, -1, 0, LINE_CURSOR, NULL);
+
+ } else if (line->type == LINE_STAT_HEAD) {
+ wattrset(view->win, get_line_attr(LINE_STAT_HEAD));
+ wchgat(view->win, -1, 0, LINE_STAT_HEAD, NULL);
+
+ } else if (!status && line->type != LINE_STAT_NONE) {
+ wattrset(view->win, get_line_attr(LINE_STAT_SECTION));
+ wchgat(view->win, -1, 0, LINE_STAT_SECTION, NULL);
+
+ } else {
+ wattrset(view->win, get_line_attr(line->type));
+ }
+
+ if (!status) {
+ switch (line->type) {
+ case LINE_STAT_STAGED:
+ text = "Changes to be committed:";
+ break;
+
+ case LINE_STAT_UNSTAGED:
+ text = "Changed but not updated:";
+ break;
+
+ case LINE_STAT_UNTRACKED:
+ text = "Untracked files:";
+ break;
+
+ case LINE_STAT_NONE:
+ text = " (no files)";
+ break;
+
+ case LINE_STAT_HEAD:
+ text = status_onbranch;
+ break;
+
+ default:
+ return FALSE;
+ }
+ } else {
+ char buf[] = { status->status, ' ', ' ', ' ', 0 };
+
+ col += draw_text(view, buf, view->width, TRUE, selected);
+ if (!selected)
+ wattrset(view->win, A_NORMAL);
+ text = status->new.name;
+ }
+
+ draw_text(view, text, view->width - col, TRUE, selected);
+ return TRUE;
+}
+
+static enum request
+status_enter(struct view *view, struct line *line)
+{
+ struct status *status = line->data;
+ char oldpath[SIZEOF_STR] = "";
+ char newpath[SIZEOF_STR] = "";
+ char *info;
+ size_t cmdsize = 0;
+
+ if (line->type == LINE_STAT_NONE ||
+ (!status && line[1].type == LINE_STAT_NONE)) {
+ report("No file to diff");
+ return REQ_NONE;
+ }
+
+ if (status) {
+ if (sq_quote(oldpath, 0, status->old.name) >= sizeof(oldpath))
+ return REQ_QUIT;
+ /* Diffs for unmerged entries are empty when pasing the
+ * new path, so leave it empty. */
+ if (status->status != 'U' &&
+ sq_quote(newpath, 0, status->new.name) >= sizeof(newpath))
+ return REQ_QUIT;
+ }
+
+ if (opt_cdup[0] &&
+ line->type != LINE_STAT_UNTRACKED &&
+ !string_format_from(opt_cmd, &cmdsize, "cd %s;", opt_cdup))
+ return REQ_QUIT;
+
+ switch (line->type) {
+ case LINE_STAT_STAGED:
+ if (opt_no_head) {
+ if (!string_format_from(opt_cmd, &cmdsize,
+ STATUS_DIFF_NO_HEAD_SHOW_CMD,
+ newpath))
+ return REQ_QUIT;
+ } else {
+ if (!string_format_from(opt_cmd, &cmdsize,
+ STATUS_DIFF_INDEX_SHOW_CMD,
+ oldpath, newpath))
+ return REQ_QUIT;
+ }
+
+ if (status)
+ info = "Staged changes to %s";
+ else
+ info = "Staged changes";
+ break;
+
+ case LINE_STAT_UNSTAGED:
+ if (!string_format_from(opt_cmd, &cmdsize,
+ STATUS_DIFF_FILES_SHOW_CMD, oldpath, newpath))
+ return REQ_QUIT;
+ if (status)
+ info = "Unstaged changes to %s";
+ else
+ info = "Unstaged changes";
+ break;
+
+ case LINE_STAT_UNTRACKED:
+ if (opt_pipe)
+ return REQ_QUIT;
+
+ if (!status) {
+ report("No file to show");
+ return REQ_NONE;
+ }
+
+ opt_pipe = fopen(status->new.name, "r");
+ info = "Untracked file %s";
+ break;
+
+ case LINE_STAT_HEAD:
+ return REQ_NONE;
+
+ default:
+ die("line type %d not handled in switch", line->type);
+ }
+
+ open_view(view, REQ_VIEW_STAGE, OPEN_RELOAD | OPEN_SPLIT);
+ if (view_is_displayed(VIEW(REQ_VIEW_STAGE))) {
+ if (status) {
+ stage_status = *status;
+ } else {
+ memset(&stage_status, 0, sizeof(stage_status));
+ }
+
+ stage_line_type = line->type;
+ string_format(VIEW(REQ_VIEW_STAGE)->ref, info, stage_status.new.name);
+ }
+
+ return REQ_NONE;
+}
+
+
+static FILE *
+status_update_prepare(enum line_type type)
+{
+ char cmd[SIZEOF_STR];
+ size_t cmdsize = 0;
+
+ if (opt_cdup[0] &&
+ type != LINE_STAT_UNTRACKED &&
+ !string_format_from(cmd, &cmdsize, "cd %s;", opt_cdup))
+ return NULL;
+
+ switch (type) {
+ case LINE_STAT_STAGED:
+ string_add(cmd, cmdsize, "git update-index -z --index-info");
+ break;
+
+ case LINE_STAT_UNSTAGED:
+ case LINE_STAT_UNTRACKED:
+ string_add(cmd, cmdsize, "git update-index -z --add --remove --stdin");
+ break;
+
+ default:
+ die("line type %d not handled in switch", type);
+ }
+
+ return popen(cmd, "w");
+}
+
+static bool
+status_update_write(FILE *pipe, struct status *status, enum line_type type)
+{
+ char buf[SIZEOF_STR];
+ size_t bufsize = 0;
+ size_t written = 0;
+
+ switch (type) {
+ case LINE_STAT_STAGED:
+ if (!string_format_from(buf, &bufsize, "%06o %s\t%s%c",
+ status->old.mode,
+ status->old.rev,
+ status->old.name, 0))
+ return FALSE;
+ break;
+
+ case LINE_STAT_UNSTAGED:
+ case LINE_STAT_UNTRACKED:
+ if (!string_format_from(buf, &bufsize, "%s%c", status->new.name, 0))
+ return FALSE;
+ break;
+
+ default:
+ die("line type %d not handled in switch", type);
+ }
+
+ while (!ferror(pipe) && written < bufsize) {
+ written += fwrite(buf + written, 1, bufsize - written, pipe);
+ }
+
+ return written == bufsize;
+}
+
+static bool
+status_update_file(struct status *status, enum line_type type)
+{
+ FILE *pipe = status_update_prepare(type);
+ bool result;
+
+ if (!pipe)
+ return FALSE;
+
+ result = status_update_write(pipe, status, type);
+ pclose(pipe);
+ return result;
+}
+
+static bool
+status_update_files(struct view *view, struct line *line)
+{
+ FILE *pipe = status_update_prepare(line->type);
+ bool result = TRUE;
+ struct line *pos = view->line + view->lines;
+ int files = 0;
+ int file, done;
+
+ if (!pipe)
+ return FALSE;
+
+ for (pos = line; pos < view->line + view->lines && pos->data; pos++)
+ files++;
+
+ for (file = 0, done = 0; result && file < files; line++, file++) {
+ int almost_done = file * 100 / files;
+
+ if (almost_done > done) {
+ done = almost_done;
+ string_format(view->ref, "updating file %u of %u (%d%% done)",
+ file, files, done);
+ update_view_title(view);
+ }
+ result = status_update_write(pipe, line->data, line->type);
+ }
+
+ pclose(pipe);
+ return result;
+}
+
+static bool
+status_update(struct view *view)
+{
+ struct line *line = &view->line[view->lineno];
+
+ assert(view->lines);
+
+ if (!line->data) {
+ /* This should work even for the "On branch" line. */
+ if (line < view->line + view->lines && !line[1].data) {
+ report("Nothing to update");
+ return FALSE;
+ }
+
+ if (!status_update_files(view, line + 1))
+ report("Failed to update file status");
+
+ } else if (!status_update_file(line->data, line->type)) {
+ report("Failed to update file status");
+ }
+
+ return TRUE;
+}
+
+static enum request
+status_request(struct view *view, enum request request, struct line *line)
+{
+ struct status *status = line->data;
+
+ switch (request) {
+ case REQ_STATUS_UPDATE:
+ if (!status_update(view))
+ return REQ_NONE;
+ break;
+
+ case REQ_STATUS_MERGE:
+ if (!status || status->status != 'U') {
+ report("Merging only possible for files with unmerged status ('U').");
+ return REQ_NONE;
+ }
+ open_mergetool(status->new.name);
+ break;
+
+ case REQ_EDIT:
+ if (!status)
+ return request;
+
+ open_editor(status->status != '?', status->new.name);
+ break;
+
+ case REQ_VIEW_BLAME:
+ if (status) {
+ string_copy(opt_file, status->new.name);
+ opt_ref[0] = 0;
+ }
+ return request;
+
+ case REQ_ENTER:
+ /* After returning the status view has been split to
+ * show the stage view. No further reloading is
+ * necessary. */
+ status_enter(view, line);
+ return REQ_NONE;
+
+ case REQ_REFRESH:
+ /* Simply reload the view. */
+ break;
+
+ default:
+ return request;
+ }
+
+ open_view(view, REQ_VIEW_STATUS, OPEN_RELOAD);
+
+ return REQ_NONE;
+}
+
+static void
+status_select(struct view *view, struct line *line)
+{
+ struct status *status = line->data;
+ char file[SIZEOF_STR] = "all files";
+ char *text;
+ char *key;
+
+ if (status && !string_format(file, "'%s'", status->new.name))
+ return;
+
+ if (!status && line[1].type == LINE_STAT_NONE)
+ line++;
+
+ switch (line->type) {
+ case LINE_STAT_STAGED:
+ text = "Press %s to unstage %s for commit";
+ break;
+
+ case LINE_STAT_UNSTAGED:
+ text = "Press %s to stage %s for commit";
+ break;
+
+ case LINE_STAT_UNTRACKED:
+ text = "Press %s to stage %s for addition";
+ break;
+
+ case LINE_STAT_HEAD:
+ case LINE_STAT_NONE:
+ text = "Nothing to update";
+ break;
+
+ default:
+ die("line type %d not handled in switch", line->type);
+ }
+
+ if (status && status->status == 'U') {
+ text = "Press %s to resolve conflict in %s";
+ key = get_key(REQ_STATUS_MERGE);
+
+ } else {
+ key = get_key(REQ_STATUS_UPDATE);
+ }
+
+ string_format(view->ref, text, key, file);
+}
+
+static bool
+status_grep(struct view *view, struct line *line)
+{
+ struct status *status = line->data;
+ enum { S_STATUS, S_NAME, S_END } state;
+ char buf[2] = "?";
+ regmatch_t pmatch;
+
+ if (!status)
+ return FALSE;
+
+ for (state = S_STATUS; state < S_END; state++) {
+ char *text;
+
+ switch (state) {
+ case S_NAME: text = status->new.name; break;
+ case S_STATUS:
+ buf[0] = status->status;
+ text = buf;
+ break;
+
+ default:
+ return FALSE;
+ }
+
+ if (regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH)
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static struct view_ops status_ops = {
+ "file",
+ status_open,
+ NULL,
+ status_draw,
+ status_request,
+ status_grep,
+ status_select,
+};
+
+
+static bool
+stage_diff_line(FILE *pipe, struct line *line)
+{
+ char *buf = line->data;
+ size_t bufsize = strlen(buf);
+ size_t written = 0;
+
+ while (!ferror(pipe) && written < bufsize) {
+ written += fwrite(buf + written, 1, bufsize - written, pipe);
+ }
+
+ fputc('\n', pipe);
+
+ return written == bufsize;
+}
+
+static struct line *
+stage_diff_hdr(struct view *view, struct line *line)
+{
+ int diff_hdr_dir = line->type == LINE_DIFF_CHUNK ? -1 : 1;
+ struct line *diff_hdr;
+
+ if (line->type == LINE_DIFF_CHUNK)
+ diff_hdr = line - 1;
+ else
+ diff_hdr = view->line + 1;
+
+ while (diff_hdr > view->line && diff_hdr < view->line + view->lines) {
+ if (diff_hdr->type == LINE_DIFF_HEADER)
+ return diff_hdr;
+
+ diff_hdr += diff_hdr_dir;
+ }
+
+ return NULL;
+}
+
+static bool
+stage_update_chunk(struct view *view, struct line *line)
+{
+ char cmd[SIZEOF_STR];
+ size_t cmdsize = 0;
+ struct line *diff_hdr, *diff_chunk, *diff_end;
+ FILE *pipe;
+
+ diff_hdr = stage_diff_hdr(view, line);
+ if (!diff_hdr)
+ return FALSE;
+
+ if (opt_cdup[0] &&
+ !string_format_from(cmd, &cmdsize, "cd %s;", opt_cdup))
+ return FALSE;
+
+ if (!string_format_from(cmd, &cmdsize,
+ "git apply --whitespace=nowarn --cached %s - && "
+ "git update-index -q --unmerged --refresh 2>/dev/null",
+ stage_line_type == LINE_STAT_STAGED ? "-R" : ""))
+ return FALSE;
+
+ pipe = popen(cmd, "w");
+ if (!pipe)
+ return FALSE;
+
+ diff_end = view->line + view->lines;
+ if (line->type != LINE_DIFF_CHUNK) {
+ diff_chunk = diff_hdr;
+
+ } else {
+ for (diff_chunk = line + 1; diff_chunk < diff_end; diff_chunk++)
+ if (diff_chunk->type == LINE_DIFF_CHUNK ||
+ diff_chunk->type == LINE_DIFF_HEADER)
+ diff_end = diff_chunk;
+
+ diff_chunk = line;
+
+ while (diff_hdr->type != LINE_DIFF_CHUNK) {
+ switch (diff_hdr->type) {
+ case LINE_DIFF_HEADER:
+ case LINE_DIFF_INDEX:
+ case LINE_DIFF_ADD:
+ case LINE_DIFF_DEL:
+ break;
+
+ default:
+ diff_hdr++;
+ continue;
+ }
+
+ if (!stage_diff_line(pipe, diff_hdr++)) {
+ pclose(pipe);
+ return FALSE;
+ }
+ }
+ }
+
+ while (diff_chunk < diff_end && stage_diff_line(pipe, diff_chunk))
+ diff_chunk++;
+
+ pclose(pipe);
+
+ if (diff_chunk != diff_end)
+ return FALSE;
+
+ return TRUE;
+}
+
+static void
+stage_update(struct view *view, struct line *line)
+{
+ if (!opt_no_head && stage_line_type != LINE_STAT_UNTRACKED &&
+ (line->type == LINE_DIFF_CHUNK || !stage_status.status)) {
+ if (!stage_update_chunk(view, line)) {
+ report("Failed to apply chunk");
+ return;
+ }
+
+ } else if (!status_update_file(&stage_status, stage_line_type)) {
+ report("Failed to update file");
+ return;
+ }
+
+ open_view(view, REQ_VIEW_STATUS, OPEN_RELOAD);
+
+ view = VIEW(REQ_VIEW_STATUS);
+ if (view_is_displayed(view))
+ status_enter(view, &view->line[view->lineno]);
+}
+
+static enum request
+stage_request(struct view *view, enum request request, struct line *line)
+{
+ switch (request) {
+ case REQ_STATUS_UPDATE:
+ stage_update(view, line);
+ break;
+
+ case REQ_EDIT:
+ if (!stage_status.new.name[0])
+ return request;
+
+ open_editor(stage_status.status != '?', stage_status.new.name);
+ break;
+
+ case REQ_VIEW_BLAME:
+ if (stage_status.new.name[0]) {
+ string_copy(opt_file, stage_status.new.name);
+ opt_ref[0] = 0;
+ }
+ return request;
+
+ case REQ_ENTER:
+ pager_request(view, request, line);
+ break;
+
+ default:
+ return request;
+ }
+
+ return REQ_NONE;
+}
+
+static struct view_ops stage_ops = {
+ "line",
+ NULL,
+ pager_read,
+ pager_draw,
+ stage_request,