Code

Move and rewrite io_format to become prepare_io
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index b11c04bb74b1fcfeb31d306874d9cc9acb24a454..86be4a2973615219fb321863eaa648e73210a8b6 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -678,6 +678,27 @@ argv_from_env(const char **argv, const char *name)
        return !env || argv_from_string(argv, &argc, env);
 }
 
+static void
+argv_free(const char *argv[])
+{
+       int argc;
+
+       for (argc = 0; argv[argc]; argc++)
+               free((void *) argv[argc]);
+       argv[0] = NULL;
+}
+
+static bool
+argv_copy(const char *dst[], const char *src[], bool allocate)
+{
+       int argc;
+
+       for (argc = 0; src[argc]; argc++)
+               if (!(dst[argc] = allocate ? strdup(src[argc]) : src[argc]))
+                       return FALSE;
+       return TRUE;
+}
+
 
 /*
  * Executing external commands.
@@ -725,12 +746,11 @@ io_init(struct io *io, const char *dir, enum io_type type)
        io->dir = dir;
 }
 
-static bool
-io_format(struct io *io, const char *dir, enum io_type type,
-         const char *argv[], enum format_flags flags)
+static void
+io_prepare(struct io *io, const char *dir, enum io_type type, const char *argv[])
 {
        io_init(io, dir, type);
-       return format_argv(io->argv, argv, flags);
+       argv_copy(io->argv, argv, FALSE);
 }
 
 static bool
@@ -850,56 +870,36 @@ io_start(struct io *io)
 static bool
 io_run(struct io *io, const char **argv, const char *dir, enum io_type type)
 {
-       io_init(io, dir, type);
-       if (!format_argv(io->argv, argv, FORMAT_NONE))
-               return FALSE;
+       io_prepare(io, dir, type, argv);
        return io_start(io);
 }
 
-static int
-io_complete(struct io *io)
-{
-       return io_start(io) && io_done(io);
-}
-
-static int
-io_run_bg(const char **argv)
+static bool
+io_complete(enum io_type type, const char **argv, const char *dir, int fd)
 {
        struct io io = {};
 
-       if (!io_format(&io, NULL, IO_BG, argv, FORMAT_NONE))
-               return FALSE;
-       return io_complete(&io);
+       io_prepare(&io, dir, type, argv);
+       io.pipe = fd;
+       return io_start(&io) && io_done(&io);
 }
 
 static bool
-io_run_fg(const char **argv, const char *dir)
+io_run_bg(const char **argv)
 {
-       struct io io = {};
-
-       if (!io_format(&io, dir, IO_FG, argv, FORMAT_NONE))
-               return FALSE;
-       return io_complete(&io);
+       return io_complete(IO_BG, argv, NULL, -1);
 }
 
 static bool
-io_run_append(const char **argv, enum format_flags flags, int fd)
+io_run_fg(const char **argv, const char *dir)
 {
-       struct io io = {};
-
-       if (!io_format(&io, NULL, IO_AP, argv, flags)) {
-               close(fd);
-               return FALSE;
-       }
-
-       io.pipe = fd;
-       return io_complete(&io);
+       return io_complete(IO_FG, argv, dir, -1);
 }
 
 static bool
-io_run_rd(struct io *io, const char **argv, const char *dir, enum format_flags flags)
+io_run_append(const char **argv, int fd)
 {
-       return io_format(io, dir, IO_RD, argv, flags) && io_start(io);
+       return io_complete(IO_AP, argv, NULL, -1);
 }
 
 static bool
@@ -1036,8 +1036,8 @@ io_run_buf(const char **argv, char buf[], size_t bufsize)
 {
        struct io io = {};
 
-       return io_run_rd(&io, argv, NULL, FORMAT_NONE)
-           && io_read_buf(&io, buf, bufsize);
+       io_prepare(&io, NULL, IO_RD, argv);
+       return io_start(&io) && io_read_buf(&io, buf, bufsize);
 }
 
 static int
@@ -1084,8 +1084,8 @@ io_run_load(const char **argv, const char *separators,
 {
        struct io io = {};
 
-       return io_format(&io, NULL, IO_RD, argv, FORMAT_NONE)
-               ? io_load(&io, separators, read_property) : ERR;
+       io_prepare(&io, NULL, IO_RD, argv);
+       return io_load(&io, separators, read_property);
 }
 
 
@@ -1737,7 +1737,7 @@ add_run_request(enum keymap keymap, int key, int argc, const char **argv)
        req->key = key;
        req->argv[0] = NULL;
 
-       if (!format_argv(req->argv, argv, FORMAT_NONE))
+       if (!argv_copy(req->argv, argv, TRUE))
                return REQ_NONE;
 
        return REQ_NONE + ++run_requests;
@@ -2214,7 +2214,6 @@ struct view {
 
        enum keymap keymap;     /* What keymap does this view have */
        bool git_dir;           /* Whether the view requires a git directory. */
