+ pager_grep,
+};
+
+
+/*
+ * Tree backend
+ */
+
+/* Parse output from git ls-tree:
+ *
+ * 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 bool
+tree_read(struct view *view, char *text)
+{
+ size_t textlen = strlen(text);
+ char buf[SIZEOF_STR];
+ unsigned long pos;
+ enum line_type type;
+
+ if (textlen <= SIZEOF_TREE_ATTR)
+ return FALSE;
+
+ type = text[STRING_SIZE("100644 ")] == 't'
+ ? LINE_TREE_DIR : LINE_TREE_FILE;
+
+ /* The first time around ... */
+ if (!view->lines) {
+ /* Add path info line */
+ if (snprintf(buf, sizeof(buf), "Directory path /%s", opt_path) < sizeof(buf) &&
+ realloc_lines(view, view->line_size + 1) &&
+ pager_read(view, buf))
+ view->line[view->lines - 1].type = LINE_DEFAULT;
+ else
+ return FALSE;
+
+ /* Insert "link" to parent directory. */
+ if (*opt_path &&
+ snprintf(buf, sizeof(buf), TREE_UP_FORMAT, view->ref) < sizeof(buf) &&
+ realloc_lines(view, view->line_size + 1) &&
+ pager_read(view, buf))
+ view->line[view->lines - 1].type = LINE_TREE_DIR;
+ else if (*opt_path)
+ 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 = ((char *) line->data) + SIZEOF_TREE_ATTR;
+ 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 (!pager_read(view, text))
+ return FALSE;
+
+ view->line[view->lines - 1].type = type;
+ return TRUE;
+}
+
+static bool
+tree_enter(struct view *view, struct line *line)
+{
+ enum open_flags flags = OPEN_DEFAULT;
+ char *data = line->data;
+ enum request request;
+
+ 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) {
+ size_t path_len = strlen(opt_path);
+ char *dirsep = opt_path + path_len - 1;
+
+ while (dirsep > opt_path && dirsep[-1] != '/')
+ dirsep--;
+
+ dirsep[0] = 0;
+
+ } else {
+ int pathlen = strlen(opt_path);
+ char *basename = data + SIZEOF_TREE_ATTR;
+
+ string_format_from(opt_path, &pathlen, "%s/", basename);
+ }
+
+ /* 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:
+ /* This causes the blob view to become split, and not having it
+ * in the tree dir case will make the blob view automatically
+ * disappear when moving to a different directory. */
+ flags |= OPEN_SPLIT;
+ request = REQ_VIEW_BLOB;
+ break;
+
+ default:
+ return TRUE;
+ }
+
+ open_view(view, request, flags);
+
+ if (!VIEW(request)->pipe)
+ return TRUE;
+
+ /* For tree views insert the path to the parent as the first line. */
+ if (request == REQ_VIEW_BLOB) {
+ /* Mirror what is showed in the title bar. */
+ string_ncopy(ref_blob, data + STRING_SIZE("100644 blob "), 40);
+ string_copy(VIEW(REQ_VIEW_BLOB)->ref, ref_blob);
+ return TRUE;
+ }
+
+ return TRUE;
+}
+
+static struct view_ops tree_ops = {
+ "file",
+ pager_draw,
+ tree_read,
+ tree_enter,
+ pager_grep,
+};
+
+static bool
+blob_read(struct view *view, char *line)
+{
+ bool state = pager_read(view, line);
+
+ if (state == TRUE)
+ view->line[view->lines - 1].type = LINE_DEFAULT;
+
+ return state;
+}
+
+static struct view_ops blob_ops = {
+ "line",
+ pager_draw,
+ blob_read,
+ pager_enter,
+ pager_grep,