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] show [git show options]
19 * tig [options] < [git command output]
23 * Browse changes in a git repository.
27 #define VERSION "tig-0.1"
47 static void die(const char *err
, ...);
48 static void report(const char *msg
, ...);
49 static void set_nonblocking_input(bool loading
);
51 #define ABS(x) ((x) >= 0 ? (x) : -(x))
52 #define MIN(x, y) ((x) < (y) ? (x) : (y))
54 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
55 #define STRING_SIZE(x) (sizeof(x) - 1)
57 #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
58 #define SIZEOF_CMD 1024 /* Size of command buffer. */
60 /* This color name can be used to refer to the default term colors. */
61 #define COLOR_DEFAULT (-1)
63 /* The format and size of the date column in the main view. */
64 #define DATE_FORMAT "%Y-%m-%d %H:%M"
65 #define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
67 /* The default interval between line numbers. */
68 #define NUMBER_INTERVAL 1
70 #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
72 /* Some ascii-shorthands fitted into the ncurses namespace. */
74 #define KEY_RETURN '\r'
77 /* User action requests. */
79 /* Offset all requests to avoid conflicts with ncurses getch values. */
80 REQ_OFFSET
= KEY_MAX
+ 1,
82 /* XXX: Keep the view request first and in sync with views[]. */
97 REQ_TOGGLE_LINE_NUMBERS
,
108 REQ_SCROLL_LINE_DOWN
,
110 REQ_SCROLL_PAGE_DOWN
,
121 char id
[41]; /* SHA1 ID. */
122 char title
[75]; /* The first line of the commit message. */
123 char author
[75]; /* The author of the commit. */
124 struct tm time
; /* Date from the author ident. */
125 struct ref
**refs
; /* Repository references; tags & branch heads. */
133 string_ncopy(char *dst
, char *src
, int dstlen
)
135 strncpy(dst
, src
, dstlen
- 1);
140 /* Shorthand for safely copying into a fixed buffer. */
141 #define string_copy(dst, src) \
142 string_ncopy(dst, src, sizeof(dst))
146 * NOTE: The following is a slightly modified copy of the git project's shell
147 * quoting routines found in the quote.c file.
149 * Help to copy the thing properly quoted for the shell safety. any single
150 * quote is replaced with '\'', any exclamation point is replaced with '\!',
151 * and the whole thing is enclosed in a
154 * original sq_quote result
155 * name ==> name ==> 'name'
156 * a b ==> a b ==> 'a b'
157 * a'b ==> a'\''b ==> 'a'\''b'
158 * a!b ==> a'\!'b ==> 'a'\!'b'
162 sq_quote(char buf
[SIZEOF_CMD
], size_t bufsize
, const char *src
)
166 #define BUFPUT(x) ( (bufsize < SIZEOF_CMD) && (buf[bufsize++] = (x)) )
169 while ((c
= *src
++)) {
170 if (c
== '\'' || c
== '!') {
190 static int opt_line_number
= FALSE
;
191 static int opt_num_interval
= NUMBER_INTERVAL
;
192 static enum request opt_request
= REQ_VIEW_MAIN
;
193 static char opt_cmd
[SIZEOF_CMD
] = "";
194 static FILE *opt_pipe
= NULL
;
196 /* Returns the index of log or diff command or -1 to exit. */
198 parse_options(int argc
, char *argv
[])
202 for (i
= 1; i
< argc
; i
++) {
207 * Start up in log view using the internal log command.
209 if (!strcmp(opt
, "-l")) {
210 opt_request
= REQ_VIEW_LOG
;
216 * Start up in diff view using the internal diff command.
218 if (!strcmp(opt
, "-d")) {
219 opt_request
= REQ_VIEW_DIFF
;
224 * -n[INTERVAL], --line-number[=INTERVAL]::
225 * Prefix line numbers in log and diff view.
226 * Optionally, with interval different than each line.
228 if (!strncmp(opt
, "-n", 2) ||
229 !strncmp(opt
, "--line-number", 13)) {
235 } else if (opt
[STRING_SIZE("--line-number")] == '=') {
236 num
= opt
+ STRING_SIZE("--line-number=");
240 opt_num_interval
= atoi(num
);
242 opt_line_number
= TRUE
;
248 * Show version and exit.
250 if (!strcmp(opt
, "-v") ||
251 !strcmp(opt
, "--version")) {
252 printf("tig version %s\n", VERSION
);
258 * End of tig(1) options. Useful when specifying commands
259 * for the main view. Example:
261 * $ tig -- --since=1.month
263 if (!strcmp(opt
, "--")) {
270 * Open log view using the given git log options.
273 * Open diff view using the given git diff options.
276 * Open diff view using the given git show options.
278 if (!strcmp(opt
, "log") ||
279 !strcmp(opt
, "diff") ||
280 !strcmp(opt
, "show")) {
281 opt_request
= opt
[0] == 'l'
282 ? REQ_VIEW_LOG
: REQ_VIEW_DIFF
;
288 * $ tig tag-1.0..HEAD
291 if (opt
[0] && opt
[0] != '-')
294 die("unknown command '%s'", opt
);
297 if (!isatty(STDIN_FILENO
)) {
301 * If stdin is a pipe, any log or diff options will be ignored and the
302 * pager view will be opened loading data from stdin. The pager mode
303 * can be used for colorizing output from various git commands.
305 * Example on how to colorize the output of git-show(1):
309 opt_request
= REQ_VIEW_PAGER
;
312 } else if (i
< argc
) {
316 * Git command options
317 * ~~~~~~~~~~~~~~~~~~~
318 * All git command options specified on the command line will
319 * be passed to the given command and all will be shell quoted
322 * NOTE: It is possible to specify options even for the main
323 * view. If doing this you should not touch the `--pretty`
326 * Example on how to open the log view and show both author and
327 * committer information:
329 * $ tig log --pretty=fuller
332 if (opt_request
== REQ_VIEW_MAIN
)
333 /* XXX: This is vulnerable to the user overriding
334 * options required for the main view parser. */
335 string_copy(opt_cmd
, "git log --stat --pretty=raw");
337 string_copy(opt_cmd
, "git");
338 buf_size
= strlen(opt_cmd
);
340 while (buf_size
< sizeof(opt_cmd
) && i
< argc
) {
341 opt_cmd
[buf_size
++] = ' ';
342 buf_size
= sq_quote(opt_cmd
, buf_size
, argv
[i
++]);
345 if (buf_size
>= sizeof(opt_cmd
))
346 die("command too long");
348 opt_cmd
[buf_size
] = 0;
361 #define HELP "(d)iff, (l)og, (m)ain, (q)uit, (v)ersion, (h)elp"
368 struct keymap keymap
[] = {
373 * Switch to diff view.
375 * Switch to log view.
377 * Switch to main view.
379 * Switch to pager view.
383 * If in main view split the view
384 * and show the diff in the bottom view.
386 * Switch to next view.
388 { 'm', REQ_VIEW_MAIN
},
389 { 'd', REQ_VIEW_DIFF
},
390 { 'l', REQ_VIEW_LOG
},
391 { 'p', REQ_VIEW_PAGER
},
392 { 'h', REQ_VIEW_HELP
},
394 { KEY_TAB
, REQ_VIEW_NEXT
},
395 { KEY_RETURN
, REQ_ENTER
},
401 * Move curser one line up.
403 * Move cursor one line down.
405 * Move curser one page up.
407 * Move cursor one page down.
409 * Jump to first line.
413 { KEY_UP
, REQ_MOVE_UP
},
414 { 'k', REQ_MOVE_UP
},
415 { KEY_DOWN
, REQ_MOVE_DOWN
},
416 { 'j', REQ_MOVE_DOWN
},
417 { KEY_HOME
, REQ_MOVE_FIRST_LINE
},
418 { KEY_END
, REQ_MOVE_LAST_LINE
},
419 { KEY_NPAGE
, REQ_MOVE_PAGE_DOWN
},
420 { KEY_PPAGE
, REQ_MOVE_PAGE_UP
},
426 * Scroll view one line up.
428 * Scroll view one line down.
430 * Scroll view one page up.
432 * Scroll view one page down.
434 { KEY_IC
, REQ_SCROLL_LINE_UP
},
435 { KEY_DC
, REQ_SCROLL_LINE_DOWN
},
436 { 'w', REQ_SCROLL_PAGE_UP
},
437 { 's', REQ_SCROLL_PAGE_DOWN
},
447 * Stop all background loading.
451 * Toggle line numbers on/off.
453 * Open prompt. This allows you to specify what git command to run.
459 { KEY_ESC
, REQ_QUIT
},
461 { 'z', REQ_STOP_LOADING
},
462 { 'v', REQ_SHOW_VERSION
},
463 { 'r', REQ_SCREEN_REDRAW
},
464 { 'n', REQ_TOGGLE_LINE_NUMBERS
},
467 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
468 { ERR
, REQ_SCREEN_UPDATE
},
470 /* Use the ncurses SIGWINCH handler. */
471 { KEY_RESIZE
, REQ_SCREEN_RESIZE
},
479 for (i
= 0; i
< ARRAY_SIZE(keymap
); i
++)
480 if (keymap
[i
].alias
== key
)
481 return keymap
[i
].request
;
483 return (enum request
) key
;
488 * Line-oriented content detection.
492 /* Line type String to match Foreground Background Attributes
493 * --------- --------------- ---------- ---------- ---------- */ \
495 LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
496 LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
497 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
498 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
499 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
500 LINE(DIFF_OLDMODE, "old mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
501 LINE(DIFF_NEWMODE, "new mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
502 LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
503 LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
504 LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
505 LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
506 /* Pretty print commit header */ \
507 LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
508 LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
509 LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
510 LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
511 LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
512 LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
513 /* Raw commit header */ \
514 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
515 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
516 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
517 LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
518 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
520 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
521 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
523 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
524 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
525 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
526 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
527 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
528 LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
529 LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
530 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
531 LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
532 LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
533 LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD),
536 #define LINE(type, line, fg, bg, attr) \
543 char *line
; /* The start of line to match. */
544 int linelen
; /* Size of string to match. */
545 int fg
, bg
, attr
; /* Color and text attributes for the lines. */
548 static struct line_info line_info
[] = {
549 #define LINE(type, line, fg, bg, attr) \
550 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
555 static enum line_type
556 get_line_type(char *line
)
558 int linelen
= strlen(line
);
561 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++)
562 /* Case insensitive search matches Signed-off-by lines better. */
563 if (linelen
>= line_info
[type
].linelen
&&
564 !strncasecmp(line_info
[type
].line
, line
, line_info
[type
].linelen
))
571 get_line_attr(enum line_type type
)
573 assert(type
< ARRAY_SIZE(line_info
));
574 return COLOR_PAIR(type
) | line_info
[type
].attr
;
580 int default_bg
= COLOR_BLACK
;
581 int default_fg
= COLOR_WHITE
;
586 if (use_default_colors() != ERR
) {
591 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++) {
592 struct line_info
*info
= &line_info
[type
];
593 int bg
= info
->bg
== COLOR_DEFAULT ? default_bg
: info
->bg
;
594 int fg
= info
->fg
== COLOR_DEFAULT ? default_fg
: info
->fg
;
596 init_pair(type
, fg
, bg
);
602 * ENVIRONMENT VARIABLES
603 * ---------------------
604 * Several options related to the interface with git can be configured
605 * via environment options.
607 * Repository references
608 * ~~~~~~~~~~~~~~~~~~~~~
609 * Commits that are referenced by tags and branch heads will be marked
610 * by the reference name surrounded by '[' and ']':
612 * 2006-04-18 23:12 Jonas Fonseca | [tig-0.2] tig version 0.2
614 * If you want to filter out certain directories under `.git/refs/`, say
615 * `tmp` you can do it by setting the following variable:
617 * $ TIG_LS_REMOTE="git ls-remote | sed '/\/tmp\//d'"
619 * Or set the variable permanently in your environment.
622 * Set command for retrieving all repository references.private
625 #define TIG_LS_REMOTE \
631 * It is possible to alter which commands are used for the different views.
632 * If for example you prefer commits in the main to be sorted by date and
633 * only show 500 commits, use:
635 * $ TIG_MAIN_CMD="git log --date-order -n500 --pretty=raw %s" tig
637 * Or set the variable permanently in your environment.
639 * Notice, how `%s` is used to specify the commit reference. There can
640 * be a maximum of 5 `%s` ref specifications.
643 * The command used for the diff view. By default, git show is used
647 * The command used for the log view.
650 * The command used for the main view. Note, you must always specify
651 * the option: `--pretty=raw` since the main view parser expects to
655 #define TIG_DIFF_CMD \
656 "git show --patch-with-stat --find-copies-harder -B -C %s"
658 #define TIG_LOG_CMD \
659 "git log --cc --stat -n100 %s"
661 #define TIG_MAIN_CMD \
662 "git log --topo-order --stat --pretty=raw %s"
664 /* We silently ignore that the following are also exported. */
666 #define TIG_HELP_CMD \
667 "man tig 2> /dev/null"
669 #define TIG_PAGER_CMD \
678 const char *name
; /* View name */
679 char *cmd_fmt
; /* Default command line format */
680 char *cmd_env
; /* Command line set via environment */
681 char *id
; /* Points to either of ref_{head,commit} */
682 size_t objsize
; /* Size of objects in the line index */
685 /* Draw one line; @lineno must be < view->height. */
686 bool (*draw
)(struct view
*view
, unsigned int lineno
);
687 /* Read one line; updates view->line. */
688 bool (*read
)(struct view
*view
, char *line
);
689 /* Depending on view, change display based on current line. */
690 bool (*enter
)(struct view
*view
);
693 char cmd
[SIZEOF_CMD
]; /* Command buffer */
694 char ref
[SIZEOF_REF
]; /* Hovered commit reference */
695 char vid
[SIZEOF_REF
]; /* View ID. Set to id member when updating. */
697 int height
, width
; /* The width and height of the main window */
698 WINDOW
*win
; /* The main window */
699 WINDOW
*title
; /* The title window living below the main window */
702 unsigned long offset
; /* Offset of the window top */
703 unsigned long lineno
; /* Current line number */
706 unsigned long lines
; /* Total number of lines */
707 void **line
; /* Line index; each line contains user data */
708 unsigned int digits
; /* Number of digits in the lines member. */
715 static struct view_ops pager_ops
;
716 static struct view_ops main_ops
;
718 char ref_head
[SIZEOF_REF
] = "HEAD";
719 char ref_commit
[SIZEOF_REF
] = "HEAD";
721 #define VIEW_STR(name, cmd, env, ref, objsize, ops) \
722 { name, cmd, #env, ref, objsize, ops }
724 #define VIEW_(id, name, ops, ref, objsize) \
725 VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, objsize, ops)
727 static struct view views
[] = {
728 VIEW_(MAIN
, "main", &main_ops
, ref_head
, sizeof(struct commit
)),
729 VIEW_(DIFF
, "diff", &pager_ops
, ref_commit
, sizeof(char)),
730 VIEW_(LOG
, "log", &pager_ops
, ref_head
, sizeof(char)),
731 VIEW_(HELP
, "help", &pager_ops
, ref_head
, sizeof(char)),
732 VIEW_(PAGER
, "pager", &pager_ops
, "static", sizeof(char)),
735 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
737 /* The display array of active views and the index of the current view. */
738 static struct view
*display
[2];
739 static unsigned int current_view
;
741 #define foreach_view(view, i) \
742 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
746 redraw_view_from(struct view
*view
, int lineno
)
748 assert(0 <= lineno
&& lineno
< view
->height
);
750 for (; lineno
< view
->height
; lineno
++) {
751 if (!view
->ops
->draw(view
, lineno
))
755 redrawwin(view
->win
);
760 redraw_view(struct view
*view
)
763 redraw_view_from(view
, 0);
770 struct view
*base
= display
[0];
771 struct view
*view
= display
[1] ? display
[1] : display
[0];
773 /* Setup window dimensions */
775 getmaxyx(stdscr
, base
->height
, base
->width
);
777 /* Make room for the status window. */
781 /* Horizontal split. */
782 view
->width
= base
->width
;
783 view
->height
= SCALE_SPLIT_VIEW(base
->height
);
784 base
->height
-= view
->height
;
786 /* Make room for the title bar. */
790 /* Make room for the title bar. */
795 foreach_view (view
, i
) {
797 view
->win
= newwin(view
->height
, 0, offset
, 0);
799 die("Failed to create %s view", view
->name
);
801 scrollok(view
->win
, TRUE
);
803 view
->title
= newwin(1, 0, offset
+ view
->height
, 0);
805 die("Failed to create title window");
808 wresize(view
->win
, view
->height
, view
->width
);
809 mvwin(view
->win
, offset
, 0);
810 mvwin(view
->title
, offset
+ view
->height
, 0);
814 offset
+= view
->height
+ 1;
819 update_view_title(struct view
*view
)
821 if (view
== display
[current_view
])
822 wbkgdset(view
->title
, get_line_attr(LINE_TITLE_FOCUS
));
824 wbkgdset(view
->title
, get_line_attr(LINE_TITLE_BLUR
));
827 wmove(view
->title
, 0, 0);
829 /* [main] ref: 334b506... - commit 6 of 4383 (0%) */
832 wprintw(view
->title
, "[%s] ref: %s", view
->name
, view
->ref
);
834 wprintw(view
->title
, "[%s]", view
->name
);
837 char *type
= view
== VIEW(REQ_VIEW_MAIN
) ?
"commit" : "line";
839 wprintw(view
->title
, " - %s %d of %d (%d%%)",
843 (view
->lineno
+ 1) * 100 / view
->lines
);
846 wrefresh(view
->title
);
853 /* Scrolling backend */
855 do_scroll_view(struct view
*view
, int lines
)
857 /* The rendering expects the new offset. */
858 view
->offset
+= lines
;
860 assert(0 <= view
->offset
&& view
->offset
< view
->lines
);
863 /* Redraw the whole screen if scrolling is pointless. */
864 if (view
->height
< ABS(lines
)) {
868 int line
= lines
> 0 ? view
->height
- lines
: 0;
869 int end
= line
+ ABS(lines
);
871 wscrl(view
->win
, lines
);
873 for (; line
< end
; line
++) {
874 if (!view
->ops
->draw(view
, line
))
879 /* Move current line into the view. */
880 if (view
->lineno
< view
->offset
) {
881 view
->lineno
= view
->offset
;
882 view
->ops
->draw(view
, 0);
884 } else if (view
->lineno
>= view
->offset
+ view
->height
) {
885 view
->lineno
= view
->offset
+ view
->height
- 1;
886 view
->ops
->draw(view
, view
->lineno
- view
->offset
);
889 assert(view
->offset
<= view
->lineno
&& view
->lineno
< view
->lines
);
891 redrawwin(view
->win
);
896 /* Scroll frontend */
898 scroll_view(struct view
*view
, enum request request
)
903 case REQ_SCROLL_PAGE_DOWN
:
904 lines
= view
->height
;
905 case REQ_SCROLL_LINE_DOWN
:
906 if (view
->offset
+ lines
> view
->lines
)
907 lines
= view
->lines
- view
->offset
;
909 if (lines
== 0 || view
->offset
+ view
->height
>= view
->lines
) {
910 report("Cannot scroll beyond the last line");
915 case REQ_SCROLL_PAGE_UP
:
916 lines
= view
->height
;
917 case REQ_SCROLL_LINE_UP
:
918 if (lines
> view
->offset
)
919 lines
= view
->offset
;
922 report("Cannot scroll beyond the first line");
930 die("request %d not handled in switch", request
);
933 do_scroll_view(view
, lines
);
938 move_view(struct view
*view
, enum request request
)
943 case REQ_MOVE_FIRST_LINE
:
944 steps
= -view
->lineno
;
947 case REQ_MOVE_LAST_LINE
:
948 steps
= view
->lines
- view
->lineno
- 1;
951 case REQ_MOVE_PAGE_UP
:
952 steps
= view
->height
> view
->lineno
953 ?
-view
->lineno
: -view
->height
;
956 case REQ_MOVE_PAGE_DOWN
:
957 steps
= view
->lineno
+ view
->height
>= view
->lines
958 ? view
->lines
- view
->lineno
- 1 : view
->height
;
970 die("request %d not handled in switch", request
);
973 if (steps
<= 0 && view
->lineno
== 0) {
974 report("Cannot move beyond the first line");
977 } else if (steps
>= 0 && view
->lineno
+ 1 >= view
->lines
) {
978 report("Cannot move beyond the last line");
982 /* Move the current line */
983 view
->lineno
+= steps
;
984 assert(0 <= view
->lineno
&& view
->lineno
< view
->lines
);
986 /* Repaint the old "current" line if we be scrolling */
987 if (ABS(steps
) < view
->height
) {
988 int prev_lineno
= view
->lineno
- steps
- view
->offset
;
990 wmove(view
->win
, prev_lineno
, 0);
991 wclrtoeol(view
->win
);
992 view
->ops
->draw(view
, prev_lineno
);
995 /* Check whether the view needs to be scrolled */
996 if (view
->lineno
< view
->offset
||
997 view
->lineno
>= view
->offset
+ view
->height
) {
998 if (steps
< 0 && -steps
> view
->offset
) {
999 steps
= -view
->offset
;
1001 } else if (steps
> 0) {
1002 if (view
->lineno
== view
->lines
- 1 &&
1003 view
->lines
> view
->height
) {
1004 steps
= view
->lines
- view
->offset
- 1;
1005 if (steps
>= view
->height
)
1006 steps
-= view
->height
- 1;
1010 do_scroll_view(view
, steps
);
1014 /* Draw the current line */
1015 view
->ops
->draw(view
, view
->lineno
- view
->offset
);
1017 redrawwin(view
->win
);
1018 wrefresh(view
->win
);
1024 * Incremental updating
1028 begin_update(struct view
*view
)
1030 char *id
= view
->id
;
1033 string_copy(view
->cmd
, opt_cmd
);
1035 /* When running random commands, the view ref could have become
1036 * invalid so clear it. */
1039 char *format
= view
->cmd_env ? view
->cmd_env
: view
->cmd_fmt
;
1041 if (snprintf(view
->cmd
, sizeof(view
->cmd
), format
,
1042 id
, id
, id
, id
, id
) >= sizeof(view
->cmd
))
1046 /* Special case for the pager view. */
1048 view
->pipe
= opt_pipe
;
1051 view
->pipe
= popen(view
->cmd
, "r");
1057 set_nonblocking_input(TRUE
);
1062 string_copy(view
->vid
, id
);
1067 for (i
= 0; i
< view
->lines
; i
++)
1069 free(view
->line
[i
]);
1075 view
->start_time
= time(NULL
);
1081 end_update(struct view
*view
)
1085 set_nonblocking_input(FALSE
);
1086 if (view
->pipe
== stdin
)
1094 update_view(struct view
*view
)
1096 char buffer
[BUFSIZ
];
1099 /* The number of lines to read. If too low it will cause too much
1100 * redrawing (and possible flickering), if too high responsiveness
1102 unsigned long lines
= view
->height
;
1103 int redraw_from
= -1;
1108 /* Only redraw if lines are visible. */
1109 if (view
->offset
+ view
->height
>= view
->lines
)
1110 redraw_from
= view
->lines
- view
->offset
;
1112 tmp
= realloc(view
->line
, sizeof(*view
->line
) * (view
->lines
+ lines
));
1118 while ((line
= fgets(buffer
, sizeof(buffer
), view
->pipe
))) {
1119 int linelen
= strlen(line
);
1122 line
[linelen
- 1] = 0;
1124 if (!view
->ops
->read(view
, line
))
1134 lines
= view
->lines
;
1135 for (digits
= 0; lines
; digits
++)
1138 /* Keep the displayed view in sync with line number scaling. */
1139 if (digits
!= view
->digits
) {
1140 view
->digits
= digits
;
1145 if (redraw_from
>= 0) {
1146 /* If this is an incremental update, redraw the previous line
1147 * since for commits some members could have changed when
1148 * loading the main view. */
1149 if (redraw_from
> 0)
1152 /* Incrementally draw avoids flickering. */
1153 redraw_view_from(view
, redraw_from
);
1156 /* Update the title _after_ the redraw so that if the redraw picks up a
1157 * commit reference in view->ref it'll be available here. */
1158 update_view_title(view
);
1160 if (ferror(view
->pipe
)) {
1161 report("Failed to read: %s", strerror(errno
));
1164 } else if (feof(view
->pipe
)) {
1165 time_t secs
= time(NULL
) - view
->start_time
;
1167 if (view
== VIEW(REQ_VIEW_HELP
)) {
1172 report("Loaded %d lines in %ld second%s", view
->lines
, secs
,
1173 secs
== 1 ?
"" : "s");
1180 report("Allocation failure");
1188 OPEN_DEFAULT
= 0, /* Use default view switching. */
1189 OPEN_SPLIT
= 1, /* Split current view. */
1190 OPEN_BACKGROUNDED
= 2, /* Backgrounded. */
1191 OPEN_RELOAD
= 4, /* Reload view even if it is the current. */
1195 open_view(struct view
*prev
, enum request request
, enum open_flags flags
)
1197 bool backgrounded
= !!(flags
& OPEN_BACKGROUNDED
);
1198 bool split
= !!(flags
& OPEN_SPLIT
);
1199 bool reload
= !!(flags
& OPEN_RELOAD
);
1200 struct view
*view
= VIEW(request
);
1201 struct view
*displayed
;
1204 /* Cycle between displayed views and count the views. */
1205 foreach_view (displayed
, nviews
) {
1207 view
== displayed
&&
1208 !strcmp(view
->vid
, prev
->vid
)) {
1209 current_view
= nviews
;
1210 /* Blur out the title of the previous view. */
1211 update_view_title(prev
);
1212 report("Switching to %s view", view
->name
);
1217 if (view
== prev
&& nviews
== 1 && !reload
) {
1218 report("Already in %s view", view
->name
);
1222 if ((reload
|| strcmp(view
->vid
, view
->id
)) &&
1223 !begin_update(view
)) {
1224 report("Failed to load %s view", view
->name
);
1229 display
[current_view
+ 1] = view
;
1233 /* Maximize the current view. */
1234 memset(display
, 0, sizeof(display
));
1236 display
[current_view
] = view
;
1241 if (split
&& prev
->lineno
- prev
->offset
>= prev
->height
) {
1242 /* Take the title line into account. */
1243 int lines
= prev
->lineno
- prev
->offset
- prev
->height
+ 1;
1245 /* Scroll the view that was split if the current line is
1246 * outside the new limited view. */
1247 do_scroll_view(prev
, lines
);
1250 if (prev
&& view
!= prev
) {
1251 /* "Blur" the previous view. */
1252 update_view_title(prev
);
1254 /* Continue loading split views in the background. */
1260 /* Clear the old view and let the incremental updating refill
1263 report("Loading...");
1272 * User request switch noodle
1276 view_driver(struct view
*view
, enum request request
)
1283 case REQ_MOVE_PAGE_UP
:
1284 case REQ_MOVE_PAGE_DOWN
:
1285 case REQ_MOVE_FIRST_LINE
:
1286 case REQ_MOVE_LAST_LINE
:
1287 move_view(view
, request
);
1290 case REQ_SCROLL_LINE_DOWN
:
1291 case REQ_SCROLL_LINE_UP
:
1292 case REQ_SCROLL_PAGE_DOWN
:
1293 case REQ_SCROLL_PAGE_UP
:
1294 scroll_view(view
, request
);
1301 case REQ_VIEW_PAGER
:
1302 open_view(view
, request
, OPEN_DEFAULT
);
1307 report("Nothing to enter");
1310 return view
->ops
->enter(view
);
1314 int nviews
= display
[1] ?
2 : 1;
1315 int next_view
= (current_view
+ 1) % nviews
;
1317 if (next_view
== current_view
) {
1318 report("Only one view is displayed");
1322 current_view
= next_view
;
1323 /* Blur out the title of the previous view. */
1324 update_view_title(view
);
1325 report("Switching to %s view", display
[current_view
]->name
);
1328 case REQ_TOGGLE_LINE_NUMBERS
:
1329 opt_line_number
= !opt_line_number
;
1334 /* Always reload^Wrerun commands from the prompt. */
1335 open_view(view
, opt_request
, OPEN_RELOAD
);
1338 case REQ_STOP_LOADING
:
1339 foreach_view (view
, i
) {
1341 report("Stopped loaded of %s view", view
->name
),
1346 case REQ_SHOW_VERSION
:
1347 report("Version: %s", VERSION
);
1350 case REQ_SCREEN_RESIZE
:
1353 case REQ_SCREEN_REDRAW
:
1354 foreach_view (view
, i
) {
1356 update_view_title(view
);
1360 case REQ_SCREEN_UPDATE
:
1368 /* An unknown key will show most commonly used commands. */
1378 * View backend handlers
1382 pager_draw(struct view
*view
, unsigned int lineno
)
1384 enum line_type type
;
1389 if (view
->offset
+ lineno
>= view
->lines
)
1392 line
= view
->line
[view
->offset
+ lineno
];
1393 type
= get_line_type(line
);
1395 if (view
->offset
+ lineno
== view
->lineno
) {
1396 if (type
== LINE_COMMIT
) {
1397 string_copy(view
->ref
, line
+ 7);
1398 string_copy(ref_commit
, view
->ref
);
1404 attr
= get_line_attr(type
);
1405 wattrset(view
->win
, attr
);
1407 linelen
= strlen(line
);
1408 linelen
= MIN(linelen
, view
->width
);
1410 if (opt_line_number
) {
1411 static char indent
[] = " ";
1412 unsigned long real_lineno
= view
->offset
+ lineno
+ 1;
1415 if (real_lineno
== 1 || (real_lineno
% opt_num_interval
) == 0)
1416 mvwprintw(view
->win
, lineno
, 0, "%.*d", view
->digits
, real_lineno
);
1418 else if (view
->digits
< sizeof(indent
))
1419 mvwaddnstr(view
->win
, lineno
, 0, indent
, view
->digits
);
1421 waddstr(view
->win
, ": ");
1424 if (*line
== '\t') {
1425 waddnstr(view
->win
, " ", 8 - (col
% 8));
1426 col
+= 8 - (col
% 8);
1430 char *tab
= strchr(line
, '\t');
1433 waddnstr(view
->win
, line
, tab
- line
);
1435 waddstr(view
->win
, line
);
1440 waddstr(view
->win
, line
);
1444 /* NOTE: Code for only highlighting the text on the cursor line.
1445 * Kept since I've not yet decided whether to highlight the
1446 * entire line or not. --fonseca */
1447 /* No empty lines makes cursor drawing and clearing implicit. */
1449 line
= " ", linelen
= 1;
1451 mvwaddnstr(view
->win
, lineno
, 0, line
, linelen
);
1454 /* Paint the rest of the line if it's the cursor line. */
1455 if (type
== LINE_CURSOR
)
1456 wchgat(view
->win
, -1, 0, type
, NULL
);
1462 pager_read(struct view
*view
, char *line
)
1464 view
->line
[view
->lines
] = strdup(line
);
1465 if (!view
->line
[view
->lines
])
1473 pager_enter(struct view
*view
)
1475 char *line
= view
->line
[view
->lineno
];
1477 if (get_line_type(line
) == LINE_COMMIT
) {
1478 open_view(view
, REQ_VIEW_DIFF
, OPEN_DEFAULT
);
1484 static struct view_ops pager_ops
= {
1491 static struct ref
**get_refs(char *id
);
1494 main_draw(struct view
*view
, unsigned int lineno
)
1496 char buf
[DATE_COLS
+ 1];
1497 struct commit
*commit
;
1498 enum line_type type
;
1502 if (view
->offset
+ lineno
>= view
->lines
)
1505 commit
= view
->line
[view
->offset
+ lineno
];
1506 if (!*commit
->author
)
1509 if (view
->offset
+ lineno
== view
->lineno
) {
1510 string_copy(view
->ref
, commit
->id
);
1511 string_copy(ref_commit
, view
->ref
);
1514 type
= LINE_MAIN_COMMIT
;
1517 wmove(view
->win
, lineno
, cols
);
1518 wattrset(view
->win
, get_line_attr(LINE_MAIN_DATE
));
1520 timelen
= strftime(buf
, sizeof(buf
), DATE_FORMAT
, &commit
->time
);
1521 waddnstr(view
->win
, buf
, timelen
);
1522 waddstr(view
->win
, " ");
1525 wmove(view
->win
, lineno
, cols
);
1526 wattrset(view
->win
, get_line_attr(LINE_MAIN_AUTHOR
));
1528 if (strlen(commit
->author
) > 19) {
1529 waddnstr(view
->win
, commit
->author
, 18);
1530 wattrset(view
->win
, get_line_attr(LINE_MAIN_DELIM
));
1531 waddch(view
->win
, '~');
1533 waddstr(view
->win
, commit
->author
);
1537 wattrset(view
->win
, A_NORMAL
);
1538 mvwaddch(view
->win
, lineno
, cols
, ACS_LTEE
);
1539 wmove(view
->win
, lineno
, cols
+ 2);
1545 if (commit
->refs
[i
]->tag
)
1546 wattrset(view
->win
, get_line_attr(LINE_MAIN_TAG
));
1548 wattrset(view
->win
, get_line_attr(LINE_MAIN_REF
));
1549 waddstr(view
->win
, "[");
1550 waddstr(view
->win
, commit
->refs
[i
]->name
);
1551 waddstr(view
->win
, "]");
1552 wattrset(view
->win
, A_NORMAL
);
1553 waddstr(view
->win
, " ");
1554 } while (commit
->refs
[i
++]->next
);
1557 wattrset(view
->win
, get_line_attr(type
));
1558 waddstr(view
->win
, commit
->title
);
1559 wattrset(view
->win
, A_NORMAL
);
1564 /* Reads git log --pretty=raw output and parses it into the commit struct. */
1566 main_read(struct view
*view
, char *line
)
1568 enum line_type type
= get_line_type(line
);
1569 struct commit
*commit
;
1573 commit
= calloc(1, sizeof(struct commit
));
1577 line
+= STRING_SIZE("commit ");
1579 view
->line
[view
->lines
++] = commit
;
1580 string_copy(commit
->id
, line
);
1581 commit
->refs
= get_refs(commit
->id
);
1586 char *ident
= line
+ STRING_SIZE("author ");
1587 char *end
= strchr(ident
, '<');
1590 for (; end
> ident
&& isspace(end
[-1]); end
--) ;
1594 commit
= view
->line
[view
->lines
- 1];
1595 string_copy(commit
->author
, ident
);
1597 /* Parse epoch and timezone */
1599 char *secs
= strchr(end
+ 1, '>');
1603 if (!secs
|| secs
[1] != ' ')
1607 time
= (time_t) atol(secs
);
1608 zone
= strchr(secs
, ' ');
1609 if (zone
&& strlen(zone
) == STRING_SIZE(" +0700")) {
1613 tz
= ('0' - zone
[1]) * 60 * 60 * 10;
1614 tz
+= ('0' - zone
[2]) * 60 * 60;
1615 tz
+= ('0' - zone
[3]) * 60;
1616 tz
+= ('0' - zone
[4]) * 60;
1623 gmtime_r(&time
, &commit
->time
);
1628 /* We should only ever end up here if there has already been a
1629 * commit line, however, be safe. */
1630 if (view
->lines
== 0)
1633 /* Fill in the commit title if it has not already been set. */
1634 commit
= view
->line
[view
->lines
- 1];
1635 if (commit
->title
[0])
1638 /* Require titles to start with a non-space character at the
1639 * offset used by git log. */
1640 /* FIXME: More gracefull handling of titles; append "..." to
1641 * shortened titles, etc. */
1642 if (strncmp(line
, " ", 4) ||
1646 string_copy(commit
->title
, line
+ 4);
1653 main_enter(struct view
*view
)
1655 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
| OPEN_BACKGROUNDED
);
1659 static struct view_ops main_ops
= {
1670 /* Whether or not the curses interface has been initialized. */
1671 bool cursed
= FALSE
;
1673 /* The status window is used for polling keystrokes. */
1674 static WINDOW
*status_win
;
1676 /* Update status and title window. */
1678 report(const char *msg
, ...)
1682 /* Update the title window first, so the cursor ends up in the status
1684 update_view_title(display
[current_view
]);
1686 va_start(args
, msg
);
1689 wmove(status_win
, 0, 0);
1691 vwprintw(status_win
, msg
, args
);
1692 wrefresh(status_win
);
1697 /* Controls when nodelay should be in effect when polling user input. */
1699 set_nonblocking_input(bool loading
)
1701 /* The number of loading views. */
1702 static unsigned int nloading
;
1704 if ((loading
== FALSE
&& nloading
-- == 1) ||
1705 (loading
== TRUE
&& nloading
++ == 0))
1706 nodelay(status_win
, loading
);
1714 /* Initialize the curses library */
1715 if (isatty(STDIN_FILENO
)) {
1716 cursed
= !!initscr();
1718 /* Leave stdin and stdout alone when acting as a pager. */
1719 FILE *io
= fopen("/dev/tty", "r+");
1721 cursed
= !!newterm(NULL
, io
, io
);
1725 die("Failed to initialize curses");
1727 nonl(); /* Tell curses not to do NL->CR/NL on output */
1728 cbreak(); /* Take input chars one at a time, no wait for \n */
1729 noecho(); /* Don't echo input */
1730 leaveok(stdscr
, TRUE
);
1735 getmaxyx(stdscr
, y
, x
);
1736 status_win
= newwin(1, 0, y
- 1, 0);
1738 die("Failed to create status window");
1740 /* Enable keyboard mapping */
1741 keypad(status_win
, TRUE
);
1742 wbkgdset(status_win
, get_line_attr(LINE_STATUS
));
1747 * Repository references
1750 static struct ref
*refs
;
1753 static struct ref
**
1756 struct ref
**id_refs
= NULL
;
1757 size_t id_refs_size
= 0;
1760 for (i
= 0; i
< refs_size
; i
++) {
1763 if (strcmp(id
, refs
[i
].id
))
1766 tmp
= realloc(id_refs
, (id_refs_size
+ 1) * sizeof(*id_refs
));
1774 id_refs
[id_refs_size
++] = &refs
[i
];
1775 if (id_refs_size
> 1)
1776 id_refs
[id_refs_size
- 1]->next
= 1;
1785 char *cmd_env
= getenv("TIG_LS_REMOTE");
1786 char *cmd
= cmd_env ? cmd_env
: TIG_LS_REMOTE
;
1787 FILE *pipe
= popen(cmd
, "r");
1788 char buffer
[BUFSIZ
];
1794 while ((line
= fgets(buffer
, sizeof(buffer
), pipe
))) {
1795 char *name
= strchr(line
, '\t');
1804 namelen
= strlen(name
) - 1;
1805 if (name
[namelen
- 1] == '}') {
1806 while (namelen
> 0 && name
[namelen
] != '^')
1811 if (!strncmp(name
, "refs/tags/", STRING_SIZE("refs/tags/"))) {
1812 name
+= STRING_SIZE("refs/tags/");
1816 refs
= realloc(refs
, sizeof(*refs
) * (refs_size
+ 1));
1820 ref
= &refs
[refs_size
++];
1822 ref
->name
= strdup(name
);
1826 string_copy(ref
->id
, line
);
1844 /* XXX: Restore tty modes and let the OS cleanup the rest! */
1850 static void die(const char *err
, ...)
1856 va_start(args
, err
);
1857 fputs("tig: ", stderr
);
1858 vfprintf(stderr
, err
, args
);
1859 fputs("\n", stderr
);
1866 main(int argc
, char *argv
[])
1869 enum request request
;
1872 signal(SIGINT
, quit
);
1874 if (!parse_options(argc
, argv
))
1877 if (load_refs() == ERR
)
1878 die("Failed to load refs.");
1880 for (i
= 0; i
< ARRAY_SIZE(views
) && (view
= &views
[i
]); i
++)
1881 view
->cmd_env
= getenv(view
->cmd_env
);
1883 request
= opt_request
;
1887 while (view_driver(display
[current_view
], request
)) {
1891 foreach_view (view
, i
)
1894 /* Refresh, accept single keystroke of input */
1895 key
= wgetch(status_win
);
1896 request
= get_request(key
);
1898 /* Some low-level request handling. This keeps handling of
1899 * status_win restricted. */
1903 /* Temporarily switch to line-oriented and echoed
1908 if (wgetnstr(status_win
, opt_cmd
+ 4, sizeof(opt_cmd
) - 4) == OK
) {
1909 memcpy(opt_cmd
, "git ", 4);
1910 opt_request
= REQ_VIEW_PAGER
;
1919 case REQ_SCREEN_RESIZE
:
1923 getmaxyx(stdscr
, height
, width
);
1925 /* Resize the status view and let the view driver take
1926 * care of resizing the displayed views. */
1927 wresize(status_win
, 1, width
);
1928 mvwin(status_win
, height
- 1, 0);
1929 wrefresh(status_win
);
1945 * Features that should be explored.
1953 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
1955 * This program is free software; you can redistribute it and/or modify
1956 * it under the terms of the GNU General Public License as published by
1957 * the Free Software Foundation; either version 2 of the License, or
1958 * (at your option) any later version.
1963 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
1964 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
1965 * gitk(1): git repository browser written using tcl/tk,
1966 * gitview(1): git repository browser written using python/gtk.