index 08ef0b4e14e5e964b46b2ece681720249eab8d4c..160f04dd0b510aeb7333eee5a5656bb8eba8e297 100644 (file)
--- a/tig.c
+++ b/tig.c
static void foreach_ref(bool (*visitor)(void *data, const struct ref *ref), void *data);
static int load_refs(void);
-enum format_flags {
- FORMAT_ALL, /* Perform replacement in all arguments. */
- FORMAT_NONE /* No replacement should be performed. */
-};
-
-static bool format_argv(const char *dst[], const char *src[], enum format_flags flags);
-
enum input_status {
INPUT_OK,
INPUT_SKIP,
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;
}
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
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
{
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
{
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);
}
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;
}
static bool
-format_argv(const char *dst_argv[], const char *src_argv[], enum format_flags flags)
+format_argv(const char *dst_argv[], const char *src_argv[], bool replace)
{
char buf[SIZEOF_STR];
int argc;
- bool noreplace = flags == FORMAT_NONE;
argv_free(dst_argv);
@@ -3205,7 +3188,7 @@ format_argv(const char *dst_argv[], const char *src_argv[], enum format_flags fl
int len = next - arg;
const char *value;
- if (!next || noreplace) {
+ if (!next || !replace) {
len = strlen(arg);
value = "";
@@ -3220,7 +3203,7 @@ format_argv(const char *dst_argv[], const char *src_argv[], enum format_flags fl
if (!string_format_from(buf, &bufpos, "%.*s%s", len, arg, value))
return FALSE;
- arg = next && !noreplace ? strchr(next, ')') + 1 : NULL;
+ arg = next && replace ? strchr(next, ')') + 1 : NULL;
}
dst_argv[argc] = strdup(buf);
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);
+}
+
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
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;
}
return;
}
- if (format_argv(argv, req->argv, FORMAT_ALL))
+ if (format_argv(argv, req->argv, TRUE))
open_external_viewer(argv, NULL);
argv_free(argv);
}
"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);
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;
if (fd == -1)
report("Failed to create temporary file");
- else if (!io_run_append(blob_argv, FORMAT_NONE, fd))
+ else if (!io_run_append(blob_argv, fd))
report("Failed to save blob data to file");
else
open_editor(file);
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] = {
};
if (!string_format(path, "%s:%s", opt_ref, opt_file) ||
- !io_run_rd(&view->io, blame_cat_file_argv, opt_cdup, FORMAT_NONE))
+ !start_update(view, blame_cat_file_argv, opt_cdup))
return FALSE;
}
"git", "blame", "--incremental",
*opt_ref ? opt_ref : "--incremental", "--", opt_file, NULL
};
- struct io io = {};
if (view->lines == 0 && !view->prev)
die("No blame exist for %s", view->vid);
- if (view->lines == 0 || !io_run_rd(&io, blame_argv, opt_cdup, FORMAT_NONE)) {
+ 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;
"--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;
}