index 2fa9018225db1e28b515e52dd1ac936de740e97b..3fce6f086685395490beacd03f7afb968c5c7670 100644 (file)
--- a/tig.c
+++ b/tig.c
/* ncurses(3): Must be defined to have extended wide-character functions. */
#define _XOPEN_SOURCE_EXTENDED
-#include <curses.h>
+#ifdef HAVE_NCURSESW_NCURSES_H
+#include <ncursesw/ncurses.h>
+#else
+#ifdef HAVE_NCURSES_NCURSES_H
+#include <ncurses/ncurses.h>
+#else
+#include <ncurses.h>
+#endif
+#endif
#if __GNUC__ >= 3
#define __NORETURN __attribute__((__noreturn__))
static void report(const char *msg, ...);
static int read_properties(FILE *pipe, const char *separators, int (*read)(char *, size_t, char *, size_t));
static void set_nonblocking_input(bool loading);
-static size_t utf8_length(const char *string, size_t max_width, int *trimmed, bool reserve);
+static size_t utf8_length(const char *string, int *width, size_t max_width, int *trimmed, bool reserve);
+static bool prompt_yesno(const char *prompt);
+static int load_refs(void);
#define ABS(x) ((x) >= 0 ? (x) : -(x))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define SIZEOF_STR 1024 /* Default string size. */
#define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
-#define SIZEOF_REV 41 /* Holds a SHA-1 and an ending NUL */
+#define SIZEOF_REV 41 /* Holds a SHA-1 and an ending NUL. */
+#define SIZEOF_ARG 32 /* Default argument array size. */
/* Revision graph */
#define REVGRAPH_BRANCH '+'
#define REVGRAPH_COMMIT '*'
#define REVGRAPH_BOUND '^'
-#define REVGRAPH_LINE '|'
#define SIZEOF_REVGRAPH 19 /* Size of revision ancestry graphics. */
@@ -99,18 +109,18 @@ static size_t utf8_length(const char *string, size_t max_width, int *trimmed, bo
/* The default interval between line numbers. */
#define NUMBER_INTERVAL 5
-#define TABSIZE 8
+#define TAB_SIZE 8
#define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
#define NULL_ID "0000000000000000000000000000000000000000"
#ifndef GIT_CONFIG
-#define GIT_CONFIG "git config"
+#define GIT_CONFIG "config"
#endif
#define TIG_LS_REMOTE \
- "git ls-remote $(git rev-parse --git-dir) 2>/dev/null"
+ "git ls-remote . 2>/dev/null"
#define TIG_DIFF_CMD \
"git show --pretty=fuller --no-color --root --patch-with-stat --find-copies-harder -C %s 2>/dev/null"
@@ -118,8 +128,11 @@ static size_t utf8_length(const char *string, size_t max_width, int *trimmed, bo
#define TIG_LOG_CMD \
"git log --no-color --cc --stat -n100 %s 2>/dev/null"
+#define TIG_MAIN_BASE \
+ "git log --no-color --pretty=raw --parents --topo-order"
+
#define TIG_MAIN_CMD \
- "git log --no-color --topo-order --parents --boundary --pretty=raw %s 2>/dev/null"
+ TIG_MAIN_BASE " %s 2>/dev/null"
#define TIG_TREE_CMD \
"git ls-tree %s %s"
unsigned int next:1; /* For ref lists: are there more refs? */
};
-static struct ref **get_refs(char *id);
+static struct ref **get_refs(const char *id);
+
+enum format_flags {
+ FORMAT_ALL, /* Perform replacement in all arguments. */
+ FORMAT_DASH, /* Perform replacement up until "--". */
+ FORMAT_NONE /* No replacement should be performed. */
+};
+
+static bool format_command(char dst[], const char *src[], enum format_flags flags);
+static bool format_argv(const char *dst[], const char *src[], enum format_flags flags);
struct int_map {
const char *name;
return 0;
}
+#define prefixcmp(str1, str2) \
+ strncmp(str1, str2, STRING_SIZE(str2))
+
+static inline int
+suffixcmp(const char *str, int slen, const char *suffix)
+{
+ size_t len = slen >= 0 ? slen : strlen(str);
+ size_t suffixlen = strlen(suffix);
+
+ return suffixlen < len ? strcmp(str + len - suffixlen, suffix) : -1;
+}
+
/* Shell quoting
*
* NOTE: The following is a slightly modified copy of the git project's shell
return bufsize;
}
+static bool
+argv_from_string(const char *argv[SIZEOF_ARG], int *argc, char *cmd)
+{
+ int valuelen;
+
+ while (*cmd && *argc < SIZEOF_ARG && (valuelen = strcspn(cmd, " \t"))) {
+ bool advance = cmd[valuelen] != 0;
+
+ cmd[valuelen] = 0;
+ argv[(*argc)++] = chomp_string(cmd);
+ cmd += valuelen + advance;
+ }
+
+ if (*argc < SIZEOF_ARG)
+ argv[*argc] = NULL;
+ return *argc < SIZEOF_ARG;
+}
+
+
+/*
+ * Executing external commands.
+ */
+
+enum io_type {
+ IO_FD, /* File descriptor based IO. */
+ IO_RD, /* Read only fork+exec IO. */
+ IO_WR, /* Write only fork+exec IO. */
+};
+
+struct io {
+ enum io_type type; /* The requested type of pipe. */
+ FILE *pipe; /* Pipe for reading or writing. */
+ int error; /* Error status. */
+ char sh[SIZEOF_STR]; /* Shell command buffer. */
+ char *buf; /* Read/write buffer. */
+ size_t bufalloc; /* Allocated buffer size. */
+};
+
+static void
+reset_io(struct io *io)
+{
+ io->pipe = NULL;
+ io->buf = NULL;
+ io->bufalloc = 0;
+ io->error = 0;
+}
+
+static void
+init_io(struct io *io, enum io_type type)
+{
+ reset_io(io);
+ io->type = type;
+}
+
+static bool
+init_io_fd(struct io *io, FILE *pipe)
+{
+ init_io(io, IO_FD);
+ io->pipe = pipe;
+ return io->pipe != NULL;
+}
+
+static bool
+done_io(struct io *io)
+{
+ free(io->buf);
+ if (io->type == IO_FD)
+ fclose(io->pipe);
+ else
+ pclose(io->pipe);
+ reset_io(io);
+ return TRUE;
+}
+
+static bool
+start_io(struct io *io)
+{
+ io->pipe = popen(io->sh, io->type == IO_RD ? "r" : "w");
+ return io->pipe != NULL;
+}
+
+static bool
+run_io(struct io *io, enum io_type type, const char *cmd)
+{
+ init_io(io, type);
+ string_ncopy(io->sh, cmd, strlen(cmd));
+ return start_io(io);
+}
+
+static bool
+run_io_format(struct io *io, const char *cmd, ...)
+{
+ va_list args;
+
+ va_start(args, cmd);
+ init_io(io, IO_RD);
+
+ if (vsnprintf(io->sh, sizeof(io->sh), cmd, args) >= sizeof(io->sh))
+ io->sh[0] = 0;
+ va_end(args);
+
+ return io->sh[0] ? start_io(io) : FALSE;
+}
+
+static bool
+io_eof(struct io *io)
+{
+ return feof(io->pipe);
+}
+
+static int
+io_error(struct io *io)
+{
+ return io->error;
+}
+
+static bool
+io_strerror(struct io *io)
+{
+ return strerror(io->error);
+}
+
+static char *
+io_gets(struct io *io)
+{
+ if (!io->buf) {
+ io->buf = malloc(BUFSIZ);
+ if (!io->buf)
+ return NULL;
+ io->bufalloc = BUFSIZ;
+ }
+
+ if (!fgets(io->buf, io->bufalloc, io->pipe)) {
+ if (ferror(io->pipe))
+ io->error = errno;
+ return NULL;
+ }
+
+ return io->buf;
+}
+
/*
* User requests
REQ_(VIEW_CLOSE, "Close the current view"), \
REQ_(QUIT, "Close all views and quit"), \
\
+ REQ_GROUP("View specific requests") \
+ REQ_(STATUS_UPDATE, "Update file status"), \
+ REQ_(STATUS_REVERT, "Revert file changes"), \
+ REQ_(STATUS_MERGE, "Merge file using external tool"), \
+ REQ_(STAGE_NEXT, "Find next chunk to stage"), \
+ REQ_(TREE_PARENT, "Switch to parent directory in tree view"), \
+ \
REQ_GROUP("Cursor navigation") \
REQ_(MOVE_UP, "Move cursor one line up"), \
REQ_(MOVE_DOWN, "Move cursor one line down"), \
REQ_(FIND_NEXT, "Find next search match"), \
REQ_(FIND_PREV, "Find previous search match"), \
\
+ REQ_GROUP("Option manipulation") \
+ REQ_(TOGGLE_LINENO, "Toggle line numbers"), \
+ REQ_(TOGGLE_DATE, "Toggle date display"), \
+ REQ_(TOGGLE_AUTHOR, "Toggle author display"), \
+ REQ_(TOGGLE_REV_GRAPH, "Toggle revision graph visualization"), \
+ REQ_(TOGGLE_REFS, "Toggle reference display (tags/branches)"), \
+ \
REQ_GROUP("Misc") \
REQ_(PROMPT, "Bring up the prompt"), \
REQ_(SCREEN_REDRAW, "Redraw the screen"), \
REQ_(SCREEN_RESIZE, "Resize the screen"), \
REQ_(SHOW_VERSION, "Show version information"), \
REQ_(STOP_LOADING, "Stop all loading views"), \
- REQ_(TOGGLE_LINENO, "Toggle line numbers"), \
- REQ_(TOGGLE_DATE, "Toggle date display"), \
- REQ_(TOGGLE_AUTHOR, "Toggle author display"), \
- REQ_(TOGGLE_REV_GRAPH, "Toggle revision graph visualization"), \
- REQ_(TOGGLE_REFS, "Toggle reference display (tags/branches)"), \
- REQ_(STATUS_UPDATE, "Update file status"), \
- REQ_(STATUS_MERGE, "Merge file using external tool"), \
- REQ_(TREE_PARENT, "Switch to parent directory in tree view"), \
REQ_(EDIT, "Open in editor"), \
REQ_(NONE, "Do nothing")
struct request_info {
enum request request;
- char *name;
+ const char *name;
int namelen;
- char *help;
+ const char *help;
};
static struct request_info req_info[] = {
static bool opt_date = TRUE;
static bool opt_author = TRUE;
static bool opt_line_number = FALSE;
+static bool opt_line_graphics = TRUE;
static bool opt_rev_graph = FALSE;
static bool opt_show_refs = TRUE;
static int opt_num_interval = NUMBER_INTERVAL;
-static int opt_tab_size = TABSIZE;
-static enum request opt_request = REQ_VIEW_MAIN;
+static int opt_tab_size = TAB_SIZE;
+static int opt_author_cols = AUTHOR_COLS-1;
static char opt_cmd[SIZEOF_STR] = "";
static char opt_path[SIZEOF_STR] = "";
static char opt_file[SIZEOF_STR] = "";
static char opt_ref[SIZEOF_REF] = "";
static char opt_head[SIZEOF_REF] = "";
+static char opt_head_rev[SIZEOF_REV] = "";
static char opt_remote[SIZEOF_REF] = "";
-static bool opt_no_head = TRUE;
static FILE *opt_pipe = NULL;
static char opt_encoding[20] = "UTF-8";
static bool opt_utf8 = TRUE;
static char opt_git_dir[SIZEOF_STR] = "";
static signed char opt_is_inside_work_tree = -1; /* set to TRUE or FALSE */
static char opt_editor[SIZEOF_STR] = "";
+static FILE *opt_tty = NULL;
-static bool
-parse_options(int argc, char *argv[])
+#define is_initial_commit() (!*opt_head_rev)
+#define is_head_commit(rev) (!strcmp((rev), "HEAD") || !strcmp(opt_head_rev, (rev)))
+
+static enum request
+parse_options(int argc, const char *argv[])
{
+ enum request request = REQ_VIEW_MAIN;
size_t buf_size;
- char *subcommand;
+ const char *subcommand;
bool seen_dashdash = FALSE;
int i;
if (!isatty(STDIN_FILENO)) {
- opt_request = REQ_VIEW_PAGER;
opt_pipe = stdin;
- return TRUE;
+ return REQ_VIEW_PAGER;
}
if (argc <= 1)
- return TRUE;
+ return REQ_VIEW_MAIN;
subcommand = argv[1];
if (!strcmp(subcommand, "status") || !strcmp(subcommand, "-S")) {
- opt_request = REQ_VIEW_STATUS;
if (!strcmp(subcommand, "-S"))
warn("`-S' has been deprecated; use `tig status' instead");
if (argc > 2)
warn("ignoring arguments after `%s'", subcommand);
- return TRUE;
+ return REQ_VIEW_STATUS;
} else if (!strcmp(subcommand, "blame")) {
- opt_request = REQ_VIEW_BLAME;
if (argc <= 2 || argc > 4)
die("invalid number of options to blame\n\n%s", usage);
}
string_ncopy(opt_file, argv[i], strlen(argv[i]));
- return TRUE;
+ return REQ_VIEW_BLAME;
} else if (!strcmp(subcommand, "show")) {
- opt_request = REQ_VIEW_DIFF;
+ request = REQ_VIEW_DIFF;
} else if (!strcmp(subcommand, "log") || !strcmp(subcommand, "diff")) {
- opt_request = subcommand[0] == 'l'
- ? REQ_VIEW_LOG : REQ_VIEW_DIFF;
+ request = subcommand[0] == 'l' ? REQ_VIEW_LOG : REQ_VIEW_DIFF;
warn("`tig %s' has been deprecated", subcommand);
} else {
if (!subcommand)
/* XXX: This is vulnerable to the user overriding
* options required for the main view parser. */
- string_copy(opt_cmd, "git log --no-color --pretty=raw --boundary --parents");
+ string_copy(opt_cmd, TIG_MAIN_BASE);
else
string_format(opt_cmd, "git %s", subcommand);
buf_size = strlen(opt_cmd);
for (i = 1 + !!subcommand; i < argc; i++) {
- char *opt = argv[i];
+ const char *opt = argv[i];
if (seen_dashdash || !strcmp(opt, "--")) {
seen_dashdash = TRUE;
} else if (!strcmp(opt, "-v") || !strcmp(opt, "--version")) {
printf("tig version %s\n", TIG_VERSION);
- return FALSE;
+ return REQ_NONE;
} else if (!strcmp(opt, "-h") || !strcmp(opt, "--help")) {
printf("%s\n", usage);
- return FALSE;
+ return REQ_NONE;
}
opt_cmd[buf_size++] = ' ';
opt_cmd[buf_size] = 0;
- return TRUE;
+ return request;
}
enum line_type {
#define LINE(type, line, fg, bg, attr) \
LINE_##type
- LINE_INFO
+ LINE_INFO,
+ LINE_NONE
#undef LINE
};
};
static enum line_type
-get_line_type(char *line)
+get_line_type(const char *line)
{
int linelen = strlen(line);
enum line_type type;
}
static struct line_info *
-get_line_info(char *name)
+get_line_info(const char *name)
{
size_t namelen = strlen(name);
enum line_type type;
struct keybinding {
int alias;
enum request request;
- struct keybinding *next;
};
static struct keybinding default_keybindings[] = {
{ KEY_UP, REQ_PREVIOUS },
{ KEY_DOWN, REQ_NEXT },
{ 'R', REQ_REFRESH },
+ { KEY_F(5), REQ_REFRESH },
{ 'O', REQ_MAXIMIZE },
/* Cursor navigation */
{ 'F', REQ_TOGGLE_REFS },
{ ':', REQ_PROMPT },
{ 'u', REQ_STATUS_UPDATE },
+ { '!', REQ_STATUS_REVERT },
{ 'M', REQ_STATUS_MERGE },
+ { '@', REQ_STAGE_NEXT },
{ ',', REQ_TREE_PARENT },
{ 'e', REQ_EDIT },
#define set_keymap(map, name) \
set_from_int_map(keymap_table, ARRAY_SIZE(keymap_table), map, name, strlen(name))
-static struct keybinding *keybindings[ARRAY_SIZE(keymap_table)];
+struct keybinding_table {
+ struct keybinding *data;
+ size_t size;
+};
+
+static struct keybinding_table keybindings[ARRAY_SIZE(keymap_table)];
static void
add_keybinding(enum keymap keymap, enum request request, int key)
{
- struct keybinding *keybinding;
+ struct keybinding_table *table = &keybindings[keymap];
- keybinding = calloc(1, sizeof(*keybinding));
- if (!keybinding)
+ table->data = realloc(table->data, (table->size + 1) * sizeof(*table->data));
+ if (!table->data)
die("Failed to allocate keybinding");
-
- keybinding->alias = key;
- keybinding->request = request;
- keybinding->next = keybindings[keymap];
- keybindings[keymap] = keybinding;
+ table->data[table->size].alias = key;
+ table->data[table->size++].request = request;
}
/* Looks for a key binding first in the given map, then in the generic map, and
static enum request
get_keybinding(enum keymap keymap, int key)
{
- struct keybinding *kbd;
- int i;
+ size_t i;
- for (kbd = keybindings[keymap]; kbd; kbd = kbd->next)
- if (kbd->alias == key)
- return kbd->request;
+ for (i = 0; i < keybindings[keymap].size; i++)
+ if (keybindings[keymap].data[i].alias == key)
+ return keybindings[keymap].data[i].request;
- for (kbd = keybindings[KEYMAP_GENERIC]; kbd; kbd = kbd->next)
- if (kbd->alias == key)
- return kbd->request;
+ for (i = 0; i < keybindings[KEYMAP_GENERIC].size; i++)
+ if (keybindings[KEYMAP_GENERIC].data[i].alias == key)
+ return keybindings[KEYMAP_GENERIC].data[i].request;
for (i = 0; i < ARRAY_SIZE(default_keybindings); i++)
if (default_keybindings[i].alias == key)
struct key {
- char *name;
+ const char *name;
int value;
};
return ERR;
}
-static char *
+static const char *
get_key_name(int key_value)
{
static char key_char[] = "'X'";
- char *seq = NULL;
+ const char *seq = NULL;
int key;
for (key = 0; key < ARRAY_SIZE(key_table); key++)
seq = key_char;
}
- return seq ? seq : "'?'";
+ return seq ? seq : "(no key)";
}
-static char *
+static const char *
get_key(enum request request)
{
static char buf[BUFSIZ];
struct run_request {
enum keymap keymap;
int key;
- char cmd[SIZEOF_STR];
+ const char *argv[SIZEOF_ARG];
};
static struct run_request *run_request;
static size_t run_requests;
static enum request
-add_run_request(enum keymap keymap, int key, int argc, char **argv)
+add_run_request(enum keymap keymap, int key, int argc, const char **argv)
{
- struct run_request *tmp;
- struct run_request req = { keymap, key };
- size_t bufpos;
-
- for (bufpos = 0; argc > 0; argc--, argv++)
- if (!string_format_from(req.cmd, &bufpos, "%s ", *argv))
- return REQ_NONE;
+ struct run_request *req;
- req.cmd[bufpos - 1] = 0;
+ if (argc >= ARRAY_SIZE(req->argv) - 1)
+ return REQ_NONE;
- tmp = realloc(run_request, (run_requests + 1) * sizeof(*run_request));
- if (!tmp)
+ req = realloc(run_request, (run_requests + 1) * sizeof(*run_request));
+ if (!req)
return REQ_NONE;
- run_request = tmp;
- run_request[run_requests++] = req;
+ run_request = req;
+ req = &run_request[run_requests];
+ req->keymap = keymap;
+ req->key = key;
+ req->argv[0] = NULL;
+
+ if (!format_argv(req->argv, argv, FORMAT_NONE))
+ return REQ_NONE;
- return REQ_NONE + run_requests;
+ return REQ_NONE + ++run_requests;
}
static struct run_request *
static void
add_builtin_run_requests(void)
{
+ const char *cherry_pick[] = { "git", "cherry-pick", "%(commit)", NULL };
+ const char *gc[] = { "git", "gc", NULL };
struct {
enum keymap keymap;
int key;
- char *argv[1];
+ int argc;
+ const char **argv;
} reqs[] = {
- { KEYMAP_MAIN, 'C', { "git cherry-pick %(commit)" } },
- { KEYMAP_GENERIC, 'G', { "git gc" } },
+ { KEYMAP_MAIN, 'C', ARRAY_SIZE(cherry_pick) - 1, cherry_pick },
+ { KEYMAP_GENERIC, 'G', ARRAY_SIZE(gc) - 1, gc },
};
int i;
for (i = 0; i < ARRAY_SIZE(reqs); i++) {
enum request req;
- req = add_run_request(reqs[i].keymap, reqs[i].key, 1, reqs[i].argv);
+ req = add_run_request(reqs[i].keymap, reqs[i].key, reqs[i].argc, reqs[i].argv);
if (req != REQ_NONE)
add_keybinding(reqs[i].keymap, req, reqs[i].key);
}
static int config_lineno;
static bool config_errors;
-static char *config_msg;
+static const char *config_msg;
/* Wants: object fgcolor bgcolor [attr] */
static int
-option_color_command(int argc, char *argv[])
+option_color_command(int argc, const char *argv[])
{
struct line_info *info;
!strcmp(s, "yes")) ? TRUE : FALSE;
}
+static int
+parse_int(const char *s, int default_value, int min, int max)
+{
+ int value = atoi(s);
+
+ return (value < min || value > max) ? default_value : value;
+}
+
/* Wants: name = value */
static int
-option_set_command(int argc, char *argv[])
+option_set_command(int argc, const char *argv[])
{
if (argc != 3) {
config_msg = "Wrong number of arguments given to set command";
return OK;
}
+ if (!strcmp(argv[0], "line-graphics")) {
+ opt_line_graphics = parse_bool(argv[2]);
+ return OK;
+ }
+
if (!strcmp(argv[0], "line-number-interval")) {
- opt_num_interval = atoi(argv[2]);
+ opt_num_interval = parse_int(argv[2], opt_num_interval, 1, 1024);
+ return OK;
+ }
+
+ if (!strcmp(argv[0], "author-width")) {
+ opt_author_cols = parse_int(argv[2], opt_author_cols, 0, 1024);
return OK;
}
if (!strcmp(argv[0], "tab-size")) {
- opt_tab_size = atoi(argv[2]);
+ opt_tab_size = parse_int(argv[2], opt_tab_size, 1, 1024);
return OK;
}
if (!strcmp(argv[0], "commit-encoding")) {
- char *arg = argv[2];
- int delimiter = *arg;
- int i;
+ const char *arg = argv[2];
+ int arglen = strlen(arg);
- switch (delimiter) {
+ switch (arg[0]) {
case '"':
case '\'':
- for (arg++, i = 0; arg[i]; i++)
- if (arg[i] == delimiter) {
- arg[i] = 0;
- break;
- }
+ if (arglen == 1 || arg[arglen - 1] != arg[0]) {
+ config_msg = "Unmatched quotation";
+ return ERR;
+ }
+ arg += 1; arglen -= 2;
default:
string_ncopy(opt_encoding, arg, strlen(arg));
return OK;
/* Wants: mode request key */
static int
-option_bind_command(int argc, char *argv[])
+option_bind_command(int argc, const char *argv[])
{
enum request request;
int keymap;
}
static int
-set_option(char *opt, char *value)
+set_option(const char *opt, char *value)
{
- char *argv[16];
- int valuelen;
+ const char *argv[SIZEOF_ARG];
int argc = 0;
- /* Tokenize */
- while (argc < ARRAY_SIZE(argv) && (valuelen = strcspn(value, " \t"))) {
- argv[argc++] = value;
- value += valuelen;
-
- /* Nothing more to tokenize or last available token. */
- if (!*value || argc >= ARRAY_SIZE(argv))
- break;
-
- *value++ = 0;
- while (isspace(*value))
- value++;
+ if (!argv_from_string(argv, &argc, value)) {
+ config_msg = "Too many option arguments";
+ return ERR;
}
if (!strcmp(opt, "color"))
static int
load_options(void)
{
- char *home = getenv("HOME");
- char *tigrc_user = getenv("TIGRC_USER");
- char *tigrc_system = getenv("TIGRC_SYSTEM");
+ const char *home = getenv("HOME");
+ const char *tigrc_user = getenv("TIGRC_USER");
+ const char *tigrc_system = getenv("TIGRC_SYSTEM");
char buf[SIZEOF_STR];
add_builtin_run_requests();
enum keymap keymap; /* What keymap does this view have */
bool git_dir; /* Whether the view requires a git directory. */
- char cmd[SIZEOF_STR]; /* Command buffer */
char ref[SIZEOF_REF]; /* Hovered commit reference */
char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */
size_t line_size; /* Total number of used lines */
unsigned int digits; /* Number of digits in the lines member. */
+ /* Drawing */
+ struct line *curline; /* Line currently being drawn. */
+ enum line_type curtype; /* Attribute currently used for drawing. */
+ unsigned long col; /* Column when drawing. */
+
/* Loading */
- FILE *pipe;
+ struct io io;
+ struct io *pipe;
time_t start_time;
};
/* Read one line; updates view->line. */
bool (*read)(struct view *view, char *data);
/* Draw one line; @lineno must be < view->height. */
- bool (*draw)(struct view *view, struct line *line, unsigned int lineno, bool selected);
+ bool (*draw)(struct view *view, struct line *line, unsigned int lineno);
/* Depending on view handle a special requests. */
enum request (*request)(struct view *view, enum request request, struct line *line);
/* Search for regex in a line. */
void (*select)(struct view *view, struct line *line);
};
-static struct view_ops pager_ops;
-static struct view_ops main_ops;
-static struct view_ops tree_ops;
-static struct view_ops blob_ops;
static struct view_ops blame_ops;
+static struct view_ops blob_ops;
static struct view_ops help_ops;
-static struct view_ops status_ops;
+static struct view_ops log_ops;
+static struct view_ops main_ops;
+static struct view_ops pager_ops;
static struct view_ops stage_ops;
+static struct view_ops status_ops;
+static struct view_ops tree_ops;
#define VIEW_STR(name, cmd, env, ref, ops, map, git) \
{ name, cmd, #env, ref, ops, map, git }
static struct view views[] = {
VIEW_(MAIN, "main", &main_ops, TRUE, ref_head),
VIEW_(DIFF, "diff", &pager_ops, TRUE, ref_commit),
- VIEW_(LOG, "log", &pager_ops, TRUE, ref_head),
+ 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),
#define view_is_displayed(view) \
(view == display[0] || view == display[1])
+
+enum line_graphic {
+ LINE_GRAPHIC_VLINE
+};
+
+static int line_graphics[] = {
+ /* LINE_GRAPHIC_VLINE: */ '|'
+};
+
+static inline void
+set_view_attr(struct view *view, enum line_type type)
+{
+ if (!view->curline->selected && view->curtype != type) {
+ wattrset(view->win, get_line_attr(type));
+ wchgat(view->win, -1, 0, type, NULL);
+ view->curtype = type;
+ }
+}
+
static int
-draw_text(struct view *view, const char *string, int max_len,
- bool use_tilde, bool selected)
+draw_chars(struct view *view, enum line_type type, const char *string,
+ int max_len, bool use_tilde)
{
int len = 0;
+ int col = 0;
int trimmed = FALSE;
if (max_len <= 0)
return 0;
if (opt_utf8) {
- len = utf8_length(string, max_len, &trimmed, use_tilde);
+ len = utf8_length(string, &col, max_len, &trimmed, use_tilde);
} else {
- len = strlen(string);
+ col = len = strlen(string);
if (len > max_len) {
if (use_tilde) {
max_len -= 1;
}
- len = max_len;
+ col = len = max_len;
trimmed = TRUE;
}
}
+ set_view_attr(view, type);
waddnstr(view->win, string, len);
if (trimmed && use_tilde) {
- if (!selected)
- wattrset(view->win, get_line_attr(LINE_DELIMITER));
+ set_view_attr(view, LINE_DELIMITER);
waddch(view->win, '~');
- len++;
+ col++;
}
- return len;
+ return col;
}
static int
-draw_lineno(struct view *view, unsigned int lineno, int max, bool selected)
+draw_space(struct view *view, enum line_type type, int max, int spaces)
+{
+ static char space[] = " ";
+ int col = 0;
+
+ spaces = MIN(max, spaces);
+
+ while (spaces > 0) {
+ int len = MIN(spaces, sizeof(space) - 1);
+
+ col += draw_chars(view, type, space, spaces, FALSE);
+ spaces -= len;
+ }
+
+ return col;
+}
+
+static bool
+draw_lineno(struct view *view, unsigned int lineno)
{
- static char fmt[] = "%1ld";
- char number[10] = " ";
+ char number[10];
int digits3 = view->digits < 3 ? 3 : view->digits;
int max_number = MIN(digits3, STRING_SIZE(number));
- bool showtrimmed = FALSE;
+ int max = view->width - view->col;
int col;
+ if (max < max_number)
+ max_number = max;
+
lineno += view->offset + 1;
if (lineno == 1 || (lineno % opt_num_interval) == 0) {
+ static char fmt[] = "%1ld";
+
if (view->digits <= 9)
fmt[1] = '0' + digits3;
if (!string_format(number, fmt, lineno))
number[0] = 0;
- showtrimmed = TRUE;
+ col = draw_chars(view, LINE_LINE_NUMBER, number, max_number, TRUE);
+ } else {
+ col = draw_space(view, LINE_LINE_NUMBER, max_number, max_number);
}
- if (max < max_number)
- max_number = max;
-
- if (!selected)
- wattrset(view->win, get_line_attr(LINE_LINE_NUMBER));
- col = draw_text(view, number, max_number, showtrimmed, selected);
if (col < max) {
- if (!selected)
- wattrset(view->win, A_NORMAL);
- waddch(view->win, ACS_VLINE);
+ set_view_attr(view, LINE_DEFAULT);
+ waddch(view->win, line_graphics[LINE_GRAPHIC_VLINE]);
col++;
}
- if (col < max) {
+
+ if (col < max)
+ col += draw_space(view, LINE_DEFAULT, max - col, 1);
+ view->col += col;
+
+ return view->width - view->col <= 0;
+}
+
+static bool
+draw_text(struct view *view, enum line_type type, const char *string, bool trim)
+{
+ view->col += draw_chars(view, type, string, view->width - view->col, trim);
+ return view->width - view->col <= 0;
+}
+
+static bool
+draw_graphic(struct view *view, enum line_type type, chtype graphic[], size_t size)
+{
+ int max = view->width - view->col;
+ int i;
+
+ if (max < size)
+ size = max;
+
+ set_view_attr(view, type);
+ /* Using waddch() instead of waddnstr() ensures that
+ * they'll be rendered correctly for the cursor line. */
+ for (i = 0; i < size; i++)
+ waddch(view->win, graphic[i]);
+
+ view->col += size;
+ if (size < max) {
waddch(view->win, ' ');
- col++;
+ view->col++;
}
- return col;
+ return view->width - view->col <= 0;
}
-static int
-draw_date(struct view *view, struct tm *time, int max, bool selected)
+static bool
+draw_field(struct view *view, enum line_type type, const char *text, int len, bool trim)
{
- char buf[DATE_COLS];
+ int max = MIN(view->width - view->col, len);
int col;
+
+ if (text)
+ col = draw_chars(view, type, text, max - 1, trim);
+ else
+ col = draw_space(view, type, max - 1, max - 1);
+
+ view->col += col + draw_space(view, LINE_DEFAULT, max - col, max - col);
+ return view->width - view->col <= 0;
+}
+
+static bool
+draw_date(struct view *view, struct tm *time)
+{
+ char buf[DATE_COLS];
+ char *date;
int timelen = 0;
- if (max > DATE_COLS)
- max = DATE_COLS;
if (time)
timelen = strftime(buf, sizeof(buf), DATE_FORMAT, time);
- if (!timelen) {
- memset(buf, ' ', sizeof(buf) - 1);
- buf[sizeof(buf) - 1] = 0;
- }
+ date = timelen ? buf : NULL;
- if (!selected)
- wattrset(view->win, get_line_attr(LINE_DATE));
- col = draw_text(view, buf, max, FALSE, selected);
- if (col < max) {
- if (!selected)
- wattrset(view->win, get_line_attr(LINE_DEFAULT));
- waddch(view->win, ' ');
- col++;
- }
-
- return col;
+ return draw_field(view, LINE_DATE, date, DATE_COLS, FALSE);
}
static bool
line = &view->line[view->offset + lineno];
wmove(view->win, lineno, 0);
+ view->col = 0;
+ view->curline = line;
+ view->curtype = LINE_NONE;
+ line->selected = FALSE;
if (selected) {
+ set_view_attr(view, LINE_CURSOR);
line->selected = TRUE;
view->ops->select(view, line);
- wchgat(view->win, -1, 0, LINE_CURSOR, NULL);
- wattrset(view->win, get_line_attr(LINE_CURSOR));
} else if (line->selected) {
- line->selected = FALSE;
wclrtoeol(view->win);
}
scrollok(view->win, FALSE);
- draw_ok = view->ops->draw(view, line, lineno, selected);
+ draw_ok = view->ops->draw(view, line, lineno);
scrollok(view->win, TRUE);
return draw_ok;
*/
static void
-end_update(struct view *view)
+reset_view(struct view *view)
+{
+ int i;
+
+ for (i = 0; i < view->lines; i++)
+ free(view->line[i].data);
+ free(view->line);
+
+ view->line = NULL;
+ view->offset = 0;
+ view->lines = 0;
+ view->lineno = 0;
+ view->line_size = 0;
+ view->line_alloc = 0;
+ view->vid[0] = 0;
+}
+
+static void
+free_argv(const char *argv[])
+{
+ int argc;
+
+ for (argc = 0; argv[argc]; argc++)
+ free((void *) argv[argc]);
+}
+
+static bool
+format_argv(const char *dst_argv[], const char *src_argv[], enum format_flags flags)
+{
+ char buf[SIZEOF_STR];
+ int argc;
+ bool noreplace = flags == FORMAT_NONE;
+
+ free_argv(dst_argv);
+
+ for (argc = 0; src_argv[argc]; argc++) {
+ const char *arg = src_argv[argc];
+ size_t bufpos = 0;
+
+ while (arg) {
+ char *next = strstr(arg, "%(");
+ int len = next - arg;
+ const char *value;
+
+ if (!next || noreplace) {
+ if (flags == FORMAT_DASH && !strcmp(arg, "--"))
+ noreplace = TRUE;
+ len = strlen(arg);
+ value = "";
+
+ } else if (!prefixcmp(next, "%(directory)")) {
+ value = opt_path;
+
+ } else if (!prefixcmp(next, "%(file)")) {
+ value = opt_file;
+
+ } else if (!prefixcmp(next, "%(ref)")) {
+ value = *opt_ref ? opt_ref : "HEAD";
+
+ } else if (!prefixcmp(next, "%(head)")) {
+ value = ref_head;
+
+ } else if (!prefixcmp(next, "%(commit)")) {
+ value = ref_commit;
+
+ } else if (!prefixcmp(next, "%(blob)")) {
+ value = ref_blob;
+
+ } else {
+ report("Unknown replacement: `%s`", next);
+ return FALSE;
+ }
+
+ if (!string_format_from(buf, &bufpos, "%.*s%s", len, arg, value))
+ return FALSE;
+
+ arg = next && !noreplace ? strchr(next, ')') + 1 : NULL;
+ }
+
+ dst_argv[argc] = strdup(buf);
+ if (!dst_argv[argc])
+ break;
+ }
+
+ dst_argv[argc] = NULL;
+
+ return src_argv[argc] == NULL;
+}
+
+static bool
+format_command(char dst[], const char *src_argv[], enum format_flags flags)
+{
+ const char *dst_argv[SIZEOF_ARG * 2] = { NULL };
+ int bufsize = 0;
+ int argc;
+
+ if (!format_argv(dst_argv, src_argv, flags)) {
+ free_argv(dst_argv);
+ return FALSE;
+ }
+
+ for (argc = 0; dst_argv[argc] && bufsize < SIZEOF_STR; argc++) {
+ if (bufsize > 0)
+ dst[bufsize++] = ' ';
+ bufsize = sq_quote(dst, bufsize, dst_argv[argc]);
+ }
+
+ if (bufsize < SIZEOF_STR)
+ dst[bufsize] = 0;
+ free_argv(dst_argv);
+
+ return src_argv[argc] == NULL && bufsize < SIZEOF_STR;
+}
+
+static void
+end_update(struct view *view, bool force)
{
if (!view->pipe)
return;
+ while (!view->ops->read(view, NULL))
+ if (!force)
+ return;
set_nonblocking_input(FALSE);
- if (view->pipe == stdin)
- fclose(view->pipe);
- else
- pclose(view->pipe);
+ done_io(view->pipe);
view->pipe = NULL;
}
+static void
+setup_update(struct view *view, const char *vid)
+{
+ set_nonblocking_input(TRUE);
+ reset_view(view);
+ string_copy_rev(view->vid, vid);
+ view->pipe = &view->io;
+ view->start_time = time(NULL);
+}
+
static bool
-begin_update(struct view *view)
+begin_update(struct view *view, bool refresh)
{
- if (view->pipe)
- end_update(view);
+ if (init_io_fd(&view->io, opt_pipe)) {
+ opt_pipe = NULL;
- if (opt_cmd[0]) {
- string_copy(view->cmd, opt_cmd);
- opt_cmd[0] = 0;
+ } else if (opt_cmd[0]) {
+ if (!run_io(&view->io, IO_RD, opt_cmd))
+ return FALSE;
/* When running random commands, initially show the
* command in the title. However, it maybe later be
* overwritten if a commit line is selected. */
if (view == VIEW(REQ_VIEW_PAGER))
- string_copy(view->ref, view->cmd);
+ string_copy(view->ref, opt_cmd);
else
view->ref[0] = 0;
+ opt_cmd[0] = 0;
+
+ } else if (refresh) {
+ if (!start_io(&view->io))
+ return FALSE;
} else if (view == VIEW(REQ_VIEW_TREE)) {
const char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt;
else if (sq_quote(path, 0, opt_path) >= sizeof(path))
return FALSE;
- if (!string_format(view->cmd, format, view->id, path))
+ if (!run_io_format(&view->io, format, view->id, path))
return FALSE;
} else {
const char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt;
const char *id = view->id;
- if (!string_format(view->cmd, format, id, id, id, id, id))
+ if (!run_io_format(&view->io, format, id, id, id, id, id))
return FALSE;
/* Put the current ref_* value to the view title ref
string_copy_rev(view->ref, view->id);
}
- /* Special case for the pager view. */
- if (opt_pipe) {
- view->pipe = opt_pipe;
- opt_pipe = NULL;
- } else {
- view->pipe = popen(view->cmd, "r");
- }
-
- if (!view->pipe)
- return FALSE;
-
- set_nonblocking_input(TRUE);
-
- view->offset = 0;
- view->lines = 0;
- view->lineno = 0;
- string_copy_rev(view->vid, view->id);
-
- if (view->line) {
- int i;
-
- for (i = 0; i < view->lines; i++)
- if (view->line[i].data)
- free(view->line[i].data);
-
- free(view->line);
- view->line = NULL;
- }
-
- view->start_time = time(NULL);
+ setup_update(view, view->id);
return TRUE;
}
static bool
update_view(struct view *view)
{
- char in_buffer[BUFSIZ];
char out_buffer[BUFSIZ * 2];
char *line;
/* The number of lines to read. If too low it will cause too much
if (!realloc_lines(view, view->lines + lines))
goto alloc_error;
- while ((line = fgets(in_buffer, sizeof(in_buffer), view->pipe))) {
+ while ((line = io_gets(view->pipe))) {
size_t linelen = strlen(line);
if (linelen)
}
}
+ if (io_error(view->pipe)) {
+ report("Failed to read: %s", io_strerror(view->pipe));
+ end_update(view, TRUE);
+
+ } else if (io_eof(view->pipe)) {
+ report("");
+ end_update(view, FALSE);
+ }
+
if (!view_is_displayed(view))
- goto check_pipe;
+ return TRUE;
if (view == VIEW(REQ_VIEW_TREE)) {
/* Clear the view and redraw everything since the tree sorting
/* Update the title _after_ the redraw so that if the redraw picks up a
* commit reference in view->ref it'll be available here. */
update_view_title(view);
-
-check_pipe:
- if (ferror(view->pipe)) {
- report("Failed to read: %s", strerror(errno));
- goto end;
-
- } else if (feof(view->pipe)) {
- report("");
- goto end;
- }
-
return TRUE;
alloc_error:
report("Allocation failure");
-
-end:
- if (view->ops->read(view, NULL))
- end_update(view);
+ end_update(view, TRUE);
return FALSE;
}
}
static struct line *
-add_line_text(struct view *view, char *data, enum line_type type)
+add_line_text(struct view *view, const char *text, enum line_type type)
{
- if (data)
- data = strdup(data);
+ char *data = text ? strdup(text) : NULL;
return data ? add_line_data(view, data, type) : NULL;
}
OPEN_BACKGROUNDED = 2, /* Backgrounded. */
OPEN_RELOAD = 4, /* Reload view even if it is the current. */
OPEN_NOMAXIMIZE = 8, /* Do not maximize the current view. */
+ OPEN_REFRESH = 16, /* Refresh view using previous command. */
};
static void
{
bool backgrounded = !!(flags & OPEN_BACKGROUNDED);
bool split = !!(flags & OPEN_SPLIT);
- bool reload = !!(flags & OPEN_RELOAD);
- bool nomaximize = !!(flags & OPEN_NOMAXIMIZE);
+ bool reload = !!(flags & (OPEN_RELOAD | OPEN_REFRESH));
+ bool nomaximize = !!(flags & (OPEN_NOMAXIMIZE | OPEN_REFRESH));
struct view *view = VIEW(request);
int nviews = displayed_views();
struct view *base_view = display[0];
(nviews == 1 && base_view != display[0]))
resize_display();
+ if (view->pipe)
+ end_update(view, TRUE);
+
if (view->ops->open) {
if (!view->ops->open(view)) {
report("Failed to load %s view", view->name);
}
} else if ((reload || strcmp(view->vid, view->id)) &&
- !begin_update(view)) {
+ !begin_update(view, flags & OPEN_REFRESH)) {
report("Failed to load %s view", view->name);
return;
}
* the screen. */
werase(view->win);
report("");
- } else {
+ } else if (view_is_displayed(view)) {
redraw_view(view);
report("");
}
update_view_title(view);
}
+static bool
+run_confirm(const char *cmd, const char *prompt)
+{
+ bool confirmation = prompt_yesno(prompt);
+
+ if (confirmation)
+ system(cmd);
+
+ return confirmation;
+}
+
static void
open_external_viewer(const char *cmd)
{
endwin(); /* restore original tty modes */
system(cmd);
fprintf(stderr, "Press Enter to continue");
- getc(stdin);
+ getc(opt_tty);
reset_prog_mode();
redraw_display();
}
{
char cmd[SIZEOF_STR];
char file_sq[SIZEOF_STR];
- char *editor;
+ const char *editor;
char *prefix = from_root ? opt_cdup : "";
editor = getenv("GIT_EDITOR");
{
struct run_request *req = get_run_request(request);
char buf[SIZEOF_STR * 2];
- size_t bufpos;
- char *cmd;
if (!req) {
report("Unknown run request");
return;
}
- bufpos = 0;
- cmd = req->cmd;
-
- while (cmd) {
- char *next = strstr(cmd, "%(");
- int len = next - cmd;
- char *value;
-
- if (!next) {
- len = strlen(cmd);
- value = "";
-
- } else if (!strncmp(next, "%(head)", 7)) {
- value = ref_head;
-
- } else if (!strncmp(next, "%(commit)", 9)) {
- value = ref_commit;
-
- } else if (!strncmp(next, "%(blob)", 7)) {
- value = ref_blob;
-
- } else {
- report("Unknown replacement in run request: `%s`", req->cmd);
- return;
- }
-
- if (!string_format_from(buf, &bufpos, "%.*s%s", len, cmd, value))
- return;
-
- if (next)
- next = strchr(next, ')') + 1;
- cmd = next;
- }
-
- open_external_viewer(buf);
+ if (format_command(buf, req->argv, FORMAT_ALL))
+ open_external_viewer(buf);
}
/*
open_run_request(request);
/* FIXME: When all views can refresh always do this. */
if (view == VIEW(REQ_VIEW_STATUS) ||
+ view == VIEW(REQ_VIEW_MAIN) ||
+ view == VIEW(REQ_VIEW_LOG) ||
view == VIEW(REQ_VIEW_STAGE))
request = REQ_REFRESH;
else
redraw_display();
break;
- case REQ_PROMPT:
- /* Always reload^Wrerun commands from the prompt. */
- open_view(view, opt_request, OPEN_RELOAD);
- break;
-
case REQ_SEARCH:
case REQ_SEARCH_BACK:
search_view(view, request);
view = &views[i];
if (view->pipe)
report("Stopped loading the %s view", view->name),
- end_update(view);
+ end_update(view, TRUE);
}
break;
report("Nothing to edit");
break;
-
case REQ_ENTER:
report("Nothing to enter");
break;
-
case REQ_VIEW_CLOSE:
/* XXX: Mark closed views by letting view->parent point to the
* view itself. Parents to closed view should never be
view->parent = view;
resize_display();
redraw_display();
+ report("");
break;
}
/* Fall-through */
return FALSE;
default:
- /* An unknown key will show most commonly used commands. */
report("Unknown key, press 'h' for help");
return TRUE;
}
*/
static bool
-pager_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
+pager_draw(struct view *view, struct line *line, unsigned int lineno)
{
- static char spaces[] = " ";
char *text = line->data;
- int col = 0;
-
- if (opt_line_number) {
- col += draw_lineno(view, lineno, view->width, selected);
- if (col >= view->width)
- return TRUE;
- }
-
- if (!selected)
- wattrset(view->win, get_line_attr(line->type));
-
- if (opt_tab_size < TABSIZE) {
- int col_offset = col;
- col = 0;
- while (text && col_offset + col < view->width) {
- int cols_max = view->width - col_offset - col;
- char *pos = text;
- int cols;
-
- if (*text == '\t') {
- text++;
- assert(sizeof(spaces) > TABSIZE);
- pos = spaces;
- cols = opt_tab_size - (col % opt_tab_size);
-
- } else {
- text = strchr(text, '\t');
- cols = line ? text - pos : strlen(pos);
- }
-
- waddnstr(view->win, pos, MIN(cols, cols_max));
- col += cols;
- }
-
- } else {
- draw_text(view, text, view->width - col, TRUE, selected);
- }
+ if (opt_line_number && draw_lineno(view, lineno))
+ return TRUE;
+ draw_text(view, line->type, text, TRUE);
return TRUE;
}
static bool
-add_describe_ref(char *buf, size_t *bufpos, char *commit_id, const char *sep)
+add_describe_ref(char *buf, size_t *bufpos, const char *commit_id, const char *sep)
{
char refbuf[SIZEOF_STR];
char *ref = NULL;
do {
struct ref *ref = refs[refpos];
- char *fmt = ref->tag ? "%s[%s]" :
- ref->remote ? "%s<%s>" : "%s%s";
+ const char *fmt = ref->tag ? "%s[%s]" :
+ ref->remote ? "%s<%s>" : "%s%s";
if (!string_format_from(buf, &bufpos, fmt, sep, ref->name))
return;
pager_select,
};
+static enum request
+log_request(struct view *view, enum request request, struct line *line)
+{
+ switch (request) {
+ case REQ_REFRESH:
+ load_refs();
+ open_view(view, REQ_VIEW_LOG, OPEN_REFRESH);
+ return REQ_NONE;
+ default:
+ return pager_request(view, request, line);
+ }
+}
+
+static struct view_ops log_ops = {
+ "line",
+ NULL,
+ pager_read,
+ pager_draw,
+ log_request,
+ pager_grep,
+ pager_select,
+};
+
/*
* Help backend
add_line_text(view, "Quick reference for tig keybindings:", LINE_DEFAULT);
for (i = 0; i < ARRAY_SIZE(req_info); i++) {
- char *key;
+ const char *key;
if (req_info[i].request == REQ_NONE)
continue;
for (i = 0; i < run_requests; i++) {
struct run_request *req = get_run_request(REQ_NONE + i + 1);
- char *key;
+ const char *key;
+ char cmd[SIZEOF_STR];
+ size_t bufpos;
+ int argc;
if (!req)
continue;
if (!*key)
key = "(no key defined)";
+ for (bufpos = 0, argc = 0; req->argv[argc]; argc++)
+ if (!string_format_from(cmd, &bufpos, "%s%s",
+ argc ? " " : "", req->argv[argc]))
+ return REQ_NONE;
+
if (!string_format(buf, " %-10s %-14s `%s`",
- keymap_table[req->keymap].name,
- key, req->cmd))
+ keymap_table[req->keymap].name, key, cmd))
continue;
add_line_text(view, buf, LINE_DEFAULT);
}
static void
-push_tree_stack_entry(char *name, unsigned long lineno)
+push_tree_stack_entry(const char *name, unsigned long lineno)
{
struct tree_stack_entry *entry = calloc(1, sizeof(*entry));
size_t pathlen = strlen(opt_path);
#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)
+tree_compare_entry(enum line_type type1, const char *name1,
+ enum line_type type2, const char *name2)
{
if (type1 != type2) {
if (type1 == LINE_TREE_DIR)
return strcmp(name1, name2);
}
-static char *
+static const char *
tree_path(struct line *line)
{
- char *path = line->data;
+ const char *path = line->data;
return path + SIZEOF_TREE_ATTR;
}
/* Skip "Directory ..." and ".." line. */
for (pos = 1 + !!*opt_path; pos < view->lines; pos++) {
struct line *line = &view->line[pos];
- char *path1 = tree_path(line);
+ const char *path1 = tree_path(line);
char *path2 = text + SIZEOF_TREE_ATTR;
int cmp = tree_compare_entry(line->type, path1, type, path2);
{
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);
+ switch (request) {
+ case REQ_VIEW_BLAME:
+ if (line->type != LINE_TREE_FILE) {
+ report("Blame only supported for files");
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];
+
+ case REQ_EDIT:
+ if (line->type != LINE_TREE_FILE) {
+ report("Edit only supported for files");
+ } else if (!is_head_commit(view->vid)) {
+ report("Edit only supported for files in the current work tree");
} else {
+ open_editor(TRUE, opt_file);
+ }
+ return REQ_NONE;
+
+ case REQ_TREE_PARENT:
+ if (!*opt_path) {
/* quit view if at top of tree */
return REQ_VIEW_CLOSE;
}
- }
- if (request != REQ_ENTER)
+ /* fake 'cd ..' */
+ line = &view->line[1];
+ break;
+
+ case REQ_ENTER:
+ break;
+
+ default:
return request;
+ }
/* Cleanup the stack if the tree view is at a different tree. */
while (!*opt_path && tree_stack)
pop_tree_stack_entry();
} else {
- char *basename = tree_path(line);
+ const char *basename = tree_path(line);
push_tree_stack_entry(basename, view->lineno);
}
if (line->type == LINE_TREE_FILE) {
string_copy_rev(ref_blob, text);
+ string_format(opt_file, "%s%s", opt_path, tree_path(line));
} else if (line->type != LINE_TREE_DIR) {
return;
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"
+#define BLAME_INCREMENTAL_CMD "git blame --incremental %s -- %s"
static bool
blame_open(struct view *view)
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;
+ if (*opt_ref || !init_io_fd(&view->io, fopen(opt_file, "r"))) {
+ const char *id = *opt_ref ? ref : "HEAD";
- for (i = 0; i < view->lines; i++)
- free(view->line[i].data);
- free(view->line);
+ if (!run_io_format(&view->io, BLAME_CAT_FILE_CMD, id, path))
+ return FALSE;
}
- 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);
+ setup_update(view, opt_file);
+ string_format(view->ref, "%s ...", opt_file);
return TRUE;
}
}
static bool
-parse_number(char **posref, size_t *number, size_t min, size_t max)
+parse_number(const char **posref, size_t *number, size_t min, size_t max)
{
- char *pos = *posref;
+ const char *pos = *posref;
*posref = NULL;
pos = strchr(pos + 1, ' ');
}
static struct blame_commit *
-parse_blame_commit(struct view *view, char *text, int *blamed)
+parse_blame_commit(struct view *view, const char *text, int *blamed)
{
struct blame_commit *commit;
struct blame *blame;
- char *pos = text + SIZEOF_REV - 1;
+ const char *pos = text + SIZEOF_REV - 1;
size_t lineno;
size_t group;
blame = line->data;
blame->commit = commit;
- blame->header = !group;
line->dirty = 1;
}
}
static bool
-blame_read_file(struct view *view, char *line)
+blame_read_file(struct view *view, const char *line, bool *read_file)
{
if (!line) {
- FILE *pipe = NULL;
+ char ref[SIZEOF_STR] = "";
+ char path[SIZEOF_STR];
+ struct io io = {};
- if (view->lines > 0)
- pipe = popen(view->cmd, "r");
- else if (!view->parent)
+ if (view->lines == 0 && !view->parent)
die("No blame exist for %s", view->vid);
- view->cmd[0] = 0;
- if (!pipe) {
+
+ if (view->lines == 0 ||
+ sq_quote(path, 0, opt_file) >= sizeof(path) ||
+ (*opt_ref && sq_quote(ref, 0, opt_ref) >= sizeof(ref)) ||
+ !run_io_format(&io, BLAME_INCREMENTAL_CMD, ref, path)) {
report("Failed to load blame data");
return TRUE;
}
- fclose(view->pipe);
- view->pipe = pipe;
+ done_io(view->pipe);
+ view->io = io;
+ *read_file = FALSE;
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;
static struct blame_commit *commit = NULL;
static int blamed = 0;
static time_t author_time;
+ static bool read_file = TRUE;
- if (*view->cmd)
- return blame_read_file(view, line);
+ if (read_file)
+ return blame_read_file(view, line, &read_file);
if (!line) {
/* Reset all! */
commit = NULL;
blamed = 0;
+ read_file = TRUE;
string_format(view->ref, "%s", view->vid);
if (view_is_displayed(view)) {
update_view_title(view);
}
static bool
-blame_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
+blame_draw(struct view *view, struct line *line, unsigned int lineno)
{
struct blame *blame = line->data;
- int col = 0;
-
- if (opt_date) {
- struct tm *time = blame->commit && *blame->commit->filename
- ? &blame->commit->time : NULL;
+ struct tm *time = NULL;
+ const char *id = NULL, *author = NULL;
- col += draw_date(view, time, view->width, selected);
- if (col >= view->width)
- return TRUE;
+ if (blame->commit && *blame->commit->filename) {
+ id = blame->commit->id;
+ author = blame->commit->author;
+ time = &blame->commit->time;
}
- if (opt_author) {
- int max = MIN(AUTHOR_COLS - 1, view->width - col);
+ if (opt_date && draw_date(view, time))
+ return TRUE;
- 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);
- }
+ if (opt_author &&
+ draw_field(view, LINE_MAIN_AUTHOR, author, opt_author_cols, TRUE))
+ return TRUE;
- {
- 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 (draw_field(view, LINE_BLAME_ID, id, ID_COLS, FALSE))
+ return TRUE;
- col += draw_lineno(view, lineno, view->width - col, selected);
- if (col >= view->width)
+ if (draw_lineno(view, lineno))
return TRUE;
- col += draw_text(view, blame->text, view->width - col, TRUE, selected);
+ draw_text(view, LINE_DEFAULT, blame->text, TRUE);
return TRUE;
}
struct blame *blame = line->data;
switch (request) {
+ case REQ_VIEW_BLAME:
+ if (!blame->commit || !strcmp(blame->commit->id, NULL_ID)) {
+ report("Commit ID unknown");
+ break;
+ }
+ string_copy(opt_ref, blame->commit->id);
+ open_view(view, REQ_VIEW_BLAME, OPEN_REFRESH);
+ return request;
+
case REQ_ENTER:
if (!blame->commit) {
report("No commit loaded yet");
break;
}
+ if (view_is_displayed(VIEW(REQ_VIEW_DIFF)) &&
+ !strcmp(blame->commit->id, VIEW(REQ_VIEW_DIFF)->ref))
+ break;
+
if (!strcmp(blame->commit->id, NULL_ID)) {
char path[SIZEOF_STR];
struct blame_commit *commit = blame->commit;
regmatch_t pmatch;
-#define MATCH(text) \
- (*text && regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH)
+#define MATCH(text, on) \
+ (on && *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))
+ if (MATCH(commit->title, 1) ||
+ MATCH(commit->author, opt_author) ||
+ MATCH(commit->id, opt_date))
return TRUE;
if (strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time) &&
- MATCH(buf))
+ MATCH(buf, 1))
return TRUE;
}
- return MATCH(blame->text);
+ return MATCH(blame->text, 1);
#undef MATCH
}
static char status_onbranch[SIZEOF_STR];
static struct status stage_status;
static enum line_type stage_line_type;
+static size_t stage_chunks;
+static int *stage_chunk;
+
+/* This should work even for the "On branch" line. */
+static inline bool
+status_has_none(struct view *view, struct line *line)
+{
+ return line < view->line + view->lines && !line[1].data;
+}
/* 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)
+status_get_diff(struct status *file, const 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;
+ const char *old_mode = buf + 1;
+ const char *new_mode = buf + 8;
+ const char *old_rev = buf + 15;
+ const char *new_rev = buf + 56;
+ const char *status = buf + 97;
if (bufsize < 99 ||
old_mode[-1] != ':' ||
#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"
+ "git ls-files -z --others --exclude-standard"
#define STATUS_LIST_NO_HEAD_CMD \
- "git ls-files -z --cached --exclude-per-directory=.gitignore"
+ "git ls-files -z --cached --exclude-standard"
#define STATUS_DIFF_INDEX_SHOW_CMD \
"git diff-index --root --patch-with-stat -C -M --cached HEAD -- %s %s 2>/dev/null"
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;
+ reset_view(view);
if (!realloc_lines(view, view->line_size + 7))
return FALSE;
add_line_data(view, NULL, LINE_STAT_HEAD);
- if (opt_no_head)
+ if (is_initial_commit())
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;
+ system("git update-index -q --refresh >/dev/null 2>/dev/null");
- cmdsize = strlen(indexcmd);
- if (opt_no_head &&
- (!string_format_from(indexcmd, &cmdsize, " %s", "--exclude-from=") ||
- sq_quote(indexcmd, cmdsize, exclude) >= sizeof(indexcmd)))
+ if (is_initial_commit()) {
+ if (!status_run(view, STATUS_LIST_NO_HEAD_CMD, 'A', LINE_STAT_STAGED))
return FALSE;
+ } else if (!status_run(view, STATUS_DIFF_INDEX_CMD, 0, LINE_STAT_STAGED)) {
+ return FALSE;
}
- system("git update-index -q --refresh >/dev/null 2>/dev/null");
-
- 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))
+ if (!status_run(view, STATUS_DIFF_FILES_CMD, 0, LINE_STAT_UNSTAGED) ||
+ !status_run(view, STATUS_LIST_OTHER_CMD, '?', LINE_STAT_UNTRACKED))
return FALSE;
/* If all went well restore the previous line number to stay in
}
static bool
-status_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
+status_draw(struct view *view, struct line *line, unsigned int lineno)
{
struct status *status = line->data;
- char *text;
- int col = 0;
-
- if (selected) {
- /* No attributes. */
-
- } 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));
- }
+ enum line_type type;
+ const char *text;
if (!status) {
switch (line->type) {
case LINE_STAT_STAGED:
+ type = LINE_STAT_SECTION;
text = "Changes to be committed:";
break;
case LINE_STAT_UNSTAGED:
+ type = LINE_STAT_SECTION;
text = "Changed but not updated:";
break;
case LINE_STAT_UNTRACKED:
+ type = LINE_STAT_SECTION;
text = "Untracked files:";
break;
case LINE_STAT_NONE:
+ type = LINE_DEFAULT;
text = " (no files)";
break;
case LINE_STAT_HEAD:
+ type = LINE_STAT_HEAD;
text = status_onbranch;
break;
@@ -4056,15 +4349,16 @@ status_draw(struct view *view, struct line *line, unsigned int lineno, bool sele
return FALSE;
}
} else {
- char buf[] = { status->status, ' ', ' ', ' ', 0 };
+ static char buf[] = { '?', ' ', ' ', ' ', 0 };
- col += draw_text(view, buf, view->width, TRUE, selected);
- if (!selected)
- wattrset(view->win, A_NORMAL);
+ buf[0] = status->status;
+ if (draw_text(view, line->type, buf, TRUE))
+ return TRUE;
+ type = LINE_DEFAULT;
text = status->new.name;
}
- draw_text(view, text, view->width - col, TRUE, selected);
+ draw_text(view, type, text, TRUE);
return TRUE;
}
struct status *status = line->data;
char oldpath[SIZEOF_STR] = "";
char newpath[SIZEOF_STR] = "";
- char *info;
+ const char *info;
size_t cmdsize = 0;
enum open_flags split;
switch (line->type) {
case LINE_STAT_STAGED:
- if (opt_no_head) {
+ if (is_initial_commit()) {
if (!string_format_from(opt_cmd, &cmdsize,
STATUS_DIFF_NO_HEAD_SHOW_CMD,
newpath))
return REQ_NONE;
}
+ if (!suffixcmp(status->new.name, -1, "/")) {
+ report("Cannot display a directory");
+ return REQ_NONE;
+ }
+
opt_pipe = fopen(status->new.name, "r");
info = "Untracked file %s";
break;
}
stage_line_type = line->type;
+ stage_chunks = 0;
string_format(VIEW(REQ_VIEW_STAGE)->ref, info, stage_status.new.name);
}
return TRUE;
}
+static bool
+status_revert(struct status *status, enum line_type type, bool has_none)
+{
+ if (!status || type != LINE_STAT_UNSTAGED) {
+ if (type == LINE_STAT_STAGED) {
+ report("Cannot revert changes to staged files");
+ } else if (type == LINE_STAT_UNTRACKED) {
+ report("Cannot revert changes to untracked files");
+ } else if (has_none) {
+ report("Nothing to revert");
+ } else {
+ report("Cannot revert changes to multiple files");
+ }
+ return FALSE;
+
+ } else {
+ char cmd[SIZEOF_STR];
+ char file_sq[SIZEOF_STR];
+
+ if (sq_quote(file_sq, 0, status->old.name) >= sizeof(file_sq) ||
+ !string_format(cmd, "git checkout -- %s%s", opt_cdup, file_sq))
+ return FALSE;
+
+ return run_confirm(cmd, "Are you sure you want to overwrite any changes?");
+ }
+}
+
static enum request
status_request(struct view *view, enum request request, struct line *line)
{
return REQ_NONE;
break;
+ case REQ_STATUS_REVERT:
+ if (!status_revert(status, line->type, status_has_none(view, line)))
+ return REQ_NONE;
+ break;
+
case REQ_STATUS_MERGE:
if (!status || status->status != 'U') {
report("Merging only possible for files with unmerged status ('U').");
case REQ_EDIT:
if (!status)
return request;
+ if (status->status == 'D') {
+ report("File has been deleted.");
+ return REQ_NONE;
+ }
open_editor(status->status != '?', status->new.name);
break;
{
struct status *status = line->data;
char file[SIZEOF_STR] = "all files";
- char *text;
- char *key;
+ const char *text;
+ const char *key;
if (status && !string_format(file, "'%s'", status->new.name))
return;
return FALSE;
for (state = S_STATUS; state < S_END; state++) {
- char *text;
+ const char *text;
switch (state) {
case S_NAME: text = status->new.name; break;
static bool
stage_diff_line(FILE *pipe, struct line *line)
{
- char *buf = line->data;
+ const char *buf = line->data;
size_t bufsize = strlen(buf);
size_t written = 0;
}
static bool
-stage_update_chunk(struct view *view, struct line *chunk)
+stage_apply_chunk(struct view *view, struct line *chunk, bool revert)
{
char cmd[SIZEOF_STR];
size_t cmdsize = 0;
return FALSE;
if (!string_format_from(cmd, &cmdsize,
- "git apply --whitespace=nowarn --cached %s - && "
+ "git apply --whitespace=nowarn %s %s - && "
"git update-index -q --unmerged --refresh 2>/dev/null",
- stage_line_type == LINE_STAT_STAGED ? "-R" : ""))
+ revert ? "" : "--cached",
+ revert || stage_line_type == LINE_STAT_STAGED ? "-R" : ""))
return FALSE;
pipe = popen(cmd, "w");
{
struct line *chunk = NULL;
- if (!opt_no_head && stage_line_type != LINE_STAT_UNTRACKED)
+ if (!is_initial_commit() && stage_line_type != LINE_STAT_UNTRACKED)
chunk = stage_diff_find(view, line, LINE_DIFF_CHUNK);
if (chunk) {
- if (!stage_update_chunk(view, chunk)) {
+ if (!stage_apply_chunk(view, chunk, FALSE)) {
report("Failed to apply chunk");
return FALSE;
}
return TRUE;
}
+static bool
+stage_revert(struct view *view, struct line *line)
+{
+ struct line *chunk = NULL;
+
+ if (!is_initial_commit() && stage_line_type == LINE_STAT_UNSTAGED)
+ chunk = stage_diff_find(view, line, LINE_DIFF_CHUNK);
+
+ if (chunk) {
+ if (!prompt_yesno("Are you sure you want to revert changes?"))
+ return FALSE;
+
+ if (!stage_apply_chunk(view, chunk, TRUE)) {
+ report("Failed to revert chunk");
+ return FALSE;
+ }
+ return TRUE;
+
+ } else {
+ return status_revert(stage_status.status ? &stage_status : NULL,
+ stage_line_type, FALSE);
+ }
+}
+
+
+static void
+stage_next(struct view *view, struct line *line)
+{
+ int i;
+
+ if (!stage_chunks) {
+ static size_t alloc = 0;
+ int *tmp;
+
+ for (line = view->line; line < view->line + view->lines; line++) {
+ if (line->type != LINE_DIFF_CHUNK)
+ continue;
+
+ tmp = realloc_items(stage_chunk, &alloc,
+ stage_chunks, sizeof(*tmp));
+ if (!tmp) {
+ report("Allocation failure");
+ return;
+ }
+
+ stage_chunk = tmp;
+ stage_chunk[stage_chunks++] = line - view->line;
+ }
+ }
+
+ for (i = 0; i < stage_chunks; i++) {
+ if (stage_chunk[i] > view->lineno) {
+ do_scroll_view(view, stage_chunk[i] - view->lineno);
+ report("Chunk %d of %d", i + 1, stage_chunks);
+ return;
+ }
+ }
+
+ report("No next chunk found");
+}
+
static enum request
stage_request(struct view *view, enum request request, struct line *line)
{
return REQ_NONE;
break;
+ case REQ_STATUS_REVERT:
+ if (!stage_revert(view, line))
+ return REQ_NONE;
+ break;
+
+ case REQ_STAGE_NEXT:
+ if (stage_line_type == LINE_STAT_UNTRACKED) {
+ report("File is untracked; press %s to add",
+ get_key(REQ_STATUS_UPDATE));
+ return REQ_NONE;
+ }
+ stage_next(view, line);
+ return REQ_NONE;
+
case REQ_EDIT:
if (!stage_status.new.name[0])
return request;
+ if (stage_status.status == 'D') {
+ report("File has been deleted.");
+ return REQ_NONE;
+ }
open_editor(stage_status.status != '?', stage_status.new.name);
break;
if (!status_exists(&stage_status, stage_line_type))
return REQ_VIEW_CLOSE;
- if (stage_line_type == LINE_STAT_UNTRACKED)
+ if (stage_line_type == LINE_STAT_UNTRACKED) {
+ if (!suffixcmp(stage_status.new.name, -1, "/")) {
+ report("Cannot display a directory");
+ return REQ_NONE;
+ }
+
opt_pipe = fopen(stage_status.new.name, "r");
- else
- string_copy(opt_cmd, view->cmd);
- open_view(view, REQ_VIEW_STAGE, OPEN_RELOAD | OPEN_NOMAXIMIZE);
+ }
+ open_view(view, REQ_VIEW_STAGE, OPEN_REFRESH);
return REQ_NONE;
}
commit->graph[commit->graph_size++] = symbol;
}
+static void
+clear_rev_graph(struct rev_graph *graph)
+{
+ graph->boundary = 0;
+ graph->size = graph->pos = 0;
+ graph->commit = NULL;
+ memset(graph->parents, 0, sizeof(*graph->parents));
+}
+
static void
done_rev_graph(struct rev_graph *graph)
{
}
}
- graph->size = graph->pos = 0;
- graph->commit = NULL;
- memset(graph->parents, 0, sizeof(*graph->parents));
+ clear_rev_graph(graph);
}
static void
-push_rev_graph(struct rev_graph *graph, char *parent)
+push_rev_graph(struct rev_graph *graph, const char *parent)
{
int i;
};
enum { DEFAULT, RSHARP, RDIAG, LDIAG };
static struct rev_filler fillers[] = {
- { ' ', REVGRAPH_LINE },
+ { ' ', '|' },
{ '`', '.' },
{ '\'', ' ' },
{ '/', ' ' },
struct rev_filler *filler;
size_t i;
+ if (opt_line_graphics)
+ fillers[DEFAULT].line = line_graphics[LINE_GRAPHIC_VLINE];
+
filler = &fillers[DEFAULT];
for (i = 0; i < graph->pos; i++) {
*/
static bool
-main_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
+main_draw(struct view *view, struct line *line, unsigned int lineno)
{
struct commit *commit = line->data;
- enum line_type type;
- int col = 0;
if (!*commit->author)
return FALSE;
- if (selected) {
- type = LINE_CURSOR;
- } else {
- type = LINE_MAIN_COMMIT;
- }
-
- if (opt_date) {
- col += draw_date(view, &commit->time, view->width, selected);
- if (col >= view->width)
- return TRUE;
- }
- if (type != LINE_CURSOR)
- wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
-
- if (opt_author) {
- int max_len;
-
- max_len = view->width - col;
- if (max_len > AUTHOR_COLS - 1)
- max_len = AUTHOR_COLS - 1;
- draw_text(view, commit->author, max_len, TRUE, selected);
- col += AUTHOR_COLS;
- if (col >= view->width)
- return TRUE;
- }
-
- if (opt_rev_graph && commit->graph_size) {
- size_t graph_size = view->width - col;
- size_t i;
-
- if (type != LINE_CURSOR)
- wattrset(view->win, get_line_attr(LINE_MAIN_REVGRAPH));
- wmove(view->win, lineno, col);
- if (graph_size > commit->graph_size)
- graph_size = commit->graph_size;
- /* Using waddch() instead of waddnstr() ensures that
- * they'll be rendered correctly for the cursor line. */
- for (i = 0; i < graph_size; i++)
- waddch(view->win, commit->graph[i]);
+ if (opt_date && draw_date(view, &commit->time))
+ return TRUE;
- col += commit->graph_size + 1;
- if (col >= view->width)
- return TRUE;
- waddch(view->win, ' ');
- }
- if (type != LINE_CURSOR)
- wattrset(view->win, A_NORMAL);
+ if (opt_author &&
+ draw_field(view, LINE_MAIN_AUTHOR, commit->author, opt_author_cols, TRUE))
+ return TRUE;
- wmove(view->win, lineno, col);
+ if (opt_rev_graph && commit->graph_size &&
+ draw_graphic(view, LINE_MAIN_REVGRAPH, commit->graph, commit->graph_size))
+ return TRUE;
if (opt_show_refs && commit->refs) {
size_t i = 0;
do {
- if (type == LINE_CURSOR)
- ;
- else if (commit->refs[i]->head)
- wattrset(view->win, get_line_attr(LINE_MAIN_HEAD));
+ enum line_type type;
+
+ if (commit->refs[i]->head)
+ type = LINE_MAIN_HEAD;
else if (commit->refs[i]->ltag)
- wattrset(view->win, get_line_attr(LINE_MAIN_LOCAL_TAG));
+ type = LINE_MAIN_LOCAL_TAG;
else if (commit->refs[i]->tag)
- wattrset(view->win, get_line_attr(LINE_MAIN_TAG));
+ type = LINE_MAIN_TAG;
else if (commit->refs[i]->tracked)
- wattrset(view->win, get_line_attr(LINE_MAIN_TRACKED));
+ type = LINE_MAIN_TRACKED;
else if (commit->refs[i]->remote)
- wattrset(view->win, get_line_attr(LINE_MAIN_REMOTE));
+ type = LINE_MAIN_REMOTE;
else
- wattrset(view->win, get_line_attr(LINE_MAIN_REF));
-
- col += draw_text(view, "[", view->width - col, TRUE, selected);
- col += draw_text(view, commit->refs[i]->name, view->width - col,
- TRUE, selected);
- col += draw_text(view, "]", view->width - col, TRUE, selected);
- if (type != LINE_CURSOR)
- wattrset(view->win, A_NORMAL);
- col += draw_text(view, " ", view->width - col, TRUE, selected);
- if (col >= view->width)
+ type = LINE_MAIN_REF;
+
+ if (draw_text(view, type, "[", TRUE) ||
+ draw_text(view, type, commit->refs[i]->name, TRUE) ||
+ draw_text(view, type, "]", TRUE))
+ return TRUE;
+
+ if (draw_text(view, LINE_DEFAULT, " ", TRUE))
return TRUE;
} while (commit->refs[i++]->next);
}
- if (type != LINE_CURSOR)
- wattrset(view->win, get_line_attr(type));
-
- draw_text(view, commit->title, view->width - col, TRUE, selected);
+ draw_text(view, LINE_DEFAULT, commit->title, TRUE);
return TRUE;
}
struct commit *commit;
if (!line) {
+ int i;
+
if (!view->lines && !view->parent)
die("No revisions match the given arguments.");
+ if (view->lines > 0) {
+ commit = view->line[view->lines - 1].data;
+ if (!*commit->author) {
+ view->lines--;
+ free(commit);
+ graph->commit = NULL;
+ }
+ }
update_rev_graph(graph);
+
+ for (i = 0; i < ARRAY_SIZE(graph_stacks); i++)
+ clear_rev_graph(&graph_stacks[i]);
return TRUE;
}
{
enum open_flags flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT;
- if (request == REQ_ENTER)
+ switch (request) {
+ case REQ_ENTER:
open_view(view, REQ_VIEW_DIFF, flags);
- else
+ break;
+ case REQ_REFRESH:
+ load_refs();
+ open_view(view, REQ_VIEW_MAIN, OPEN_REFRESH);
+ break;
+ default:
return request;
+ }
return REQ_NONE;
}
+static bool
+grep_refs(struct ref **refs, regex_t *regex)
+{
+ regmatch_t pmatch;
+ size_t i = 0;
+
+ if (!refs)
+ return FALSE;
+ do {
+ if (regexec(regex, refs[i]->name, 1, &pmatch, 0) != REG_NOMATCH)
+ return TRUE;
+ } while (refs[i++]->next);
+
+ return FALSE;
+}
+
static bool
main_grep(struct view *view, struct line *line)
{
struct commit *commit = line->data;
- enum { S_TITLE, S_AUTHOR, S_DATE, S_END } state;
+ enum { S_TITLE, S_AUTHOR, S_DATE, S_REFS, S_END } state;
char buf[DATE_COLS + 1];
regmatch_t pmatch;
switch (state) {
case S_TITLE: text = commit->title; break;
- case S_AUTHOR: text = commit->author; break;
+ case S_AUTHOR:
+ if (!opt_author)
+ continue;
+ text = commit->author;
+ break;
case S_DATE:
+ if (!opt_date)
+ continue;
if (!strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time))
continue;
text = buf;
break;
-
+ case S_REFS:
+ if (!opt_show_refs)
+ continue;
+ if (grep_refs(commit->refs, view->regex) == TRUE)
+ return TRUE;
+ continue;
default:
return FALSE;
}
*
* Returns the number of bytes to output from string to satisfy max_width. */
static size_t
-utf8_length(const char *string, size_t max_width, int *trimmed, bool reserve)
+utf8_length(const char *string, int *width, size_t max_width, int *trimmed, bool reserve)
{
const char *start = string;
const char *end = strchr(string, '\0');
unsigned char last_bytes = 0;
- size_t width = 0;
+ size_t last_ucwidth = 0;
+ *width = 0;
*trimmed = 0;
while (string < end) {
@@ -5259,17 +5691,20 @@ utf8_length(const char *string, size_t max_width, int *trimmed, bool reserve)
break;
ucwidth = unicode_width(unicode);
- width += ucwidth;
- if (width > max_width) {
+ *width += ucwidth;
+ if (*width > max_width) {
*trimmed = 1;
- if (reserve && width - ucwidth == max_width) {
+ *width -= ucwidth;
+ if (reserve && *width == max_width) {
string -= last_bytes;
+ *width -= last_ucwidth;
}
break;
}
string += bytes;
last_bytes = bytes;
+ last_ucwidth = ucwidth;
}
return string - start;
/* Initialize the curses library */
if (isatty(STDIN_FILENO)) {
cursed = !!initscr();
+ opt_tty = stdin;
} else {
/* Leave stdin and stdout alone when acting as a pager. */
- FILE *io = fopen("/dev/tty", "r+");
-
- if (!io)
+ opt_tty = fopen("/dev/tty", "r+");
+ if (!opt_tty)
die("Failed to open /dev/tty");
- cursed = !!newterm(NULL, io, io);
+ cursed = !!newterm(NULL, opt_tty, opt_tty);
}
if (!cursed)
/* Enable keyboard mapping */
keypad(status_win, TRUE);
wbkgdset(status_win, get_line_attr(LINE_STATUS));
+
+ TABSIZE = opt_tab_size;
+ if (opt_line_graphics) {
+ line_graphics[LINE_GRAPHIC_VLINE] = ACS_VLINE;
+ }
+}
+
+static bool
+prompt_yesno(const char *prompt)
+{
+ enum { WAIT, STOP, CANCEL } status = WAIT;
+ bool answer = FALSE;
+
+ while (status == WAIT) {
+ struct view *view;
+ int i, key;
+
+ input_mode = TRUE;
+
+ foreach_view (view, i)
+ update_view(view);
+
+ input_mode = FALSE;
+
+ mvwprintw(status_win, 0, 0, "%s [Yy]/[Nn]", prompt);
+ wclrtoeol(status_win);
+
+ /* Refresh, accept single keystroke of input */
+ key = wgetch(status_win);
+ switch (key) {
+ case ERR:
+ break;
+
+ case 'y':
+ case 'Y':
+ answer = TRUE;
+ status = STOP;
+ break;
+
+ case KEY_ESC:
+ case KEY_RETURN:
+ case KEY_ENTER:
+ case KEY_BACKSPACE:
+ case 'n':
+ case 'N':
+ case '\n':
+ default:
+ answer = FALSE;
+ status = CANCEL;
+ }
+ }
+
+ /* Clear the status window */
+ status_empty = FALSE;
+ report("");
+
+ return answer;
}
static char *
static size_t id_refs_alloc = 0;
static size_t id_refs_size = 0;
+static int
+compare_refs(const void *ref1_, const void *ref2_)
+{
+ const struct ref *ref1 = *(const struct ref **)ref1_;
+ const struct ref *ref2 = *(const struct ref **)ref2_;
+
+ if (ref1->tag != ref2->tag)
+ return ref2->tag - ref1->tag;
+ if (ref1->ltag != ref2->ltag)
+ return ref2->ltag - ref2->ltag;
+ if (ref1->head != ref2->head)
+ return ref2->head - ref1->head;
+ if (ref1->tracked != ref2->tracked)
+ return ref2->tracked - ref1->tracked;
+ if (ref1->remote != ref2->remote)
+ return ref2->remote - ref1->remote;
+ return strcmp(ref1->name, ref2->name);
+}
+
static struct ref **
-get_refs(char *id)
+get_refs(const char *id)
{
struct ref ***tmp_id_refs;
struct ref **ref_list = NULL;
}
ref_list = tmp;
- if (ref_list_size > 0)
- ref_list[ref_list_size - 1]->next = 1;
ref_list[ref_list_size] = &refs[i];
-
/* XXX: The properties of the commit chains ensures that we can
* safely modify the shared ref. The repo references will
* always be similar for the same id. */
- ref_list[ref_list_size]->next = 0;
+ ref_list[ref_list_size]->next = 1;
+
ref_list_size++;
}
- if (ref_list)
+ if (ref_list) {
+ qsort(ref_list, ref_list_size, sizeof(*ref_list), compare_refs);
+ ref_list[ref_list_size - 1]->next = 0;
id_refs[id_refs_size++] = ref_list;
+ }
return ref_list;
}
bool check_replace = FALSE;
bool head = FALSE;
- if (!strncmp(name, "refs/tags/", STRING_SIZE("refs/tags/"))) {
- if (!strcmp(name + namelen - 3, "^{}")) {
+ if (!prefixcmp(name, "refs/tags/")) {
+ if (!suffixcmp(name, namelen, "^{}")) {
namelen -= 3;
name[namelen] = 0;
if (refs_size > 0 && refs[refs_size - 1].ltag == TRUE)
namelen -= STRING_SIZE("refs/tags/");
name += STRING_SIZE("refs/tags/");
- } else if (!strncmp(name, "refs/remotes/", STRING_SIZE("refs/remotes/"))) {
+ } else if (!prefixcmp(name, "refs/remotes/")) {
remote = TRUE;
namelen -= STRING_SIZE("refs/remotes/");
name += STRING_SIZE("refs/remotes/");
tracked = !strcmp(opt_remote, name);
- } else if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) {
+ } else if (!prefixcmp(name, "refs/heads/")) {
namelen -= STRING_SIZE("refs/heads/");
name += STRING_SIZE("refs/heads/");
head = !strncmp(opt_head, name, namelen);
} else if (!strcmp(name, "HEAD")) {
- opt_no_head = FALSE;
+ string_ncopy(opt_head_rev, id, idlen);
return OK;
}
if (check_replace && !strcmp(name, refs[refs_size - 1].name)) {
/* it's an annotated tag, replace the previous sha1 with the
* resolved commit id; relies on the fact git-ls-remote lists
- * the commit id of an annotated tag right beofre the commit id
+ * the commit id of an annotated tag right before the commit id
* it points to. */
refs[refs_size - 1].ltag = ltag;
string_copy_rev(refs[refs_size - 1].id, id);
const char *cmd_env = getenv("TIG_LS_REMOTE");
const char *cmd = cmd_env && *cmd_env ? cmd_env : TIG_LS_REMOTE;
+ if (!*opt_git_dir)
+ return OK;
+
+ while (refs_size > 0)
+ free(refs[--refs_size].name);
+ while (id_refs_size > 0)
+ free(id_refs[--id_refs_size]);
+
return read_properties(popen(cmd, "r"), "\t", read_ref);
}
@@ -5618,7 +6138,7 @@ read_repo_config_option(char *name, size_t namelen, char *value, size_t valuelen
!strcmp(name + 7 + strlen(opt_head), ".merge")) {
size_t from = strlen(opt_remote);
- if (!strncmp(value, "refs/heads/", STRING_SIZE("refs/heads/"))) {
+ if (!prefixcmp(value, "refs/heads/")) {
value += STRING_SIZE("refs/heads/");
valuelen -= STRING_SIZE("refs/heads/");
}
@@ -5633,7 +6153,7 @@ read_repo_config_option(char *name, size_t namelen, char *value, size_t valuelen
static int
load_git_config(void)
{
- return read_properties(popen(GIT_CONFIG " --list", "r"),
+ return read_properties(popen("git " GIT_CONFIG " --list", "r"),
"=", read_repo_config_option);
}
} else if (opt_cdup[0] == ' ') {
string_ncopy(opt_cdup, name, namelen);
} else {
- if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) {
+ if (!prefixcmp(name, "refs/heads/")) {
namelen -= STRING_SIZE("refs/heads/");
name += STRING_SIZE("refs/heads/");
string_ncopy(opt_head, name, namelen);
}
int
-main(int argc, char *argv[])
+main(int argc, const char *argv[])
{
struct view *view;
enum request request;
if (load_git_config() == ERR)
die("Failed to load repo config.");
- if (!parse_options(argc, argv))
+ request = parse_options(argc, argv);
+ if (request == REQ_NONE)
return 0;
/* Require a git repository unless when running in pager mode. */
- if (!opt_git_dir[0] && opt_request != REQ_VIEW_PAGER)
+ if (!opt_git_dir[0] && request != REQ_VIEW_PAGER)
die("Not a git repository");
if (*opt_encoding && strcasecmp(opt_encoding, "UTF-8"))
die("Failed to initialize character set conversion");
}
- if (*opt_git_dir && load_refs() == ERR)
+ if (load_refs() == ERR)
die("Failed to load refs.");
- for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++)
+ foreach_view (view, i)
view->cmd_env = getenv(view->cmd_env);
- request = opt_request;
-
init_display();
while (view_driver(display[current_view], request)) {
foreach_view (view, i)
update_view(view);
+ view = display[current_view];
/* Refresh, accept single keystroke of input */
key = wgetch(status_win);
continue;
}
- request = get_keybinding(display[current_view]->keymap, key);
+ request = get_keybinding(view->keymap, key);
/* Some low-level request handling. This keeps access to
* status_win restricted. */
if (cmd && string_format(opt_cmd, "git %s", cmd)) {
if (strncmp(cmd, "show", 4) && isspace(cmd[4])) {
- opt_request = REQ_VIEW_DIFF;
+ request = REQ_VIEW_DIFF;
} else {
- opt_request = REQ_VIEW_PAGER;
+ request = REQ_VIEW_PAGER;
}
- break;
+
+ /* Always reload^Wrerun commands from the prompt. */
+ open_view(view, request, OPEN_RELOAD);
}
request = REQ_NONE;
case REQ_SEARCH:
case REQ_SEARCH_BACK:
{
- const char *prompt = request == REQ_SEARCH
- ? "/" : "?";
+ const char *prompt = request == REQ_SEARCH ? "/" : "?";
char *search = read_prompt(prompt);
if (search)