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"
45 static void die(const char *err, ...);
46 static void report(const char *msg, ...);
47 static void set_nonblocking_input(int boolean);
49 #define ABS(x) ((x) >= 0 ? (x) : -(x))
50 #define MIN(x, y) ((x) < (y) ? (x) : (y))
52 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
53 #define STRING_SIZE(x) (sizeof(x) - 1)
55 #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
56 #define SIZEOF_CMD 1024 /* Size of command buffer. */
58 /* This color name can be used to refer to the default term colors. */
59 #define COLOR_DEFAULT (-1)
61 /* The format and size of the date column in the main view. */
62 #define DATE_FORMAT "%Y-%m-%d %H:%M"
63 #define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
65 /* The default interval between line numbers. */
66 #define NUMBER_INTERVAL 1
68 #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
70 /* Some ascii-shorthands that fit into the ncurses namespace. */
72 #define KEY_RETURN '\r'
75 /* User action requests. */
77 /* Offset all requests to avoid conflicts with ncurses getch values. */
78 REQ_OFFSET = KEY_MAX + 1,
80 /* XXX: Keep the view request first and in sync with views[]. */
93 REQ_TOGGLE_LINE_NUMBERS,
104 REQ_SCROLL_LINE_DOWN,
106 REQ_SCROLL_PAGE_DOWN,
110 char id[41]; /* SHA1 ID. */
111 char title[75]; /* The first line of the commit message. */
112 char author[75]; /* The author of the commit. */
113 struct tm time; /* Date from the author ident. */
121 string_ncopy(char *dst, char *src, int dstlen)
123 strncpy(dst, src, dstlen - 1);
128 /* Shorthand for safely copying into a fixed buffer. */
129 #define string_copy(dst, src) \
130 string_ncopy(dst, src, sizeof(dst))
134 * NOTE: The following is a slightly modified copy of the git project's shell
135 * quoting routines found in the quote.c file.
137 * Help to copy the thing properly quoted for the shell safety. any single
138 * quote is replaced with '\'', any exclamation point is replaced with '\!',
139 * and the whole thing is enclosed in a
142 * original sq_quote result
143 * name ==> name ==> 'name'
144 * a b ==> a b ==> 'a b'
145 * a'b ==> a'\''b ==> 'a'\''b'
146 * a!b ==> a'\!'b ==> 'a'\!'b'
150 sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
154 #define BUFPUT(x) ( (bufsize < SIZEOF_CMD) && (buf[bufsize++] = (x)) )
157 while ((c = *src++)) {
158 if (c == '\'' || c == '!') {
178 static int opt_line_number = FALSE;
179 static int opt_num_interval = NUMBER_INTERVAL;
180 static enum request opt_request = REQ_VIEW_MAIN;
181 static char opt_cmd[SIZEOF_CMD] = "";
183 char ref_head[SIZEOF_REF] = "HEAD";
184 char ref_commit[SIZEOF_REF] = "HEAD";
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, "--"))
272 * $ tig tag-1.0..HEAD
276 if (opt[0] && opt[0] != '-')
279 die("unknown command '%s'", opt);
291 #define HELP "(d)iff, (l)og, (m)ain, (q)uit, (v)ersion, (h)elp"
298 struct keymap keymap[] = {
303 * Switch to diff view.
305 * Switch to log view.
307 * Switch to main view.
311 * If in main view split the view
312 * and show the diff in the bottom view.
314 * Switch to next view.
316 { 'm', REQ_VIEW_MAIN },
317 { 'd', REQ_VIEW_DIFF },
318 { 'l', REQ_VIEW_LOG },
319 { 'h', REQ_VIEW_HELP },
321 { KEY_TAB, REQ_VIEW_NEXT },
322 { KEY_RETURN, REQ_ENTER },
328 * Move curser one line up.
330 * Move cursor one line down.
332 * Move curser one page up.
334 * Move cursor one page down.
336 * Jump to first line.
340 { KEY_UP, REQ_MOVE_UP },
341 { 'k', REQ_MOVE_UP },
342 { KEY_DOWN, REQ_MOVE_DOWN },
343 { 'j', REQ_MOVE_DOWN },
344 { KEY_HOME, REQ_MOVE_FIRST_LINE },
345 { KEY_END, REQ_MOVE_LAST_LINE },
346 { KEY_NPAGE, REQ_MOVE_PAGE_DOWN },
347 { KEY_PPAGE, REQ_MOVE_PAGE_UP },
353 * Scroll view one line up.
355 * Scroll view one line down.
357 * Scroll view one page up.
359 * Scroll view one page down.
361 { KEY_IC, REQ_SCROLL_LINE_UP },
362 { KEY_DC, REQ_SCROLL_LINE_DOWN },
363 { 'w', REQ_SCROLL_PAGE_UP },
364 { 's', REQ_SCROLL_PAGE_DOWN },
374 * Stop all background loading.
378 * Toggle line numbers on/off.
382 { KEY_ESC, REQ_QUIT },
384 { 'z', REQ_STOP_LOADING },
385 { 'v', REQ_SHOW_VERSION },
386 { 'r', REQ_SCREEN_REDRAW },
387 { 'n', REQ_TOGGLE_LINE_NUMBERS },
390 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
391 { ERR, REQ_SCREEN_UPDATE },
399 for (i = 0; i < ARRAY_SIZE(keymap); i++)
400 if (keymap[i].alias == key)
401 return keymap[i].request;
403 return (enum request) key;
408 * Line-oriented content detection.
412 /* Line type String to match Foreground Background Attributes
413 * --------- --------------- ---------- ---------- ---------- */ \
415 LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
416 LINE(INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
417 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
418 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
419 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
420 LINE(DIFF_OLDMODE, "old mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
421 LINE(DIFF_NEWMODE, "new mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
422 LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
423 LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
424 LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
425 LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
426 /* Pretty print commit header */ \
427 LINE(AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
428 LINE(MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
429 LINE(DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
430 /* Raw commit header */ \
431 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
432 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
433 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
434 LINE(AUTHOR_IDENT, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
435 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
437 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
438 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
440 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
441 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
442 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
443 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
444 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
445 LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
446 LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
447 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
448 LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0),
451 #define LINE(type, line, fg, bg, attr) \
458 char *line; /* The start of line to match. */
459 int linelen; /* Size of string to match. */
460 int fg, bg, attr; /* Color and text attributes for the lines. */
463 static struct line_info line_info[] = {
464 #define LINE(type, line, fg, bg, attr) \
465 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
470 static enum line_type
471 get_line_type(char *line)
473 int linelen = strlen(line);
476 for (type = 0; type < ARRAY_SIZE(line_info); type++)
477 /* Case insensitive search matches Signed-off-by lines better. */
478 if (linelen >= line_info[type].linelen &&
479 !strncasecmp(line_info[type].line, line, line_info[type].linelen))
486 get_line_attr(enum line_type type)
488 assert(type < ARRAY_SIZE(line_info));
489 return COLOR_PAIR(type) | line_info[type].attr;
495 int default_bg = COLOR_BLACK;
496 int default_fg = COLOR_WHITE;
501 if (use_default_colors() != ERR) {
506 for (type = 0; type < ARRAY_SIZE(line_info); type++) {
507 struct line_info *info = &line_info[type];
508 int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
509 int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
511 init_pair(type, fg, bg);
521 const char *name; /* View name */
522 const char *defcmd; /* Default command line */
523 char *id; /* Points to either of ref_{head,commit} */
524 size_t objsize; /* Size of objects in the line index */
527 bool (*draw)(struct view *view, unsigned int lineno);
528 bool (*read)(struct view *view, char *line);
529 bool (*enter)(struct view *view);
532 char cmd[SIZEOF_CMD]; /* Command buffer */
533 char ref[SIZEOF_REF]; /* Hovered Commit reference */
534 /* The view reference that describes the content of this view. */
535 char vref[SIZEOF_REF];
542 unsigned long offset; /* Offset of the window top */
543 unsigned long lineno; /* Current line number */
546 unsigned long lines; /* Total number of lines */
547 void **line; /* Line index */
554 static struct view_ops pager_ops;
555 static struct view_ops main_ops;
558 "git log --stat -n1 %s ; echo; " \
559 "git diff --find-copies-harder -B -C %s^ %s"
562 "git log --cc --stat -n100 %s"
565 "git log --stat --pretty=raw %s"
568 "man tig 2> /dev/null"
570 static struct view views[] = {
571 { "main", MAIN_CMD, ref_head, sizeof(struct commit), &main_ops },
572 { "diff", DIFF_CMD, ref_commit, sizeof(char), &pager_ops },
573 { "log", LOG_CMD, ref_head, sizeof(char), &pager_ops },
574 { "help", HELP_CMD, ref_head, sizeof(char), &pager_ops },
577 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
579 /* The display array of active views and the index of the current view. */
580 static struct view *display[2];
581 static unsigned int current_view;
583 #define foreach_view(view, i) \
584 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
588 redraw_view_from(struct view *view, int lineno)
590 assert(0 <= lineno && lineno < view->height);
592 for (; lineno < view->height; lineno++) {
593 if (!view->ops->draw(view, lineno))
597 redrawwin(view->win);
602 redraw_view(struct view *view)
605 redraw_view_from(view, 0);
612 struct view *base = display[0];
613 struct view *view = display[1] ? display[1] : display[0];
615 /* Setup window dimensions */
617 getmaxyx(stdscr, base->height, base->width);
619 /* Make room for the status window. */
623 /* Horizontal split. */
624 view->width = base->width;
625 view->height = SCALE_SPLIT_VIEW(base->height);
626 base->height -= view->height;
628 /* Make room for the title bar. */
632 /* Make room for the title bar. */
637 foreach_view (view, i) {
639 view->win = newwin(view->height, 0, offset, 0);
641 die("Failed to create %s view", view->name);
643 scrollok(view->win, TRUE);
645 view->title = newwin(1, 0, offset + view->height, 0);
647 die("Failed to create title window");
650 wresize(view->win, view->height, view->width);
651 mvwin(view->win, offset, 0);
652 mvwin(view->title, offset + view->height, 0);
656 offset += view->height + 1;
661 update_view_title(struct view *view)
663 if (view == display[current_view])
664 wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS));
666 wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR));
669 wmove(view->title, 0, 0);
671 /* [main] ref: 334b506... - commit 6 of 4383 (0%) */
672 wprintw(view->title, "[%s] ref: %s", view->name, view->ref);
674 char *type = view == VIEW(REQ_VIEW_MAIN) ? "commit" : "line";
676 wprintw(view->title, " - %s %d of %d (%d%%)",
680 (view->lineno + 1) * 100 / view->lines);
683 wrefresh(view->title);
690 /* Scrolling backend */
692 do_scroll_view(struct view *view, int lines)
694 /* The rendering expects the new offset. */
695 view->offset += lines;
697 assert(0 <= view->offset && view->offset < view->lines);
700 /* Redraw the whole screen if scrolling is pointless. */
701 if (view->height < ABS(lines)) {
705 int line = lines > 0 ? view->height - lines : 0;
706 int end = line + ABS(lines);
708 wscrl(view->win, lines);
710 for (; line < end; line++) {
711 if (!view->ops->draw(view, line))
716 /* Move current line into the view. */
717 if (view->lineno < view->offset) {
718 view->lineno = view->offset;
719 view->ops->draw(view, 0);
721 } else if (view->lineno >= view->offset + view->height) {
722 view->lineno = view->offset + view->height - 1;
723 view->ops->draw(view, view->lineno - view->offset);
726 assert(view->offset <= view->lineno && view->lineno < view->lines);
728 redrawwin(view->win);
733 /* Scroll frontend */
735 scroll_view(struct view *view, enum request request)
740 case REQ_SCROLL_PAGE_DOWN:
741 lines = view->height;
742 case REQ_SCROLL_LINE_DOWN:
743 if (view->offset + lines > view->lines)
744 lines = view->lines - view->offset;
746 if (lines == 0 || view->offset + view->height >= view->lines) {
747 report("Already on last line");
752 case REQ_SCROLL_PAGE_UP:
753 lines = view->height;
754 case REQ_SCROLL_LINE_UP:
755 if (lines > view->offset)
756 lines = view->offset;
759 report("Already on first line");
767 die("request %d not handled in switch", request);
770 do_scroll_view(view, lines);
775 move_view(struct view *view, enum request request)
780 case REQ_MOVE_FIRST_LINE:
781 steps = -view->lineno;
784 case REQ_MOVE_LAST_LINE:
785 steps = view->lines - view->lineno - 1;
788 case REQ_MOVE_PAGE_UP:
789 steps = view->height > view->lineno
790 ? -view->lineno : -view->height;
793 case REQ_MOVE_PAGE_DOWN:
794 steps = view->lineno + view->height >= view->lines
795 ? view->lines - view->lineno - 1 : view->height;
807 die("request %d not handled in switch", request);
810 if (steps <= 0 && view->lineno == 0) {
811 report("Already on first line");
814 } else if (steps >= 0 && view->lineno + 1 == view->lines) {
815 report("Already on last line");
819 /* Move the current line */
820 view->lineno += steps;
821 assert(0 <= view->lineno && view->lineno < view->lines);
823 /* Repaint the old "current" line if we be scrolling */
824 if (ABS(steps) < view->height) {
825 int prev_lineno = view->lineno - steps - view->offset;
827 wmove(view->win, prev_lineno, 0);
828 wclrtoeol(view->win);
829 view->ops->draw(view, prev_lineno);
832 /* Check whether the view needs to be scrolled */
833 if (view->lineno < view->offset ||
834 view->lineno >= view->offset + view->height) {
835 if (steps < 0 && -steps > view->offset) {
836 steps = -view->offset;
838 } else if (steps > 0) {
839 if (view->lineno == view->lines - 1 &&
840 view->lines > view->height) {
841 steps = view->lines - view->offset - 1;
842 if (steps >= view->height)
843 steps -= view->height - 1;
847 do_scroll_view(view, steps);
851 /* Draw the current line */
852 view->ops->draw(view, view->lineno - view->offset);
854 redrawwin(view->win);
861 * Incremental updating
865 begin_update(struct view *view)
870 string_copy(view->cmd, opt_cmd);
873 if (snprintf(view->cmd, sizeof(view->cmd), view->defcmd,
874 id, id, id) >= sizeof(view->cmd))
878 view->pipe = popen(view->cmd, "r");
882 set_nonblocking_input(TRUE);
891 for (i = 0; i < view->lines; i++)
899 view->start_time = time(NULL);
905 end_update(struct view *view)
909 set_nonblocking_input(FALSE);
915 update_view(struct view *view)
920 /* The number of lines to read. If too low it will cause too much
921 * redrawing (and possible flickering), if too high responsiveness
923 int lines = view->height;
924 int redraw_from = -1;
929 /* Only redraw if lines are visible. */
930 if (view->offset + view->height >= view->lines)
931 redraw_from = view->lines - view->offset;
933 tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines));
939 while ((line = fgets(buffer, sizeof(buffer), view->pipe))) {
942 linelen = strlen(line);
944 line[linelen - 1] = 0;
946 if (!view->ops->read(view, line))
953 /* CPU hogilicious! */
954 update_view_title(view);
956 if (redraw_from >= 0) {
957 /* If this is an incremental update, redraw the previous line
958 * since for commits some members could have changed when
959 * loading the main view. */
963 /* Incrementally draw avoids flickering. */
964 redraw_view_from(view, redraw_from);
967 if (ferror(view->pipe)) {
968 report("Failed to read: %s", strerror(errno));
971 } else if (feof(view->pipe)) {
972 time_t secs = time(NULL) - view->start_time;
974 if (view == VIEW(REQ_VIEW_HELP)) {
979 report("Loaded %d lines in %ld second%s", view->lines, secs,
980 secs == 1 ? "" : "s");
987 report("Allocation failure");
995 switch_view(struct view *prev, enum request request,
996 bool backgrounded, bool split)
998 struct view *view = VIEW(request);
999 struct view *displayed;
1002 /* Cycle between displayed views and count the views. */
1003 foreach_view (displayed, nviews) {
1005 view == displayed &&
1006 !strcmp(view->id, prev->id)) {
1007 current_view = nviews;
1008 /* Blur out the title of the previous view. */
1009 update_view_title(prev);
1010 report("Switching to %s view", view->name);
1015 if (view == prev && nviews == 1) {
1016 report("Already in %s view", view->name);
1020 if (strcmp(view->vref, view->id) &&
1021 !begin_update(view)) {
1022 report("Failed to load %s view", view->name);
1027 display[current_view + 1] = view;
1031 /* Maximize the current view. */
1032 memset(display, 0, sizeof(display));
1034 display[current_view] = view;
1039 if (split && prev->lineno - prev->offset > prev->height) {
1040 /* Take the title line into account. */
1041 int lines = prev->lineno - prev->height + 1;
1043 /* Scroll the view that was split if the current line is
1044 * outside the new limited view. */
1045 do_scroll_view(prev, lines);
1048 if (prev && view != prev) {
1049 /* "Blur" the previous view. */
1050 update_view_title(prev);
1052 /* Continue loading split views in the background. */
1058 /* Clear the old view and let the incremental updating refill
1061 report("Loading...");
1070 * User request switch noodle
1074 view_driver(struct view *view, enum request request)
1081 case REQ_MOVE_PAGE_UP:
1082 case REQ_MOVE_PAGE_DOWN:
1083 case REQ_MOVE_FIRST_LINE:
1084 case REQ_MOVE_LAST_LINE:
1085 move_view(view, request);
1088 case REQ_SCROLL_LINE_DOWN:
1089 case REQ_SCROLL_LINE_UP:
1090 case REQ_SCROLL_PAGE_DOWN:
1091 case REQ_SCROLL_PAGE_UP:
1092 scroll_view(view, request);
1099 switch_view(view, request, FALSE, FALSE);
1103 return view->ops->enter(view);
1107 int nviews = display[1] ? 2 : 1;
1108 int next_view = (current_view + 1) % nviews;
1110 if (next_view == current_view) {
1111 report("Only one view is displayed");
1115 current_view = next_view;
1116 /* Blur out the title of the previous view. */
1117 update_view_title(view);
1118 report("Switching to %s view", display[current_view]->name);
1121 case REQ_TOGGLE_LINE_NUMBERS:
1122 opt_line_number = !opt_line_number;
1127 switch_view(view, opt_request, FALSE, FALSE);
1130 case REQ_STOP_LOADING:
1131 foreach_view (view, i) {
1133 report("Stopped loaded of %s view", view->name),
1138 case REQ_SHOW_VERSION:
1139 report("Version: %s", VERSION);
1142 case REQ_SCREEN_REDRAW:
1146 case REQ_SCREEN_UPDATE:
1154 /* An unknown key will show most commonly used commands. */
1164 * View backend handlers
1168 pager_draw(struct view *view, unsigned int lineno)
1170 enum line_type type;
1175 if (view->offset + lineno >= view->lines)
1178 line = view->line[view->offset + lineno];
1179 type = get_line_type(line);
1181 if (view->offset + lineno == view->lineno) {
1182 if (type == LINE_COMMIT) {
1183 string_copy(view->ref, line + 7);
1184 string_copy(ref_commit, view->ref);
1190 attr = get_line_attr(type);
1191 wattrset(view->win, attr);
1193 linelen = strlen(line);
1194 linelen = MIN(linelen, view->width);
1196 if (opt_line_number) {
1197 unsigned int real_lineno = view->offset + lineno + 1;
1200 if (real_lineno == 1 || (real_lineno % opt_num_interval) == 0)
1201 mvwprintw(view->win, lineno, 0, "%4d: ", real_lineno);
1203 mvwaddstr(view->win, lineno, 0, " : ");
1206 if (*line == '\t') {
1207 waddnstr(view->win, " ", 8 - (col % 8));
1208 col += 8 - (col % 8);
1212 char *tab = strchr(line, '\t');
1215 waddnstr(view->win, line, tab - line);
1217 waddstr(view->win, line);
1222 waddstr(view->win, line);
1226 /* NOTE: Code for only highlighting the text on the cursor line.
1227 * Kept since I've not yet decided whether to highlight the
1228 * entire line or not. --fonseca */
1229 /* No empty lines makes cursor drawing and clearing implicit. */
1231 line = " ", linelen = 1;
1233 mvwaddnstr(view->win, lineno, 0, line, linelen);
1236 /* Paint the rest of the line if it's the cursor line. */
1237 if (type == LINE_CURSOR)
1238 wchgat(view->win, -1, 0, type, NULL);
1244 pager_read(struct view *view, char *line)
1246 view->line[view->lines] = strdup(line);
1247 if (!view->line[view->lines])
1255 pager_enter(struct view *view)
1257 char *line = view->line[view->lineno];
1259 if (get_line_type(line) == LINE_COMMIT) {
1260 switch_view(view, REQ_VIEW_DIFF, FALSE, FALSE);
1267 static struct view_ops pager_ops = {
1274 main_draw(struct view *view, unsigned int lineno)
1276 char buf[DATE_COLS + 1];
1277 struct commit *commit;
1278 enum line_type type;
1282 if (view->offset + lineno >= view->lines)
1285 commit = view->line[view->offset + lineno];
1286 if (!*commit->author)
1289 if (view->offset + lineno == view->lineno) {
1290 string_copy(view->ref, commit->id);
1293 type = LINE_MAIN_COMMIT;
1296 wmove(view->win, lineno, cols);
1297 wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
1299 timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time);
1300 waddnstr(view->win, buf, timelen);
1301 waddstr(view->win, " ");
1304 wmove(view->win, lineno, cols);
1305 wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
1307 if (strlen(commit->author) > 19) {
1308 waddnstr(view->win, commit->author, 18);
1309 wattrset(view->win, get_line_attr(LINE_MAIN_DELIM));
1310 waddch(view->win, '~');
1312 waddstr(view->win, commit->author);
1316 wattrset(view->win, A_NORMAL);
1317 mvwaddch(view->win, lineno, cols, ACS_LTEE);
1318 wattrset(view->win, get_line_attr(type));
1319 mvwaddstr(view->win, lineno, cols + 2, commit->title);
1320 wattrset(view->win, A_NORMAL);
1325 /* Reads git log --pretty=raw output and parses it into the commit struct. */
1327 main_read(struct view *view, char *line)
1329 enum line_type type = get_line_type(line);
1330 struct commit *commit;
1334 commit = calloc(1, sizeof(struct commit));
1338 line += STRING_SIZE("commit ");
1340 view->line[view->lines++] = commit;
1341 string_copy(commit->id, line);
1344 case LINE_AUTHOR_IDENT:
1346 char *ident = line + STRING_SIZE("author ");
1347 char *end = strchr(ident, '<');
1350 for (; end > ident && isspace(end[-1]); end--) ;
1354 commit = view->line[view->lines - 1];
1355 string_copy(commit->author, ident);
1357 /* Parse epoch and timezone */
1359 char *secs = strchr(end + 1, '>');
1363 if (!secs || secs[1] != ' ')
1367 time = (time_t) atol(secs);
1368 zone = strchr(secs, ' ');
1369 if (zone && strlen(zone) == STRING_SIZE(" +0700")) {
1373 tz = ('0' - zone[1]) * 60 * 60 * 10;
1374 tz += ('0' - zone[2]) * 60 * 60;
1375 tz += ('0' - zone[3]) * 60;
1376 tz += ('0' - zone[4]) * 60;
1383 gmtime_r(&time, &commit->time);
1388 /* We should only ever end up here if there has already been a
1389 * commit line, however, be safe. */
1390 if (view->lines == 0)
1393 /* Fill in the commit title if it has not already been set. */
1394 commit = view->line[view->lines - 1];
1395 if (commit->title[0])
1398 /* Require titles to start with a non-space character at the
1399 * offset used by git log. */
1400 if (strncmp(line, " ", 4) ||
1404 string_copy(commit->title, line + 4);
1411 main_enter(struct view *view)
1413 switch_view(view, REQ_VIEW_DIFF, TRUE, TRUE);
1417 static struct view_ops main_ops = {
1427 /* The status window is used for polling keystrokes. */
1428 static WINDOW *status_win;
1430 /* Update status and title window. */
1432 report(const char *msg, ...)
1436 va_start(args, msg);
1438 /* Update the title window first, so the cursor ends up in the status
1440 update_view_title(display[current_view]);
1443 wmove(status_win, 0, 0);
1444 vwprintw(status_win, msg, args);
1445 wrefresh(status_win);
1450 /* Controls when nodelay should be in effect when polling user input. */
1452 set_nonblocking_input(int loading)
1454 /* The number of loading views. */
1455 static unsigned int nloading;
1457 if (loading == TRUE) {
1458 if (nloading++ == 0)
1459 nodelay(status_win, TRUE);
1463 if (nloading-- == 1)
1464 nodelay(status_win, FALSE);
1472 initscr(); /* Initialize the curses library */
1473 nonl(); /* Tell curses not to do NL->CR/NL on output */
1474 cbreak(); /* Take input chars one at a time, no wait for \n */
1475 noecho(); /* Don't echo input */
1476 leaveok(stdscr, TRUE);
1481 getmaxyx(stdscr, y, x);
1482 status_win = newwin(1, 0, y - 1, 0);
1484 die("Failed to create status window");
1486 /* Enable keyboard mapping */
1487 keypad(status_win, TRUE);
1488 wbkgdset(status_win, get_line_attr(LINE_STATUS));
1502 /* FIXME: Shutdown gracefully. */
1507 static void die(const char *err, ...)
1513 va_start(args, err);
1514 fputs("tig: ", stderr);
1515 vfprintf(stderr, err, args);
1516 fputs("\n", stderr);
1523 main(int argc, char *argv[])
1525 enum request request;
1528 signal(SIGINT, quit);
1530 git_arg = parse_options(argc, argv);
1534 if (git_arg < argc) {
1537 /* XXX: This is vulnerable to the user overriding options
1538 * required for the main view parser. */
1539 if (opt_request == REQ_VIEW_MAIN)
1540 string_copy(opt_cmd, "git log --stat --pretty=raw");
1542 string_copy(opt_cmd, "git");
1543 buf_size = strlen(opt_cmd);
1545 while (buf_size < sizeof(opt_cmd) && git_arg < argc) {
1546 opt_cmd[buf_size++] = ' ';
1547 buf_size = sq_quote(opt_cmd, buf_size, argv[git_arg++]);
1550 if (buf_size >= sizeof(opt_cmd))
1551 die("command too long");
1553 opt_cmd[buf_size] = 0;
1556 request = opt_request;
1560 while (view_driver(display[current_view], request)) {
1565 foreach_view (view, i)
1568 /* Refresh, accept single keystroke of input */
1569 key = wgetch(status_win);
1570 request = get_request(key);
1572 if (request == REQ_PROMPT) {
1576 if (wgetnstr(status_win, opt_cmd, sizeof(opt_cmd)) == OK)
1578 cbreak(); /* Take input chars one at a time, no wait for \n */
1579 noecho(); /* Don't echo input */
1591 * Features that should be explored.
1593 * - Dynamic scaling of line number indentation.
1595 * - Internal command line (exmode-inspired) which allows to specify what git
1596 * log or git diff command to run. Example:
1600 * - Terminal resizing support. I am yet to figure out whether catching
1601 * SIGWINCH is preferred over using ncurses' built-in support for resizing.
1607 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
1609 * This program is free software; you can redistribute it and/or modify
1610 * it under the terms of the GNU General Public License as published by
1611 * the Free Software Foundation; either version 2 of the License, or
1612 * (at your option) any later version.
1617 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
1618 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
1619 * gitk(1): git repository browser written using tcl/tk,
1620 * gitview(1): git repository browser written using python/gtk.