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
, ...);
47 #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
48 #define SIZEOF_CMD 1024 /* Size of command buffer. */
50 /* This color name can be used to refer to the default term colors. */
51 #define COLOR_DEFAULT (-1)
53 /* The format and size of the date column in the main view. */
54 #define DATE_FORMAT "%Y-%m-%d %H:%M"
55 #define DATE_COLS (STRING_SIZE("2006-04-29 14:21 "))
57 /* The default interval between line numbers. */
58 #define NUMBER_INTERVAL 1
60 #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
62 #define ABS(x) ((x) >= 0 ? (x) : -(x))
63 #define MIN(x, y) ((x) < (y) ? (x) : (y))
65 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
66 #define STRING_SIZE(x) (sizeof(x) - 1)
68 /* Some ascii-shorthands that fit into the ncurses namespace. */
70 #define KEY_RETURN '\r'
73 /* User action requests. */
75 /* Offset all requests to avoid conflicts with ncurses getch values. */
76 REQ_OFFSET
= KEY_MAX
+ 1,
78 /* XXX: Keep the view request first and in sync with views[]. */
90 REQ_TOGGLE_LINE_NUMBERS
,
100 REQ_SCROLL_LINE_DOWN
,
102 REQ_SCROLL_PAGE_DOWN
,
106 char id
[41]; /* SHA1 ID. */
107 char title
[75]; /* The first line of the commit message. */
108 char author
[75]; /* The author of the commit. */
109 struct tm time
; /* Date from the author ident. */
114 string_ncopy(char *dst
, char *src
, int dstlen
)
116 strncpy(dst
, src
, dstlen
- 1);
120 /* Shorthand for safely copying into a fixed buffer. */
121 #define string_copy(dst, src) \
122 string_ncopy(dst, src, sizeof(dst))
130 static int opt_line_number
= FALSE
;
131 static int opt_num_interval
= NUMBER_INTERVAL
;
132 static int opt_request
= REQ_VIEW_MAIN
;
134 char ref_head
[SIZEOF_REF
] = "HEAD";
135 char ref_commit
[SIZEOF_REF
] = "HEAD";
137 /* Returns the index of log or diff command or -1 to exit. */
139 parse_options(int argc
, char *argv
[])
143 for (i
= 1; i
< argc
; i
++) {
153 if (!strcmp(opt
, "log") ||
154 !strcmp(opt
, "diff")) {
155 opt_request
= opt
[0] == 'l'
156 ? REQ_VIEW_LOG
: REQ_VIEW_DIFF
;
161 * Start up in log view.
163 } else if (!strcmp(opt
, "-l")) {
164 opt_request
= REQ_VIEW_LOG
;
168 * Start up in diff view.
170 } else if (!strcmp(opt
, "-d")) {
171 opt_request
= REQ_VIEW_DIFF
;
174 * -n[INTERVAL], --line-number[=INTERVAL]::
175 * Prefix line numbers in log and diff view.
176 * Optionally, with interval different than each line.
178 } else if (!strncmp(opt
, "-n", 2) ||
179 !strncmp(opt
, "--line-number", 13)) {
185 } else if (opt
[STRING_SIZE("--line-number")] == '=') {
186 num
= opt
+ STRING_SIZE("--line-number=");
190 opt_num_interval
= atoi(num
);
196 * Show version and exit.
198 } else if (!strcmp(opt
, "-v") ||
199 !strcmp(opt
, "--version")) {
200 printf("tig version %s\n", VERSION
);
205 * Commit reference, symbolic or raw SHA1 ID.
207 } else if (opt
[0] && opt
[0] != '-') {
208 string_copy(ref_head
, opt
);
209 string_copy(ref_commit
, opt
);
212 die("unknown command '%s'", opt
);
233 * stop all background loading
244 #define HELP "(d)iff, (l)og, (m)ain, (q)uit, (v)ersion, (h)elp"
251 struct keymap keymap
[] = {
252 /* Cursor navigation */
253 { KEY_UP
, REQ_MOVE_UP
},
254 { 'k', REQ_MOVE_UP
},
255 { KEY_DOWN
, REQ_MOVE_DOWN
},
256 { 'j', REQ_MOVE_DOWN
},
257 { KEY_HOME
, REQ_MOVE_FIRST_LINE
},
258 { KEY_END
, REQ_MOVE_LAST_LINE
},
259 { KEY_NPAGE
, REQ_MOVE_PAGE_DOWN
},
260 { KEY_PPAGE
, REQ_MOVE_PAGE_UP
},
263 { KEY_IC
, REQ_SCROLL_LINE_UP
},
264 { KEY_DC
, REQ_SCROLL_LINE_DOWN
},
265 { 'w', REQ_SCROLL_PAGE_UP
},
266 { 's', REQ_SCROLL_PAGE_DOWN
},
268 { KEY_RETURN
, REQ_SHOW_COMMIT
},
271 { 'm', REQ_VIEW_MAIN
},
272 { 'd', REQ_VIEW_DIFF
},
273 { 'l', REQ_VIEW_LOG
},
274 { 'h', REQ_VIEW_HELP
},
277 { KEY_ESC
, REQ_QUIT
},
279 { 'z', REQ_STOP_LOADING
},
280 { 'v', REQ_SHOW_VERSION
},
281 { 'r', REQ_SCREEN_REDRAW
},
282 { 'n', REQ_TOGGLE_LINE_NUMBERS
},
284 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
285 { ERR
, REQ_SCREEN_UPDATE
},
289 get_request(int request
)
293 for (i
= 0; i
< ARRAY_SIZE(keymap
); i
++)
294 if (keymap
[i
].alias
== request
)
295 return keymap
[i
].request
;
302 * Line-oriented content detection.
304 * Line type String to match Foreground Background Attributes
305 * --------- --------------- ---------- ---------- ---------- */
308 LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
309 LINE(INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
310 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
311 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
312 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
313 LINE(DIFF_OLDMODE, "old mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
314 LINE(DIFF_NEWMODE, "new mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
315 LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
316 LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
317 LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
318 LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
319 /* Pretty print commit header */ \
320 LINE(AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
321 LINE(MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
322 LINE(DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
323 /* Raw commit header */ \
324 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
325 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
326 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
327 LINE(AUTHOR_IDENT, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
328 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
330 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
331 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
333 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
334 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
335 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
336 LINE(TITLE, "", COLOR_WHITE, COLOR_BLUE, 0), \
337 LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
338 LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
339 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
340 LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0),
343 #define LINE(type, line, fg, bg, attr) \
350 char *line
; /* The start of line to match. */
351 int linelen
; /* Size of string to match. */
352 int fg
, bg
, attr
; /* Color and text attributes for the lines. */
355 static struct line_info line_info
[] = {
356 #define LINE(type, line, fg, bg, attr) \
357 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
362 static enum line_type
363 get_line_type(char *line
)
365 int linelen
= strlen(line
);
368 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++)
369 /* Case insensitive search matches Signed-off-by lines better. */
370 if (linelen
>= line_info
[type
].linelen
&&
371 !strncasecmp(line_info
[type
].line
, line
, line_info
[type
].linelen
))
378 get_line_attr(enum line_type type
)
380 assert(type
< ARRAY_SIZE(line_info
));
381 return COLOR_PAIR(type
) | line_info
[type
].attr
;
387 int default_bg
= COLOR_BLACK
;
388 int default_fg
= COLOR_WHITE
;
393 if (use_default_colors() != ERR
) {
398 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++) {
399 struct line_info
*info
= &line_info
[type
];
400 int bg
= info
->bg
== COLOR_DEFAULT ? default_bg
: info
->bg
;
401 int fg
= info
->fg
== COLOR_DEFAULT ? default_fg
: info
->fg
;
403 init_pair(type
, fg
, bg
);
418 int (*read
)(struct view
*, char *);
419 int (*draw
)(struct view
*, unsigned int);
420 size_t objsize
; /* Size of objects in the line index */
422 char cmdbuf
[SIZEOF_CMD
];
429 unsigned long offset
; /* Offset of the window top */
430 unsigned long lineno
; /* Current line number */
433 unsigned long lines
; /* Total number of lines */
434 void **line
; /* Line index */
441 static int pager_draw(struct view
*view
, unsigned int lineno
);
442 static int pager_read(struct view
*view
, char *line
);
444 static int main_draw(struct view
*view
, unsigned int lineno
);
445 static int main_read(struct view
*view
, char *line
);
447 static void update_title(struct view
*view
);
450 "git log --stat -n1 %s ; echo; " \
451 "git diff --find-copies-harder -B -C %s^ %s"
454 "git log --cc --stat -n100 %s"
457 "git log --stat --pretty=raw %s"
460 "man tig 2> /dev/null"
462 /* The status window is used for polling keystrokes. */
463 static WINDOW
*status_win
;
465 /* The number of loading views. Controls when nodelay should be in effect when
466 * polling user input. */
467 static unsigned int nloading
;
469 static struct view views
[] = {
470 { "main", MAIN_CMD
, ref_head
, main_read
, main_draw
, sizeof(struct commit
) },
471 { "diff", DIFF_CMD
, ref_commit
, pager_read
, pager_draw
, sizeof(char) },
472 { "log", LOG_CMD
, ref_head
, pager_read
, pager_draw
, sizeof(char) },
473 { "help", HELP_CMD
, ref_head
, pager_read
, pager_draw
, sizeof(char) },
476 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
478 /* The display array of active views and the index of the current view. */
479 static struct view
*display
[ARRAY_SIZE(views
)];
480 static unsigned int current_view
;
482 #define foreach_view(view, i) \
483 for (i = 0; i < sizeof(display) && (view = display[i]); i++)
487 redraw_view_from(struct view
*view
, int lineno
)
489 assert(0 <= lineno
&& lineno
< view
->height
);
491 for (; lineno
< view
->height
; lineno
++) {
492 if (!view
->draw(view
, lineno
))
496 redrawwin(view
->win
);
501 redraw_view(struct view
*view
)
504 redraw_view_from(view
, 0);
508 resize_view(struct view
*view
)
512 getmaxyx(stdscr
, lines
, cols
);
515 mvwin(view
->win
, 0, 0);
516 wresize(view
->win
, lines
- 2, cols
);
519 view
->win
= newwin(lines
- 2, 0, 0, 0);
521 report("Failed to create %s view", view
->name
);
524 scrollok(view
->win
, TRUE
);
526 view
->title
= newwin(1, 0, lines
- 2, 0);
530 report("Failed to create title window");
533 wbkgdset(view
->title
, get_line_attr(LINE_TITLE
));
537 getmaxyx(view
->win
, view
->height
, view
->width
);
547 /* Scrolling backend */
549 do_scroll_view(struct view
*view
, int lines
)
551 /* The rendering expects the new offset. */
552 view
->offset
+= lines
;
554 assert(0 <= view
->offset
&& view
->offset
< view
->lines
);
557 /* Redraw the whole screen if scrolling is pointless. */
558 if (view
->height
< ABS(lines
)) {
562 int line
= lines
> 0 ? view
->height
- lines
: 0;
563 int end
= line
+ ABS(lines
);
565 wscrl(view
->win
, lines
);
567 for (; line
< end
; line
++) {
568 if (!view
->draw(view
, line
))
573 /* Move current line into the view. */
574 if (view
->lineno
< view
->offset
) {
575 view
->lineno
= view
->offset
;
578 } else if (view
->lineno
>= view
->offset
+ view
->height
) {
579 view
->lineno
= view
->offset
+ view
->height
- 1;
580 view
->draw(view
, view
->lineno
- view
->offset
);
583 assert(view
->offset
<= view
->lineno
&& view
->lineno
< view
->lines
);
585 redrawwin(view
->win
);
590 /* Scroll frontend */
592 scroll_view(struct view
*view
, int request
)
597 case REQ_SCROLL_PAGE_DOWN
:
598 lines
= view
->height
;
599 case REQ_SCROLL_LINE_DOWN
:
600 if (view
->offset
+ lines
> view
->lines
)
601 lines
= view
->lines
- view
->offset
;
603 if (lines
== 0 || view
->offset
+ view
->height
>= view
->lines
) {
604 report("Already at last line");
609 case REQ_SCROLL_PAGE_UP
:
610 lines
= view
->height
;
611 case REQ_SCROLL_LINE_UP
:
612 if (lines
> view
->offset
)
613 lines
= view
->offset
;
616 report("Already at first line");
624 do_scroll_view(view
, lines
);
629 move_view(struct view
*view
, int request
)
634 case REQ_MOVE_FIRST_LINE
:
635 steps
= -view
->lineno
;
638 case REQ_MOVE_LAST_LINE
:
639 steps
= view
->lines
- view
->lineno
- 1;
642 case REQ_MOVE_PAGE_UP
:
643 steps
= view
->height
> view
->lineno
644 ?
-view
->lineno
: -view
->height
;
647 case REQ_MOVE_PAGE_DOWN
:
648 steps
= view
->lineno
+ view
->height
>= view
->lines
649 ? view
->lines
- view
->lineno
- 1 : view
->height
;
661 if (steps
<= 0 && view
->lineno
== 0) {
662 report("Already at first line");
665 } else if (steps
>= 0 && view
->lineno
+ 1 == view
->lines
) {
666 report("Already at last line");
670 /* Move the current line */
671 view
->lineno
+= steps
;
672 assert(0 <= view
->lineno
&& view
->lineno
< view
->lines
);
674 /* Repaint the old "current" line if we be scrolling */
675 if (ABS(steps
) < view
->height
) {
676 int prev_lineno
= view
->lineno
- steps
- view
->offset
;
678 wmove(view
->win
, prev_lineno
, 0);
679 wclrtoeol(view
->win
);
680 view
->draw(view
, view
->lineno
- steps
- view
->offset
);
683 /* Check whether the view needs to be scrolled */
684 if (view
->lineno
< view
->offset
||
685 view
->lineno
>= view
->offset
+ view
->height
) {
686 if (steps
< 0 && -steps
> view
->offset
) {
687 steps
= -view
->offset
;
689 } else if (steps
> 0) {
690 if (view
->lineno
== view
->lines
- 1 &&
691 view
->lines
> view
->height
) {
692 steps
= view
->lines
- view
->offset
- 1;
693 if (steps
>= view
->height
)
694 steps
-= view
->height
- 1;
698 do_scroll_view(view
, steps
);
702 /* Draw the current line */
703 view
->draw(view
, view
->lineno
- view
->offset
);
705 redrawwin(view
->win
);
712 * Incremental updating
716 begin_update(struct view
*view
)
720 if (snprintf(view
->cmdbuf
, sizeof(view
->cmdbuf
), view
->cmd
,
721 id
, id
, id
) < sizeof(view
->cmdbuf
))
722 view
->pipe
= popen(view
->cmdbuf
, "r");
728 nodelay(status_win
, TRUE
);
737 for (i
= 0; i
< view
->lines
; i
++)
745 view
->start_time
= time(NULL
);
751 end_update(struct view
*view
)
754 nodelay(status_win
, FALSE
);
761 update_view(struct view
*view
)
766 /* The number of lines to read. If too low it will cause too much
767 * redrawing (and possible flickering), if too high responsiveness
769 int lines
= view
->height
;
770 int redraw_from
= -1;
775 /* Only redraw if lines are visible. */
776 if (view
->offset
+ view
->height
>= view
->lines
)
777 redraw_from
= view
->lines
- view
->offset
;
779 tmp
= realloc(view
->line
, sizeof(*view
->line
) * (view
->lines
+ lines
));
785 while ((line
= fgets(buffer
, sizeof(buffer
), view
->pipe
))) {
788 linelen
= strlen(line
);
790 line
[linelen
- 1] = 0;
792 if (!view
->read(view
, line
))
799 /* CPU hogilicious! */
802 if (redraw_from
>= 0) {
803 /* If this is an incremental update, redraw the previous line
804 * since for commits some members could have changed when
805 * loading the main view. */
809 /* Incrementally draw avoids flickering. */
810 redraw_view_from(view
, redraw_from
);
813 if (ferror(view
->pipe
)) {
814 report("Failed to read %s: %s", view
->cmd
, strerror(errno
));
817 } else if (feof(view
->pipe
)) {
818 time_t secs
= time(NULL
) - view
->start_time
;
820 if (view
== VIEW(REQ_VIEW_HELP
)) {
825 report("Loaded %d lines in %ld second%s", view
->lines
, secs
,
826 secs
== 1 ?
"" : "s");
833 report("Allocation failure");
841 switch_view(struct view
*prev
, int request
)
843 struct view
*view
= VIEW(request
);
844 struct view
*displayed
;
847 if (!view
->win
&& !resize_view(view
))
851 foreach_view (displayed
, i
)
855 report("Already in %s view", view
->name
);
859 report("FIXME: Maximize");
863 foreach_view (displayed
, i
) {
864 if (view
== displayed
) {
866 /* Blur out the title of the previous view. */
868 report("Switching to %s view", view
->name
);
873 /* Split to diff view */
875 SCALE_SPLIT_VIEW(prev
->height
) > 3 &&
876 prev
== VIEW(REQ_VIEW_MAIN
) &&
877 view
== VIEW(REQ_VIEW_DIFF
)) {
878 view
->height
= SCALE_SPLIT_VIEW(prev
->height
) - 1;
879 prev
->height
-= view
->height
+ 1;
881 wresize(prev
->win
, prev
->height
, prev
->width
);
882 mvwin(prev
->title
, prev
->height
, 0);
884 wresize(view
->win
, view
->height
, view
->width
);
885 mvwin(view
->win
, prev
->height
+ 1, 0);
886 mvwin(view
->title
, prev
->height
+ 1 + view
->height
, 0);
893 /* Continue loading split views in the background. */
894 if (prev
&& prev
->pipe
&& current_view
< 1)
897 if (begin_update(view
)) {
898 /* Clear the old view and let the incremental updating refill
900 display
[current_view
] = view
;
902 report("Loading...");
909 /* Process keystrokes */
911 view_driver(struct view
*view
, int key
)
913 int request
= get_request(key
);
921 case REQ_MOVE_PAGE_UP
:
922 case REQ_MOVE_PAGE_DOWN
:
923 case REQ_MOVE_FIRST_LINE
:
924 case REQ_MOVE_LAST_LINE
:
925 move_view(view
, request
);
928 case REQ_SCROLL_LINE_DOWN
:
929 case REQ_SCROLL_LINE_UP
:
930 case REQ_SCROLL_PAGE_DOWN
:
931 case REQ_SCROLL_PAGE_UP
:
932 scroll_view(view
, request
);
939 view
= switch_view(view
, request
);
942 case REQ_TOGGLE_LINE_NUMBERS
:
943 opt_line_number
= !opt_line_number
;
947 case REQ_STOP_LOADING
:
948 foreach_view (view
, i
)
953 case REQ_SHOW_VERSION
:
954 report("Version: %s", VERSION
);
957 case REQ_SCREEN_REDRAW
:
961 case REQ_SCREEN_UPDATE
:
969 /* An unknown key will show most commonly used commands. */
983 pager_draw(struct view
*view
, unsigned int lineno
)
990 if (view
->offset
+ lineno
>= view
->lines
)
993 line
= view
->line
[view
->offset
+ lineno
];
994 type
= get_line_type(line
);
996 if (view
->offset
+ lineno
== view
->lineno
) {
997 if (type
== LINE_COMMIT
)
998 string_copy(ref_commit
, line
+ 7);
1002 attr
= get_line_attr(type
);
1003 wattrset(view
->win
, attr
);
1005 linelen
= strlen(line
);
1006 linelen
= MIN(linelen
, view
->width
);
1008 if (opt_line_number
) {
1009 unsigned int real_lineno
= view
->offset
+ lineno
+ 1;
1012 if (real_lineno
== 1 || (real_lineno
% opt_num_interval
) == 0)
1013 mvwprintw(view
->win
, lineno
, 0, "%4d: ", real_lineno
);
1015 mvwaddstr(view
->win
, lineno
, 0, " : ");
1018 if (*line
== '\t') {
1019 waddnstr(view
->win
, " ", 8 - (col
% 8));
1020 col
+= 8 - (col
% 8);
1024 char *tab
= strchr(line
, '\t');
1027 waddnstr(view
->win
, line
, tab
- line
);
1029 waddstr(view
->win
, line
);
1034 waddstr(view
->win
, line
);
1038 /* NOTE: Code for only highlighting the text on the cursor line.
1039 * Kept since I've not yet decided whether to highlight the
1040 * entire line or not. --fonseca */
1041 /* No empty lines makes cursor drawing and clearing implicit. */
1043 line
= " ", linelen
= 1;
1045 mvwaddnstr(view
->win
, lineno
, 0, line
, linelen
);
1048 /* Paint the rest of the line if it's the cursor line. */
1049 if (type
== LINE_CURSOR
)
1050 wchgat(view
->win
, -1, 0, type
, NULL
);
1056 pager_read(struct view
*view
, char *line
)
1058 view
->line
[view
->lines
] = strdup(line
);
1059 if (!view
->line
[view
->lines
])
1067 main_draw(struct view
*view
, unsigned int lineno
)
1069 char buf
[DATE_COLS
+ 1];
1070 struct commit
*commit
;
1071 enum line_type type
;
1075 if (view
->offset
+ lineno
>= view
->lines
)
1078 commit
= view
->line
[view
->offset
+ lineno
];
1079 if (!*commit
->author
)
1082 if (view
->offset
+ lineno
== view
->lineno
) {
1083 string_copy(ref_commit
, commit
->id
);
1086 type
= LINE_MAIN_COMMIT
;
1089 wmove(view
->win
, lineno
, cols
);
1090 wattrset(view
->win
, get_line_attr(LINE_MAIN_DATE
));
1092 timelen
= strftime(buf
, sizeof(buf
), DATE_FORMAT
, &commit
->time
);
1093 waddnstr(view
->win
, buf
, timelen
);
1094 waddstr(view
->win
, " ");
1097 wmove(view
->win
, lineno
, cols
);
1098 wattrset(view
->win
, get_line_attr(LINE_MAIN_AUTHOR
));
1100 if (strlen(commit
->author
) > 19) {
1101 waddnstr(view
->win
, commit
->author
, 18);
1102 wattrset(view
->win
, get_line_attr(LINE_MAIN_DELIM
));
1103 waddch(view
->win
, '~');
1105 waddstr(view
->win
, commit
->author
);
1109 wattrset(view
->win
, A_NORMAL
);
1110 mvwaddch(view
->win
, lineno
, cols
, ACS_LTEE
);
1111 wattrset(view
->win
, get_line_attr(type
));
1112 mvwaddstr(view
->win
, lineno
, cols
+ 2, commit
->title
);
1113 wattrset(view
->win
, A_NORMAL
);
1118 /* Reads git log --pretty=raw output and parses it into the commit struct. */
1120 main_read(struct view
*view
, char *line
)
1122 enum line_type type
= get_line_type(line
);
1123 struct commit
*commit
;
1127 commit
= calloc(1, sizeof(struct commit
));
1131 line
+= STRING_SIZE("commit ");
1133 view
->line
[view
->lines
++] = commit
;
1134 string_copy(commit
->id
, line
);
1137 case LINE_AUTHOR_IDENT
:
1139 char *ident
= line
+ STRING_SIZE("author ");
1140 char *end
= strchr(ident
, '<');
1143 for (; end
> ident
&& isspace(end
[-1]); end
--) ;
1147 commit
= view
->line
[view
->lines
- 1];
1148 string_copy(commit
->author
, ident
);
1150 /* Parse epoch and timezone */
1152 char *secs
= strchr(end
+ 1, '>');
1156 if (!secs
|| secs
[1] != ' ')
1160 time
= (time_t) atol(secs
);
1161 zone
= strchr(secs
, ' ');
1162 if (zone
&& strlen(zone
) == STRING_SIZE(" +0700")) {
1166 tz
= ('0' - zone
[1]) * 60 * 60 * 10;
1167 tz
+= ('0' - zone
[2]) * 60 * 60;
1168 tz
+= ('0' - zone
[3]) * 60;
1169 tz
+= ('0' - zone
[4]) * 60;
1176 gmtime_r(&time
, &commit
->time
);
1181 /* We should only ever end up here if there has already been a
1182 * commit line, however, be safe. */
1183 if (view
->lines
== 0)
1186 /* Fill in the commit title if it has not already been set. */
1187 commit
= view
->line
[view
->lines
- 1];
1188 if (commit
->title
[0])
1191 /* Require titles to start with a non-space character at the
1192 * offset used by git log. */
1193 if (strncmp(line
, " ", 4) ||
1197 string_copy(commit
->title
, line
+ 4);
1215 /* FIXME: Shutdown gracefully. */
1220 static void die(const char *err
, ...)
1226 va_start(args
, err
);
1227 fputs("tig: ", stderr
);
1228 vfprintf(stderr
, err
, args
);
1229 fputs("\n", stderr
);
1236 update_title(struct view
*view
)
1238 werase(view
->title
);
1239 wmove(view
->title
, 0, 0);
1241 if (view
== &views
[current_view
])
1242 wattrset(view
->title
, A_BOLD
);
1244 wattrset(view
->title
, A_NORMAL
);
1246 /* [main] ref: 334b506... - line 6 of 4383 (0%) */
1247 wprintw(view
->title
, "[%s] ref: %s", view
->name
, ref_commit
);
1249 wprintw(view
->title
, " - line %d of %d (%d%%)",
1252 (view
->lineno
+ 1) * 100 / view
->lines
);
1255 wrefresh(view
->title
);
1258 /* Update status and title window. */
1260 report(const char *msg
, ...)
1264 va_start(args
, msg
);
1266 /* Update the title window first, so the cursor ends up in the status
1268 update_title(display
[current_view
]);
1271 wmove(status_win
, 0, 0);
1272 vwprintw(status_win
, msg
, args
);
1273 wrefresh(status_win
);
1279 main(int argc
, char *argv
[])
1285 signal(SIGINT
, quit
);
1287 git_cmd
= parse_options(argc
, argv
);
1290 if (git_cmd
< argc
) {
1291 die("Too many options");
1294 request
= opt_request
;
1296 initscr(); /* Initialize the curses library */
1297 nonl(); /* Tell curses not to do NL->CR/NL on output */
1298 cbreak(); /* Take input chars one at a time, no wait for \n */
1299 noecho(); /* Don't echo input */
1300 leaveok(stdscr
, TRUE
);
1305 getmaxyx(stdscr
, y
, x
);
1306 status_win
= newwin(1, 0, y
- 1, 0);
1308 die("Failed to create status window");
1310 /* Enable keyboard mapping */
1311 keypad(status_win
, TRUE
);
1312 wbkgdset(status_win
, get_line_attr(LINE_STATUS
));
1314 while (view_driver(display
[current_view
], request
)) {
1318 foreach_view (view
, i
) {
1324 /* Refresh, accept single keystroke of input */
1325 request
= wgetch(status_win
);
1326 if (request
== KEY_RESIZE
) {
1329 getmaxyx(stdscr
, lines
, cols
);
1331 mvwin(status_win
, lines
- 1, 0);
1332 wresize(status_win
, 1, cols
- 1);
1344 * Features that should be explored.
1346 * - Dynamic scaling of line number indentation.
1348 * - Proper command line handling; ability to take the command that should be
1353 * - Internal command line (exmode-inspired) which allows to specify what git
1354 * log or git diff command to run. Example:
1358 * - Proper resizing support. I am yet to figure out whether catching SIGWINCH
1359 * is preferred over using ncurses' built-in support for resizing.
1365 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
1367 * This program is free software; you can redistribute it and/or modify
1368 * it under the terms of the GNU General Public License as published by
1369 * the Free Software Foundation; either version 2 of the License, or
1370 * (at your option) any later version.
1375 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
1376 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
1377 * gitk(1): git repository browser written using tcl/tk,
1378 * gitview(1): git repository browser written using python/gtk.