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(1) options. Useful when specifying commands
263 * for the main view. Example:
265 * $ tig -- --since=1.month
267 if (!strcmp(opt, "--")) {
274 * $ tig tag-1.0..HEAD
278 if (opt[0] && opt[0] != '-')
281 die("unknown command '%s'", opt);
287 * If stdin is a pipe, any log or diff options will be ignored and the
288 * pager view will be opened loading data from stdin. The pager mode
289 * can be used for colorizing output from various git commands.
291 * Example on how to colorize the output of git-show(1):
295 if (!isatty(STDIN_FILENO)) {
296 opt_request = REQ_VIEW_PAGER;
299 } else if (i < argc) {
302 /* XXX: This is vulnerable to the user overriding options
303 * required for the main view parser. */
304 if (opt_request == REQ_VIEW_MAIN)
305 string_copy(opt_cmd, "git log --stat --pretty=raw");
307 string_copy(opt_cmd, "git");
308 buf_size = strlen(opt_cmd);
310 while (buf_size < sizeof(opt_cmd) && i < argc) {
311 opt_cmd[buf_size++] = ' ';
312 buf_size = sq_quote(opt_cmd, buf_size, argv[i++]);
315 if (buf_size >= sizeof(opt_cmd))
316 die("command too long");
318 opt_cmd[buf_size] = 0;
331 #define HELP "(d)iff, (l)og, (m)ain, (q)uit, (v)ersion, (h)elp"
338 struct keymap keymap[] = {
343 * Switch to diff view.
345 * Switch to log view.
347 * Switch to main view.
351 * If in main view split the view
352 * and show the diff in the bottom view.
354 * Switch to next view.
356 { 'm', REQ_VIEW_MAIN },
357 { 'd', REQ_VIEW_DIFF },
358 { 'l', REQ_VIEW_LOG },
359 { 'h', REQ_VIEW_HELP },
361 { KEY_TAB, REQ_VIEW_NEXT },
362 { KEY_RETURN, REQ_ENTER },
368 * Move curser one line up.
370 * Move cursor one line down.
372 * Move curser one page up.
374 * Move cursor one page down.
376 * Jump to first line.
380 { KEY_UP, REQ_MOVE_UP },
381 { 'k', REQ_MOVE_UP },
382 { KEY_DOWN, REQ_MOVE_DOWN },
383 { 'j', REQ_MOVE_DOWN },
384 { KEY_HOME, REQ_MOVE_FIRST_LINE },
385 { KEY_END, REQ_MOVE_LAST_LINE },
386 { KEY_NPAGE, REQ_MOVE_PAGE_DOWN },
387 { KEY_PPAGE, REQ_MOVE_PAGE_UP },
393 * Scroll view one line up.
395 * Scroll view one line down.
397 * Scroll view one page up.
399 * Scroll view one page down.
401 { KEY_IC, REQ_SCROLL_LINE_UP },
402 { KEY_DC, REQ_SCROLL_LINE_DOWN },
403 { 'w', REQ_SCROLL_PAGE_UP },
404 { 's', REQ_SCROLL_PAGE_DOWN },
414 * Stop all background loading.
418 * Toggle line numbers on/off.
422 { KEY_ESC, REQ_QUIT },
424 { 'z', REQ_STOP_LOADING },
425 { 'v', REQ_SHOW_VERSION },
426 { 'r', REQ_SCREEN_REDRAW },
427 { 'n', REQ_TOGGLE_LINE_NUMBERS },
430 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
431 { ERR, REQ_SCREEN_UPDATE },
439 for (i = 0; i < ARRAY_SIZE(keymap); i++)
440 if (keymap[i].alias == key)
441 return keymap[i].request;
443 return (enum request) key;
448 * Line-oriented content detection.
452 /* Line type String to match Foreground Background Attributes
453 * --------- --------------- ---------- ---------- ---------- */ \
455 LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
456 LINE(INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
457 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
458 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
459 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
460 LINE(DIFF_OLDMODE, "old mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
461 LINE(DIFF_NEWMODE, "new mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
462 LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
463 LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
464 LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
465 LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
466 /* Pretty print commit header */ \
467 LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
468 LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
469 LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
470 LINE(PP_COMMIT, "Commit: ", COLOR_GREEN, COLOR_DEFAULT, 0), \
471 /* Raw commit header */ \
472 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
473 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
474 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
475 LINE(AUTHOR_IDENT, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
476 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
478 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
479 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
481 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
482 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
483 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
484 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
485 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
486 LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
487 LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
488 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
489 LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0),
492 #define LINE(type, line, fg, bg, attr) \
499 char *line; /* The start of line to match. */
500 int linelen; /* Size of string to match. */
501 int fg, bg, attr; /* Color and text attributes for the lines. */
504 static struct line_info line_info[] = {
505 #define LINE(type, line, fg, bg, attr) \
506 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
511 static enum line_type
512 get_line_type(char *line)
514 int linelen = strlen(line);
517 for (type = 0; type < ARRAY_SIZE(line_info); type++)
518 /* Case insensitive search matches Signed-off-by lines better. */
519 if (linelen >= line_info[type].linelen &&
520 !strncasecmp(line_info[type].line, line, line_info[type].linelen))
527 get_line_attr(enum line_type type)
529 assert(type < ARRAY_SIZE(line_info));
530 return COLOR_PAIR(type) | line_info[type].attr;
536 int default_bg = COLOR_BLACK;
537 int default_fg = COLOR_WHITE;
542 if (use_default_colors() != ERR) {
547 for (type = 0; type < ARRAY_SIZE(line_info); type++) {
548 struct line_info *info = &line_info[type];
549 int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
550 int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
552 init_pair(type, fg, bg);
562 const char *name; /* View name */
563 const char *cmdfmt; /* Default command line format */
564 char *id; /* Points to either of ref_{head,commit} */
565 size_t objsize; /* Size of objects in the line index */
568 bool (*draw)(struct view *view, unsigned int lineno);
569 bool (*read)(struct view *view, char *line);
570 bool (*enter)(struct view *view);
573 char cmd[SIZEOF_CMD]; /* Command buffer */
574 char ref[SIZEOF_REF]; /* Hovered commit reference */
575 char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */
582 unsigned long offset; /* Offset of the window top */
583 unsigned long lineno; /* Current line number */
586 unsigned long lines; /* Total number of lines */
587 void **line; /* Line index */
594 static struct view_ops pager_ops;
595 static struct view_ops main_ops;
598 "git log --stat -n1 %s ; echo; " \
599 "git diff --find-copies-harder -B -C %s^ %s"
602 "git log --cc --stat -n100 %s"
605 "git log --stat --pretty=raw %s"
608 "man tig 2> /dev/null"
610 char ref_head[SIZEOF_REF] = "HEAD";
611 char ref_commit[SIZEOF_REF] = "HEAD";
613 static struct view views[] = {
614 { "main", MAIN_CMD, ref_head, sizeof(struct commit), &main_ops },
615 { "diff", DIFF_CMD, ref_commit, sizeof(char), &pager_ops },
616 { "log", LOG_CMD, ref_head, sizeof(char), &pager_ops },
617 { "help", HELP_CMD, ref_head, sizeof(char), &pager_ops },
618 { "pager", "cat", ref_head, sizeof(char), &pager_ops },
621 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
623 /* The display array of active views and the index of the current view. */
624 static struct view *display[2];
625 static unsigned int current_view;
627 #define foreach_view(view, i) \
628 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
632 redraw_view_from(struct view *view, int lineno)
634 assert(0 <= lineno && lineno < view->height);
636 for (; lineno < view->height; lineno++) {
637 if (!view->ops->draw(view, lineno))
641 redrawwin(view->win);
646 redraw_view(struct view *view)
649 redraw_view_from(view, 0);
656 struct view *base = display[0];
657 struct view *view = display[1] ? display[1] : display[0];
659 /* Setup window dimensions */
661 getmaxyx(stdscr, base->height, base->width);
663 /* Make room for the status window. */
667 /* Horizontal split. */
668 view->width = base->width;
669 view->height = SCALE_SPLIT_VIEW(base->height);
670 base->height -= view->height;
672 /* Make room for the title bar. */
676 /* Make room for the title bar. */
681 foreach_view (view, i) {
683 view->win = newwin(view->height, 0, offset, 0);
685 die("Failed to create %s view", view->name);
687 scrollok(view->win, TRUE);
689 view->title = newwin(1, 0, offset + view->height, 0);
691 die("Failed to create title window");
694 wresize(view->win, view->height, view->width);
695 mvwin(view->win, offset, 0);
696 mvwin(view->title, offset + view->height, 0);
700 offset += view->height + 1;
705 update_view_title(struct view *view)
707 if (view == display[current_view])
708 wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS));
710 wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR));
713 wmove(view->title, 0, 0);
715 /* [main] ref: 334b506... - commit 6 of 4383 (0%) */
718 wprintw(view->title, "[%s] ref: %s", view->name, view->ref);
720 wprintw(view->title, "[%s]", view->name);
723 char *type = view == VIEW(REQ_VIEW_MAIN) ? "commit" : "line";
725 wprintw(view->title, " - %s %d of %d (%d%%)",
729 (view->lineno + 1) * 100 / view->lines);
732 wrefresh(view->title);
739 /* Scrolling backend */
741 do_scroll_view(struct view *view, int lines)
743 /* The rendering expects the new offset. */
744 view->offset += lines;
746 assert(0 <= view->offset && view->offset < view->lines);
749 /* Redraw the whole screen if scrolling is pointless. */
750 if (view->height < ABS(lines)) {
754 int line = lines > 0 ? view->height - lines : 0;
755 int end = line + ABS(lines);
757 wscrl(view->win, lines);
759 for (; line < end; line++) {
760 if (!view->ops->draw(view, line))
765 /* Move current line into the view. */
766 if (view->lineno < view->offset) {
767 view->lineno = view->offset;
768 view->ops->draw(view, 0);
770 } else if (view->lineno >= view->offset + view->height) {
771 view->lineno = view->offset + view->height - 1;
772 view->ops->draw(view, view->lineno - view->offset);
775 assert(view->offset <= view->lineno && view->lineno < view->lines);
777 redrawwin(view->win);
782 /* Scroll frontend */
784 scroll_view(struct view *view, enum request request)
789 case REQ_SCROLL_PAGE_DOWN:
790 lines = view->height;
791 case REQ_SCROLL_LINE_DOWN:
792 if (view->offset + lines > view->lines)
793 lines = view->lines - view->offset;
795 if (lines == 0 || view->offset + view->height >= view->lines) {
796 report("Already on last line");
801 case REQ_SCROLL_PAGE_UP:
802 lines = view->height;
803 case REQ_SCROLL_LINE_UP:
804 if (lines > view->offset)
805 lines = view->offset;
808 report("Already on first line");
816 die("request %d not handled in switch", request);
819 do_scroll_view(view, lines);
824 move_view(struct view *view, enum request request)
829 case REQ_MOVE_FIRST_LINE:
830 steps = -view->lineno;
833 case REQ_MOVE_LAST_LINE:
834 steps = view->lines - view->lineno - 1;
837 case REQ_MOVE_PAGE_UP:
838 steps = view->height > view->lineno
839 ? -view->lineno : -view->height;
842 case REQ_MOVE_PAGE_DOWN:
843 steps = view->lineno + view->height >= view->lines
844 ? view->lines - view->lineno - 1 : view->height;
856 die("request %d not handled in switch", request);
859 if (steps <= 0 && view->lineno == 0) {
860 report("Already on first line");
863 } else if (steps >= 0 && view->lineno + 1 >= view->lines) {
864 report("Already on last line");
868 /* Move the current line */
869 view->lineno += steps;
870 assert(0 <= view->lineno && view->lineno < view->lines);
872 /* Repaint the old "current" line if we be scrolling */
873 if (ABS(steps) < view->height) {
874 int prev_lineno = view->lineno - steps - view->offset;
876 wmove(view->win, prev_lineno, 0);
877 wclrtoeol(view->win);
878 view->ops->draw(view, prev_lineno);
881 /* Check whether the view needs to be scrolled */
882 if (view->lineno < view->offset ||
883 view->lineno >= view->offset + view->height) {
884 if (steps < 0 && -steps > view->offset) {
885 steps = -view->offset;
887 } else if (steps > 0) {
888 if (view->lineno == view->lines - 1 &&
889 view->lines > view->height) {
890 steps = view->lines - view->offset - 1;
891 if (steps >= view->height)
892 steps -= view->height - 1;
896 do_scroll_view(view, steps);
900 /* Draw the current line */
901 view->ops->draw(view, view->lineno - view->offset);
903 redrawwin(view->win);
910 * Incremental updating
914 begin_update(struct view *view)
919 string_copy(view->cmd, opt_cmd);
922 if (snprintf(view->cmd, sizeof(view->cmd), view->cmdfmt,
923 id, id, id) >= sizeof(view->cmd))
927 /* Special case for the pager view. */
929 view->pipe = opt_pipe;
932 view->pipe = popen(view->cmd, "r");
938 set_nonblocking_input(TRUE);
943 string_copy(view->vid, id);
948 for (i = 0; i < view->lines; i++)
956 view->start_time = time(NULL);
962 end_update(struct view *view)
966 set_nonblocking_input(FALSE);
972 update_view(struct view *view)
977 /* The number of lines to read. If too low it will cause too much
978 * redrawing (and possible flickering), if too high responsiveness
980 int lines = view->height;
981 int redraw_from = -1;
986 /* Only redraw if lines are visible. */
987 if (view->offset + view->height >= view->lines)
988 redraw_from = view->lines - view->offset;
990 tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines));
996 while ((line = fgets(buffer, sizeof(buffer), view->pipe))) {
999 linelen = strlen(line);
1001 line[linelen - 1] = 0;
1003 if (!view->ops->read(view, line))
1010 /* CPU hogilicious! */
1011 update_view_title(view);
1013 if (redraw_from >= 0) {
1014 /* If this is an incremental update, redraw the previous line
1015 * since for commits some members could have changed when
1016 * loading the main view. */
1017 if (redraw_from > 0)
1020 /* Incrementally draw avoids flickering. */
1021 redraw_view_from(view, redraw_from);
1024 if (ferror(view->pipe)) {
1025 report("Failed to read: %s", strerror(errno));
1028 } else if (feof(view->pipe)) {
1029 time_t secs = time(NULL) - view->start_time;
1031 if (view == VIEW(REQ_VIEW_HELP)) {
1036 report("Loaded %d lines in %ld second%s", view->lines, secs,
1037 secs == 1 ? "" : "s");
1044 report("Allocation failure");
1052 OPEN_DEFAULT = 0, /* Use default view switching. */
1053 OPEN_SPLIT = 1, /* Split current view. */
1054 OPEN_BACKGROUNDED = 2, /* Backgrounded. */
1055 OPEN_RELOAD = 4, /* Reload view even if it is the current. */
1059 open_view(struct view *prev, enum request request, enum open_flags flags)
1061 bool backgrounded = !!(flags & OPEN_BACKGROUNDED);
1062 bool split = !!(flags & OPEN_SPLIT);
1063 bool reload = !!(flags & OPEN_RELOAD);
1064 struct view *view = VIEW(request);
1065 struct view *displayed;
1068 /* Cycle between displayed views and count the views. */
1069 foreach_view (displayed, nviews) {
1071 view == displayed &&
1072 !strcmp(view->id, prev->id)) {
1073 current_view = nviews;
1074 /* Blur out the title of the previous view. */
1075 update_view_title(prev);
1076 report("Switching to %s view", view->name);
1081 if (view == prev && nviews == 1 && !reload) {
1082 report("Already in %s view", view->name);
1086 if (strcmp(view->vid, view->id) &&
1087 !begin_update(view)) {
1088 report("Failed to load %s view", view->name);
1093 display[current_view + 1] = view;
1097 /* Maximize the current view. */
1098 memset(display, 0, sizeof(display));
1100 display[current_view] = view;
1105 if (split && prev->lineno - prev->offset > prev->height) {
1106 /* Take the title line into account. */
1107 int lines = prev->lineno - prev->height + 1;
1109 /* Scroll the view that was split if the current line is
1110 * outside the new limited view. */
1111 do_scroll_view(prev, lines);
1114 if (prev && view != prev) {
1115 /* "Blur" the previous view. */
1116 update_view_title(prev);
1118 /* Continue loading split views in the background. */
1124 /* Clear the old view and let the incremental updating refill
1127 report("Loading...");
1136 * User request switch noodle
1140 view_driver(struct view *view, enum request request)
1147 case REQ_MOVE_PAGE_UP:
1148 case REQ_MOVE_PAGE_DOWN:
1149 case REQ_MOVE_FIRST_LINE:
1150 case REQ_MOVE_LAST_LINE:
1151 move_view(view, request);
1154 case REQ_SCROLL_LINE_DOWN:
1155 case REQ_SCROLL_LINE_UP:
1156 case REQ_SCROLL_PAGE_DOWN:
1157 case REQ_SCROLL_PAGE_UP:
1158 scroll_view(view, request);
1165 case REQ_VIEW_PAGER:
1166 open_view(view, request, OPEN_DEFAULT);
1171 report("Nothing to enter");
1174 return view->ops->enter(view);
1178 int nviews = display[1] ? 2 : 1;
1179 int next_view = (current_view + 1) % nviews;
1181 if (next_view == current_view) {
1182 report("Only one view is displayed");
1186 current_view = next_view;
1187 /* Blur out the title of the previous view. */
1188 update_view_title(view);
1189 report("Switching to %s view", display[current_view]->name);
1192 case REQ_TOGGLE_LINE_NUMBERS:
1193 opt_line_number = !opt_line_number;
1198 open_view(view, opt_request, OPEN_RELOAD);
1201 case REQ_STOP_LOADING:
1202 foreach_view (view, i) {
1204 report("Stopped loaded of %s view", view->name),
1209 case REQ_SHOW_VERSION:
1210 report("Version: %s", VERSION);
1213 case REQ_SCREEN_REDRAW:
1217 case REQ_SCREEN_UPDATE:
1225 /* An unknown key will show most commonly used commands. */
1235 * View backend handlers
1239 pager_draw(struct view *view, unsigned int lineno)
1241 enum line_type type;
1246 if (view->offset + lineno >= view->lines)
1249 line = view->line[view->offset + lineno];
1250 type = get_line_type(line);
1252 if (view->offset + lineno == view->lineno) {
1255 string_copy(view->ref, line + 7);
1256 string_copy(ref_commit, view->ref);
1258 case LINE_PP_COMMIT:
1259 string_copy(view->ref, line + 8);
1260 string_copy(ref_commit, view->ref);
1267 attr = get_line_attr(type);
1268 wattrset(view->win, attr);
1270 linelen = strlen(line);
1271 linelen = MIN(linelen, view->width);
1273 if (opt_line_number) {
1274 unsigned int real_lineno = view->offset + lineno + 1;
1277 if (real_lineno == 1 || (real_lineno % opt_num_interval) == 0)
1278 mvwprintw(view->win, lineno, 0, "%4d: ", real_lineno);
1280 mvwaddstr(view->win, lineno, 0, " : ");
1283 if (*line == '\t') {
1284 waddnstr(view->win, " ", 8 - (col % 8));
1285 col += 8 - (col % 8);
1289 char *tab = strchr(line, '\t');
1292 waddnstr(view->win, line, tab - line);
1294 waddstr(view->win, line);
1299 waddstr(view->win, line);
1303 /* NOTE: Code for only highlighting the text on the cursor line.
1304 * Kept since I've not yet decided whether to highlight the
1305 * entire line or not. --fonseca */
1306 /* No empty lines makes cursor drawing and clearing implicit. */
1308 line = " ", linelen = 1;
1310 mvwaddnstr(view->win, lineno, 0, line, linelen);
1313 /* Paint the rest of the line if it's the cursor line. */
1314 if (type == LINE_CURSOR)
1315 wchgat(view->win, -1, 0, type, NULL);
1321 pager_read(struct view *view, char *line)
1323 view->line[view->lines] = strdup(line);
1324 if (!view->line[view->lines])
1332 pager_enter(struct view *view)
1334 char *line = view->line[view->lineno];
1336 if (get_line_type(line) == LINE_COMMIT) {
1337 open_view(view, REQ_VIEW_DIFF, OPEN_DEFAULT);
1344 static struct view_ops pager_ops = {
1351 main_draw(struct view *view, unsigned int lineno)
1353 char buf[DATE_COLS + 1];
1354 struct commit *commit;
1355 enum line_type type;
1359 if (view->offset + lineno >= view->lines)
1362 commit = view->line[view->offset + lineno];
1363 if (!*commit->author)
1366 if (view->offset + lineno == view->lineno) {
1367 string_copy(view->ref, commit->id);
1368 string_copy(ref_commit, view->ref);
1371 type = LINE_MAIN_COMMIT;
1374 wmove(view->win, lineno, cols);
1375 wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
1377 timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time);
1378 waddnstr(view->win, buf, timelen);
1379 waddstr(view->win, " ");
1382 wmove(view->win, lineno, cols);
1383 wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
1385 if (strlen(commit->author) > 19) {
1386 waddnstr(view->win, commit->author, 18);
1387 wattrset(view->win, get_line_attr(LINE_MAIN_DELIM));
1388 waddch(view->win, '~');
1390 waddstr(view->win, commit->author);
1394 wattrset(view->win, A_NORMAL);
1395 mvwaddch(view->win, lineno, cols, ACS_LTEE);
1396 wattrset(view->win, get_line_attr(type));
1397 mvwaddstr(view->win, lineno, cols + 2, commit->title);
1398 wattrset(view->win, A_NORMAL);
1403 /* Reads git log --pretty=raw output and parses it into the commit struct. */
1405 main_read(struct view *view, char *line)
1407 enum line_type type = get_line_type(line);
1408 struct commit *commit;
1412 commit = calloc(1, sizeof(struct commit));
1416 line += STRING_SIZE("commit ");
1418 view->line[view->lines++] = commit;
1419 string_copy(commit->id, line);
1422 case LINE_AUTHOR_IDENT:
1424 char *ident = line + STRING_SIZE("author ");
1425 char *end = strchr(ident, '<');
1428 for (; end > ident && isspace(end[-1]); end--) ;
1432 commit = view->line[view->lines - 1];
1433 string_copy(commit->author, ident);
1435 /* Parse epoch and timezone */
1437 char *secs = strchr(end + 1, '>');
1441 if (!secs || secs[1] != ' ')
1445 time = (time_t) atol(secs);
1446 zone = strchr(secs, ' ');
1447 if (zone && strlen(zone) == STRING_SIZE(" +0700")) {
1451 tz = ('0' - zone[1]) * 60 * 60 * 10;
1452 tz += ('0' - zone[2]) * 60 * 60;
1453 tz += ('0' - zone[3]) * 60;
1454 tz += ('0' - zone[4]) * 60;
1461 gmtime_r(&time, &commit->time);
1466 /* We should only ever end up here if there has already been a
1467 * commit line, however, be safe. */
1468 if (view->lines == 0)
1471 /* Fill in the commit title if it has not already been set. */
1472 commit = view->line[view->lines - 1];
1473 if (commit->title[0])
1476 /* Require titles to start with a non-space character at the
1477 * offset used by git log. */
1478 if (strncmp(line, " ", 4) ||
1482 string_copy(commit->title, line + 4);
1489 main_enter(struct view *view)
1491 open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT | OPEN_BACKGROUNDED);
1495 static struct view_ops main_ops = {
1505 /* The status window is used for polling keystrokes. */
1506 static WINDOW *status_win;
1508 /* Update status and title window. */
1510 report(const char *msg, ...)
1514 va_start(args, msg);
1516 /* Update the title window first, so the cursor ends up in the status
1518 update_view_title(display[current_view]);
1521 wmove(status_win, 0, 0);
1522 vwprintw(status_win, msg, args);
1523 wrefresh(status_win);
1528 /* Controls when nodelay should be in effect when polling user input. */
1530 set_nonblocking_input(int loading)
1532 /* The number of loading views. */
1533 static unsigned int nloading;
1535 if (loading == TRUE) {
1536 if (nloading++ == 0)
1537 nodelay(status_win, TRUE);
1541 if (nloading-- == 1)
1542 nodelay(status_win, FALSE);
1550 /* Initialize the curses library */
1551 if (isatty(STDIN_FILENO)) {
1554 /* Leave stdin and stdout alone when acting as a pager. */
1555 FILE *io = fopen("/dev/tty", "r+");
1557 newterm(NULL, io, io);
1560 nonl(); /* Tell curses not to do NL->CR/NL on output */
1561 cbreak(); /* Take input chars one at a time, no wait for \n */
1562 noecho(); /* Don't echo input */
1563 leaveok(stdscr, TRUE);
1568 getmaxyx(stdscr, y, x);
1569 status_win = newwin(1, 0, y - 1, 0);
1571 die("Failed to create status window");
1573 /* Enable keyboard mapping */
1574 keypad(status_win, TRUE);
1575 wbkgdset(status_win, get_line_attr(LINE_STATUS));
1589 /* FIXME: Shutdown gracefully. */
1594 static void die(const char *err, ...)
1600 va_start(args, err);
1601 fputs("tig: ", stderr);
1602 vfprintf(stderr, err, args);
1603 fputs("\n", stderr);
1610 main(int argc, char *argv[])
1612 enum request request;
1615 signal(SIGINT, quit);
1617 git_arg = parse_options(argc, argv);
1621 request = opt_request;
1625 while (view_driver(display[current_view], request)) {
1630 foreach_view (view, i)
1633 /* Refresh, accept single keystroke of input */
1634 key = wgetch(status_win);
1635 request = get_request(key);
1637 if (request == REQ_PROMPT) {
1639 /* Temporarily switch to line-oriented and echoed
1644 if (wgetnstr(status_win, opt_cmd + 4, sizeof(opt_cmd) - 4) == OK) {
1645 memcpy(opt_cmd, "git ", 4);
1646 opt_request = REQ_VIEW_PAGER;
1664 * Features that should be explored.
1666 * - Dynamic scaling of line number indentation.
1668 * - Internal command line (exmode-inspired) which allows to specify what git
1669 * log or git diff command to run. Example:
1673 * - Terminal resizing support. I am yet to figure out whether catching
1674 * SIGWINCH is preferred over using ncurses' built-in support for resizing.
1680 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
1682 * This program is free software; you can redistribute it and/or modify
1683 * it under the terms of the GNU General Public License as published by
1684 * the Free Software Foundation; either version 2 of the License, or
1685 * (at your option) any later version.
1690 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
1691 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
1692 * gitk(1): git repository browser written using tcl/tk,
1693 * gitview(1): git repository browser written using python/gtk.