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] log [git log options]
16 * tig [options] diff [git diff options]
17 * tig [options] < [git log or git diff output]
21 * Browse changes in a git repository.
29 #define VERSION "tig-0.1"
44 static void die(const char *err, ...);
45 static void report(const char *msg, ...);
46 static void set_nonblocking_input(int boolean);
48 #define ABS(x) ((x) >= 0 ? (x) : -(x))
49 #define MIN(x, y) ((x) < (y) ? (x) : (y))
51 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
52 #define STRING_SIZE(x) (sizeof(x) - 1)
54 #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
55 #define SIZEOF_CMD 1024 /* Size of command buffer. */
57 /* This color name can be used to refer to the default term colors. */
58 #define COLOR_DEFAULT (-1)
60 /* The format and size of the date column in the main view. */
61 #define DATE_FORMAT "%Y-%m-%d %H:%M"
62 #define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
64 /* The default interval between line numbers. */
65 #define NUMBER_INTERVAL 1
67 #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
69 /* Some ascii-shorthands that fit into the ncurses namespace. */
71 #define KEY_RETURN '\r'
74 /* User action requests. */
76 /* Offset all requests to avoid conflicts with ncurses getch values. */
77 REQ_OFFSET = KEY_MAX + 1,
79 /* XXX: Keep the view request first and in sync with views[]. */
91 REQ_TOGGLE_LINE_NUMBERS,
101 REQ_SCROLL_LINE_DOWN,
103 REQ_SCROLL_PAGE_DOWN,
107 char id[41]; /* SHA1 ID. */
108 char title[75]; /* The first line of the commit message. */
109 char author[75]; /* The author of the commit. */
110 struct tm time; /* Date from the author ident. */
115 string_ncopy(char *dst, char *src, int dstlen)
117 strncpy(dst, src, dstlen - 1);
121 /* Shorthand for safely copying into a fixed buffer. */
122 #define string_copy(dst, src) \
123 string_ncopy(dst, src, sizeof(dst))
131 static int opt_line_number = FALSE;
132 static int opt_num_interval = NUMBER_INTERVAL;
133 static enum request opt_request = REQ_VIEW_MAIN;
135 char ref_head[SIZEOF_REF] = "HEAD";
136 char ref_commit[SIZEOF_REF] = "HEAD";
138 /* Returns the index of log or diff command or -1 to exit. */
140 parse_options(int argc, char *argv[])
144 for (i = 1; i < argc; i++) {
154 if (!strcmp(opt, "log") ||
155 !strcmp(opt, "diff")) {
156 opt_request = opt[0] == 'l'
157 ? REQ_VIEW_LOG : REQ_VIEW_DIFF;
163 * Start up in log view.
165 if (!strcmp(opt, "-l")) {
166 opt_request = REQ_VIEW_LOG;
172 * Start up in diff view.
174 if (!strcmp(opt, "-d")) {
175 opt_request = REQ_VIEW_DIFF;
180 * -n[INTERVAL], --line-number[=INTERVAL]::
181 * Prefix line numbers in log and diff view.
182 * Optionally, with interval different than each line.
184 if (!strncmp(opt, "-n", 2) ||
185 !strncmp(opt, "--line-number", 13)) {
191 } else if (opt[STRING_SIZE("--line-number")] == '=') {
192 num = opt + STRING_SIZE("--line-number=");
196 opt_num_interval = atoi(num);
198 opt_line_number = TRUE;
204 * Show version and exit.
206 if (!strcmp(opt, "-v") ||
207 !strcmp(opt, "--version")) {
208 printf("tig version %s\n", VERSION);
214 * Commit reference, symbolic or raw SHA1 ID.
216 if (opt[0] && opt[0] != '-') {
217 string_copy(ref_head, opt);
218 string_copy(ref_commit, opt);
222 die("unknown command '%s'", opt);
234 #define HELP "(d)iff, (l)og, (m)ain, (q)uit, (v)ersion, (h)elp"
241 struct keymap keymap[] = {
246 * Switch to diff view.
248 * Switch to log view.
250 * Switch to main view.
254 * If in main view split the view
255 * and show the diff in the bottom view.
257 { 'm', REQ_VIEW_MAIN },
258 { 'd', REQ_VIEW_DIFF },
259 { 'l', REQ_VIEW_LOG },
260 { 'h', REQ_VIEW_HELP },
262 { KEY_RETURN, REQ_ENTER },
268 * Move curser one line up.
270 * Move cursor one line down.
272 * Move curser one page up.
274 * Move cursor one page down.
276 * Jump to first line.
280 { KEY_UP, REQ_MOVE_UP },
281 { 'k', REQ_MOVE_UP },
282 { KEY_DOWN, REQ_MOVE_DOWN },
283 { 'j', REQ_MOVE_DOWN },
284 { KEY_HOME, REQ_MOVE_FIRST_LINE },
285 { KEY_END, REQ_MOVE_LAST_LINE },
286 { KEY_NPAGE, REQ_MOVE_PAGE_DOWN },
287 { KEY_PPAGE, REQ_MOVE_PAGE_UP },
293 * Scroll view one line up.
295 * Scroll view one line down.
297 * Scroll view one page up.
299 * Scroll view one page down.
301 { KEY_IC, REQ_SCROLL_LINE_UP },
302 { KEY_DC, REQ_SCROLL_LINE_DOWN },
303 { 'w', REQ_SCROLL_PAGE_UP },
304 { 's', REQ_SCROLL_PAGE_DOWN },
314 * Stop all background loading.
318 * Toggle line numbers on/off.
320 { KEY_ESC, REQ_QUIT },
322 { 'z', REQ_STOP_LOADING },
323 { 'v', REQ_SHOW_VERSION },
324 { 'r', REQ_SCREEN_REDRAW },
325 { 'n', REQ_TOGGLE_LINE_NUMBERS },
327 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
328 { ERR, REQ_SCREEN_UPDATE },
336 for (i = 0; i < ARRAY_SIZE(keymap); i++)
337 if (keymap[i].alias == key)
338 return keymap[i].request;
340 return (enum request) key;
345 * Line-oriented content detection.
349 /* Line type String to match Foreground Background Attributes
350 * --------- --------------- ---------- ---------- ---------- */ \
352 LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
353 LINE(INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
354 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
355 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
356 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
357 LINE(DIFF_OLDMODE, "old mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
358 LINE(DIFF_NEWMODE, "new mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
359 LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
360 LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
361 LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
362 LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
363 /* Pretty print commit header */ \
364 LINE(AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
365 LINE(MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
366 LINE(DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
367 /* Raw commit header */ \
368 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
369 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
370 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
371 LINE(AUTHOR_IDENT, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
372 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
374 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
375 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
377 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
378 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
379 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
380 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
381 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
382 LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
383 LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
384 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
385 LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0),
388 #define LINE(type, line, fg, bg, attr) \
395 char *line; /* The start of line to match. */
396 int linelen; /* Size of string to match. */
397 int fg, bg, attr; /* Color and text attributes for the lines. */
400 static struct line_info line_info[] = {
401 #define LINE(type, line, fg, bg, attr) \
402 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
407 static enum line_type
408 get_line_type(char *line)
410 int linelen = strlen(line);
413 for (type = 0; type < ARRAY_SIZE(line_info); type++)
414 /* Case insensitive search matches Signed-off-by lines better. */
415 if (linelen >= line_info[type].linelen &&
416 !strncasecmp(line_info[type].line, line, line_info[type].linelen))
423 get_line_attr(enum line_type type)
425 assert(type < ARRAY_SIZE(line_info));
426 return COLOR_PAIR(type) | line_info[type].attr;
432 int default_bg = COLOR_BLACK;
433 int default_fg = COLOR_WHITE;
438 if (use_default_colors() != ERR) {
443 for (type = 0; type < ARRAY_SIZE(line_info); type++) {
444 struct line_info *info = &line_info[type];
445 int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
446 int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
448 init_pair(type, fg, bg);
462 size_t objsize; /* Size of objects in the line index */
465 bool (*draw)(struct view *view, unsigned int lineno);
466 bool (*read)(struct view *view, char *line);
467 bool (*enter)(struct view *view);
470 char cmdbuf[SIZEOF_CMD];
471 char ref[SIZEOF_REF]; /* Commit reference */
478 unsigned long offset; /* Offset of the window top */
479 unsigned long lineno; /* Current line number */
482 unsigned long lines; /* Total number of lines */
483 void **line; /* Line index */
490 static struct view_ops pager_ops;
491 static struct view_ops main_ops;
494 "git log --stat -n1 %s ; echo; " \
495 "git diff --find-copies-harder -B -C %s^ %s"
498 "git log --cc --stat -n100 %s"
501 "git log --stat --pretty=raw %s"
504 "man tig 2> /dev/null"
506 static struct view views[] = {
507 { "main", MAIN_CMD, ref_head, sizeof(struct commit), &main_ops },
508 { "diff", DIFF_CMD, ref_commit, sizeof(char), &pager_ops },
509 { "log", LOG_CMD, ref_head, sizeof(char), &pager_ops },
510 { "help", HELP_CMD, ref_head, sizeof(char), &pager_ops },
513 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
515 /* The display array of active views and the index of the current view. */
516 static struct view *display[2];
517 static unsigned int current_view;
519 #define foreach_view(view, i) \
520 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
524 redraw_view_from(struct view *view, int lineno)
526 assert(0 <= lineno && lineno < view->height);
528 for (; lineno < view->height; lineno++) {
529 if (!view->ops->draw(view, lineno))
533 redrawwin(view->win);
538 redraw_view(struct view *view)
541 redraw_view_from(view, 0);
547 int height, width, offset, i;
548 struct view *base = display[0];
549 struct view *view = display[1] ? display[1] : display[0];
551 /* Setup window dimensions */
553 getmaxyx(stdscr, height, width);
555 /* Make room for the status window. */
556 base->height = height - 1;
560 view->height = SCALE_SPLIT_VIEW(base->height);
561 base->height -= view->height;
563 /* Make room for the title bar. */
567 /* Make room for the title bar. */
572 foreach_view (view, i) {
574 view->win = newwin(view->height, 0, offset, 0);
576 die("Failed to create %s view", view->name);
578 scrollok(view->win, TRUE);
580 view->title = newwin(1, 0, offset + view->height, 0);
582 die("Failed to create title window");
585 wresize(view->win, view->height, view->width);
586 mvwin(view->win, offset, 0);
587 mvwin(view->title, offset + view->height, 0);
591 offset += view->height + 1;
596 update_view_title(struct view *view)
598 if (view == display[current_view])
599 wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS));
601 wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR));
604 wmove(view->title, 0, 0);
606 /* [main] ref: 334b506... - commit 6 of 4383 (0%) */
607 wprintw(view->title, "[%s] ref: %s", view->name, ref_commit);
609 char *type = view == VIEW(REQ_VIEW_MAIN) ? "commit" : "line";
611 wprintw(view->title, " - %s %d of %d (%d%%)",
615 (view->lineno + 1) * 100 / view->lines);
618 wrefresh(view->title);
625 /* Scrolling backend */
627 do_scroll_view(struct view *view, int lines)
629 /* The rendering expects the new offset. */
630 view->offset += lines;
632 assert(0 <= view->offset && view->offset < view->lines);
635 /* Redraw the whole screen if scrolling is pointless. */
636 if (view->height < ABS(lines)) {
640 int line = lines > 0 ? view->height - lines : 0;
641 int end = line + ABS(lines);
643 wscrl(view->win, lines);
645 for (; line < end; line++) {
646 if (!view->ops->draw(view, line))
651 /* Move current line into the view. */
652 if (view->lineno < view->offset) {
653 view->lineno = view->offset;
654 view->ops->draw(view, 0);
656 } else if (view->lineno >= view->offset + view->height) {
657 view->lineno = view->offset + view->height - 1;
658 view->ops->draw(view, view->lineno - view->offset);
661 assert(view->offset <= view->lineno && view->lineno < view->lines);
663 redrawwin(view->win);
668 /* Scroll frontend */
670 scroll_view(struct view *view, enum request request)
675 case REQ_SCROLL_PAGE_DOWN:
676 lines = view->height;
677 case REQ_SCROLL_LINE_DOWN:
678 if (view->offset + lines > view->lines)
679 lines = view->lines - view->offset;
681 if (lines == 0 || view->offset + view->height >= view->lines) {
682 report("Already at last line");
687 case REQ_SCROLL_PAGE_UP:
688 lines = view->height;
689 case REQ_SCROLL_LINE_UP:
690 if (lines > view->offset)
691 lines = view->offset;
694 report("Already at first line");
701 die("request %d not handled in switch", request);
704 do_scroll_view(view, lines);
709 move_view(struct view *view, enum request request)
714 case REQ_MOVE_FIRST_LINE:
715 steps = -view->lineno;
718 case REQ_MOVE_LAST_LINE:
719 steps = view->lines - view->lineno - 1;
722 case REQ_MOVE_PAGE_UP:
723 steps = view->height > view->lineno
724 ? -view->lineno : -view->height;
727 case REQ_MOVE_PAGE_DOWN:
728 steps = view->lineno + view->height >= view->lines
729 ? view->lines - view->lineno - 1 : view->height;
741 die("request %d not handled in switch", request);
744 if (steps <= 0 && view->lineno == 0) {
745 report("Already at first line");
748 } else if (steps >= 0 && view->lineno + 1 == view->lines) {
749 report("Already at last line");
753 /* Move the current line */
754 view->lineno += steps;
755 assert(0 <= view->lineno && view->lineno < view->lines);
757 /* Repaint the old "current" line if we be scrolling */
758 if (ABS(steps) < view->height) {
759 int prev_lineno = view->lineno - steps - view->offset;
761 wmove(view->win, prev_lineno, 0);
762 wclrtoeol(view->win);
763 view->ops->draw(view, view->lineno - steps - view->offset);
766 /* Check whether the view needs to be scrolled */
767 if (view->lineno < view->offset ||
768 view->lineno >= view->offset + view->height) {
769 if (steps < 0 && -steps > view->offset) {
770 steps = -view->offset;
772 } else if (steps > 0) {
773 if (view->lineno == view->lines - 1 &&
774 view->lines > view->height) {
775 steps = view->lines - view->offset - 1;
776 if (steps >= view->height)
777 steps -= view->height - 1;
781 do_scroll_view(view, steps);
785 /* Draw the current line */
786 view->ops->draw(view, view->lineno - view->offset);
788 redrawwin(view->win);
795 * Incremental updating
799 begin_update(struct view *view)
803 if (snprintf(view->cmdbuf, sizeof(view->cmdbuf), view->cmd,
804 id, id, id) < sizeof(view->cmdbuf))
805 view->pipe = popen(view->cmdbuf, "r");
810 set_nonblocking_input(TRUE);
819 for (i = 0; i < view->lines; i++)
827 view->start_time = time(NULL);
833 end_update(struct view *view)
835 set_nonblocking_input(FALSE);
841 update_view(struct view *view)
846 /* The number of lines to read. If too low it will cause too much
847 * redrawing (and possible flickering), if too high responsiveness
849 int lines = view->height;
850 int redraw_from = -1;
855 /* Only redraw if lines are visible. */
856 if (view->offset + view->height >= view->lines)
857 redraw_from = view->lines - view->offset;
859 tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines));
865 while ((line = fgets(buffer, sizeof(buffer), view->pipe))) {
868 linelen = strlen(line);
870 line[linelen - 1] = 0;
872 if (!view->ops->read(view, line))
879 /* CPU hogilicious! */
880 update_view_title(view);
882 if (redraw_from >= 0) {
883 /* If this is an incremental update, redraw the previous line
884 * since for commits some members could have changed when
885 * loading the main view. */
889 /* Incrementally draw avoids flickering. */
890 redraw_view_from(view, redraw_from);
893 if (ferror(view->pipe)) {
894 report("Failed to read %s: %s", view->cmd, strerror(errno));
897 } else if (feof(view->pipe)) {
898 time_t secs = time(NULL) - view->start_time;
900 if (view == VIEW(REQ_VIEW_HELP)) {
905 report("Loaded %d lines in %ld second%s", view->lines, secs,
906 secs == 1 ? "" : "s");
913 report("Allocation failure");
921 switch_view(struct view *prev, enum request request,
922 bool backgrounded, bool split)
924 struct view *view = VIEW(request);
925 struct view *displayed;
928 /* Cycle between displayed views */
929 foreach_view (displayed, nviews) {
930 if (view == displayed && prev != view) {
931 current_view = nviews;
932 /* Blur out the title of the previous view. */
933 update_view_title(prev);
934 report("Switching to %s view", view->name);
939 if (view == prev && nviews == 1) {
940 report("Already in %s view", view->name);
944 /* Split to diff view? */
947 SCALE_SPLIT_VIEW(prev->height) > 3 &&
948 prev == VIEW(REQ_VIEW_MAIN) &&
949 view == VIEW(REQ_VIEW_DIFF)) {
953 if (!begin_update(view)) {
954 report("Failed to load %s view", view->name);
959 display[current_view + 1] = view;
963 /* Maximize the current view. */
964 memset(display, 0, sizeof(display));
966 display[current_view] = view;
971 if (prev && view != prev) {
972 /* "Blur" the previous view. */
973 update_view_title(prev);
975 /* Continue loading split views in the background. */
976 if (!split && prev->pipe)
980 /* Clear the old view and let the incremental updating refill
983 report("Loading...");
988 * User request switch noodle
992 view_driver(struct view *view, enum request request)
999 case REQ_MOVE_PAGE_UP:
1000 case REQ_MOVE_PAGE_DOWN:
1001 case REQ_MOVE_FIRST_LINE:
1002 case REQ_MOVE_LAST_LINE:
1003 move_view(view, request);
1006 case REQ_SCROLL_LINE_DOWN:
1007 case REQ_SCROLL_LINE_UP:
1008 case REQ_SCROLL_PAGE_DOWN:
1009 case REQ_SCROLL_PAGE_UP:
1010 scroll_view(view, request);
1017 switch_view(view, request, FALSE, FALSE);
1021 return view->ops->enter(view);
1023 case REQ_TOGGLE_LINE_NUMBERS:
1024 opt_line_number = !opt_line_number;
1028 case REQ_STOP_LOADING:
1029 foreach_view (view, i)
1034 case REQ_SHOW_VERSION:
1035 report("Version: %s", VERSION);
1038 case REQ_SCREEN_REDRAW:
1042 case REQ_SCREEN_UPDATE:
1050 /* An unknown key will show most commonly used commands. */
1060 * View backend handlers
1064 pager_draw(struct view *view, unsigned int lineno)
1066 enum line_type type;
1071 if (view->offset + lineno >= view->lines)
1074 line = view->line[view->offset + lineno];
1075 type = get_line_type(line);
1077 if (view->offset + lineno == view->lineno) {
1078 if (type == LINE_COMMIT)
1079 string_copy(ref_commit, line + 7);
1083 attr = get_line_attr(type);
1084 wattrset(view->win, attr);
1086 linelen = strlen(line);
1087 linelen = MIN(linelen, view->width);
1089 if (opt_line_number) {
1090 unsigned int real_lineno = view->offset + lineno + 1;
1093 if (real_lineno == 1 || (real_lineno % opt_num_interval) == 0)
1094 mvwprintw(view->win, lineno, 0, "%4d: ", real_lineno);
1096 mvwaddstr(view->win, lineno, 0, " : ");
1099 if (*line == '\t') {
1100 waddnstr(view->win, " ", 8 - (col % 8));
1101 col += 8 - (col % 8);
1105 char *tab = strchr(line, '\t');
1108 waddnstr(view->win, line, tab - line);
1110 waddstr(view->win, line);
1115 waddstr(view->win, line);
1119 /* NOTE: Code for only highlighting the text on the cursor line.
1120 * Kept since I've not yet decided whether to highlight the
1121 * entire line or not. --fonseca */
1122 /* No empty lines makes cursor drawing and clearing implicit. */
1124 line = " ", linelen = 1;
1126 mvwaddnstr(view->win, lineno, 0, line, linelen);
1129 /* Paint the rest of the line if it's the cursor line. */
1130 if (type == LINE_CURSOR)
1131 wchgat(view->win, -1, 0, type, NULL);
1137 pager_read(struct view *view, char *line)
1139 view->line[view->lines] = strdup(line);
1140 if (!view->line[view->lines])
1148 pager_enter(struct view *view)
1150 char *line = view->line[view->lineno];
1152 if (get_line_type(line) == LINE_COMMIT) {
1153 report("FIXME: Open commit");
1160 static struct view_ops pager_ops = {
1167 main_draw(struct view *view, unsigned int lineno)
1169 char buf[DATE_COLS + 1];
1170 struct commit *commit;
1171 enum line_type type;
1175 if (view->offset + lineno >= view->lines)
1178 commit = view->line[view->offset + lineno];
1179 if (!*commit->author)
1182 if (view->offset + lineno == view->lineno) {
1183 string_copy(ref_commit, commit->id);
1186 type = LINE_MAIN_COMMIT;
1189 wmove(view->win, lineno, cols);
1190 wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
1192 timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time);
1193 waddnstr(view->win, buf, timelen);
1194 waddstr(view->win, " ");
1197 wmove(view->win, lineno, cols);
1198 wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
1200 if (strlen(commit->author) > 19) {
1201 waddnstr(view->win, commit->author, 18);
1202 wattrset(view->win, get_line_attr(LINE_MAIN_DELIM));
1203 waddch(view->win, '~');
1205 waddstr(view->win, commit->author);
1209 wattrset(view->win, A_NORMAL);
1210 mvwaddch(view->win, lineno, cols, ACS_LTEE);
1211 wattrset(view->win, get_line_attr(type));
1212 mvwaddstr(view->win, lineno, cols + 2, commit->title);
1213 wattrset(view->win, A_NORMAL);
1218 /* Reads git log --pretty=raw output and parses it into the commit struct. */
1220 main_read(struct view *view, char *line)
1222 enum line_type type = get_line_type(line);
1223 struct commit *commit;
1227 commit = calloc(1, sizeof(struct commit));
1231 line += STRING_SIZE("commit ");
1233 view->line[view->lines++] = commit;
1234 string_copy(commit->id, line);
1237 case LINE_AUTHOR_IDENT:
1239 char *ident = line + STRING_SIZE("author ");
1240 char *end = strchr(ident, '<');
1243 for (; end > ident && isspace(end[-1]); end--) ;
1247 commit = view->line[view->lines - 1];
1248 string_copy(commit->author, ident);
1250 /* Parse epoch and timezone */
1252 char *secs = strchr(end + 1, '>');
1256 if (!secs || secs[1] != ' ')
1260 time = (time_t) atol(secs);
1261 zone = strchr(secs, ' ');
1262 if (zone && strlen(zone) == STRING_SIZE(" +0700")) {
1266 tz = ('0' - zone[1]) * 60 * 60 * 10;
1267 tz += ('0' - zone[2]) * 60 * 60;
1268 tz += ('0' - zone[3]) * 60;
1269 tz += ('0' - zone[4]) * 60;
1276 gmtime_r(&time, &commit->time);
1281 /* We should only ever end up here if there has already been a
1282 * commit line, however, be safe. */
1283 if (view->lines == 0)
1286 /* Fill in the commit title if it has not already been set. */
1287 commit = view->line[view->lines - 1];
1288 if (commit->title[0])
1291 /* Require titles to start with a non-space character at the
1292 * offset used by git log. */
1293 if (strncmp(line, " ", 4) ||
1297 string_copy(commit->title, line + 4);
1304 main_enter(struct view *view)
1306 switch_view(view, REQ_VIEW_DIFF, FALSE, TRUE);
1310 static struct view_ops main_ops = {
1320 /* The status window is used for polling keystrokes. */
1321 static WINDOW *status_win;
1323 /* Update status and title window. */
1325 report(const char *msg, ...)
1329 va_start(args, msg);
1331 /* Update the title window first, so the cursor ends up in the status
1333 update_view_title(display[current_view]);
1336 wmove(status_win, 0, 0);
1337 vwprintw(status_win, msg, args);
1338 wrefresh(status_win);
1343 /* Controls when nodelay should be in effect when polling user input. */
1345 set_nonblocking_input(int loading)
1347 /* The number of loading views. */
1348 static unsigned int nloading;
1350 if (loading == TRUE) {
1351 if (nloading++ == 0)
1352 nodelay(status_win, TRUE);
1356 if (nloading-- == 1)
1357 nodelay(status_win, FALSE);
1365 initscr(); /* Initialize the curses library */
1366 nonl(); /* Tell curses not to do NL->CR/NL on output */
1367 cbreak(); /* Take input chars one at a time, no wait for \n */
1368 noecho(); /* Don't echo input */
1369 leaveok(stdscr, TRUE);
1374 getmaxyx(stdscr, y, x);
1375 status_win = newwin(1, 0, y - 1, 0);
1377 die("Failed to create status window");
1379 /* Enable keyboard mapping */
1380 keypad(status_win, TRUE);
1381 wbkgdset(status_win, get_line_attr(LINE_STATUS));
1395 /* FIXME: Shutdown gracefully. */
1400 static void die(const char *err, ...)
1406 va_start(args, err);
1407 fputs("tig: ", stderr);
1408 vfprintf(stderr, err, args);
1409 fputs("\n", stderr);
1416 main(int argc, char *argv[])
1418 enum request request;
1421 signal(SIGINT, quit);
1423 git_cmd = parse_options(argc, argv);
1427 if (git_cmd < argc) {
1428 die("FIXME: Handle git diff/log options");
1431 request = opt_request;
1435 while (view_driver(display[current_view], request)) {
1440 foreach_view (view, i)
1443 /* Refresh, accept single keystroke of input */
1444 key = wgetch(status_win);
1445 request = get_request(key);
1456 * Features that should be explored.
1458 * - Dynamic scaling of line number indentation.
1460 * - Proper command line handling; ability to take the command that should be
1465 * - Internal command line (exmode-inspired) which allows to specify what git
1466 * log or git diff command to run. Example:
1470 * - Proper resizing support. I am yet to figure out whether catching SIGWINCH
1471 * is preferred over using ncurses' built-in support for resizing.
1477 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
1479 * This program is free software; you can redistribute it and/or modify
1480 * it under the terms of the GNU General Public License as published by
1481 * the Free Software Foundation; either version 2 of the License, or
1482 * (at your option) any later version.
1487 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
1488 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
1489 * gitk(1): git repository browser written using tcl/tk,
1490 * gitview(1): git repository browser written using python/gtk.