1 /* Copyright (c) 2006 Jonas Fonseca <fonseca@diku.dk>
2 * See license info at the bottom. */
9 * tig - text-mode interface for git
15 * tig [options] [--] [git log options]
16 * tig [options] log [git log options]
17 * tig [options] diff [git diff options]
18 * tig [options] < [git log or git diff output]
22 * Browse changes in a git repository.
30 #define VERSION "tig-0.1"
46 static void die(const char *err, ...);
47 static void report(const char *msg, ...);
48 static void set_nonblocking_input(int boolean);
50 #define ABS(x) ((x) >= 0 ? (x) : -(x))
51 #define MIN(x, y) ((x) < (y) ? (x) : (y))
53 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
54 #define STRING_SIZE(x) (sizeof(x) - 1)
56 #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
57 #define SIZEOF_CMD 1024 /* Size of command buffer. */
59 /* This color name can be used to refer to the default term colors. */
60 #define COLOR_DEFAULT (-1)
62 /* The format and size of the date column in the main view. */
63 #define DATE_FORMAT "%Y-%m-%d %H:%M"
64 #define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
66 /* The default interval between line numbers. */
67 #define NUMBER_INTERVAL 1
69 #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
71 /* Some ascii-shorthands that fit into the ncurses namespace. */
73 #define KEY_RETURN '\r'
76 /* User action requests. */
78 /* Offset all requests to avoid conflicts with ncurses getch values. */
79 REQ_OFFSET = KEY_MAX + 1,
81 /* XXX: Keep the view request first and in sync with views[]. */
95 REQ_TOGGLE_LINE_NUMBERS,
106 REQ_SCROLL_LINE_DOWN,
108 REQ_SCROLL_PAGE_DOWN,
112 char id[41]; /* SHA1 ID. */
113 char title[75]; /* The first line of the commit message. */
114 char author[75]; /* The author of the commit. */
115 struct tm time; /* Date from the author ident. */
123 string_ncopy(char *dst, char *src, int dstlen)
125 strncpy(dst, src, dstlen - 1);
130 /* Shorthand for safely copying into a fixed buffer. */
131 #define string_copy(dst, src) \
132 string_ncopy(dst, src, sizeof(dst))
136 * NOTE: The following is a slightly modified copy of the git project's shell
137 * quoting routines found in the quote.c file.
139 * Help to copy the thing properly quoted for the shell safety. any single
140 * quote is replaced with '\'', any exclamation point is replaced with '\!',
141 * and the whole thing is enclosed in a
144 * original sq_quote result
145 * name ==> name ==> 'name'
146 * a b ==> a b ==> 'a b'
147 * a'b ==> a'\''b ==> 'a'\''b'
148 * a!b ==> a'\!'b ==> 'a'\!'b'
152 sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
156 #define BUFPUT(x) ( (bufsize < SIZEOF_CMD) && (buf[bufsize++] = (x)) )
159 while ((c = *src++)) {
160 if (c == '\'' || c == '!') {
180 static int opt_line_number = FALSE;
181 static int opt_num_interval = NUMBER_INTERVAL;
182 static enum request opt_request = REQ_VIEW_MAIN;
183 static char opt_cmd[SIZEOF_CMD] = "";
184 static FILE *opt_pipe = NULL;
186 /* Returns the index of log or diff command or -1 to exit. */
188 parse_options(int argc, char *argv[])
192 for (i = 1; i < argc; i++) {
202 if (!strcmp(opt, "log") ||
203 !strcmp(opt, "diff")) {
204 opt_request = opt[0] == 'l'
205 ? REQ_VIEW_LOG : REQ_VIEW_DIFF;
211 * Start up in log view.
213 if (!strcmp(opt, "-l")) {
214 opt_request = REQ_VIEW_LOG;
220 * Start up in diff view.
222 if (!strcmp(opt, "-d")) {
223 opt_request = REQ_VIEW_DIFF;
228 * -n[INTERVAL], --line-number[=INTERVAL]::
229 * Prefix line numbers in log and diff view.
230 * Optionally, with interval different than each line.
232 if (!strncmp(opt, "-n", 2) ||
233 !strncmp(opt, "--line-number", 13)) {
239 } else if (opt[STRING_SIZE("--line-number")] == '=') {
240 num = opt + STRING_SIZE("--line-number=");
244 opt_num_interval = atoi(num);
246 opt_line_number = TRUE;
252 * Show version and exit.
254 if (!strcmp(opt, "-v") ||
255 !strcmp(opt, "--version")) {
256 printf("tig version %s\n", VERSION);
262 * End of tig options. Useful when specifying commands
263 * for the main view. Example:
265 * $ tig -- --pretty=raw tag-1.0..HEAD
267 if (!strcmp(opt, "--")) {
274 * $ tig tag-1.0..HEAD
278 if (opt[0] && opt[0] != '-')
281 die("unknown command '%s'", opt);
284 if (!isatty(STDIN_FILENO)) {
285 /* XXX: When pager mode has been requested, silently override
286 * view startup options. */
287 opt_request = REQ_VIEW_PAGER;
290 } else if (i < argc) {
293 /* XXX: This is vulnerable to the user overriding options
294 * required for the main view parser. */
295 if (opt_request == REQ_VIEW_MAIN)
296 string_copy(opt_cmd, "git log --stat --pretty=raw");
298 string_copy(opt_cmd, "git");
299 buf_size = strlen(opt_cmd);
301 while (buf_size < sizeof(opt_cmd) && i < argc) {
302 opt_cmd[buf_size++] = ' ';
303 buf_size = sq_quote(opt_cmd, buf_size, argv[i++]);
306 if (buf_size >= sizeof(opt_cmd))
307 die("command too long");
309 opt_cmd[buf_size] = 0;
322 #define HELP "(d)iff, (l)og, (m)ain, (q)uit, (v)ersion, (h)elp"
329 struct keymap keymap[] = {
334 * Switch to diff view.
336 * Switch to log view.
338 * Switch to main view.
342 * If in main view split the view
343 * and show the diff in the bottom view.
345 * Switch to next view.
347 { 'm', REQ_VIEW_MAIN },
348 { 'd', REQ_VIEW_DIFF },
349 { 'l', REQ_VIEW_LOG },
350 { 'h', REQ_VIEW_HELP },
352 { KEY_TAB, REQ_VIEW_NEXT },
353 { KEY_RETURN, REQ_ENTER },
359 * Move curser one line up.
361 * Move cursor one line down.
363 * Move curser one page up.
365 * Move cursor one page down.
367 * Jump to first line.
371 { KEY_UP, REQ_MOVE_UP },
372 { 'k', REQ_MOVE_UP },
373 { KEY_DOWN, REQ_MOVE_DOWN },
374 { 'j', REQ_MOVE_DOWN },
375 { KEY_HOME, REQ_MOVE_FIRST_LINE },
376 { KEY_END, REQ_MOVE_LAST_LINE },
377 { KEY_NPAGE, REQ_MOVE_PAGE_DOWN },
378 { KEY_PPAGE, REQ_MOVE_PAGE_UP },
384 * Scroll view one line up.
386 * Scroll view one line down.
388 * Scroll view one page up.
390 * Scroll view one page down.
392 { KEY_IC, REQ_SCROLL_LINE_UP },
393 { KEY_DC, REQ_SCROLL_LINE_DOWN },
394 { 'w', REQ_SCROLL_PAGE_UP },
395 { 's', REQ_SCROLL_PAGE_DOWN },
405 * Stop all background loading.
409 * Toggle line numbers on/off.
413 { KEY_ESC, REQ_QUIT },
415 { 'z', REQ_STOP_LOADING },
416 { 'v', REQ_SHOW_VERSION },
417 { 'r', REQ_SCREEN_REDRAW },
418 { 'n', REQ_TOGGLE_LINE_NUMBERS },
421 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
422 { ERR, REQ_SCREEN_UPDATE },
430 for (i = 0; i < ARRAY_SIZE(keymap); i++)
431 if (keymap[i].alias == key)
432 return keymap[i].request;
434 return (enum request) key;
439 * Line-oriented content detection.
443 /* Line type String to match Foreground Background Attributes
444 * --------- --------------- ---------- ---------- ---------- */ \
446 LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
447 LINE(INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
448 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
449 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
450 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
451 LINE(DIFF_OLDMODE, "old mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
452 LINE(DIFF_NEWMODE, "new mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
453 LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
454 LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
455 LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
456 LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
457 /* Pretty print commit header */ \
458 LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
459 LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
460 LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
461 LINE(PP_COMMIT, "Commit: ", COLOR_GREEN, COLOR_DEFAULT, 0), \
462 /* Raw commit header */ \
463 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
464 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
465 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
466 LINE(AUTHOR_IDENT, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
467 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
469 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
470 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
472 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
473 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
474 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
475 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
476 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
477 LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
478 LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
479 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
480 LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0),
483 #define LINE(type, line, fg, bg, attr) \
490 char *line; /* The start of line to match. */
491 int linelen; /* Size of string to match. */
492 int fg, bg, attr; /* Color and text attributes for the lines. */
495 static struct line_info line_info[] = {
496 #define LINE(type, line, fg, bg, attr) \
497 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
502 static enum line_type
503 get_line_type(char *line)
505 int linelen = strlen(line);
508 for (type = 0; type < ARRAY_SIZE(line_info); type++)
509 /* Case insensitive search matches Signed-off-by lines better. */
510 if (linelen >= line_info[type].linelen &&
511 !strncasecmp(line_info[type].line, line, line_info[type].linelen))
518 get_line_attr(enum line_type type)
520 assert(type < ARRAY_SIZE(line_info));
521 return COLOR_PAIR(type) | line_info[type].attr;
527 int default_bg = COLOR_BLACK;
528 int default_fg = COLOR_WHITE;
533 if (use_default_colors() != ERR) {
538 for (type = 0; type < ARRAY_SIZE(line_info); type++) {
539 struct line_info *info = &line_info[type];
540 int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
541 int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
543 init_pair(type, fg, bg);
553 const char *name; /* View name */
554 const char *cmdfmt; /* Default command line format */
555 char *id; /* Points to either of ref_{head,commit} */
556 size_t objsize; /* Size of objects in the line index */
559 bool (*draw)(struct view *view, unsigned int lineno);
560 bool (*read)(struct view *view, char *line);
561 bool (*enter)(struct view *view);
564 char cmd[SIZEOF_CMD]; /* Command buffer */
565 char ref[SIZEOF_REF]; /* Hovered commit reference */
566 char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */
573 unsigned long offset; /* Offset of the window top */
574 unsigned long lineno; /* Current line number */
577 unsigned long lines; /* Total number of lines */
578 void **line; /* Line index */
585 static struct view_ops pager_ops;
586 static struct view_ops main_ops;
589 "git log --stat -n1 %s ; echo; " \
590 "git diff --find-copies-harder -B -C %s^ %s"
593 "git log --cc --stat -n100 %s"
596 "git log --stat --pretty=raw %s"
599 "man tig 2> /dev/null"
601 char ref_head[SIZEOF_REF] = "HEAD";
602 char ref_commit[SIZEOF_REF] = "HEAD";
604 static struct view views[] = {
605 { "main", MAIN_CMD, ref_head, sizeof(struct commit), &main_ops },
606 { "diff", DIFF_CMD, ref_commit, sizeof(char), &pager_ops },
607 { "log", LOG_CMD, ref_head, sizeof(char), &pager_ops },
608 { "help", HELP_CMD, ref_head, sizeof(char), &pager_ops },
609 { "pager", "cat", ref_head, sizeof(char), &pager_ops },
612 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
614 /* The display array of active views and the index of the current view. */
615 static struct view *display[2];
616 static unsigned int current_view;
618 #define foreach_view(view, i) \
619 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
623 redraw_view_from(struct view *view, int lineno)
625 assert(0 <= lineno && lineno < view->height);
627 for (; lineno < view->height; lineno++) {
628 if (!view->ops->draw(view, lineno))
632 redrawwin(view->win);
637 redraw_view(struct view *view)
640 redraw_view_from(view, 0);
647 struct view *base = display[0];
648 struct view *view = display[1] ? display[1] : display[0];
650 /* Setup window dimensions */
652 getmaxyx(stdscr, base->height, base->width);
654 /* Make room for the status window. */
658 /* Horizontal split. */
659 view->width = base->width;
660 view->height = SCALE_SPLIT_VIEW(base->height);
661 base->height -= view->height;
663 /* Make room for the title bar. */
667 /* Make room for the title bar. */
672 foreach_view (view, i) {
674 view->win = newwin(view->height, 0, offset, 0);
676 die("Failed to create %s view", view->name);
678 scrollok(view->win, TRUE);
680 view->title = newwin(1, 0, offset + view->height, 0);
682 die("Failed to create title window");
685 wresize(view->win, view->height, view->width);
686 mvwin(view->win, offset, 0);
687 mvwin(view->title, offset + view->height, 0);
691 offset += view->height + 1;
696 update_view_title(struct view *view)
698 if (view == display[current_view])
699 wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS));
701 wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR));
704 wmove(view->title, 0, 0);
706 /* [main] ref: 334b506... - commit 6 of 4383 (0%) */
709 wprintw(view->title, "[%s] ref: %s", view->name, view->ref);
711 wprintw(view->title, "[%s]", view->name);
714 char *type = view == VIEW(REQ_VIEW_MAIN) ? "commit" : "line";
716 wprintw(view->title, " - %s %d of %d (%d%%)",
720 (view->lineno + 1) * 100 / view->lines);
723 wrefresh(view->title);
730 /* Scrolling backend */
732 do_scroll_view(struct view *view, int lines)
734 /* The rendering expects the new offset. */
735 view->offset += lines;
737 assert(0 <= view->offset && view->offset < view->lines);
740 /* Redraw the whole screen if scrolling is pointless. */
741 if (view->height < ABS(lines)) {
745 int line = lines > 0 ? view->height - lines : 0;
746 int end = line + ABS(lines);
748 wscrl(view->win, lines);
750 for (; line < end; line++) {
751 if (!view->ops->draw(view, line))
756 /* Move current line into the view. */
757 if (view->lineno < view->offset) {
758 view->lineno = view->offset;
759 view->ops->draw(view, 0);
761 } else if (view->lineno >= view->offset + view->height) {
762 view->lineno = view->offset + view->height - 1;
763 view->ops->draw(view, view->lineno - view->offset);
766 assert(view->offset <= view->lineno && view->lineno < view->lines);
768 redrawwin(view->win);
773 /* Scroll frontend */
775 scroll_view(struct view *view, enum request request)
780 case REQ_SCROLL_PAGE_DOWN:
781 lines = view->height;
782 case REQ_SCROLL_LINE_DOWN:
783 if (view->offset + lines > view->lines)
784 lines = view->lines - view->offset;
786 if (lines == 0 || view->offset + view->height >= view->lines) {
787 report("Already on last line");
792 case REQ_SCROLL_PAGE_UP:
793 lines = view->height;
794 case REQ_SCROLL_LINE_UP:
795 if (lines > view->offset)
796 lines = view->offset;
799 report("Already on first line");
807 die("request %d not handled in switch", request);
810 do_scroll_view(view, lines);
815 move_view(struct view *view, enum request request)
820 case REQ_MOVE_FIRST_LINE:
821 steps = -view->lineno;
824 case REQ_MOVE_LAST_LINE:
825 steps = view->lines - view->lineno - 1;
828 case REQ_MOVE_PAGE_UP:
829 steps = view->height > view->lineno
830 ? -view->lineno : -view->height;
833 case REQ_MOVE_PAGE_DOWN:
834 steps = view->lineno + view->height >= view->lines
835 ? view->lines - view->lineno - 1 : view->height;
847 die("request %d not handled in switch", request);
850 if (steps <= 0 && view->lineno == 0) {
851 report("Already on first line");
854 } else if (steps >= 0 && view->lineno + 1 >= view->lines) {
855 report("Already on last line");
859 /* Move the current line */
860 view->lineno += steps;
861 assert(0 <= view->lineno && view->lineno < view->lines);
863 /* Repaint the old "current" line if we be scrolling */
864 if (ABS(steps) < view->height) {
865 int prev_lineno = view->lineno - steps - view->offset;
867 wmove(view->win, prev_lineno, 0);
868 wclrtoeol(view->win);
869 view->ops->draw(view, prev_lineno);
872 /* Check whether the view needs to be scrolled */
873 if (view->lineno < view->offset ||
874 view->lineno >= view->offset + view->height) {
875 if (steps < 0 && -steps > view->offset) {
876 steps = -view->offset;
878 } else if (steps > 0) {
879 if (view->lineno == view->lines - 1 &&
880 view->lines > view->height) {
881 steps = view->lines - view->offset - 1;
882 if (steps >= view->height)
883 steps -= view->height - 1;
887 do_scroll_view(view, steps);
891 /* Draw the current line */
892 view->ops->draw(view, view->lineno - view->offset);
894 redrawwin(view->win);
901 * Incremental updating
905 begin_update(struct view *view)
910 string_copy(view->cmd, opt_cmd);
913 if (snprintf(view->cmd, sizeof(view->cmd), view->cmdfmt,
914 id, id, id) >= sizeof(view->cmd))
918 /* Special case for the pager view. */
920 view->pipe = opt_pipe;
923 view->pipe = popen(view->cmd, "r");
929 set_nonblocking_input(TRUE);
934 string_copy(view->vid, id);
939 for (i = 0; i < view->lines; i++)
947 view->start_time = time(NULL);
953 end_update(struct view *view)
957 set_nonblocking_input(FALSE);
963 update_view(struct view *view)
968 /* The number of lines to read. If too low it will cause too much
969 * redrawing (and possible flickering), if too high responsiveness
971 int lines = view->height;
972 int redraw_from = -1;
977 /* Only redraw if lines are visible. */
978 if (view->offset + view->height >= view->lines)
979 redraw_from = view->lines - view->offset;
981 tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines));
987 while ((line = fgets(buffer, sizeof(buffer), view->pipe))) {
990 linelen = strlen(line);
992 line[linelen - 1] = 0;
994 if (!view->ops->read(view, line))
1001 /* CPU hogilicious! */
1002 update_view_title(view);
1004 if (redraw_from >= 0) {
1005 /* If this is an incremental update, redraw the previous line
1006 * since for commits some members could have changed when
1007 * loading the main view. */
1008 if (redraw_from > 0)
1011 /* Incrementally draw avoids flickering. */
1012 redraw_view_from(view, redraw_from);
1015 if (ferror(view->pipe)) {
1016 report("Failed to read: %s", strerror(errno));
1019 } else if (feof(view->pipe)) {
1020 time_t secs = time(NULL) - view->start_time;
1022 if (view == VIEW(REQ_VIEW_HELP)) {
1027 report("Loaded %d lines in %ld second%s", view->lines, secs,
1028 secs == 1 ? "" : "s");
1035 report("Allocation failure");
1043 OPEN_DEFAULT = 0, /* Use default view switching. */
1044 OPEN_SPLIT = 1, /* Split current view. */
1045 OPEN_BACKGROUNDED = 2, /* Backgrounded. */
1046 OPEN_RELOAD = 4, /* Reload view even if it is the current. */
1050 open_view(struct view *prev, enum request request, enum open_flags flags)
1052 bool backgrounded = !!(flags & OPEN_BACKGROUNDED);
1053 bool split = !!(flags & OPEN_SPLIT);
1054 bool reload = !!(flags & OPEN_RELOAD);
1055 struct view *view = VIEW(request);
1056 struct view *displayed;
1059 /* Cycle between displayed views and count the views. */
1060 foreach_view (displayed, nviews) {
1062 view == displayed &&
1063 !strcmp(view->id, prev->id)) {
1064 current_view = nviews;
1065 /* Blur out the title of the previous view. */
1066 update_view_title(prev);
1067 report("Switching to %s view", view->name);
1072 if (view == prev && nviews == 1 && !reload) {
1073 report("Already in %s view", view->name);
1077 if (strcmp(view->vid, view->id) &&
1078 !begin_update(view)) {
1079 report("Failed to load %s view", view->name);
1084 display[current_view + 1] = view;
1088 /* Maximize the current view. */
1089 memset(display, 0, sizeof(display));
1091 display[current_view] = view;
1096 if (split && prev->lineno - prev->offset > prev->height) {
1097 /* Take the title line into account. */
1098 int lines = prev->lineno - prev->height + 1;
1100 /* Scroll the view that was split if the current line is
1101 * outside the new limited view. */
1102 do_scroll_view(prev, lines);
1105 if (prev && view != prev) {
1106 /* "Blur" the previous view. */
1107 update_view_title(prev);
1109 /* Continue loading split views in the background. */
1115 /* Clear the old view and let the incremental updating refill
1118 report("Loading...");
1127 * User request switch noodle
1131 view_driver(struct view *view, enum request request)
1138 case REQ_MOVE_PAGE_UP:
1139 case REQ_MOVE_PAGE_DOWN:
1140 case REQ_MOVE_FIRST_LINE:
1141 case REQ_MOVE_LAST_LINE:
1142 move_view(view, request);
1145 case REQ_SCROLL_LINE_DOWN:
1146 case REQ_SCROLL_LINE_UP:
1147 case REQ_SCROLL_PAGE_DOWN:
1148 case REQ_SCROLL_PAGE_UP:
1149 scroll_view(view, request);
1156 case REQ_VIEW_PAGER:
1157 open_view(view, request, OPEN_DEFAULT);
1162 report("Nothing to enter");
1165 return view->ops->enter(view);
1169 int nviews = display[1] ? 2 : 1;
1170 int next_view = (current_view + 1) % nviews;
1172 if (next_view == current_view) {
1173 report("Only one view is displayed");
1177 current_view = next_view;
1178 /* Blur out the title of the previous view. */
1179 update_view_title(view);
1180 report("Switching to %s view", display[current_view]->name);
1183 case REQ_TOGGLE_LINE_NUMBERS:
1184 opt_line_number = !opt_line_number;
1189 open_view(view, opt_request, OPEN_RELOAD);
1192 case REQ_STOP_LOADING:
1193 foreach_view (view, i) {
1195 report("Stopped loaded of %s view", view->name),
1200 case REQ_SHOW_VERSION:
1201 report("Version: %s", VERSION);
1204 case REQ_SCREEN_REDRAW:
1208 case REQ_SCREEN_UPDATE:
1216 /* An unknown key will show most commonly used commands. */
1226 * View backend handlers
1230 pager_draw(struct view *view, unsigned int lineno)
1232 enum line_type type;
1237 if (view->offset + lineno >= view->lines)
1240 line = view->line[view->offset + lineno];
1241 type = get_line_type(line);
1243 if (view->offset + lineno == view->lineno) {
1246 string_copy(view->ref, line + 7);
1247 string_copy(ref_commit, view->ref);
1249 case LINE_PP_COMMIT:
1250 string_copy(view->ref, line + 8);
1251 string_copy(ref_commit, view->ref);
1258 attr = get_line_attr(type);
1259 wattrset(view->win, attr);
1261 linelen = strlen(line);
1262 linelen = MIN(linelen, view->width);
1264 if (opt_line_number) {
1265 unsigned int real_lineno = view->offset + lineno + 1;
1268 if (real_lineno == 1 || (real_lineno % opt_num_interval) == 0)
1269 mvwprintw(view->win, lineno, 0, "%4d: ", real_lineno);
1271 mvwaddstr(view->win, lineno, 0, " : ");
1274 if (*line == '\t') {
1275 waddnstr(view->win, " ", 8 - (col % 8));
1276 col += 8 - (col % 8);
1280 char *tab = strchr(line, '\t');
1283 waddnstr(view->win, line, tab - line);
1285 waddstr(view->win, line);
1290 waddstr(view->win, line);
1294 /* NOTE: Code for only highlighting the text on the cursor line.
1295 * Kept since I've not yet decided whether to highlight the
1296 * entire line or not. --fonseca */
1297 /* No empty lines makes cursor drawing and clearing implicit. */
1299 line = " ", linelen = 1;
1301 mvwaddnstr(view->win, lineno, 0, line, linelen);
1304 /* Paint the rest of the line if it's the cursor line. */
1305 if (type == LINE_CURSOR)
1306 wchgat(view->win, -1, 0, type, NULL);
1312 pager_read(struct view *view, char *line)
1314 view->line[view->lines] = strdup(line);
1315 if (!view->line[view->lines])
1323 pager_enter(struct view *view)
1325 char *line = view->line[view->lineno];
1327 if (get_line_type(line) == LINE_COMMIT) {
1328 open_view(view, REQ_VIEW_DIFF, OPEN_DEFAULT);
1335 static struct view_ops pager_ops = {
1342 main_draw(struct view *view, unsigned int lineno)
1344 char buf[DATE_COLS + 1];
1345 struct commit *commit;
1346 enum line_type type;
1350 if (view->offset + lineno >= view->lines)
1353 commit = view->line[view->offset + lineno];
1354 if (!*commit->author)
1357 if (view->offset + lineno == view->lineno) {
1358 string_copy(view->ref, commit->id);
1359 string_copy(ref_commit, view->ref);
1362 type = LINE_MAIN_COMMIT;
1365 wmove(view->win, lineno, cols);
1366 wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
1368 timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time);
1369 waddnstr(view->win, buf, timelen);
1370 waddstr(view->win, " ");
1373 wmove(view->win, lineno, cols);
1374 wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
1376 if (strlen(commit->author) > 19) {
1377 waddnstr(view->win, commit->author, 18);
1378 wattrset(view->win, get_line_attr(LINE_MAIN_DELIM));
1379 waddch(view->win, '~');
1381 waddstr(view->win, commit->author);
1385 wattrset(view->win, A_NORMAL);
1386 mvwaddch(view->win, lineno, cols, ACS_LTEE);
1387 wattrset(view->win, get_line_attr(type));
1388 mvwaddstr(view->win, lineno, cols + 2, commit->title);
1389 wattrset(view->win, A_NORMAL);
1394 /* Reads git log --pretty=raw output and parses it into the commit struct. */
1396 main_read(struct view *view, char *line)
1398 enum line_type type = get_line_type(line);
1399 struct commit *commit;
1403 commit = calloc(1, sizeof(struct commit));
1407 line += STRING_SIZE("commit ");
1409 view->line[view->lines++] = commit;
1410 string_copy(commit->id, line);
1413 case LINE_AUTHOR_IDENT:
1415 char *ident = line + STRING_SIZE("author ");
1416 char *end = strchr(ident, '<');
1419 for (; end > ident && isspace(end[-1]); end--) ;
1423 commit = view->line[view->lines - 1];
1424 string_copy(commit->author, ident);
1426 /* Parse epoch and timezone */
1428 char *secs = strchr(end + 1, '>');
1432 if (!secs || secs[1] != ' ')
1436 time = (time_t) atol(secs);
1437 zone = strchr(secs, ' ');
1438 if (zone && strlen(zone) == STRING_SIZE(" +0700")) {
1442 tz = ('0' - zone[1]) * 60 * 60 * 10;
1443 tz += ('0' - zone[2]) * 60 * 60;
1444 tz += ('0' - zone[3]) * 60;
1445 tz += ('0' - zone[4]) * 60;
1452 gmtime_r(&time, &commit->time);
1457 /* We should only ever end up here if there has already been a
1458 * commit line, however, be safe. */
1459 if (view->lines == 0)
1462 /* Fill in the commit title if it has not already been set. */
1463 commit = view->line[view->lines - 1];
1464 if (commit->title[0])
1467 /* Require titles to start with a non-space character at the
1468 * offset used by git log. */
1469 if (strncmp(line, " ", 4) ||
1473 string_copy(commit->title, line + 4);
1480 main_enter(struct view *view)
1482 open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT | OPEN_BACKGROUNDED);
1486 static struct view_ops main_ops = {
1496 /* The status window is used for polling keystrokes. */
1497 static WINDOW *status_win;
1499 /* Update status and title window. */
1501 report(const char *msg, ...)
1505 va_start(args, msg);
1507 /* Update the title window first, so the cursor ends up in the status
1509 update_view_title(display[current_view]);
1512 wmove(status_win, 0, 0);
1513 vwprintw(status_win, msg, args);
1514 wrefresh(status_win);
1519 /* Controls when nodelay should be in effect when polling user input. */
1521 set_nonblocking_input(int loading)
1523 /* The number of loading views. */
1524 static unsigned int nloading;
1526 if (loading == TRUE) {
1527 if (nloading++ == 0)
1528 nodelay(status_win, TRUE);
1532 if (nloading-- == 1)
1533 nodelay(status_win, FALSE);
1541 /* Initialize the curses library */
1542 if (isatty(STDIN_FILENO)) {
1545 /* Leave stdin and stdout alone when acting as a pager. */
1546 FILE *io = fopen("/dev/tty", "r+");
1548 newterm(NULL, io, io);
1551 nonl(); /* Tell curses not to do NL->CR/NL on output */
1552 cbreak(); /* Take input chars one at a time, no wait for \n */
1553 noecho(); /* Don't echo input */
1554 leaveok(stdscr, TRUE);
1559 getmaxyx(stdscr, y, x);
1560 status_win = newwin(1, 0, y - 1, 0);
1562 die("Failed to create status window");
1564 /* Enable keyboard mapping */
1565 keypad(status_win, TRUE);
1566 wbkgdset(status_win, get_line_attr(LINE_STATUS));
1580 /* FIXME: Shutdown gracefully. */
1585 static void die(const char *err, ...)
1591 va_start(args, err);
1592 fputs("tig: ", stderr);
1593 vfprintf(stderr, err, args);
1594 fputs("\n", stderr);
1601 main(int argc, char *argv[])
1603 enum request request;
1606 signal(SIGINT, quit);
1608 git_arg = parse_options(argc, argv);
1612 request = opt_request;
1616 while (view_driver(display[current_view], request)) {
1621 foreach_view (view, i)
1624 /* Refresh, accept single keystroke of input */
1625 key = wgetch(status_win);
1626 request = get_request(key);
1628 if (request == REQ_PROMPT) {
1630 /* Temporarily switch to line-oriented and echoed
1635 if (wgetnstr(status_win, opt_cmd + 4, sizeof(opt_cmd) - 4) == OK) {
1636 memcpy(opt_cmd, "git ", 4);
1637 opt_request = REQ_VIEW_PAGER;
1655 * Features that should be explored.
1657 * - Dynamic scaling of line number indentation.
1659 * - Internal command line (exmode-inspired) which allows to specify what git
1660 * log or git diff command to run. Example:
1664 * - Terminal resizing support. I am yet to figure out whether catching
1665 * SIGWINCH is preferred over using ncurses' built-in support for resizing.
1671 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
1673 * This program is free software; you can redistribute it and/or modify
1674 * it under the terms of the GNU General Public License as published by
1675 * the Free Software Foundation; either version 2 of the License, or
1676 * (at your option) any later version.
1681 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
1682 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
1683 * gitk(1): git repository browser written using tcl/tk,
1684 * gitview(1): git repository browser written using python/gtk.