+ pager_grep,
+ pager_select,
+};
+
+
+/*
+ * 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;
+ bool first_read = view->lines == 0;
+
+ 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) &&
+ pager_read(view, buf))
+ view->line[view->lines - 1].type = LINE_DEFAULT;
+ else
+ return FALSE;
+
+ /* Insert "link" to parent directory. */
+ if (*opt_path &&
+ string_format(buf, TREE_UP_FORMAT, view->ref) &&
+ 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;
+
+ /* Move the current line to the first tree entry. */
+ if (first_read)
+ view->lineno++;
+
+ view->line[view->lines - 1].type = type;
+ return TRUE;
+}
+
+static bool
+tree_enter(struct view *view, struct line *line)
+{
+ enum open_flags flags = display[0] == view ? OPEN_SPLIT : 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 {
+ size_t pathlen = strlen(opt_path);
+ size_t origlen = pathlen;
+ char *basename = data + SIZEOF_TREE_ATTR;
+
+ if (!string_format_from(opt_path, &pathlen, "%s/", basename)) {
+ opt_path[origlen] = 0;
+ return TRUE;
+ }
+ }
+
+ /* 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:
+ request = REQ_VIEW_BLOB;
+ break;
+
+ default:
+ return TRUE;
+ }
+
+ open_view(view, request, flags);
+
+ return TRUE;
+}
+
+static void
+tree_select(struct view *view, struct line *line)
+{
+ char *text = line->data;
+
+ text += STRING_SIZE("100644 blob ");
+
+ if (line->type == LINE_TREE_FILE) {
+ string_ncopy(ref_blob, text, 40);
+ /* Also update the blob view's ref, since all there must always
+ * be in sync. */
+ string_copy(VIEW(REQ_VIEW_BLOB)->ref, ref_blob);
+
+ } else if (line->type == LINE_TREE_DIR) {
+ string_ncopy(view->ref, text, 40);
+ }
+}
+
+static struct view_ops tree_ops = {
+ "file",
+ pager_draw,
+ tree_read,
+ tree_enter,
+ pager_grep,
+ tree_select,
+};
+
+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,
+ pager_select,