* tig [options]
* tig [options] log [git log options]
* tig [options] diff [git diff options]
- * tig [options] < [git log or git diff output]
+ * tig [options] < [git log or git diff output]
*
* DESCRIPTION
* -----------
* Browse changes in a git repository.
**/
-#define DEBUG
#ifndef DEBUG
#define NDEBUG
#endif
#endif
#include <assert.h>
+#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <stdarg.h>
static void die(const char *err, ...);
static void report(const char *msg, ...);
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
-
+/* Some ascii-shorthands that fit into the ncurses namespace. */
#define KEY_TAB 9
#define KEY_ESC 27
#define KEY_DEL 127
-#define REQ_OFFSET (MAX_COMMAND + 1)
+/* View requests */
+/* REQ_* values from form.h is used as a basis for user actions. */
+enum request {
+ /* Offset new values relative to MAX_COMMAND from form.h. */
+ REQ_OFFSET = MAX_COMMAND,
+
+ /* XXX: Keep the view request first and in sync with views[]. */
+ REQ_DIFF,
+ REQ_LOG,
+ REQ_MAIN,
+
+ REQ_QUIT,
+ REQ_VERSION,
+ REQ_STOP,
+ REQ_UPDATE,
+ REQ_REDRAW,
+ REQ_FIRST_LINE,
+ REQ_LAST_LINE,
+ REQ_LINE_NUMBER,
+};
+
+/* The request are used for adressing the view array. */
+#define VIEW_OFFSET(r) ((r) - REQ_OFFSET - 1)
+
+/* Size of symbolic or SHA1 ID. */
+#define SIZEOF_REF 256
-/* Requests for switching between the different views. */
-#define REQ_DIFF (REQ_OFFSET + 0)
-#define REQ_LOG (REQ_OFFSET + 1)
-#define REQ_MAIN (REQ_OFFSET + 2)
-#define REQ_VIEWS (REQ_OFFSET + 3)
+/* This color name can be used to refer to the default term colors. */
+#define COLOR_DEFAULT (-1)
-#define REQ_QUIT (REQ_OFFSET + 11)
-#define REQ_VERSION (REQ_OFFSET + 12)
-#define REQ_STOP (REQ_OFFSET + 13)
-#define REQ_UPDATE (REQ_OFFSET + 14)
-#define REQ_REDRAW (REQ_OFFSET + 15)
-#define REQ_FIRST_LINE (REQ_OFFSET + 16)
-#define REQ_LAST_LINE (REQ_OFFSET + 17)
-#define REQ_LINE_NUMBER (REQ_OFFSET + 18)
+/* The format and size of the date column in the main view. */
+#define DATE_FORMAT "%Y-%m-%d %H:%M"
+#define DATE_COLS (STRING_SIZE("2006-04-29 14:21") + 1)
-#define SIZEOF_VIEWS (REQ_VIEWS - REQ_OFFSET)
-#define SIZEOF_ID 1024
+/* The interval between line numbers. */
+#define NUMBER_INTERVAL 5
+
+#define ABS(x) ((x) >= 0 ? (x) : -(x))
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+#define STRING_SIZE(x) (sizeof(x) - 1)
+
+struct commit {
+ char id[41];
+ char title[75];
+ char author[75];
+ struct tm time;
+};
+
+
+static inline void
+string_ncopy(char *dst, char *src, int dstlen)
+{
+ strncpy(dst, src, dstlen - 1);
+ dst[dstlen - 1] = 0;
+}
-#define COLOR_TRANSP (-1)
+/* Shorthand for safely copying into a fixed buffer. */
+#define string_copy(dst, src) \
+ string_ncopy(dst, src, sizeof(dst))
/**
static int opt_line_number;
static int opt_request = REQ_MAIN;
-char head_id[SIZEOF_ID] = "HEAD";
-char commit_id[SIZEOF_ID] = "HEAD";
-
+char head_id[SIZEOF_REF] = "HEAD";
+char commit_id[SIZEOF_REF] = "HEAD";
/* Returns the index of log or diff command or -1 to exit. */
static int
* Commit reference, symbolic or raw SHA1 ID.
**/
} else if (opt[0] && opt[0] != '-') {
- strncpy(head_id, opt, SIZEOF_ID);
- strncpy(commit_id, opt, SIZEOF_ID);
+ string_copy(head_id, opt);
+ string_copy(commit_id, opt);
} else {
- die("Unknown command: '%s'", opt);
+ die("unknown command '%s'", opt);
}
}
LINE_MAIN_DELIM,
LINE_MERGE,
LINE_PARENT,
+ LINE_SIGNOFF,
LINE_STATUS,
LINE_TITLE,
LINE_TREE,
};
#define LINE(type, line, fg, bg, attr) \
- { LINE_##type, (line), sizeof(line) - 1, (fg), (bg), (attr) }
+ { LINE_##type, (line), STRING_SIZE(line), (fg), (bg), (attr) }
static struct line_info line_info[] = {
/* Diff markup */
- LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_TRANSP, 0),
- LINE(INDEX, "index ", COLOR_BLUE, COLOR_TRANSP, 0),
- LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_TRANSP, 0),
- LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_TRANSP, 0),
- LINE(DIFF_DEL, "-", COLOR_RED, COLOR_TRANSP, 0),
- LINE(DIFF_OLDMODE, "old mode ", COLOR_YELLOW, COLOR_TRANSP, 0),
- LINE(DIFF_NEWMODE, "new mode ", COLOR_YELLOW, COLOR_TRANSP, 0),
- LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_TRANSP, 0),
- LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_TRANSP, 0),
- LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_TRANSP, 0),
- LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_TRANSP, 0),
+ LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0),
+ LINE(INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0),
+ LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0),
+ LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0),
+ LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0),
+ LINE(DIFF_OLDMODE, "old mode ", COLOR_YELLOW, COLOR_DEFAULT, 0),
+ LINE(DIFF_NEWMODE, "new mode ", COLOR_YELLOW, COLOR_DEFAULT, 0),
+ LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0),
+ LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0),
+ LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0),
+ LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0),
/* Pretty print commit header */
- LINE(AUTHOR, "Author: ", COLOR_CYAN, COLOR_TRANSP, 0),
- LINE(MERGE, "Merge: ", COLOR_BLUE, COLOR_TRANSP, 0),
- LINE(DATE, "Date: ", COLOR_YELLOW, COLOR_TRANSP, 0),
+ LINE(AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0),
+ LINE(MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0),
+ LINE(DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0),
/* Raw commit header */
- LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_TRANSP, 0),
- LINE(PARENT, "parent ", COLOR_BLUE, COLOR_TRANSP, 0),
- LINE(TREE, "tree ", COLOR_BLUE, COLOR_TRANSP, 0),
- LINE(AUTHOR_IDENT, "author ", COLOR_CYAN, COLOR_TRANSP, 0),
- LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_TRANSP, 0),
+ LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0),
+ LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0),
+ LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0),
+ LINE(AUTHOR_IDENT, "author ", COLOR_CYAN, COLOR_DEFAULT, 0),
+ LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0),
- LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_TRANSP, 0),
+ /* Misc */
+ LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0),
+ LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0),
/* UI colors */
- LINE(DEFAULT, "", COLOR_TRANSP, COLOR_TRANSP, A_NORMAL),
+ LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD),
- LINE(STATUS, "", COLOR_GREEN, COLOR_TRANSP, 0),
+ LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0),
LINE(TITLE, "", COLOR_YELLOW, COLOR_BLUE, A_BOLD),
- LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_TRANSP, 0),
- LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_TRANSP, 0),
- LINE(MAIN_COMMIT, "", COLOR_TRANSP, COLOR_TRANSP, 0),
- LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_TRANSP, 0),
+ LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0),
+ LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0),
+ LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0),
+ LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0),
};
static struct line_info *
for (i = 0; i < ARRAY_SIZE(line_info); i++) {
if (linelen < line_info[i].linelen
- || strncmp(line_info[i].line, line, line_info[i].linelen))
+ || strncasecmp(line_info[i].line, line, line_info[i].linelen))
continue;
return &line_info[i];
static void
init_colors(void)
{
- int transparent_bg = COLOR_BLACK;
- int transparent_fg = COLOR_WHITE;
+ int default_bg = COLOR_BLACK;
+ int default_fg = COLOR_WHITE;
int i;
start_color();
if (use_default_colors() != ERR) {
- transparent_bg = -1;
- transparent_fg = -1;
+ default_bg = -1;
+ default_fg = -1;
}
for (i = 0; i < ARRAY_SIZE(line_info); i++) {
struct line_info *info = &line_info[i];
- int bg = info->bg == COLOR_TRANSP ? transparent_bg : info->bg;
- int fg = info->fg == COLOR_TRANSP ? transparent_fg : info->fg;
+ int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
+ int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
init_pair(info->type, fg, bg);
}
* help
* v::
* version
- *
**/
#define HELP "(d)iff, (l)og, (m)ain, (q)uit, (v)ersion, (h)elp"
{ 's', REQ_SCR_FPAGE }, /* scroll field forward a page */
{ 'w', REQ_SCR_BPAGE }, /* scroll field backward a page */
+ /* View switching */
{ 'd', REQ_DIFF },
{ 'l', REQ_LOG },
{ 'm', REQ_MAIN },
+ /* Line number toggling */
{ 'n', REQ_LINE_NUMBER },
-
/* No input from wgetch() with nodelay() enabled. */
{ ERR, REQ_UPDATE },
+ /* Misc */
{ KEY_ESC, REQ_QUIT },
{ 'q', REQ_QUIT },
{ 's', REQ_STOP },
FILE *pipe;
};
-struct commit {
- char id[41];
- char title[75];
- char author[75];
- struct tm time;
-};
-
static int pager_draw(struct view *view, unsigned int lineno);
static int pager_read(struct view *view, char *line);
#define MAIN_CMD \
"git log --stat --pretty=raw %s"
-/* The status window at the bottom. Used for polling keystrokes. */
+/* The status window is used for polling keystrokes. */
static WINDOW *status_win;
-
static WINDOW *title_win;
-static unsigned int current_view;
+/* The number of loading views. Controls when nodelay should be in effect when
+ * polling user input. */
static unsigned int nloading;
-static struct view views[];
-static struct view *display[];
-
static struct view views[] = {
{ "diff", DIFF_CMD, commit_id, pager_read, pager_draw, sizeof(char) },
{ "log", LOG_CMD, head_id, pager_read, pager_draw, sizeof(char) },
{ "main", MAIN_CMD, head_id, main_read, main_draw, sizeof(struct commit) },
};
+/* The display array of active views and the index of the current view. */
static struct view *display[ARRAY_SIZE(views)];
-
+static unsigned int current_view;
#define foreach_view(view, i) \
for (i = 0; i < sizeof(display) && (view = display[i]); i++)
+
static void
-redraw_view(struct view *view)
+redraw_view_from(struct view *view, int lineno)
{
- int lineno;
-
- wclear(view->win);
- wmove(view->win, 0, 0);
+ assert(0 <= lineno && lineno < view->height);
- for (lineno = 0; lineno < view->height; lineno++) {
+ for (; lineno < view->height; lineno++) {
if (!view->draw(view, lineno))
break;
}
}
static void
+redraw_view(struct view *view)
+{
+ wclear(view->win);
+ redraw_view_from(view, 0);
+}
+
+static void
resize_view(struct view *view)
{
int lines, cols;
static void
report_position(struct view *view, int all)
{
- report(all ? "line %d of %d (%d%%) viewing from %d"
+ report(all ? "line %d of %d (%d%%)"
: "line %d of %d",
view->lineno + 1,
view->lines,
- view->lines ? view->offset * 100 / view->lines : 0,
- view->offset);
+ view->lines ? (view->lineno + 1) * 100 / view->lines : 0);
}
assert(0 <= view->offset && view->offset < view->lines);
assert(lines);
- if (view->height < (lines > 0 ? lines : -lines)) {
+ /* Redraw the whole screen if scrolling is pointless. */
+ if (view->height < ABS(lines)) {
redraw_view(view);
} else {
int line = lines > 0 ? view->height - lines : 0;
- int end = line + (lines > 0 ? lines : -lines);
+ int end = line + ABS(lines);
wscrl(view->win, lines);
view->draw(view, view->lineno - view->offset);
}
- assert(view->offset <= view->lineno && view->lineno <= view->lines);
+ assert(view->offset <= view->lineno && view->lineno < view->lines);
redrawwin(view->win);
wrefresh(view->win);
break;
}
- if (steps < 0 && view->lineno == 0) {
+ if (steps <= 0 && view->lineno == 0) {
report("already at first line");
return;
- } else if (steps > 0 && view->lineno + 1 == view->lines) {
+ } else if (steps >= 0 && view->lineno + 1 == view->lines) {
report("already at last line");
return;
}
+ /* Move the current line */
view->lineno += steps;
- view->draw(view, view->lineno - steps - view->offset);
+ assert(0 <= view->lineno && view->lineno < view->lines);
+
+ /* Repaint the old "current" line if we be scrolling */
+ if (ABS(steps) < view->height)
+ view->draw(view, view->lineno - steps - view->offset);
+ /* Check whether the view needs to be scrolled */
if (view->lineno < view->offset ||
view->lineno >= view->offset + view->height) {
if (steps < 0 && -steps > view->offset) {
return;
}
- /* Draw the cursor line */
+ /* Draw the current line */
view->draw(view, view->lineno - view->offset);
redrawwin(view->win);
char buffer[BUFSIZ];
char *line;
void **tmp;
- int redraw;
+ /* The number of lines to read. If too low it will cause too much
+ * redrawing (and possible flickering), if too high responsiveness
+ * will suffer. */
int lines = view->height;
+ int redraw_from = -1;
if (!view->pipe)
return TRUE;
- /* Only redraw after the first reading session. */
- /* FIXME: ... and possibly more. */
- redraw = view->height > view->lines;
+ /* Only redraw if lines are visible. */
+ if (view->offset + view->height >= view->lines)
+ redraw_from = view->lines - view->offset;
tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines));
if (!tmp)
break;
}
- if (redraw)
- redraw_view(view);
+ if (redraw_from >= 0) {
+ /* If this is an incremental update, redraw the previous line
+ * since for commits some members could have changed. */
+ if (redraw_from > 0)
+ redraw_from--;
+
+ /* Incrementally draw avoids flickering. */
+ redraw_view_from(view, redraw_from);
+ }
if (ferror(view->pipe)) {
- report("failed to read %s", view->cmd);
+ report("failed to read %s: %s", view->cmd, strerror(errno));
goto end;
} else if (feof(view->pipe)) {
static struct view *
switch_view(struct view *prev, int request)
{
- struct view *view = &views[request - REQ_OFFSET];
+ struct view *view = &views[VIEW_OFFSET(request)];
struct view *displayed;
int i;
end_update(prev);
if (begin_update(view)) {
- if (!view->cmd)
+ if (!view->cmd) {
report("%s", HELP);
- else
+ } else {
+ /* Clear the old view and let the incremental updating
+ * refill the screen. */
+ wclear(view->win);
report("loading...");
+ }
}
return view;
{
enum line_type type;
char *line;
+ int linelen;
int attr;
if (view->offset + lineno >= view->lines)
if (view->offset + lineno == view->lineno) {
if (type == LINE_COMMIT)
- strncpy(commit_id, line + 7, SIZEOF_ID);
+ string_copy(commit_id, line + 7);
type = LINE_CURSOR;
}
attr = get_line_attr(type);
wattrset(view->win, attr);
+ linelen = strlen(line);
+ linelen = MIN(linelen, view->width);
+
if (opt_line_number) {
- mvwprintw(view->win, lineno, 0, "%4d: ", view->offset + lineno + 1);
+ unsigned int real_lineno = view->offset + lineno + 1;
+ int col = 1;
+
+ if (real_lineno == 1 || (real_lineno % NUMBER_INTERVAL) == 0)
+ mvwprintw(view->win, lineno, 0, "%4d: ", real_lineno);
+ else
+ mvwaddstr(view->win, lineno, 0, " : ");
+
while (line) {
if (*line == '\t') {
- waddstr(view->win, " ");
+ waddnstr(view->win, " ", 8 - (col % 8));
+ col += 8 - (col % 8);
line++;
+
} else {
char *tab = strchr(line, '\t');
waddnstr(view->win, line, tab - line);
else
waddstr(view->win, line);
+ col += tab - line;
line = tab;
}
}
} else {
/* No empty lines makes cursor drawing and clearing implicit. */
if (!*line)
- line = " ";
- mvwaddstr(view->win, lineno, 0, line);
+ line = " ", linelen = 1;
+ mvwaddnstr(view->win, lineno, 0, line, linelen);
}
return TRUE;
return FALSE;
commit = view->line[view->offset + lineno];
- if (!commit) return FALSE;
+ if (!*commit->author)
+ return FALSE;
if (view->offset + lineno == view->lineno) {
- strncpy(commit_id, commit->id, SIZEOF_ID);
+ string_copy(commit_id, commit->id);
type = LINE_CURSOR;
} else {
type = LINE_MAIN_COMMIT;
wmove(view->win, lineno, cols);
wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
- timelen = strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S ", &commit->time);
+ timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time);
waddnstr(view->win, buf, timelen);
+ waddstr(view->win, " ");
- cols += 20;
+ cols += DATE_COLS;
wmove(view->win, lineno, cols);
wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
return TRUE;
}
+/* Reads git log --pretty=raw output and parses it into the commit struct. */
static int
main_read(struct view *view, char *line)
{
if (!commit)
return FALSE;
- line += sizeof("commit ") - 1;
+ line += STRING_SIZE("commit ");
view->line[view->lines++] = commit;
- strncpy(commit->id, line, sizeof(commit->id));
+ string_copy(commit->id, line);
break;
case LINE_AUTHOR_IDENT:
{
- char *ident = line + sizeof("author ") - 1;
+ char *ident = line + STRING_SIZE("author ");
char *end = strchr(ident, '<');
if (end) {
}
commit = view->line[view->lines - 1];
- strncpy(commit->author, ident, sizeof(commit->author));
+ string_copy(commit->author, ident);
+ /* Parse epoch and timezone */
if (end) {
char *secs = strchr(end + 1, '>');
char *zone;
secs += 2;
time = (time_t) atol(secs);
zone = strchr(secs, ' ');
- if (zone && strlen(zone) == sizeof(" +0700") - 1) {
+ if (zone && strlen(zone) == STRING_SIZE(" +0700")) {
long tz;
zone++;
break;
}
default:
+ /* Fill in the commit title */
commit = view->line[view->lines - 1];
- if (!commit->title[0] &&
- !strncmp(line, " ", 4) &&
- !isspace(line[5]))
- strncpy(commit->title, line + 4, sizeof(commit->title));
+ if (commit->title[0] ||
+ strncmp(line, " ", 4) ||
+ isspace(line[5]))
+ break;
+
+ string_copy(commit->title, line + 4);
}
return TRUE;
git_cmd = parse_options(argc, argv);
if (git_cmd < 0)
return 0;
+ if (git_cmd < argc) {
+ die("opts");
+ }
request = opt_request;
}
/**
+ * BUGS
+ * ----
+ * Known bugs and problems:
+ *
+ * Redrawing of the main view while loading::
+ * If only part of a commit has been parsed not all fields will be visible
+ * or even redrawn when the whole commit have loaded. This can be
+ * triggered when continuously moving to the last line. Use 'r' to redraw
+ * the whole screen.
+ *
+ * TODO
+ * ----
+ * Features that should be explored.
+ *
+ * - Dynamic scaling of line number indentation.
+ *
+ * - Proper command line handling; ability to take the command that should be
+ * shown. Example:
+ *
+ * $ tig log -p
+ *
+ * - Internal command line (exmode-inspired) which allows to specify what git
+ * log or git diff command to run. Example:
+ *
+ * :log -p
+ *
+ * - Proper resizing support. I am yet to figure out whether catching SIGWINCH
+ * is preferred over using ncurses' built-in support for resizing.
+ *
+ * - Locale support.
+ *
* COPYRIGHT
* ---------
* Copyright (c) Jonas Fonseca, 2006
*
* SEE ALSO
* --------
+ * [verse]
* link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
* link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
+ * gitk(1): git repository browser written using tcl/tk,
+ * gitview(1): git repository browser written using python/gtk.
**/