-       bool refresh;           /* Whether the view supports refreshing. */
 
        char ref[SIZEOF_REF];   /* Hovered commit reference */
        char vid[SIZEOF_REF];   /* View ID. Set to id member when updating. */
@@ -2292,24 +2291,24 @@ static struct view_ops status_ops;
 static struct view_ops tree_ops;
 static struct view_ops branch_ops;
 
-#define VIEW_STR(type, name, env, ref, ops, map, git, refresh) \
-       { type, name, #env, ref, ops, map, git, refresh }
+#define VIEW_STR(type, name, env, ref, ops, map, git) \
+       { type, name, #env, ref, ops, map, git }
 
-#define VIEW_(id, name, ops, git, refresh, ref) \
-       VIEW_STR(VIEW_##id, name, TIG_##id##_CMD, ref, ops, KEYMAP_##id, git, refresh)
+#define VIEW_(id, name, ops, git, ref) \
+       VIEW_STR(VIEW_##id, name, TIG_##id##_CMD, ref, ops, KEYMAP_##id, git)
 
 static struct view views[] = {
-       VIEW_(MAIN,   "main",   &main_ops,   TRUE,  TRUE,  ref_head),
-       VIEW_(DIFF,   "diff",   &diff_ops,   TRUE,  FALSE, ref_commit),
-       VIEW_(LOG,    "log",    &log_ops,    TRUE,  TRUE,  ref_head),
-       VIEW_(TREE,   "tree",   &tree_ops,   TRUE,  FALSE, ref_commit),
-       VIEW_(BLOB,   "blob",   &blob_ops,   TRUE,  FALSE, ref_blob),
-       VIEW_(BLAME,  "blame",  &blame_ops,  TRUE,  FALSE, ref_commit),
-       VIEW_(BRANCH, "branch", &branch_ops, TRUE,  TRUE,  ref_head),
-       VIEW_(HELP,   "help",   &help_ops,   FALSE, FALSE, ""),
-       VIEW_(PAGER,  "pager",  &pager_ops,  FALSE, FALSE, "stdin"),
-       VIEW_(STATUS, "status", &status_ops, TRUE,  TRUE,  ""),
-       VIEW_(STAGE,  "stage",  &stage_ops,  TRUE,  TRUE,  ""),
+       VIEW_(MAIN,   "main",   &main_ops,   TRUE,  ref_head),
+       VIEW_(DIFF,   "diff",   &diff_ops,   TRUE,  ref_commit),
+       VIEW_(LOG,    "log",    &log_ops,    TRUE,  ref_head),
+       VIEW_(TREE,   "tree",   &tree_ops,   TRUE,  ref_commit),
+       VIEW_(BLOB,   "blob",   &blob_ops,   TRUE,  ref_blob),
+       VIEW_(BLAME,  "blame",  &blame_ops,  TRUE,  ref_commit),
+       VIEW_(BRANCH, "branch", &branch_ops, TRUE,  ref_head),
+       VIEW_(HELP,   "help",   &help_ops,   FALSE, ""),
+       VIEW_(PAGER,  "pager",  &pager_ops,  FALSE, "stdin"),
+       VIEW_(STATUS, "status", &status_ops, TRUE,  ""),
+       VIEW_(STAGE,  "stage",  &stage_ops,  TRUE,  ""),
 };
 
 #define VIEW(req)      (&views[(req) - REQ_OFFSET - 1])
@@ -2320,6 +2319,14 @@ static struct view views[] = {
 #define view_is_displayed(view) \
        (view == display[0] || view == display[1])
 
+static enum request
+view_request(struct view *view, enum request request)
+{
+       if (!view || !view->lines)
+               return request;
+       return view->ops->request(view, request, &view->line[view->lineno]);
+}
+
 
 /*
  * View drawing.
@@ -3142,15 +3149,6 @@ reset_view(struct view *view)
        view->update_secs = 0;
 }
 
-static void
-free_argv(const char *argv[])
-{
-       int argc;
-
-       for (argc = 0; argv[argc]; argc++)
-               free((void *) argv[argc]);
-}
-
 static const char *
 format_arg(const char *name)
 {
@@ -3187,7 +3185,7 @@ format_argv(const char *dst_argv[], const char *src_argv[], enum format_flags fl
        int argc;
        bool noreplace = flags == FORMAT_NONE;
 
-       free_argv(dst_argv);
+       argv_free(dst_argv);
 
        for (argc = 0; src_argv[argc]; argc++) {
                const char *arg = src_argv[argc];
@@ -3272,12 +3270,28 @@ setup_update(struct view *view, const char *vid)
        view->start_time = time(NULL);
 }
 
+static bool
+prepare_io(struct view *view, const char *dir, const char *argv[], bool replace)
+{
+       io_init(&view->io, dir, IO_RD);
+       return format_argv(view->io.argv, argv, replace ? FORMAT_ALL : FORMAT_NONE);
+}
+
 static bool
 prepare_update(struct view *view, const char *argv[], const char *dir)
 {
        if (view->pipe)
                end_update(view, TRUE);
-       return io_format(&view->io, dir, IO_RD, argv, FORMAT_NONE);
+       return prepare_io(view, dir, argv, FALSE);
+}
+
+static bool
+start_update(struct view *view, const char **argv, const char *dir)
+{
+       if (view->pipe)
+               io_done(view->pipe);
+       return prepare_io(view, dir, argv, FALSE) &&
+              io_start(&view->io);
 }
 
 static bool
@@ -3298,7 +3312,7 @@ begin_update(struct view *view, bool refresh)
                if (view->ops->prepare) {
                        if (!view->ops->prepare(view))
                                return FALSE;
-               } else if (!io_format(&view->io, NULL, IO_RD, view->ops->argv, FORMAT_ALL)) {
+               } else if (!prepare_io(view, NULL, view->ops->argv, TRUE)) {
                        return FALSE;
                }
 
@@ -3597,7 +3611,7 @@ open_run_request(enum request request)
 
        if (format_argv(argv, req->argv, FORMAT_ALL))
                open_external_viewer(argv, NULL);
-       free_argv(argv);
+       argv_free(argv);
 }
 
 /*
@@ -3614,18 +3628,13 @@ view_driver(struct view *view, enum request request)
 
        if (request > REQ_NONE) {
                open_run_request(request);
-               /* FIXME: When all views can refresh always do this. */
-               if (view->refresh)
-                       request = REQ_REFRESH;
-               else
-                       return TRUE;
+               view_request(view, REQ_REFRESH);
+               return TRUE;
        }
 
-       if (view && view->lines) {
-               request = view->ops->request(view, request, &view->line[view->lineno]);
-               if (request == REQ_NONE)
-                       return TRUE;
-       }
+       request = view_request(view, request);
+       if (request == REQ_NONE)
+               return TRUE;
 
        switch (request) {
        case REQ_MOVE_UP:
@@ -3712,9 +3721,7 @@ view_driver(struct view *view, enum request request)
                        if (view_is_displayed(view))
                                update_view_title(view);
                        if (line != view->lineno)
-                               view->ops->request(view, REQ_ENTER,
-                                                  &view->line[view->lineno]);
-
+                               view_request(view, REQ_ENTER);
                } else {
                        move_view(view, request);
                }
@@ -4519,7 +4526,6 @@ tree_read_date(struct view *view, char *text, bool *read_date)
                        "git", "log", "--no-color", "--pretty=raw",
                                "--cc", "--raw", view->id, "--", path, NULL
                };
-               struct io io = {};
 
                if (!view->lines) {
                        tree_entry(view, LINE_TREE_HEAD, opt_path, NULL, NULL);
@@ -4527,13 +4533,11 @@ tree_read_date(struct view *view, char *text, bool *read_date)
                        return TRUE;
                }
 
-               if (!io_run_rd(&io, log_file, opt_cdup, FORMAT_NONE)) {
+               if (!start_update(view, log_file, opt_cdup)) {
                        report("Failed to load tree data");
                        return TRUE;
                }
 
-               io_done(view->pipe);
-               view->io = io;
                *read_date = TRUE;
                return FALSE;
 
@@ -4662,14 +4666,15 @@ tree_draw(struct view *view, struct line *line, unsigned int lineno)
 }
 
 static void
-open_blob_editor()
+open_blob_editor(const char *id)
 {
+       const char *blob_argv[] = { "git", "cat-file", "blob", id, NULL };
        char file[SIZEOF_STR] = "/tmp/tigblob.XXXXXX";
        int fd = mkstemp(file);
 
        if (fd == -1)
                report("Failed to create temporary file");
-       else if (!io_run_append(blob_ops.argv, FORMAT_ALL, fd))
+       else if (!io_run_append(blob_argv, fd))
                report("Failed to save blob data to file");
        else
                open_editor(file);
@@ -4681,6 +4686,7 @@ static enum request
 tree_request(struct view *view, enum request request, struct line *line)
 {
        enum open_flags flags;
+       struct tree_entry *entry = line->data;
 
        switch (request) {
        case REQ_VIEW_BLAME:
@@ -4696,7 +4702,7 @@ tree_request(struct view *view, enum request request, struct line *line)
                if (line->type != LINE_TREE_FILE) {
                        report("Edit only supported for files");
                } else if (!is_head_commit(view->vid)) {
-                       open_blob_editor();
+                       open_blob_editor(entry->id);
                } else {
                        open_editor(opt_file);
                }
@@ -4747,7 +4753,7 @@ tree_request(struct view *view, enum request request, struct line *line)
                break;
 
        case LINE_TREE_FILE:
-               flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT;
+               flags = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT;
                request = REQ_VIEW_BLOB;
                break;
 
@@ -4815,7 +4821,7 @@ tree_prepare(struct view *view)
                opt_path[0] = 0;
        }
 
-       return io_format(&view->io, opt_cdup, IO_RD, view->ops->argv, FORMAT_ALL);
+       return prepare_io(view, opt_cdup, view->ops->argv, TRUE);
 }
 
 static const char *tree_argv[SIZEOF_ARG] = {
@@ -4847,7 +4853,7 @@ blob_request(struct view *view, enum request request, struct line *line)
 {
        switch (request) {
        case REQ_EDIT:
-               open_blob_editor();
+               open_blob_editor(view->vid);
                return REQ_NONE;
        default:
                return pager_request(view, request, line);
@@ -4880,18 +4886,6 @@ static struct view_ops blob_ops = {
  *     reading output from git-blame.
  */
 
-static const char *blame_head_argv[] = {
-       "git", "blame", "--incremental", "--", "%(file)", NULL
-};
-
-static const char *blame_ref_argv[] = {
-       "git", "blame", "--incremental", "%(ref)", "--", "%(file)", NULL
-};
-
-static const char *blame_cat_file_argv[] = {
-       "git", "cat-file", "blob", "%(ref):%(file)", NULL
-};
-
 struct blame_commit {
        char id[SIZEOF_REV];            /* SHA1 ID. */
        char title[128];                /* First line of the commit message. */
@@ -4919,7 +4913,12 @@ blame_open(struct view *view)
        }
 
        if (*opt_ref || !io_open(&view->io, "%s%s", opt_cdup, opt_file)) {
-               if (!io_run_rd(&view->io, blame_cat_file_argv, opt_cdup, FORMAT_ALL))
+               const char *blame_cat_file_argv[] = {
+                       "git", "cat-file", "blob", path, NULL
+               };
+
+               if (!string_format(path, "%s:%s", opt_ref, opt_file) ||
+                   !start_update(view, blame_cat_file_argv, opt_cdup))
                        return FALSE;
        }
 
@@ -5009,19 +5008,19 @@ static bool
 blame_read_file(struct view *view, const char *line, bool *read_file)
 {
        if (!line) {
-               const char **argv = *opt_ref ? blame_ref_argv : blame_head_argv;
-               struct io io = {};
+               const char *blame_argv[] = {
+                       "git", "blame", "--incremental",
+                               *opt_ref ? opt_ref : "--incremental", "--", opt_file, NULL
+               };
 
                if (view->lines == 0 && !view->prev)
                        die("No blame exist for %s", view->vid);
 
-               if (view->lines == 0 || !io_run_rd(&io, argv, opt_cdup, FORMAT_ALL)) {
+               if (view->lines == 0 || !start_update(view, blame_argv, opt_cdup)) {
                        report("Failed to load blame data");
                        return TRUE;
                }
 
-               io_done(view->pipe);
-               view->io = io;
                *read_file = FALSE;
                return FALSE;
 
@@ -5184,7 +5183,7 @@ setup_blame_parent_line(struct view *view, struct blame *blame)
 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;
+       enum open_flags flags = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT;
        struct blame *blame = line->data;
 
        switch (request) {
@@ -5449,7 +5448,7 @@ branch_open(struct view *view)
                        "--simplify-by-decoration", "--all", NULL
        };
 
-       if (!io_run_rd(&view->io, branch_log, NULL, FORMAT_NONE)) {
+       if (!start_update(view, branch_log, NULL)) {
                report("Failed to load branch data");
                return TRUE;
        }
@@ -5910,7 +5909,7 @@ status_enter(struct view *view, struct line *line)
                die("line type %d not handled in switch", line->type);
        }
 
-       split = view_is_displayed(view) ? OPEN_SPLIT : 0;
+       split = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT;
        open_view(view, REQ_VIEW_STAGE, OPEN_PREPARED | split);
        if (view_is_displayed(VIEW(REQ_VIEW_STAGE))) {
                if (status) {
@@ -6880,7 +6879,7 @@ main_read(struct view *view, char *line)
 static enum request
 main_request(struct view *view, enum request request, struct line *line)
 {
-       enum open_flags flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT;
+       enum open_flags flags = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT;
 
        switch (request) {
        case REQ_ENTER: