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. Additionally, tig(1) can also act
24 * as a pager for output of various git commands.
26 * When browsing repositories, tig(1) uses the underlying git commands
27 * to present the user with various views, such as summarized commit log
28 * and showing the commit with the log message, diffstat, and the diff.
30 * Using tig(1) as a pager, it will display input from stdin and try
35 #define VERSION "tig-0.3"
55 static void die(const char *err
, ...);
56 static void report(const char *msg
, ...);
57 static void set_nonblocking_input(bool loading
);
58 static size_t utf8_length(const char *string
, size_t max_width
, int *coloffset
, int *trimmed
);
60 #define ABS(x) ((x) >= 0 ? (x) : -(x))
61 #define MIN(x, y) ((x) < (y) ? (x) : (y))
63 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
64 #define STRING_SIZE(x) (sizeof(x) - 1)
66 #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
67 #define SIZEOF_CMD 1024 /* Size of command buffer. */
69 /* This color name can be used to refer to the default term colors. */
70 #define COLOR_DEFAULT (-1)
72 #define TIG_HELP "(d)iff, (l)og, (m)ain, (q)uit, (h)elp"
74 /* The format and size of the date column in the main view. */
75 #define DATE_FORMAT "%Y-%m-%d %H:%M"
76 #define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
78 #define AUTHOR_COLS 20
80 /* The default interval between line numbers. */
81 #define NUMBER_INTERVAL 1
85 #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
87 /* Some ascii-shorthands fitted into the ncurses namespace. */
89 #define KEY_RETURN '\r'
93 /* User action requests. */
95 /* Offset all requests to avoid conflicts with ncurses getch values. */
96 REQ_OFFSET
= KEY_MAX
+ 1,
98 /* XXX: Keep the view request first and in sync with views[]. */
113 REQ_TOGGLE_LINE_NUMBERS
,
127 REQ_SCROLL_LINE_DOWN
,
129 REQ_SCROLL_PAGE_DOWN
,
133 char *name
; /* Ref name; tag or head names are shortened. */
134 char id
[41]; /* Commit SHA1 ID */
135 unsigned int tag
:1; /* Is it a tag? */
136 unsigned int next
:1; /* For ref lists: are there more refs? */
140 char id
[41]; /* SHA1 ID. */
141 char title
[75]; /* The first line of the commit message. */
142 char author
[75]; /* The author of the commit. */
143 struct tm time
; /* Date from the author ident. */
144 struct ref
**refs
; /* Repository references; tags & branch heads. */
153 string_ncopy(char *dst
, const char *src
, int dstlen
)
155 strncpy(dst
, src
, dstlen
- 1);
160 /* Shorthand for safely copying into a fixed buffer. */
161 #define string_copy(dst, src) \
162 string_ncopy(dst, src, sizeof(dst))
167 * NOTE: The following is a slightly modified copy of the git project's shell
168 * quoting routines found in the quote.c file.
170 * Help to copy the thing properly quoted for the shell safety. any single
171 * quote is replaced with '\'', any exclamation point is replaced with '\!',
172 * and the whole thing is enclosed in a
175 * original sq_quote result
176 * name ==> name ==> 'name'
177 * a b ==> a b ==> 'a b'
178 * a'b ==> a'\''b ==> 'a'\''b'
179 * a!b ==> a'\!'b ==> 'a'\!'b'
183 sq_quote(char buf
[SIZEOF_CMD
], size_t bufsize
, const char *src
)
187 #define BUFPUT(x) do { if (bufsize < SIZEOF_CMD) buf[bufsize++] = (x); } while (0)
190 while ((c
= *src
++)) {
191 if (c
== '\'' || c
== '!') {
211 /* Option and state variables. */
212 static bool opt_line_number
= FALSE
;
213 static int opt_num_interval
= NUMBER_INTERVAL
;
214 static int opt_tab_size
= TABSIZE
;
215 static enum request opt_request
= REQ_VIEW_MAIN
;
216 static char opt_cmd
[SIZEOF_CMD
] = "";
217 static FILE *opt_pipe
= NULL
;
219 /* Returns the index of log or diff command or -1 to exit. */
221 parse_options(int argc
, char *argv
[])
225 for (i
= 1; i
< argc
; i
++) {
230 * Start up in log view using the internal log command.
232 if (!strcmp(opt
, "-l")) {
233 opt_request
= REQ_VIEW_LOG
;
239 * Start up in diff view using the internal diff command.
241 if (!strcmp(opt
, "-d")) {
242 opt_request
= REQ_VIEW_DIFF
;
247 * -n[INTERVAL], --line-number[=INTERVAL]::
248 * Prefix line numbers in log and diff view.
249 * Optionally, with interval different than each line.
251 if (!strncmp(opt
, "-n", 2) ||
252 !strncmp(opt
, "--line-number", 13)) {
258 } else if (opt
[STRING_SIZE("--line-number")] == '=') {
259 num
= opt
+ STRING_SIZE("--line-number=");
263 opt_num_interval
= atoi(num
);
265 opt_line_number
= TRUE
;
270 * -t[NSPACES], --tab-size[=NSPACES]::
271 * Set the number of spaces tabs should be expanded to.
273 if (!strncmp(opt
, "-t", 2) ||
274 !strncmp(opt
, "--tab-size", 10)) {
280 } else if (opt
[STRING_SIZE("--tab-size")] == '=') {
281 num
= opt
+ STRING_SIZE("--tab-size=");
285 opt_tab_size
= MIN(atoi(num
), TABSIZE
);
291 * Show version and exit.
293 if (!strcmp(opt
, "-v") ||
294 !strcmp(opt
, "--version")) {
295 printf("tig version %s\n", VERSION
);
301 * End of tig(1) options. Useful when specifying command
302 * options for the main view. Example:
304 * $ tig -- --since=1.month
306 if (!strcmp(opt
, "--")) {
312 * log [git log options]::
313 * Open log view using the given git log options.
315 * diff [git diff options]::
316 * Open diff view using the given git diff options.
318 * show [git show options]::
319 * Open diff view using the given git show options.
321 if (!strcmp(opt
, "log") ||
322 !strcmp(opt
, "diff") ||
323 !strcmp(opt
, "show")) {
324 opt_request
= opt
[0] == 'l'
325 ? REQ_VIEW_LOG
: REQ_VIEW_DIFF
;
330 * [git log options]::
331 * tig(1) will stop the option parsing when the first
332 * command line parameter not starting with "-" is
333 * encountered. All options including this one will be
334 * passed to git log when loading the main view.
335 * This makes it possible to say:
337 * $ tig tag-1.0..HEAD
339 if (opt
[0] && opt
[0] != '-')
342 die("unknown command '%s'", opt
);
345 if (!isatty(STDIN_FILENO
)) {
349 * If stdin is a pipe, any log or diff options will be ignored and the
350 * pager view will be opened loading data from stdin. The pager mode
351 * can be used for colorizing output from various git commands.
353 * Example on how to colorize the output of git-show(1):
357 opt_request
= REQ_VIEW_PAGER
;
360 } else if (i
< argc
) {
364 * Git command options
365 * ~~~~~~~~~~~~~~~~~~~
366 * All git command options specified on the command line will
367 * be passed to the given command and all will be shell quoted
368 * before they are passed to the shell.
370 * NOTE: If you specify options for the main view, you should
371 * not use the `--pretty` option as this option will be set
372 * automatically to the format expected by the main view.
374 * Example on how to open the log view and show both author and
375 * committer information:
377 * $ tig log --pretty=fuller
379 * See the <<refspec, "Specifying revisions">> section below
380 * for an introduction to revision options supported by the git
381 * commands. For details on specific git command options, refer
382 * to the man page of the command in question.
385 if (opt_request
== REQ_VIEW_MAIN
)
386 /* XXX: This is vulnerable to the user overriding
387 * options required for the main view parser. */
388 string_copy(opt_cmd
, "git log --stat --pretty=raw");
390 string_copy(opt_cmd
, "git");
391 buf_size
= strlen(opt_cmd
);
393 while (buf_size
< sizeof(opt_cmd
) && i
< argc
) {
394 opt_cmd
[buf_size
++] = ' ';
395 buf_size
= sq_quote(opt_cmd
, buf_size
, argv
[i
++]);
398 if (buf_size
>= sizeof(opt_cmd
))
399 die("command too long");
401 opt_cmd
[buf_size
] = 0;
410 * Line-oriented content detection.
414 /* Line type String to match Foreground Background Attributes
415 * --------- --------------- ---------- ---------- ---------- */ \
417 LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
418 LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
419 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
420 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
421 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
422 LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
423 LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
424 LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
425 LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
426 LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
427 LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
428 /* Pretty print commit header */ \
429 LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
430 LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
431 LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
432 LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
433 LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
434 LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
435 /* Raw commit header */ \
436 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
437 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
438 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
439 LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
440 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
442 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
443 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
445 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
446 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
447 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
448 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
449 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
450 LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
451 LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
452 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
453 LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
454 LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
455 LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD),
458 #define LINE(type, line, fg, bg, attr) \
465 const char *line
; /* The start of line to match. */
466 int linelen
; /* Size of string to match. */
467 int fg
, bg
, attr
; /* Color and text attributes for the lines. */
470 static struct line_info line_info
[] = {
471 #define LINE(type, line, fg, bg, attr) \
472 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
477 static enum line_type
478 get_line_type(char *line
)
480 int linelen
= strlen(line
);
483 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++)
484 /* Case insensitive search matches Signed-off-by lines better. */
485 if (linelen
>= line_info
[type
].linelen
&&
486 !strncasecmp(line_info
[type
].line
, line
, line_info
[type
].linelen
))
493 get_line_attr(enum line_type type
)
495 assert(type
< ARRAY_SIZE(line_info
));
496 return COLOR_PAIR(type
) | line_info
[type
].attr
;
502 int default_bg
= COLOR_BLACK
;
503 int default_fg
= COLOR_WHITE
;
508 if (use_default_colors() != ERR
) {
513 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++) {
514 struct line_info
*info
= &line_info
[type
];
515 int bg
= info
->bg
== COLOR_DEFAULT ? default_bg
: info
->bg
;
516 int fg
= info
->fg
== COLOR_DEFAULT ? default_fg
: info
->fg
;
518 init_pair(type
, fg
, bg
);
524 * ENVIRONMENT VARIABLES
525 * ---------------------
526 * Several options related to the interface with git can be configured
527 * via environment options.
529 * Repository references
530 * ~~~~~~~~~~~~~~~~~~~~~
531 * Commits that are referenced by tags and branch heads will be marked
532 * by the reference name surrounded by '[' and ']':
534 * 2006-03-26 19:42 Petr Baudis | [cogito-0.17.1] Cogito 0.17.1
536 * If you want to filter out certain directories under `.git/refs/`, say
537 * `tmp` you can do it by setting the following variable:
539 * $ TIG_LS_REMOTE="git ls-remote . | sed /\/tmp\//d" tig
541 * Or set the variable permanently in your environment.
544 * Set command for retrieving all repository references. The command
545 * should output data in the same format as git-ls-remote(1).
548 #define TIG_LS_REMOTE \
549 "git ls-remote . 2>/dev/null"
555 * It is possible to alter which commands are used for the different views.
556 * If for example you prefer commits in the main view to be sorted by date
557 * and only show 500 commits, use:
559 * $ TIG_MAIN_CMD="git log --date-order -n500 --pretty=raw %s" tig
561 * Or set the variable permanently in your environment.
563 * Notice, how `%s` is used to specify the commit reference. There can
564 * be a maximum of 5 `%s` ref specifications.
567 * The command used for the diff view. By default, git show is used
571 * The command used for the log view. If you prefer to have both
572 * author and committer shown in the log view be sure to pass
573 * `--pretty=fuller` to git log.
576 * The command used for the main view. Note, you must always specify
577 * the option: `--pretty=raw` since the main view parser expects to
581 #define TIG_DIFF_CMD \
582 "git show --patch-with-stat --find-copies-harder -B -C %s"
584 #define TIG_LOG_CMD \
585 "git log --cc --stat -n100 %s"
587 #define TIG_MAIN_CMD \
588 "git log --topo-order --stat --pretty=raw %s"
590 /* ... silently ignore that the following are also exported. */
592 #define TIG_HELP_CMD \
593 "man tig 2>/dev/null"
595 #define TIG_PAGER_CMD \
602 * The display consists of a status window on the last line of the screen and
603 * one or more views. The default is to only show one view at the time but it
604 * is possible to split both the main and log view to also show the commit
607 * If you are in the log view and press 'Enter' when the current line is a
608 * commit line, such as:
610 * commit 4d55caff4cc89335192f3e566004b4ceef572521
612 * You will split the view so that the log view is displayed in the top window
613 * and the diff view in the bottom window. You can switch between the two
614 * views by pressing 'Tab'. To maximize the log view again, simply press 'l'.
619 /* The display array of active views and the index of the current view. */
620 static struct view
*display
[2];
621 static unsigned int current_view
;
623 #define foreach_view(view, i) \
624 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
628 * Current head and commit ID
629 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
630 * The viewer keeps track of both what head and commit ID you are currently
631 * viewing. The commit ID will follow the cursor line and change everytime time
632 * you highlight a different commit. Whenever you reopen the diff view it
633 * will be reloaded, if the commit ID changed.
635 * The head ID is used when opening the main and log view to indicate from
636 * what revision to show history.
639 static char ref_commit
[SIZEOF_REF
] = "HEAD";
640 static char ref_head
[SIZEOF_REF
] = "HEAD";
644 const char *name
; /* View name */
645 const char *cmd_fmt
; /* Default command line format */
646 const char *cmd_env
; /* Command line set via environment */
647 const char *id
; /* Points to either of ref_{head,commit} */
648 size_t objsize
; /* Size of objects in the line index */
651 /* What type of content being displayed. Used in the
654 /* Draw one line; @lineno must be < view->height. */
655 bool (*draw
)(struct view
*view
, unsigned int lineno
);
656 /* Read one line; updates view->line. */
657 bool (*read
)(struct view
*view
, char *line
);
658 /* Depending on view, change display based on current line. */
659 bool (*enter
)(struct view
*view
);
662 char cmd
[SIZEOF_CMD
]; /* Command buffer */
663 char ref
[SIZEOF_REF
]; /* Hovered commit reference */
664 char vid
[SIZEOF_REF
]; /* View ID. Set to id member when updating. */
666 int height
, width
; /* The width and height of the main window */
667 WINDOW
*win
; /* The main window */
668 WINDOW
*title
; /* The title window living below the main window */
671 unsigned long offset
; /* Offset of the window top */
672 unsigned long lineno
; /* Current line number */
675 unsigned long lines
; /* Total number of lines */
676 void **line
; /* Line index; each line contains user data */
677 unsigned int digits
; /* Number of digits in the lines member. */
684 static struct view_ops pager_ops
;
685 static struct view_ops main_ops
;
687 #define VIEW_STR(name, cmd, env, ref, objsize, ops) \
688 { name, cmd, #env, ref, objsize, ops }
690 #define VIEW_(id, name, ops, ref, objsize) \
691 VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, objsize, ops)
696 * tig(1) presents various 'views' of a repository. Each view is based on output
697 * from an external command, most often 'git log', 'git diff', or 'git show'.
700 * Is the default view, and it shows a one line summary of each commit
701 * in the chosen list of revisions. The summary includes commit date,
702 * author, and the first line of the log message. Additionally, any
703 * repository references, such as tags, will be shown.
706 * Presents a more rich view of the revision log showing the whole log
707 * message and the diffstat.
710 * Shows either the diff of the current working tree, that is, what
711 * has changed since the last commit, or the commit diff complete
712 * with log message, diffstat and diff.
715 * Is used for displaying both input from stdin and output from git
716 * commands entered in the internal prompt.
719 * Displays the information from the tig(1) man page. For the help view
720 * to work you need to have the tig(1) man page installed.
723 static struct view views
[] = {
724 VIEW_(MAIN
, "main", &main_ops
, ref_head
, sizeof(struct commit
)),
725 VIEW_(DIFF
, "diff", &pager_ops
, ref_commit
, sizeof(char)),
726 VIEW_(LOG
, "log", &pager_ops
, ref_head
, sizeof(char)),
727 VIEW_(HELP
, "help", &pager_ops
, "static", sizeof(char)),
728 VIEW_(PAGER
, "pager", &pager_ops
, "static", sizeof(char)),
731 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
735 redraw_view_from(struct view
*view
, int lineno
)
737 assert(0 <= lineno
&& lineno
< view
->height
);
739 for (; lineno
< view
->height
; lineno
++) {
740 if (!view
->ops
->draw(view
, lineno
))
744 redrawwin(view
->win
);
749 redraw_view(struct view
*view
)
752 redraw_view_from(view
, 0);
759 * Each view has a title window which shows the name of the view, current
760 * commit ID if available, and where the view is positioned:
762 * [main] c622eefaa485995320bc743431bae0d497b1d875 - commit 1 of 61 (1%)
764 * By default, the title of the current view is highlighted using bold font.
768 update_view_title(struct view
*view
)
770 if (view
== display
[current_view
])
771 wbkgdset(view
->title
, get_line_attr(LINE_TITLE_FOCUS
));
773 wbkgdset(view
->title
, get_line_attr(LINE_TITLE_BLUR
));
776 wmove(view
->title
, 0, 0);
779 wprintw(view
->title
, "[%s] %s", view
->name
, view
->ref
);
781 wprintw(view
->title
, "[%s]", view
->name
);
784 wprintw(view
->title
, " - %s %d of %d (%d%%)",
788 (view
->lineno
+ 1) * 100 / view
->lines
);
791 wrefresh(view
->title
);
798 struct view
*base
= display
[0];
799 struct view
*view
= display
[1] ? display
[1] : display
[0];
801 /* Setup window dimensions */
803 getmaxyx(stdscr
, base
->height
, base
->width
);
805 /* Make room for the status window. */
809 /* Horizontal split. */
810 view
->width
= base
->width
;
811 view
->height
= SCALE_SPLIT_VIEW(base
->height
);
812 base
->height
-= view
->height
;
814 /* Make room for the title bar. */
818 /* Make room for the title bar. */
823 foreach_view (view
, i
) {
824 /* Keep the height of all view->win windows one larger than is
825 * required so that the cursor can wrap-around on the last line
826 * without scrolling the window. */
828 view
->win
= newwin(view
->height
+ 1, 0, offset
, 0);
830 die("Failed to create %s view", view
->name
);
832 scrollok(view
->win
, TRUE
);
834 view
->title
= newwin(1, 0, offset
+ view
->height
, 0);
836 die("Failed to create title window");
839 wresize(view
->win
, view
->height
+ 1, view
->width
);
840 mvwin(view
->win
, offset
, 0);
841 mvwin(view
->title
, offset
+ view
->height
, 0);
845 offset
+= view
->height
+ 1;
855 foreach_view (view
, i
) {
857 update_view_title(view
);
866 /* Scrolling backend */
868 do_scroll_view(struct view
*view
, int lines
)
870 /* The rendering expects the new offset. */
871 view
->offset
+= lines
;
873 assert(0 <= view
->offset
&& view
->offset
< view
->lines
);
876 /* Redraw the whole screen if scrolling is pointless. */
877 if (view
->height
< ABS(lines
)) {
881 int line
= lines
> 0 ? view
->height
- lines
: 0;
882 int end
= line
+ ABS(lines
);
884 wscrl(view
->win
, lines
);
886 for (; line
< end
; line
++) {
887 if (!view
->ops
->draw(view
, line
))
892 /* Move current line into the view. */
893 if (view
->lineno
< view
->offset
) {
894 view
->lineno
= view
->offset
;
895 view
->ops
->draw(view
, 0);
897 } else if (view
->lineno
>= view
->offset
+ view
->height
) {
898 if (view
->lineno
== view
->offset
+ view
->height
) {
899 /* Clear the hidden line so it doesn't show if the view
901 wmove(view
->win
, view
->height
, 0);
902 wclrtoeol(view
->win
);
904 view
->lineno
= view
->offset
+ view
->height
- 1;
905 view
->ops
->draw(view
, view
->lineno
- view
->offset
);
908 assert(view
->offset
<= view
->lineno
&& view
->lineno
< view
->lines
);
910 redrawwin(view
->win
);
915 /* Scroll frontend */
917 scroll_view(struct view
*view
, enum request request
)
922 case REQ_SCROLL_PAGE_DOWN
:
923 lines
= view
->height
;
924 case REQ_SCROLL_LINE_DOWN
:
925 if (view
->offset
+ lines
> view
->lines
)
926 lines
= view
->lines
- view
->offset
;
928 if (lines
== 0 || view
->offset
+ view
->height
>= view
->lines
) {
929 report("Cannot scroll beyond the last line");
934 case REQ_SCROLL_PAGE_UP
:
935 lines
= view
->height
;
936 case REQ_SCROLL_LINE_UP
:
937 if (lines
> view
->offset
)
938 lines
= view
->offset
;
941 report("Cannot scroll beyond the first line");
949 die("request %d not handled in switch", request
);
952 do_scroll_view(view
, lines
);
957 move_view(struct view
*view
, enum request request
)
962 case REQ_MOVE_FIRST_LINE
:
963 steps
= -view
->lineno
;
966 case REQ_MOVE_LAST_LINE
:
967 steps
= view
->lines
- view
->lineno
- 1;
970 case REQ_MOVE_PAGE_UP
:
971 steps
= view
->height
> view
->lineno
972 ?
-view
->lineno
: -view
->height
;
975 case REQ_MOVE_PAGE_DOWN
:
976 steps
= view
->lineno
+ view
->height
>= view
->lines
977 ? view
->lines
- view
->lineno
- 1 : view
->height
;
981 case REQ_MOVE_UP_ENTER
:
986 case REQ_MOVE_DOWN_ENTER
:
991 die("request %d not handled in switch", request
);
994 if (steps
<= 0 && view
->lineno
== 0) {
995 report("Cannot move beyond the first line");
998 } else if (steps
>= 0 && view
->lineno
+ 1 >= view
->lines
) {
999 report("Cannot move beyond the last line");
1003 /* Move the current line */
1004 view
->lineno
+= steps
;
1005 assert(0 <= view
->lineno
&& view
->lineno
< view
->lines
);
1007 /* Repaint the old "current" line if we be scrolling */
1008 if (ABS(steps
) < view
->height
) {
1009 int prev_lineno
= view
->lineno
- steps
- view
->offset
;
1011 wmove(view
->win
, prev_lineno
, 0);
1012 wclrtoeol(view
->win
);
1013 view
->ops
->draw(view
, prev_lineno
);
1016 /* Check whether the view needs to be scrolled */
1017 if (view
->lineno
< view
->offset
||
1018 view
->lineno
>= view
->offset
+ view
->height
) {
1019 if (steps
< 0 && -steps
> view
->offset
) {
1020 steps
= -view
->offset
;
1022 } else if (steps
> 0) {
1023 if (view
->lineno
== view
->lines
- 1 &&
1024 view
->lines
> view
->height
) {
1025 steps
= view
->lines
- view
->offset
- 1;
1026 if (steps
>= view
->height
)
1027 steps
-= view
->height
- 1;
1031 do_scroll_view(view
, steps
);
1035 /* Draw the current line */
1036 view
->ops
->draw(view
, view
->lineno
- view
->offset
);
1038 redrawwin(view
->win
);
1039 wrefresh(view
->win
);
1045 * Incremental updating
1049 begin_update(struct view
*view
)
1051 const char *id
= view
->id
;
1054 string_copy(view
->cmd
, opt_cmd
);
1056 /* When running random commands, the view ref could have become
1057 * invalid so clear it. */
1060 const char *format
= view
->cmd_env ? view
->cmd_env
: view
->cmd_fmt
;
1062 if (snprintf(view
->cmd
, sizeof(view
->cmd
), format
,
1063 id
, id
, id
, id
, id
) >= sizeof(view
->cmd
))
1067 /* Special case for the pager view. */
1069 view
->pipe
= opt_pipe
;
1072 view
->pipe
= popen(view
->cmd
, "r");
1078 set_nonblocking_input(TRUE
);
1083 string_copy(view
->vid
, id
);
1088 for (i
= 0; i
< view
->lines
; i
++)
1090 free(view
->line
[i
]);
1096 view
->start_time
= time(NULL
);
1102 end_update(struct view
*view
)
1106 set_nonblocking_input(FALSE
);
1107 if (view
->pipe
== stdin
)
1115 update_view(struct view
*view
)
1117 char buffer
[BUFSIZ
];
1120 /* The number of lines to read. If too low it will cause too much
1121 * redrawing (and possible flickering), if too high responsiveness
1123 unsigned long lines
= view
->height
;
1124 int redraw_from
= -1;
1129 /* Only redraw if lines are visible. */
1130 if (view
->offset
+ view
->height
>= view
->lines
)
1131 redraw_from
= view
->lines
- view
->offset
;
1133 tmp
= realloc(view
->line
, sizeof(*view
->line
) * (view
->lines
+ lines
));
1139 while ((line
= fgets(buffer
, sizeof(buffer
), view
->pipe
))) {
1140 int linelen
= strlen(line
);
1143 line
[linelen
- 1] = 0;
1145 if (!view
->ops
->read(view
, line
))
1155 lines
= view
->lines
;
1156 for (digits
= 0; lines
; digits
++)
1159 /* Keep the displayed view in sync with line number scaling. */
1160 if (digits
!= view
->digits
) {
1161 view
->digits
= digits
;
1166 if (redraw_from
>= 0) {
1167 /* If this is an incremental update, redraw the previous line
1168 * since for commits some members could have changed when
1169 * loading the main view. */
1170 if (redraw_from
> 0)
1173 /* Incrementally draw avoids flickering. */
1174 redraw_view_from(view
, redraw_from
);
1177 /* Update the title _after_ the redraw so that if the redraw picks up a
1178 * commit reference in view->ref it'll be available here. */
1179 update_view_title(view
);
1181 if (ferror(view
->pipe
)) {
1182 report("Failed to read: %s", strerror(errno
));
1185 } else if (feof(view
->pipe
)) {
1186 time_t secs
= time(NULL
) - view
->start_time
;
1188 if (view
== VIEW(REQ_VIEW_HELP
)) {
1189 const char *msg
= TIG_HELP
;
1191 if (view
->lines
== 0) {
1192 /* Slightly ugly, but abusing view->ref keeps
1193 * the error message. */
1194 string_copy(view
->ref
, "No help available");
1195 msg
= "The tig(1) manpage is not installed";
1202 report("Loaded %d lines in %ld second%s", view
->lines
, secs
,
1203 secs
== 1 ?
"" : "s");
1210 report("Allocation failure");
1218 OPEN_DEFAULT
= 0, /* Use default view switching. */
1219 OPEN_SPLIT
= 1, /* Split current view. */
1220 OPEN_BACKGROUNDED
= 2, /* Backgrounded. */
1221 OPEN_RELOAD
= 4, /* Reload view even if it is the current. */
1225 open_view(struct view
*prev
, enum request request
, enum open_flags flags
)
1227 bool backgrounded
= !!(flags
& OPEN_BACKGROUNDED
);
1228 bool split
= !!(flags
& OPEN_SPLIT
);
1229 bool reload
= !!(flags
& OPEN_RELOAD
);
1230 struct view
*view
= VIEW(request
);
1231 struct view
*displayed
;
1234 /* Cycle between displayed views and count the views. */
1235 foreach_view (displayed
, nviews
) {
1237 view
== displayed
&&
1238 !strcmp(view
->vid
, prev
->vid
)) {
1239 current_view
= nviews
;
1240 /* Blur out the title of the previous view. */
1241 update_view_title(prev
);
1247 if (view
== prev
&& nviews
== 1 && !reload
) {
1248 report("Already in %s view", view
->name
);
1252 if ((reload
|| strcmp(view
->vid
, view
->id
)) &&
1253 !begin_update(view
)) {
1254 report("Failed to load %s view", view
->name
);
1259 display
[current_view
+ 1] = view
;
1263 /* Maximize the current view. */
1264 memset(display
, 0, sizeof(display
));
1266 display
[current_view
] = view
;
1271 if (split
&& prev
->lineno
- prev
->offset
>= prev
->height
) {
1272 /* Take the title line into account. */
1273 int lines
= prev
->lineno
- prev
->offset
- prev
->height
+ 1;
1275 /* Scroll the view that was split if the current line is
1276 * outside the new limited view. */
1277 do_scroll_view(prev
, lines
);
1280 if (prev
&& view
!= prev
) {
1281 /* "Blur" the previous view. */
1283 update_view_title(prev
);
1285 /* Continue loading split views in the background. */
1291 /* Clear the old view and let the incremental updating refill
1294 report("Loading...");
1297 if (view
== VIEW(REQ_VIEW_HELP
))
1298 report("%s", TIG_HELP
);
1303 /* If the view is backgrounded the above calls to report()
1304 * won't redraw the view title. */
1306 update_view_title(view
);
1311 * User request switch noodle
1315 view_driver(struct view
*view
, enum request request
)
1322 case REQ_MOVE_PAGE_UP
:
1323 case REQ_MOVE_PAGE_DOWN
:
1324 case REQ_MOVE_FIRST_LINE
:
1325 case REQ_MOVE_LAST_LINE
:
1326 move_view(view
, request
);
1329 case REQ_SCROLL_LINE_DOWN
:
1330 case REQ_SCROLL_LINE_UP
:
1331 case REQ_SCROLL_PAGE_DOWN
:
1332 case REQ_SCROLL_PAGE_UP
:
1333 scroll_view(view
, request
);
1340 case REQ_VIEW_PAGER
:
1341 open_view(view
, request
, OPEN_DEFAULT
);
1344 case REQ_MOVE_UP_ENTER
:
1345 case REQ_MOVE_DOWN_ENTER
:
1346 move_view(view
, request
);
1351 report("Nothing to enter");
1354 return view
->ops
->enter(view
);
1358 int nviews
= display
[1] ?
2 : 1;
1359 int next_view
= (current_view
+ 1) % nviews
;
1361 if (next_view
== current_view
) {
1362 report("Only one view is displayed");
1366 current_view
= next_view
;
1367 /* Blur out the title of the previous view. */
1368 update_view_title(view
);
1372 case REQ_TOGGLE_LINE_NUMBERS
:
1373 opt_line_number
= !opt_line_number
;
1378 /* Always reload^Wrerun commands from the prompt. */
1379 open_view(view
, opt_request
, OPEN_RELOAD
);
1382 case REQ_STOP_LOADING
:
1383 foreach_view (view
, i
) {
1385 report("Stopped loaded the %s view", view
->name
),
1390 case REQ_SHOW_VERSION
:
1391 report("Version: %s", VERSION
);
1394 case REQ_SCREEN_RESIZE
:
1397 case REQ_SCREEN_REDRAW
:
1401 case REQ_SCREEN_UPDATE
:
1405 case REQ_VIEW_CLOSE
:
1407 view
= display
[(current_view
+ 1) % ARRAY_SIZE(display
)];
1408 memset(display
, 0, sizeof(display
));
1410 display
[current_view
] = view
;
1420 /* An unknown key will show most commonly used commands. */
1421 report("Unknown key, press 'h' for help");
1430 * View backend handlers
1434 pager_draw(struct view
*view
, unsigned int lineno
)
1436 enum line_type type
;
1441 if (view
->offset
+ lineno
>= view
->lines
)
1444 line
= view
->line
[view
->offset
+ lineno
];
1445 type
= get_line_type(line
);
1447 wmove(view
->win
, lineno
, 0);
1449 if (view
->offset
+ lineno
== view
->lineno
) {
1450 if (type
== LINE_COMMIT
) {
1451 string_copy(view
->ref
, line
+ 7);
1452 string_copy(ref_commit
, view
->ref
);
1456 wchgat(view
->win
, -1, 0, type
, NULL
);
1459 attr
= get_line_attr(type
);
1460 wattrset(view
->win
, attr
);
1462 linelen
= strlen(line
);
1464 if (opt_line_number
|| opt_tab_size
< TABSIZE
) {
1465 static char spaces
[] = " ";
1466 int col_offset
= 0, col
= 0;
1468 if (opt_line_number
) {
1469 unsigned long real_lineno
= view
->offset
+ lineno
+ 1;
1471 if (real_lineno
== 1 ||
1472 (real_lineno
% opt_num_interval
) == 0) {
1473 wprintw(view
->win
, "%.*d", view
->digits
, real_lineno
);
1476 waddnstr(view
->win
, spaces
,
1477 MIN(view
->digits
, STRING_SIZE(spaces
)));
1479 waddstr(view
->win
, ": ");
1480 col_offset
= view
->digits
+ 2;
1483 while (line
&& col_offset
+ col
< view
->width
) {
1484 int cols_max
= view
->width
- col_offset
- col
;
1488 if (*line
== '\t') {
1489 assert(sizeof(spaces
) > TABSIZE
);
1492 cols
= opt_tab_size
- (col
% opt_tab_size
);
1495 line
= strchr(line
, '\t');
1496 cols
= line ? line
- text
: strlen(text
);
1499 waddnstr(view
->win
, text
, MIN(cols
, cols_max
));
1504 int col
= 0, pos
= 0;
1506 for (; pos
< linelen
&& col
< view
->width
; pos
++, col
++)
1507 if (line
[pos
] == '\t')
1508 col
+= TABSIZE
- (col
% TABSIZE
) - 1;
1510 waddnstr(view
->win
, line
, pos
);
1517 pager_read(struct view
*view
, char *line
)
1519 /* Compress empty lines in the help view. */
1520 if (view
== VIEW(REQ_VIEW_HELP
) &&
1523 !*((char *) view
->line
[view
->lines
- 1]))
1526 view
->line
[view
->lines
] = strdup(line
);
1527 if (!view
->line
[view
->lines
])
1535 pager_enter(struct view
*view
)
1537 char *line
= view
->line
[view
->lineno
];
1539 if (view
== VIEW(REQ_VIEW_DIFF
)) {
1540 scroll_view(view
, REQ_SCROLL_LINE_DOWN
);
1544 if (get_line_type(line
) == LINE_COMMIT
) {
1545 if (view
== VIEW(REQ_VIEW_LOG
))
1546 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
| OPEN_BACKGROUNDED
);
1548 open_view(view
, REQ_VIEW_DIFF
, OPEN_DEFAULT
);
1554 static struct view_ops pager_ops
= {
1562 static struct ref
**get_refs(char *id
);
1565 main_draw(struct view
*view
, unsigned int lineno
)
1567 char buf
[DATE_COLS
+ 1];
1568 struct commit
*commit
;
1569 enum line_type type
;
1575 if (view
->offset
+ lineno
>= view
->lines
)
1578 commit
= view
->line
[view
->offset
+ lineno
];
1579 if (!*commit
->author
)
1582 wmove(view
->win
, lineno
, col
);
1584 if (view
->offset
+ lineno
== view
->lineno
) {
1585 string_copy(view
->ref
, commit
->id
);
1586 string_copy(ref_commit
, view
->ref
);
1588 wattrset(view
->win
, get_line_attr(type
));
1589 wchgat(view
->win
, -1, 0, type
, NULL
);
1592 type
= LINE_MAIN_COMMIT
;
1593 wattrset(view
->win
, get_line_attr(LINE_MAIN_DATE
));
1596 timelen
= strftime(buf
, sizeof(buf
), DATE_FORMAT
, &commit
->time
);
1597 waddnstr(view
->win
, buf
, timelen
);
1598 waddstr(view
->win
, " ");
1601 wmove(view
->win
, lineno
, col
);
1602 if (type
!= LINE_CURSOR
)
1603 wattrset(view
->win
, get_line_attr(LINE_MAIN_AUTHOR
));
1605 /* FIXME: Make this optional, and add i18n.commitEncoding support. */
1606 authorlen
= utf8_length(commit
->author
, AUTHOR_COLS
- 2, &col
, &trimmed
);
1609 waddnstr(view
->win
, commit
->author
, authorlen
);
1610 if (type
!= LINE_CURSOR
)
1611 wattrset(view
->win
, get_line_attr(LINE_MAIN_DELIM
));
1612 waddch(view
->win
, '~');
1614 waddstr(view
->win
, commit
->author
);
1618 if (type
!= LINE_CURSOR
)
1619 wattrset(view
->win
, A_NORMAL
);
1621 mvwaddch(view
->win
, lineno
, col
, ACS_LTEE
);
1622 wmove(view
->win
, lineno
, col
+ 2);
1629 if (type
== LINE_CURSOR
)
1631 else if (commit
->refs
[i
]->tag
)
1632 wattrset(view
->win
, get_line_attr(LINE_MAIN_TAG
));
1634 wattrset(view
->win
, get_line_attr(LINE_MAIN_REF
));
1635 waddstr(view
->win
, "[");
1636 waddstr(view
->win
, commit
->refs
[i
]->name
);
1637 waddstr(view
->win
, "]");
1638 if (type
!= LINE_CURSOR
)
1639 wattrset(view
->win
, A_NORMAL
);
1640 waddstr(view
->win
, " ");
1641 col
+= strlen(commit
->refs
[i
]->name
) + STRING_SIZE("[] ");
1642 } while (commit
->refs
[i
++]->next
);
1645 if (type
!= LINE_CURSOR
)
1646 wattrset(view
->win
, get_line_attr(type
));
1649 int titlelen
= strlen(commit
->title
);
1651 if (col
+ titlelen
> view
->width
)
1652 titlelen
= view
->width
- col
;
1654 waddnstr(view
->win
, commit
->title
, titlelen
);
1660 /* Reads git log --pretty=raw output and parses it into the commit struct. */
1662 main_read(struct view
*view
, char *line
)
1664 enum line_type type
= get_line_type(line
);
1665 struct commit
*commit
;
1669 commit
= calloc(1, sizeof(struct commit
));
1673 line
+= STRING_SIZE("commit ");
1675 view
->line
[view
->lines
++] = commit
;
1676 string_copy(commit
->id
, line
);
1677 commit
->refs
= get_refs(commit
->id
);
1682 char *ident
= line
+ STRING_SIZE("author ");
1683 char *end
= strchr(ident
, '<');
1686 for (; end
> ident
&& isspace(end
[-1]); end
--) ;
1690 commit
= view
->line
[view
->lines
- 1];
1691 string_copy(commit
->author
, ident
);
1693 /* Parse epoch and timezone */
1695 char *secs
= strchr(end
+ 1, '>');
1699 if (!secs
|| secs
[1] != ' ')
1703 time
= (time_t) atol(secs
);
1704 zone
= strchr(secs
, ' ');
1705 if (zone
&& strlen(zone
) == STRING_SIZE(" +0700")) {
1709 tz
= ('0' - zone
[1]) * 60 * 60 * 10;
1710 tz
+= ('0' - zone
[2]) * 60 * 60;
1711 tz
+= ('0' - zone
[3]) * 60;
1712 tz
+= ('0' - zone
[4]) * 60;
1719 gmtime_r(&time
, &commit
->time
);
1724 /* We should only ever end up here if there has already been a
1725 * commit line, however, be safe. */
1726 if (view
->lines
== 0)
1729 /* Fill in the commit title if it has not already been set. */
1730 commit
= view
->line
[view
->lines
- 1];
1731 if (commit
->title
[0])
1734 /* Require titles to start with a non-space character at the
1735 * offset used by git log. */
1736 /* FIXME: More gracefull handling of titles; append "..." to
1737 * shortened titles, etc. */
1738 if (strncmp(line
, " ", 4) ||
1742 string_copy(commit
->title
, line
+ 4);
1749 main_enter(struct view
*view
)
1751 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
);
1755 static struct view_ops main_ops
= {
1766 * Below the default key bindings are shown.
1774 static struct keymap keymap
[] = {
1779 * Switch to main view.
1781 * Switch to diff view.
1783 * Switch to log view.
1785 * Switch to pager view.
1789 * Close view if multiple views are open, else quit.
1791 * This key is "context sensitive" depending on what view you are
1792 * currently in. When in log view on a commit line or in the main
1793 * view, split the view and show the commit diff. In the diff view
1794 * pressing Enter will simply scroll the view one line down.
1796 * Switch to next view.
1798 { 'm', REQ_VIEW_MAIN
},
1799 { 'd', REQ_VIEW_DIFF
},
1800 { 'l', REQ_VIEW_LOG
},
1801 { 'p', REQ_VIEW_PAGER
},
1802 { 'h', REQ_VIEW_HELP
},
1804 { 'q', REQ_VIEW_CLOSE
},
1805 { KEY_TAB
, REQ_VIEW_NEXT
},
1806 { KEY_RETURN
, REQ_ENTER
},
1812 * Move cursor one line up.
1814 * Move cursor one line down.
1816 * Move cursor one line up and enter. When used in the main view
1817 * this will always show the diff of the current commit in the
1820 * Move cursor one line down and enter.
1823 * Move cursor one page up.
1826 * Move cursor one page down.
1828 * Jump to first line.
1830 * Jump to last line.
1832 { KEY_UP
, REQ_MOVE_UP
},
1833 { KEY_DOWN
, REQ_MOVE_DOWN
},
1834 { 'k', REQ_MOVE_UP_ENTER
},
1835 { 'j', REQ_MOVE_DOWN_ENTER
},
1836 { KEY_HOME
, REQ_MOVE_FIRST_LINE
},
1837 { KEY_END
, REQ_MOVE_LAST_LINE
},
1838 { KEY_NPAGE
, REQ_MOVE_PAGE_DOWN
},
1839 { ' ', REQ_MOVE_PAGE_DOWN
},
1840 { KEY_PPAGE
, REQ_MOVE_PAGE_UP
},
1841 { 'b', REQ_MOVE_PAGE_UP
},
1847 * Scroll view one line up.
1849 * Scroll view one line down.
1851 * Scroll view one page up.
1853 * Scroll view one page down.
1855 { KEY_IC
, REQ_SCROLL_LINE_UP
},
1856 { KEY_DC
, REQ_SCROLL_LINE_DOWN
},
1857 { 'w', REQ_SCROLL_PAGE_UP
},
1858 { 's', REQ_SCROLL_PAGE_DOWN
},
1868 * Stop all background loading. This can be useful if you use
1869 * tig(1) in a repository with a long history without limiting
1874 * Toggle line numbers on/off.
1876 * Open prompt. This allows you to specify what git command
1882 { 'z', REQ_STOP_LOADING
},
1883 { 'v', REQ_SHOW_VERSION
},
1884 { 'r', REQ_SCREEN_REDRAW
},
1885 { 'n', REQ_TOGGLE_LINE_NUMBERS
},
1886 { ':', REQ_PROMPT
},
1888 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
1889 { ERR
, REQ_SCREEN_UPDATE
},
1891 /* Use the ncurses SIGWINCH handler. */
1892 { KEY_RESIZE
, REQ_SCREEN_RESIZE
},
1896 get_request(int key
)
1900 for (i
= 0; i
< ARRAY_SIZE(keymap
); i
++)
1901 if (keymap
[i
].alias
== key
)
1902 return keymap
[i
].request
;
1904 return (enum request
) key
;
1909 * Unicode / UTF-8 handling
1911 * NOTE: Much of the following code for dealing with unicode is derived from
1912 * ELinks' UTF-8 code developed by Scrool <scroolik@gmail.com>. Origin file is
1913 * src/intl/charset.c from the utf8 branch commit elinks-0.11.0-g31f2c28.
1916 /* I've (over)annotated a lot of code snippets because I am not entirely
1917 * confident that the approach taken by this small UTF-8 interface is correct.
1921 unicode_width(unsigned long c
)
1924 (c
<= 0x115f /* Hangul Jamo */
1927 || (c
>= 0x2e80 && c
<= 0xa4cf && c
!= 0x303f)
1929 || (c
>= 0xac00 && c
<= 0xd7a3) /* Hangul Syllables */
1930 || (c
>= 0xf900 && c
<= 0xfaff) /* CJK Compatibility Ideographs */
1931 || (c
>= 0xfe30 && c
<= 0xfe6f) /* CJK Compatibility Forms */
1932 || (c
>= 0xff00 && c
<= 0xff60) /* Fullwidth Forms */
1933 || (c
>= 0xffe0 && c
<= 0xffe6)
1934 || (c
>= 0x20000 && c
<= 0x2fffd)
1935 || (c
>= 0x30000 && c
<= 0x3fffd)))
1941 /* Number of bytes used for encoding a UTF-8 character indexed by first byte.
1942 * Illegal bytes are set one. */
1943 static const unsigned char utf8_bytes
[256] = {
1944 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1945 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1946 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1947 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1948 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1949 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1950 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,
1951 3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4, 5,5,5,5,6,6,1,1,
1954 /* Decode UTF-8 multi-byte representation into a unicode character. */
1955 static inline unsigned long
1956 utf8_to_unicode(const char *string
, size_t length
)
1958 unsigned long unicode
;
1962 unicode
= string
[0];
1965 unicode
= (string
[0] & 0x1f) << 6;
1966 unicode
+= (string
[1] & 0x3f);
1969 unicode
= (string
[0] & 0x0f) << 12;
1970 unicode
+= ((string
[1] & 0x3f) << 6);
1971 unicode
+= (string
[2] & 0x3f);
1974 unicode
= (string
[0] & 0x0f) << 18;
1975 unicode
+= ((string
[1] & 0x3f) << 12);
1976 unicode
+= ((string
[2] & 0x3f) << 6);
1977 unicode
+= (string
[3] & 0x3f);
1980 unicode
= (string
[0] & 0x0f) << 24;
1981 unicode
+= ((string
[1] & 0x3f) << 18);
1982 unicode
+= ((string
[2] & 0x3f) << 12);
1983 unicode
+= ((string
[3] & 0x3f) << 6);
1984 unicode
+= (string
[4] & 0x3f);
1987 unicode
= (string
[0] & 0x01) << 30;
1988 unicode
+= ((string
[1] & 0x3f) << 24);
1989 unicode
+= ((string
[2] & 0x3f) << 18);
1990 unicode
+= ((string
[3] & 0x3f) << 12);
1991 unicode
+= ((string
[4] & 0x3f) << 6);
1992 unicode
+= (string
[5] & 0x3f);
1995 die("Invalid unicode length");
1998 /* Invalid characters could return the special 0xfffd value but NUL
1999 * should be just as good. */
2000 return unicode
> 0xffff ?
0 : unicode
;
2003 /* Calculates how much of string can be shown within the given maximum width
2004 * and sets trimmed parameter to non-zero value if all of string could not be
2007 * Additionally, adds to coloffset how many many columns to move to align with
2008 * the expected position. Takes into account how multi-byte and double-width
2009 * characters will effect the cursor position.
2011 * Returns the number of bytes to output from string to satisfy max_width. */
2013 utf8_length(const char *string
, size_t max_width
, int *coloffset
, int *trimmed
)
2015 const char *start
= string
;
2016 const char *end
= strchr(string
, '\0');
2022 while (string
< end
) {
2023 int c
= *(unsigned char *) string
;
2024 unsigned char bytes
= utf8_bytes
[c
];
2026 unsigned long unicode
;
2028 if (string
+ bytes
> end
)
2031 /* Change representation to figure out whether
2032 * it is a single- or double-width character. */
2034 unicode
= utf8_to_unicode(string
, bytes
);
2035 /* FIXME: Graceful handling of invalid unicode character. */
2039 ucwidth
= unicode_width(unicode
);
2041 if (width
> max_width
) {
2046 /* The column offset collects the differences between the
2047 * number of bytes encoding a character and the number of
2048 * columns will be used for rendering said character.
2050 * So if some character A is encoded in 2 bytes, but will be
2051 * represented on the screen using only 1 byte this will and up
2052 * adding 1 to the multi-byte column offset.
2054 * Assumes that no double-width character can be encoding in
2055 * less than two bytes. */
2056 if (bytes
> ucwidth
)
2057 mbwidth
+= bytes
- ucwidth
;
2062 *coloffset
+= mbwidth
;
2064 return string
- start
;
2072 /* Whether or not the curses interface has been initialized. */
2073 bool cursed
= FALSE
;
2075 /* The status window is used for polling keystrokes. */
2076 static WINDOW
*status_win
;
2078 /* Update status and title window. */
2080 report(const char *msg
, ...)
2082 static bool empty
= TRUE
;
2083 struct view
*view
= display
[current_view
];
2085 if (!empty
|| *msg
) {
2088 va_start(args
, msg
);
2091 wmove(status_win
, 0, 0);
2093 vwprintw(status_win
, msg
, args
);
2098 wrefresh(status_win
);
2103 update_view_title(view
);
2105 /* Move the cursor to the right-most column of the cursor line.
2107 * XXX: This could turn out to be a bit expensive, but it ensures that
2108 * the cursor does not jump around. */
2110 wmove(view
->win
, view
->lineno
- view
->offset
, view
->width
- 1);
2111 wrefresh(view
->win
);
2115 /* Controls when nodelay should be in effect when polling user input. */
2117 set_nonblocking_input(bool loading
)
2119 static unsigned int loading_views
;
2121 if ((loading
== FALSE
&& loading_views
-- == 1) ||
2122 (loading
== TRUE
&& loading_views
++ == 0))
2123 nodelay(status_win
, loading
);
2131 /* Initialize the curses library */
2132 if (isatty(STDIN_FILENO
)) {
2133 cursed
= !!initscr();
2135 /* Leave stdin and stdout alone when acting as a pager. */
2136 FILE *io
= fopen("/dev/tty", "r+");
2138 cursed
= !!newterm(NULL
, io
, io
);
2142 die("Failed to initialize curses");
2144 nonl(); /* Tell curses not to do NL->CR/NL on output */
2145 cbreak(); /* Take input chars one at a time, no wait for \n */
2146 noecho(); /* Don't echo input */
2147 leaveok(stdscr
, TRUE
);
2152 getmaxyx(stdscr
, y
, x
);
2153 status_win
= newwin(1, 0, y
- 1, 0);
2155 die("Failed to create status window");
2157 /* Enable keyboard mapping */
2158 keypad(status_win
, TRUE
);
2159 wbkgdset(status_win
, get_line_attr(LINE_STATUS
));
2164 * Repository references
2167 static struct ref
*refs
;
2168 static size_t refs_size
;
2170 static struct ref
**
2173 struct ref
**id_refs
= NULL
;
2174 size_t id_refs_size
= 0;
2177 for (i
= 0; i
< refs_size
; i
++) {
2180 if (strcmp(id
, refs
[i
].id
))
2183 tmp
= realloc(id_refs
, (id_refs_size
+ 1) * sizeof(*id_refs
));
2191 if (id_refs_size
> 0)
2192 id_refs
[id_refs_size
- 1]->next
= 1;
2193 id_refs
[id_refs_size
] = &refs
[i
];
2195 /* XXX: The properties of the commit chains ensures that we can
2196 * safely modify the shared ref. The repo references will
2197 * always be similar for the same id. */
2198 id_refs
[id_refs_size
]->next
= 0;
2208 const char *cmd_env
= getenv("TIG_LS_REMOTE");
2209 const char *cmd
= cmd_env
&& *cmd_env ? cmd_env
: TIG_LS_REMOTE
;
2210 FILE *pipe
= popen(cmd
, "r");
2211 char buffer
[BUFSIZ
];
2217 while ((line
= fgets(buffer
, sizeof(buffer
), pipe
))) {
2218 char *name
= strchr(line
, '\t');
2222 bool tag_commit
= FALSE
;
2228 namelen
= strlen(name
) - 1;
2230 /* Commits referenced by tags has "^{}" appended. */
2231 if (name
[namelen
- 1] == '}') {
2232 while (namelen
> 0 && name
[namelen
] != '^')
2239 if (!strncmp(name
, "refs/tags/", STRING_SIZE("refs/tags/"))) {
2242 name
+= STRING_SIZE("refs/tags/");
2245 } else if (!strncmp(name
, "refs/heads/", STRING_SIZE("refs/heads/"))) {
2246 name
+= STRING_SIZE("refs/heads/");
2248 } else if (!strcmp(name
, "HEAD")) {
2252 refs
= realloc(refs
, sizeof(*refs
) * (refs_size
+ 1));
2256 ref
= &refs
[refs_size
++];
2258 ref
->name
= strdup(name
);
2262 string_copy(ref
->id
, line
);
2271 die("Not a git repository");
2281 #define __NORETURN __attribute__((__noreturn__))
2286 static void __NORETURN
2289 /* XXX: Restore tty modes and let the OS cleanup the rest! */
2295 static void __NORETURN
2296 die(const char *err
, ...)
2302 va_start(args
, err
);
2303 fputs("tig: ", stderr
);
2304 vfprintf(stderr
, err
, args
);
2305 fputs("\n", stderr
);
2312 main(int argc
, char *argv
[])
2315 enum request request
;
2318 signal(SIGINT
, quit
);
2320 if (!parse_options(argc
, argv
))
2323 if (load_refs() == ERR
)
2324 die("Failed to load refs.");
2326 for (i
= 0; i
< ARRAY_SIZE(views
) && (view
= &views
[i
]); i
++)
2327 view
->cmd_env
= getenv(view
->cmd_env
);
2329 request
= opt_request
;
2333 while (view_driver(display
[current_view
], request
)) {
2337 foreach_view (view
, i
)
2340 /* Refresh, accept single keystroke of input */
2341 key
= wgetch(status_win
);
2342 request
= get_request(key
);
2344 /* Some low-level request handling. This keeps access to
2345 * status_win restricted. */
2349 /* Temporarily switch to line-oriented and echoed
2354 if (wgetnstr(status_win
, opt_cmd
+ 4, sizeof(opt_cmd
) - 4) == OK
) {
2355 memcpy(opt_cmd
, "git ", 4);
2356 opt_request
= REQ_VIEW_PAGER
;
2365 case REQ_SCREEN_RESIZE
:
2369 getmaxyx(stdscr
, height
, width
);
2371 /* Resize the status view and let the view driver take
2372 * care of resizing the displayed views. */
2373 wresize(status_win
, 1, width
);
2374 mvwin(status_win
, height
- 1, 0);
2375 wrefresh(status_win
);
2390 * Revision specification
2391 * ----------------------
2392 * This section describes various ways to specify what revisions to display
2393 * or otherwise limit the view to. tig(1) does not itself parse the described
2394 * revision options so refer to the relevant git man pages for futher
2395 * information. Relevant man pages besides git-log(1) are git-diff(1) and
2398 * You can tune the interaction with git by making use of the options
2399 * explained in this section. For example, by configuring the environment
2400 * variables described in the <<view-commands, "View commands">> section.
2402 * Limit by path name
2403 * ~~~~~~~~~~~~~~~~~~
2404 * If you are interested only in those revisions that made changes to a
2405 * specific file (or even several files) list the files like this:
2407 * $ tig log Makefile README
2409 * To avoid ambiguity with repository references such as tag name, be sure
2410 * to separate file names from other git options using "\--". So if you
2411 * have a file named 'master' it will clash with the reference named
2412 * 'master', and thus you will have to use:
2414 * $ tig log -- master
2416 * NOTE: For the main view, avoiding ambiguity will in some cases require
2417 * you to specify two "\--" options. The first will make tig(1) stop
2418 * option processing and the latter will be passed to git log.
2420 * Limit by date or number
2421 * ~~~~~~~~~~~~~~~~~~~~~~~
2422 * To speed up interaction with git, you can limit the amount of commits
2423 * to show both for the log and main view. Either limit by date using
2424 * e.g. `--since=1.month` or limit by the number of commits using `-n400`.
2426 * If you are only interested in changed that happened between two dates
2429 * $ tig -- --after="May 5th" --before="2006-05-16 15:44"
2431 * NOTE: If you want to avoid having to quote dates containing spaces you
2432 * can use "." instead, e.g. `--after=May.5th`.
2434 * Limiting by commit ranges
2435 * ~~~~~~~~~~~~~~~~~~~~~~~~~
2436 * Alternatively, commits can be limited to a specific range, such as
2437 * "all commits between 'tag-1.0' and 'tag-2.0'". For example:
2439 * $ tig log tag-1.0..tag-2.0
2441 * This way of commit limiting makes it trivial to only browse the commits
2442 * which haven't been pushed to a remote branch. Assuming 'origin' is your
2443 * upstream remote branch, using:
2445 * $ tig log origin..HEAD
2447 * will list what will be pushed to the remote branch. Optionally, the ending
2448 * 'HEAD' can be left out since it is implied.
2450 * Limiting by reachability
2451 * ~~~~~~~~~~~~~~~~~~~~~~~~
2452 * Git interprets the range specifier "tag-1.0..tag-2.0" as
2453 * "all commits reachable from 'tag-2.0' but not from 'tag-1.0'".
2454 * Where reachability refers to what commits are ancestors (or part of the
2455 * history) of the branch or tagged revision in question.
2457 * If you prefer to specify which commit to preview in this way use the
2460 * $ tig log tag-2.0 ^tag-1.0
2462 * You can think of '^' as a negation operator. Using this alternate syntax,
2463 * it is possible to further prune commits by specifying multiple branch
2466 * Combining revisions specification
2467 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2468 * Revisions options can to some degree be combined, which makes it possible
2469 * to say "show at most 20 commits from within the last month that changed
2470 * files under the Documentation/ directory."
2472 * $ tig -- --since=1.month -n20 -- Documentation/
2474 * Examining all repository references
2475 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2476 * In some cases, it can be useful to query changes across all references
2477 * in a repository. An example is to ask "did any line of development in
2478 * this repository change a particular file within the last week". This
2479 * can be accomplished using:
2481 * $ tig -- --all --since=1.week -- Makefile
2485 * Known bugs and problems:
2487 * - In it's current state tig is pretty much UTF-8 only.
2489 * - If the screen width is very small the main view can draw
2490 * outside the current view causing bad wrapping. Same goes
2491 * for title and status windows.
2495 * Features that should be explored.
2503 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
2505 * This program is free software; you can redistribute it and/or modify
2506 * it under the terms of the GNU General Public License as published by
2507 * the Free Software Foundation; either version 2 of the License, or
2508 * (at your option) any later version.
2513 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
2514 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
2515 * gitk(1): git repository browser written using tcl/tk,
2516 * qgit(1): git repository browser written using c++/Qt,
2517 * gitview(1): git repository browser written using python/gtk.