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 underlaying 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.1"
55 static void die(const char *err
, ...);
56 static void report(const char *msg
, ...);
57 static void set_nonblocking_input(bool loading
);
59 #define ABS(x) ((x) >= 0 ? (x) : -(x))
60 #define MIN(x, y) ((x) < (y) ? (x) : (y))
62 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
63 #define STRING_SIZE(x) (sizeof(x) - 1)
65 #define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
66 #define SIZEOF_CMD 1024 /* Size of command buffer. */
68 /* This color name can be used to refer to the default term colors. */
69 #define COLOR_DEFAULT (-1)
71 #define TIG_HELP "(d)iff, (l)og, (m)ain, (q)uit, (h)elp, (Enter) show diff"
73 /* The format and size of the date column in the main view. */
74 #define DATE_FORMAT "%Y-%m-%d %H:%M"
75 #define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
77 /* The default interval between line numbers. */
78 #define NUMBER_INTERVAL 1
82 #define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
84 /* Some ascii-shorthands fitted into the ncurses namespace. */
86 #define KEY_RETURN '\r'
90 /* User action requests. */
92 /* Offset all requests to avoid conflicts with ncurses getch values. */
93 REQ_OFFSET
= KEY_MAX
+ 1,
95 /* XXX: Keep the view request first and in sync with views[]. */
110 REQ_TOGGLE_LINE_NUMBERS
,
123 REQ_SCROLL_LINE_DOWN
,
125 REQ_SCROLL_PAGE_DOWN
,
129 char *name
; /* Ref name; tag or head names are shortened. */
130 char id
[41]; /* Commit SHA1 ID */
131 unsigned int tag
:1; /* Is it a tag? */
132 unsigned int next
:1; /* For ref lists: are there more refs? */
136 char id
[41]; /* SHA1 ID. */
137 char title
[75]; /* The first line of the commit message. */
138 char author
[75]; /* The author of the commit. */
139 struct tm time
; /* Date from the author ident. */
140 struct ref
**refs
; /* Repository references; tags & branch heads. */
149 string_ncopy(char *dst
, char *src
, int dstlen
)
151 strncpy(dst
, src
, dstlen
- 1);
156 /* Shorthand for safely copying into a fixed buffer. */
157 #define string_copy(dst, src) \
158 string_ncopy(dst, src, sizeof(dst))
163 * NOTE: The following is a slightly modified copy of the git project's shell
164 * quoting routines found in the quote.c file.
166 * Help to copy the thing properly quoted for the shell safety. any single
167 * quote is replaced with '\'', any exclamation point is replaced with '\!',
168 * and the whole thing is enclosed in a
171 * original sq_quote result
172 * name ==> name ==> 'name'
173 * a b ==> a b ==> 'a b'
174 * a'b ==> a'\''b ==> 'a'\''b'
175 * a!b ==> a'\!'b ==> 'a'\!'b'
179 sq_quote(char buf
[SIZEOF_CMD
], size_t bufsize
, const char *src
)
183 #define BUFPUT(x) ( (bufsize < SIZEOF_CMD) && (buf[bufsize++] = (x)) )
186 while ((c
= *src
++)) {
187 if (c
== '\'' || c
== '!') {
207 /* Option and state variables. */
208 static bool opt_line_number
= FALSE
;
209 static int opt_num_interval
= NUMBER_INTERVAL
;
210 static int opt_tab_size
= TABSIZE
;
211 static enum request opt_request
= REQ_VIEW_MAIN
;
212 static char opt_cmd
[SIZEOF_CMD
] = "";
213 static FILE *opt_pipe
= NULL
;
215 /* Returns the index of log or diff command or -1 to exit. */
217 parse_options(int argc
, char *argv
[])
221 for (i
= 1; i
< argc
; i
++) {
226 * Start up in log view using the internal log command.
228 if (!strcmp(opt
, "-l")) {
229 opt_request
= REQ_VIEW_LOG
;
235 * Start up in diff view using the internal diff command.
237 if (!strcmp(opt
, "-d")) {
238 opt_request
= REQ_VIEW_DIFF
;
243 * -n[INTERVAL], --line-number[=INTERVAL]::
244 * Prefix line numbers in log and diff view.
245 * Optionally, with interval different than each line.
247 if (!strncmp(opt
, "-n", 2) ||
248 !strncmp(opt
, "--line-number", 13)) {
254 } else if (opt
[STRING_SIZE("--line-number")] == '=') {
255 num
= opt
+ STRING_SIZE("--line-number=");
259 opt_num_interval
= atoi(num
);
261 opt_line_number
= TRUE
;
266 * -t[NSPACES], --tab-size[=NSPACES]::
267 * Set the number of spaces tabs should be expanded to.
269 if (!strncmp(opt
, "-t", 2) ||
270 !strncmp(opt
, "--tab-size", 10)) {
276 } else if (opt
[STRING_SIZE("--tab-size")] == '=') {
277 num
= opt
+ STRING_SIZE("--tab-size=");
281 opt_tab_size
= MIN(atoi(num
), TABSIZE
);
287 * Show version and exit.
289 if (!strcmp(opt
, "-v") ||
290 !strcmp(opt
, "--version")) {
291 printf("tig version %s\n", VERSION
);
297 * End of tig(1) options. Useful when specifying commands
298 * for the main view. Example:
300 * $ tig -- --since=1.month
302 if (!strcmp(opt
, "--")) {
309 * Open log view using the given git log options.
312 * Open diff view using the given git diff options.
315 * Open diff view using the given git show options.
317 if (!strcmp(opt
, "log") ||
318 !strcmp(opt
, "diff") ||
319 !strcmp(opt
, "show")) {
320 opt_request
= opt
[0] == 'l'
321 ? REQ_VIEW_LOG
: REQ_VIEW_DIFF
;
327 * $ tig tag-1.0..HEAD
330 if (opt
[0] && opt
[0] != '-')
333 die("unknown command '%s'", opt
);
336 if (!isatty(STDIN_FILENO
)) {
340 * If stdin is a pipe, any log or diff options will be ignored and the
341 * pager view will be opened loading data from stdin. The pager mode
342 * can be used for colorizing output from various git commands.
344 * Example on how to colorize the output of git-show(1):
348 opt_request
= REQ_VIEW_PAGER
;
351 } else if (i
< argc
) {
355 * Git command options
356 * ~~~~~~~~~~~~~~~~~~~
357 * All git command options specified on the command line will
358 * be passed to the given command and all will be shell quoted
359 * before they are passed to the shell.
361 * NOTE: If you specify options for the main view, you should
362 * not use the `--pretty` option as this option will be set
363 * automatically to the format expected by the main view.
365 * Example on how to open the log view and show both author and
366 * committer information:
368 * $ tig log --pretty=fuller
370 * See the "Specifying revisions" section below for an introduction
371 * to revision options supported by the git commands. For
372 * details on specific git command options, refer to the man
373 * page of the command in question.
376 if (opt_request
== REQ_VIEW_MAIN
)
377 /* XXX: This is vulnerable to the user overriding
378 * options required for the main view parser. */
379 string_copy(opt_cmd
, "git log --stat --pretty=raw");
381 string_copy(opt_cmd
, "git");
382 buf_size
= strlen(opt_cmd
);
384 while (buf_size
< sizeof(opt_cmd
) && i
< argc
) {
385 opt_cmd
[buf_size
++] = ' ';
386 buf_size
= sq_quote(opt_cmd
, buf_size
, argv
[i
++]);
389 if (buf_size
>= sizeof(opt_cmd
))
390 die("command too long");
392 opt_cmd
[buf_size
] = 0;
401 * Line-oriented content detection.
405 /* Line type String to match Foreground Background Attributes
406 * --------- --------------- ---------- ---------- ---------- */ \
408 LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
409 LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
410 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
411 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
412 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
413 LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
414 LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
415 LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
416 LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
417 LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
418 LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
419 /* Pretty print commit header */ \
420 LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
421 LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
422 LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
423 LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
424 LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
425 LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
426 /* Raw commit header */ \
427 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
428 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
429 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
430 LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
431 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
433 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
434 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
436 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
437 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
438 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
439 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
440 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
441 LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
442 LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
443 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
444 LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
445 LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
446 LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD),
449 #define LINE(type, line, fg, bg, attr) \
456 char *line
; /* The start of line to match. */
457 int linelen
; /* Size of string to match. */
458 int fg
, bg
, attr
; /* Color and text attributes for the lines. */
461 static struct line_info line_info
[] = {
462 #define LINE(type, line, fg, bg, attr) \
463 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
468 static enum line_type
469 get_line_type(char *line
)
471 int linelen
= strlen(line
);
474 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++)
475 /* Case insensitive search matches Signed-off-by lines better. */
476 if (linelen
>= line_info
[type
].linelen
&&
477 !strncasecmp(line_info
[type
].line
, line
, line_info
[type
].linelen
))
484 get_line_attr(enum line_type type
)
486 assert(type
< ARRAY_SIZE(line_info
));
487 return COLOR_PAIR(type
) | line_info
[type
].attr
;
493 int default_bg
= COLOR_BLACK
;
494 int default_fg
= COLOR_WHITE
;
499 if (use_default_colors() != ERR
) {
504 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++) {
505 struct line_info
*info
= &line_info
[type
];
506 int bg
= info
->bg
== COLOR_DEFAULT ? default_bg
: info
->bg
;
507 int fg
= info
->fg
== COLOR_DEFAULT ? default_fg
: info
->fg
;
509 init_pair(type
, fg
, bg
);
515 * ENVIRONMENT VARIABLES
516 * ---------------------
517 * Several options related to the interface with git can be configured
518 * via environment options.
520 * Repository references
521 * ~~~~~~~~~~~~~~~~~~~~~
522 * Commits that are referenced by tags and branch heads will be marked
523 * by the reference name surrounded by '[' and ']':
525 * 2006-03-26 19:42 Petr Baudis | [cogito-0.17.1] Cogito 0.17.1
527 * If you want to filter out certain directories under `.git/refs/`, say
528 * `tmp` you can do it by setting the following variable:
530 * $ TIG_LS_REMOTE="git ls-remote . | sed /\/tmp\//d" tig
532 * Or set the variable permanently in your environment.
535 * Set command for retrieving all repository references. The command
536 * should output data in the same format as git-ls-remote(1).
539 #define TIG_LS_REMOTE \
545 * It is possible to alter which commands are used for the different views.
546 * If for example you prefer commits in the main view to be sorted by date
547 * and only show 500 commits, use:
549 * $ TIG_MAIN_CMD="git log --date-order -n500 --pretty=raw %s" tig
551 * Or set the variable permanently in your environment.
553 * Notice, how `%s` is used to specify the commit reference. There can
554 * be a maximum of 5 `%s` ref specifications.
557 * The command used for the diff view. By default, git show is used
561 * The command used for the log view. If you prefer to have both
562 * author and committer shown in the log view be sure to pass
563 * `--pretty=fuller` to git log.
566 * The command used for the main view. Note, you must always specify
567 * the option: `--pretty=raw` since the main view parser expects to
571 #define TIG_DIFF_CMD \
572 "git show --patch-with-stat --find-copies-harder -B -C %s"
574 #define TIG_LOG_CMD \
575 "git log --cc --stat -n100 %s"
577 #define TIG_MAIN_CMD \
578 "git log --topo-order --stat --pretty=raw %s"
580 /* We silently ignore that the following are also exported. */
582 #define TIG_HELP_CMD \
583 "man tig 2> /dev/null"
585 #define TIG_PAGER_CMD \
592 * tig(1) presents various 'views' of a repository. Each view is based on output
593 * from an external command, most often 'git log', 'git diff', or 'git show'.
596 * Is the default view, and it shows a one line summary of each commit
597 * in the chosen list of revision. The summary includes commit date,
598 * author, and the first line of the log message. Additionally, any
599 * repository references, such as tags, will be shown.
602 * Presents a more rich view of the revision log showing the whole log
603 * message and the diffstat.
606 * Shows either the diff of the current working tree, that is, what
607 * has changed since the last commit, or the commit diff complete
608 * with log message, diffstat and diff.
611 * Is used for displaying both input from stdin and output from git
612 * commands entered in the internal prompt.
615 * Displays the information from the tig(1) man page. For the help view
616 * to work you need to have the tig(1) man page installed.
620 const char *name
; /* View name */
621 char *cmd_fmt
; /* Default command line format */
622 char *cmd_env
; /* Command line set via environment */
623 char *id
; /* Points to either of ref_{head,commit} */
624 size_t objsize
; /* Size of objects in the line index */
627 /* What type of content being displayed. Used in the
630 /* Draw one line; @lineno must be < view->height. */
631 bool (*draw
)(struct view
*view
, unsigned int lineno
);
632 /* Read one line; updates view->line. */
633 bool (*read
)(struct view
*view
, char *line
);
634 /* Depending on view, change display based on current line. */
635 bool (*enter
)(struct view
*view
);
638 char cmd
[SIZEOF_CMD
]; /* Command buffer */
639 char ref
[SIZEOF_REF
]; /* Hovered commit reference */
640 char vid
[SIZEOF_REF
]; /* View ID. Set to id member when updating. */
642 int height
, width
; /* The width and height of the main window */
643 WINDOW
*win
; /* The main window */
644 WINDOW
*title
; /* The title window living below the main window */
647 unsigned long offset
; /* Offset of the window top */
648 unsigned long lineno
; /* Current line number */
651 unsigned long lines
; /* Total number of lines */
652 void **line
; /* Line index; each line contains user data */
653 unsigned int digits
; /* Number of digits in the lines member. */
660 static struct view_ops pager_ops
;
661 static struct view_ops main_ops
;
663 char ref_head
[SIZEOF_REF
] = "HEAD";
664 char ref_commit
[SIZEOF_REF
] = "HEAD";
666 #define VIEW_STR(name, cmd, env, ref, objsize, ops) \
667 { name, cmd, #env, ref, objsize, ops }
669 #define VIEW_(id, name, ops, ref, objsize) \
670 VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, objsize, ops)
672 static struct view views
[] = {
673 VIEW_(MAIN
, "main", &main_ops
, ref_head
, sizeof(struct commit
)),
674 VIEW_(DIFF
, "diff", &pager_ops
, ref_commit
, sizeof(char)),
675 VIEW_(LOG
, "log", &pager_ops
, ref_head
, sizeof(char)),
676 VIEW_(HELP
, "help", &pager_ops
, ref_head
, sizeof(char)),
677 VIEW_(PAGER
, "pager", &pager_ops
, "static", sizeof(char)),
680 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
682 /* The display array of active views and the index of the current view. */
683 static struct view
*display
[2];
684 static unsigned int current_view
;
686 #define foreach_view(view, i) \
687 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
691 redraw_view_from(struct view
*view
, int lineno
)
693 assert(0 <= lineno
&& lineno
< view
->height
);
695 for (; lineno
< view
->height
; lineno
++) {
696 if (!view
->ops
->draw(view
, lineno
))
700 redrawwin(view
->win
);
705 redraw_view(struct view
*view
)
708 redraw_view_from(view
, 0);
715 struct view
*base
= display
[0];
716 struct view
*view
= display
[1] ? display
[1] : display
[0];
718 /* Setup window dimensions */
720 getmaxyx(stdscr
, base
->height
, base
->width
);
722 /* Make room for the status window. */
726 /* Horizontal split. */
727 view
->width
= base
->width
;
728 view
->height
= SCALE_SPLIT_VIEW(base
->height
);
729 base
->height
-= view
->height
;
731 /* Make room for the title bar. */
735 /* Make room for the title bar. */
740 foreach_view (view
, i
) {
741 /* Keep the size of the all view windows one lager than is
742 * required. This makes current line management easier when the
743 * cursor will go outside the window. */
745 view
->win
= newwin(view
->height
+ 1, 0, offset
, 0);
747 die("Failed to create %s view", view
->name
);
749 scrollok(view
->win
, TRUE
);
751 view
->title
= newwin(1, 0, offset
+ view
->height
, 0);
753 die("Failed to create title window");
756 wresize(view
->win
, view
->height
+ 1, view
->width
);
757 mvwin(view
->win
, offset
, 0);
758 mvwin(view
->title
, offset
+ view
->height
, 0);
762 offset
+= view
->height
+ 1;
767 update_view_title(struct view
*view
)
769 if (view
== display
[current_view
])
770 wbkgdset(view
->title
, get_line_attr(LINE_TITLE_FOCUS
));
772 wbkgdset(view
->title
, get_line_attr(LINE_TITLE_BLUR
));
775 wmove(view
->title
, 0, 0);
777 /* [main] ref: 334b506... - commit 6 of 4383 (0%) */
780 wprintw(view
->title
, "[%s] %s", view
->name
, view
->ref
);
782 wprintw(view
->title
, "[%s]", view
->name
);
785 wprintw(view
->title
, " - %s %d of %d (%d%%)",
789 (view
->lineno
+ 1) * 100 / view
->lines
);
792 wrefresh(view
->title
);
799 /* Scrolling backend */
801 do_scroll_view(struct view
*view
, int lines
)
803 /* The rendering expects the new offset. */
804 view
->offset
+= lines
;
806 assert(0 <= view
->offset
&& view
->offset
< view
->lines
);
809 /* Redraw the whole screen if scrolling is pointless. */
810 if (view
->height
< ABS(lines
)) {
814 int line
= lines
> 0 ? view
->height
- lines
: 0;
815 int end
= line
+ ABS(lines
);
817 wscrl(view
->win
, lines
);
819 for (; line
< end
; line
++) {
820 if (!view
->ops
->draw(view
, line
))
825 /* Move current line into the view. */
826 if (view
->lineno
< view
->offset
) {
827 view
->lineno
= view
->offset
;
828 view
->ops
->draw(view
, 0);
830 } else if (view
->lineno
>= view
->offset
+ view
->height
) {
831 if (view
->lineno
== view
->offset
+ view
->height
) {
832 /* Clear the hidden line so it doesn't show if the view
834 wmove(view
->win
, view
->height
, 0);
835 wclrtoeol(view
->win
);
837 view
->lineno
= view
->offset
+ view
->height
- 1;
838 view
->ops
->draw(view
, view
->lineno
- view
->offset
);
841 assert(view
->offset
<= view
->lineno
&& view
->lineno
< view
->lines
);
843 redrawwin(view
->win
);
848 /* Scroll frontend */
850 scroll_view(struct view
*view
, enum request request
)
855 case REQ_SCROLL_PAGE_DOWN
:
856 lines
= view
->height
;
857 case REQ_SCROLL_LINE_DOWN
:
858 if (view
->offset
+ lines
> view
->lines
)
859 lines
= view
->lines
- view
->offset
;
861 if (lines
== 0 || view
->offset
+ view
->height
>= view
->lines
) {
862 report("Cannot scroll beyond the last line");
867 case REQ_SCROLL_PAGE_UP
:
868 lines
= view
->height
;
869 case REQ_SCROLL_LINE_UP
:
870 if (lines
> view
->offset
)
871 lines
= view
->offset
;
874 report("Cannot scroll beyond the first line");
882 die("request %d not handled in switch", request
);
885 do_scroll_view(view
, lines
);
890 move_view(struct view
*view
, enum request request
)
895 case REQ_MOVE_FIRST_LINE
:
896 steps
= -view
->lineno
;
899 case REQ_MOVE_LAST_LINE
:
900 steps
= view
->lines
- view
->lineno
- 1;
903 case REQ_MOVE_PAGE_UP
:
904 steps
= view
->height
> view
->lineno
905 ?
-view
->lineno
: -view
->height
;
908 case REQ_MOVE_PAGE_DOWN
:
909 steps
= view
->lineno
+ view
->height
>= view
->lines
910 ? view
->lines
- view
->lineno
- 1 : view
->height
;
914 case REQ_MOVE_UP_ENTER
:
919 case REQ_MOVE_DOWN_ENTER
:
924 die("request %d not handled in switch", request
);
927 if (steps
<= 0 && view
->lineno
== 0) {
928 report("Cannot move beyond the first line");
931 } else if (steps
>= 0 && view
->lineno
+ 1 >= view
->lines
) {
932 report("Cannot move beyond the last line");
936 /* Move the current line */
937 view
->lineno
+= steps
;
938 assert(0 <= view
->lineno
&& view
->lineno
< view
->lines
);
940 /* Repaint the old "current" line if we be scrolling */
941 if (ABS(steps
) < view
->height
) {
942 int prev_lineno
= view
->lineno
- steps
- view
->offset
;
944 wmove(view
->win
, prev_lineno
, 0);
945 wclrtoeol(view
->win
);
946 view
->ops
->draw(view
, prev_lineno
);
949 /* Check whether the view needs to be scrolled */
950 if (view
->lineno
< view
->offset
||
951 view
->lineno
>= view
->offset
+ view
->height
) {
952 if (steps
< 0 && -steps
> view
->offset
) {
953 steps
= -view
->offset
;
955 } else if (steps
> 0) {
956 if (view
->lineno
== view
->lines
- 1 &&
957 view
->lines
> view
->height
) {
958 steps
= view
->lines
- view
->offset
- 1;
959 if (steps
>= view
->height
)
960 steps
-= view
->height
- 1;
964 do_scroll_view(view
, steps
);
968 /* Draw the current line */
969 view
->ops
->draw(view
, view
->lineno
- view
->offset
);
971 redrawwin(view
->win
);
978 * Incremental updating
982 begin_update(struct view
*view
)
987 string_copy(view
->cmd
, opt_cmd
);
989 /* When running random commands, the view ref could have become
990 * invalid so clear it. */
993 char *format
= view
->cmd_env ? view
->cmd_env
: view
->cmd_fmt
;
995 if (snprintf(view
->cmd
, sizeof(view
->cmd
), format
,
996 id
, id
, id
, id
, id
) >= sizeof(view
->cmd
))
1000 /* Special case for the pager view. */
1002 view
->pipe
= opt_pipe
;
1005 view
->pipe
= popen(view
->cmd
, "r");
1011 set_nonblocking_input(TRUE
);
1016 string_copy(view
->vid
, id
);
1021 for (i
= 0; i
< view
->lines
; i
++)
1023 free(view
->line
[i
]);
1029 view
->start_time
= time(NULL
);
1035 end_update(struct view
*view
)
1039 set_nonblocking_input(FALSE
);
1040 if (view
->pipe
== stdin
)
1048 update_view(struct view
*view
)
1050 char buffer
[BUFSIZ
];
1053 /* The number of lines to read. If too low it will cause too much
1054 * redrawing (and possible flickering), if too high responsiveness
1056 unsigned long lines
= view
->height
;
1057 int redraw_from
= -1;
1062 /* Only redraw if lines are visible. */
1063 if (view
->offset
+ view
->height
>= view
->lines
)
1064 redraw_from
= view
->lines
- view
->offset
;
1066 tmp
= realloc(view
->line
, sizeof(*view
->line
) * (view
->lines
+ lines
));
1072 while ((line
= fgets(buffer
, sizeof(buffer
), view
->pipe
))) {
1073 int linelen
= strlen(line
);
1076 line
[linelen
- 1] = 0;
1078 if (!view
->ops
->read(view
, line
))
1088 lines
= view
->lines
;
1089 for (digits
= 0; lines
; digits
++)
1092 /* Keep the displayed view in sync with line number scaling. */
1093 if (digits
!= view
->digits
) {
1094 view
->digits
= digits
;
1099 if (redraw_from
>= 0) {
1100 /* If this is an incremental update, redraw the previous line
1101 * since for commits some members could have changed when
1102 * loading the main view. */
1103 if (redraw_from
> 0)
1106 /* Incrementally draw avoids flickering. */
1107 redraw_view_from(view
, redraw_from
);
1110 /* Update the title _after_ the redraw so that if the redraw picks up a
1111 * commit reference in view->ref it'll be available here. */
1112 update_view_title(view
);
1114 if (ferror(view
->pipe
)) {
1115 report("Failed to read: %s", strerror(errno
));
1118 } else if (feof(view
->pipe
)) {
1119 time_t secs
= time(NULL
) - view
->start_time
;
1121 if (view
== VIEW(REQ_VIEW_HELP
)) {
1122 char *msg
= TIG_HELP
;
1124 if (view
->lines
== 0) {
1125 /* Slightly ugly, but abusing view->ref keeps
1126 * the error message. */
1127 string_copy(view
->ref
, "No help available");
1128 msg
= "The tig(1) manpage is not installed";
1135 report("Loaded %d lines in %ld second%s", view
->lines
, secs
,
1136 secs
== 1 ?
"" : "s");
1143 report("Allocation failure");
1151 OPEN_DEFAULT
= 0, /* Use default view switching. */
1152 OPEN_SPLIT
= 1, /* Split current view. */
1153 OPEN_BACKGROUNDED
= 2, /* Backgrounded. */
1154 OPEN_RELOAD
= 4, /* Reload view even if it is the current. */
1158 open_view(struct view
*prev
, enum request request
, enum open_flags flags
)
1160 bool backgrounded
= !!(flags
& OPEN_BACKGROUNDED
);
1161 bool split
= !!(flags
& OPEN_SPLIT
);
1162 bool reload
= !!(flags
& OPEN_RELOAD
);
1163 struct view
*view
= VIEW(request
);
1164 struct view
*displayed
;
1167 /* Cycle between displayed views and count the views. */
1168 foreach_view (displayed
, nviews
) {
1170 view
== displayed
&&
1171 !strcmp(view
->vid
, prev
->vid
)) {
1172 current_view
= nviews
;
1173 /* Blur out the title of the previous view. */
1174 update_view_title(prev
);
1180 if (view
== prev
&& nviews
== 1 && !reload
) {
1181 report("Already in %s view", view
->name
);
1185 if ((reload
|| strcmp(view
->vid
, view
->id
)) &&
1186 !begin_update(view
)) {
1187 report("Failed to load %s view", view
->name
);
1192 display
[current_view
+ 1] = view
;
1196 /* Maximize the current view. */
1197 memset(display
, 0, sizeof(display
));
1199 display
[current_view
] = view
;
1204 if (split
&& prev
->lineno
- prev
->offset
>= prev
->height
) {
1205 /* Take the title line into account. */
1206 int lines
= prev
->lineno
- prev
->offset
- prev
->height
+ 1;
1208 /* Scroll the view that was split if the current line is
1209 * outside the new limited view. */
1210 do_scroll_view(prev
, lines
);
1213 if (prev
&& view
!= prev
) {
1214 /* "Blur" the previous view. */
1216 update_view_title(prev
);
1218 /* Continue loading split views in the background. */
1224 /* Clear the old view and let the incremental updating refill
1227 report("Loading...");
1230 if (view
== VIEW(REQ_VIEW_HELP
))
1231 report("%s", TIG_HELP
);
1236 /* If the view is backgrounded the above calls to report()
1237 * won't redraw the view title. */
1239 update_view_title(view
);
1244 * User request switch noodle
1248 view_driver(struct view
*view
, enum request request
)
1255 case REQ_MOVE_PAGE_UP
:
1256 case REQ_MOVE_PAGE_DOWN
:
1257 case REQ_MOVE_FIRST_LINE
:
1258 case REQ_MOVE_LAST_LINE
:
1259 move_view(view
, request
);
1262 case REQ_SCROLL_LINE_DOWN
:
1263 case REQ_SCROLL_LINE_UP
:
1264 case REQ_SCROLL_PAGE_DOWN
:
1265 case REQ_SCROLL_PAGE_UP
:
1266 scroll_view(view
, request
);
1273 case REQ_VIEW_PAGER
:
1274 open_view(view
, request
, OPEN_DEFAULT
);
1277 case REQ_MOVE_UP_ENTER
:
1278 case REQ_MOVE_DOWN_ENTER
:
1279 move_view(view
, request
);
1284 report("Nothing to enter");
1287 return view
->ops
->enter(view
);
1291 int nviews
= display
[1] ?
2 : 1;
1292 int next_view
= (current_view
+ 1) % nviews
;
1294 if (next_view
== current_view
) {
1295 report("Only one view is displayed");
1299 current_view
= next_view
;
1300 /* Blur out the title of the previous view. */
1301 update_view_title(view
);
1305 case REQ_TOGGLE_LINE_NUMBERS
:
1306 opt_line_number
= !opt_line_number
;
1308 update_view_title(view
);
1312 /* Always reload^Wrerun commands from the prompt. */
1313 open_view(view
, opt_request
, OPEN_RELOAD
);
1316 case REQ_STOP_LOADING
:
1317 foreach_view (view
, i
) {
1319 report("Stopped loaded the %s view", view
->name
),
1324 case REQ_SHOW_VERSION
:
1325 report("Version: %s", VERSION
);
1328 case REQ_SCREEN_RESIZE
:
1331 case REQ_SCREEN_REDRAW
:
1332 foreach_view (view
, i
) {
1334 update_view_title(view
);
1338 case REQ_SCREEN_UPDATE
:
1346 /* An unknown key will show most commonly used commands. */
1347 report("Unknown key, press 'h' for help");
1356 * View backend handlers
1360 pager_draw(struct view
*view
, unsigned int lineno
)
1362 enum line_type type
;
1367 if (view
->offset
+ lineno
>= view
->lines
)
1370 line
= view
->line
[view
->offset
+ lineno
];
1371 type
= get_line_type(line
);
1373 wmove(view
->win
, lineno
, 0);
1375 if (view
->offset
+ lineno
== view
->lineno
) {
1376 if (type
== LINE_COMMIT
) {
1377 string_copy(view
->ref
, line
+ 7);
1378 string_copy(ref_commit
, view
->ref
);
1382 wchgat(view
->win
, -1, 0, type
, NULL
);
1385 attr
= get_line_attr(type
);
1386 wattrset(view
->win
, attr
);
1388 linelen
= strlen(line
);
1390 if (opt_line_number
|| opt_tab_size
< TABSIZE
) {
1391 static char spaces
[] = " ";
1392 int col_offset
= 0, col
= 0;
1394 if (opt_line_number
) {
1395 unsigned long real_lineno
= view
->offset
+ lineno
+ 1;
1397 if (real_lineno
== 1 ||
1398 (real_lineno
% opt_num_interval
) == 0) {
1399 wprintw(view
->win
, "%.*d", view
->digits
, real_lineno
);
1402 waddnstr(view
->win
, spaces
,
1403 MIN(view
->digits
, STRING_SIZE(spaces
)));
1405 waddstr(view
->win
, ": ");
1406 col_offset
= view
->digits
+ 2;
1409 while (line
&& col_offset
+ col
< view
->width
) {
1410 int cols_max
= view
->width
- col_offset
- col
;
1414 if (*line
== '\t') {
1415 assert(sizeof(spaces
) > TABSIZE
);
1418 cols
= opt_tab_size
- (col
% opt_tab_size
);
1421 line
= strchr(line
, '\t');
1422 cols
= line ? line
- text
: strlen(text
);
1425 waddnstr(view
->win
, text
, MIN(cols
, cols_max
));
1430 int col
= 0, pos
= 0;
1432 for (; pos
< linelen
&& col
< view
->width
; pos
++, col
++)
1433 if (line
[pos
] == '\t')
1434 col
+= TABSIZE
- (col
% TABSIZE
) - 1;
1436 waddnstr(view
->win
, line
, pos
);
1443 pager_read(struct view
*view
, char *line
)
1445 /* Compress empty lines in the help view. */
1446 if (view
== VIEW(REQ_VIEW_HELP
) &&
1449 !*((char *) view
->line
[view
->lines
- 1]))
1452 view
->line
[view
->lines
] = strdup(line
);
1453 if (!view
->line
[view
->lines
])
1461 pager_enter(struct view
*view
)
1463 char *line
= view
->line
[view
->lineno
];
1465 if (get_line_type(line
) == LINE_COMMIT
) {
1466 if (view
== VIEW(REQ_VIEW_LOG
))
1467 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
| OPEN_BACKGROUNDED
);
1469 open_view(view
, REQ_VIEW_DIFF
, OPEN_DEFAULT
);
1475 static struct view_ops pager_ops
= {
1483 static struct ref
**get_refs(char *id
);
1486 main_draw(struct view
*view
, unsigned int lineno
)
1488 char buf
[DATE_COLS
+ 1];
1489 struct commit
*commit
;
1490 enum line_type type
;
1494 if (view
->offset
+ lineno
>= view
->lines
)
1497 commit
= view
->line
[view
->offset
+ lineno
];
1498 if (!*commit
->author
)
1501 wmove(view
->win
, lineno
, col
);
1503 if (view
->offset
+ lineno
== view
->lineno
) {
1504 string_copy(view
->ref
, commit
->id
);
1505 string_copy(ref_commit
, view
->ref
);
1507 wattrset(view
->win
, get_line_attr(type
));
1508 wchgat(view
->win
, -1, 0, type
, NULL
);
1511 type
= LINE_MAIN_COMMIT
;
1512 wattrset(view
->win
, get_line_attr(LINE_MAIN_DATE
));
1515 timelen
= strftime(buf
, sizeof(buf
), DATE_FORMAT
, &commit
->time
);
1516 waddnstr(view
->win
, buf
, timelen
);
1517 waddstr(view
->win
, " ");
1520 wmove(view
->win
, lineno
, col
);
1521 if (type
!= LINE_CURSOR
)
1522 wattrset(view
->win
, get_line_attr(LINE_MAIN_AUTHOR
));
1524 if (strlen(commit
->author
) > 19) {
1525 waddnstr(view
->win
, commit
->author
, 18);
1526 if (type
!= LINE_CURSOR
)
1527 wattrset(view
->win
, get_line_attr(LINE_MAIN_DELIM
));
1528 waddch(view
->win
, '~');
1530 waddstr(view
->win
, commit
->author
);
1534 if (type
!= LINE_CURSOR
)
1535 wattrset(view
->win
, A_NORMAL
);
1537 mvwaddch(view
->win
, lineno
, col
, ACS_LTEE
);
1538 wmove(view
->win
, lineno
, col
+ 2);
1545 if (type
== LINE_CURSOR
)
1547 else if (commit
->refs
[i
]->tag
)
1548 wattrset(view
->win
, get_line_attr(LINE_MAIN_TAG
));
1550 wattrset(view
->win
, get_line_attr(LINE_MAIN_REF
));
1551 waddstr(view
->win
, "[");
1552 waddstr(view
->win
, commit
->refs
[i
]->name
);
1553 waddstr(view
->win
, "]");
1554 if (type
!= LINE_CURSOR
)
1555 wattrset(view
->win
, A_NORMAL
);
1556 waddstr(view
->win
, " ");
1557 col
+= strlen(commit
->refs
[i
]->name
) + STRING_SIZE("[] ");
1558 } while (commit
->refs
[i
++]->next
);
1561 if (type
!= LINE_CURSOR
)
1562 wattrset(view
->win
, get_line_attr(type
));
1565 int titlelen
= strlen(commit
->title
);
1567 if (col
+ titlelen
> view
->width
)
1568 titlelen
= view
->width
- col
;
1570 waddnstr(view
->win
, commit
->title
, titlelen
);
1576 /* Reads git log --pretty=raw output and parses it into the commit struct. */
1578 main_read(struct view
*view
, char *line
)
1580 enum line_type type
= get_line_type(line
);
1581 struct commit
*commit
;
1585 commit
= calloc(1, sizeof(struct commit
));
1589 line
+= STRING_SIZE("commit ");
1591 view
->line
[view
->lines
++] = commit
;
1592 string_copy(commit
->id
, line
);
1593 commit
->refs
= get_refs(commit
->id
);
1598 char *ident
= line
+ STRING_SIZE("author ");
1599 char *end
= strchr(ident
, '<');
1602 for (; end
> ident
&& isspace(end
[-1]); end
--) ;
1606 commit
= view
->line
[view
->lines
- 1];
1607 string_copy(commit
->author
, ident
);
1609 /* Parse epoch and timezone */
1611 char *secs
= strchr(end
+ 1, '>');
1615 if (!secs
|| secs
[1] != ' ')
1619 time
= (time_t) atol(secs
);
1620 zone
= strchr(secs
, ' ');
1621 if (zone
&& strlen(zone
) == STRING_SIZE(" +0700")) {
1625 tz
= ('0' - zone
[1]) * 60 * 60 * 10;
1626 tz
+= ('0' - zone
[2]) * 60 * 60;
1627 tz
+= ('0' - zone
[3]) * 60;
1628 tz
+= ('0' - zone
[4]) * 60;
1635 gmtime_r(&time
, &commit
->time
);
1640 /* We should only ever end up here if there has already been a
1641 * commit line, however, be safe. */
1642 if (view
->lines
== 0)
1645 /* Fill in the commit title if it has not already been set. */
1646 commit
= view
->line
[view
->lines
- 1];
1647 if (commit
->title
[0])
1650 /* Require titles to start with a non-space character at the
1651 * offset used by git log. */
1652 /* FIXME: More gracefull handling of titles; append "..." to
1653 * shortened titles, etc. */
1654 if (strncmp(line
, " ", 4) ||
1658 string_copy(commit
->title
, line
+ 4);
1665 main_enter(struct view
*view
)
1667 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
| OPEN_BACKGROUNDED
);
1671 static struct view_ops main_ops
= {
1682 * Below the default key bindings are shown.
1690 struct keymap keymap
[] = {
1695 * Switch to main view.
1697 * Switch to diff view.
1699 * Switch to log view.
1701 * Switch to pager view.
1705 * If on a commit line show the commit diff. Addiionally, if in
1706 * main or log view this will split the view. To open the commit
1707 * diff in full size view either use 'd' or press Return twice.
1709 * Switch to next view.
1711 { 'm', REQ_VIEW_MAIN
},
1712 { 'd', REQ_VIEW_DIFF
},
1713 { 'l', REQ_VIEW_LOG
},
1714 { 'p', REQ_VIEW_PAGER
},
1715 { 'h', REQ_VIEW_HELP
},
1717 { KEY_TAB
, REQ_VIEW_NEXT
},
1718 { KEY_RETURN
, REQ_ENTER
},
1724 * Move curser one line up.
1726 * Move cursor one line down.
1728 * Move curser one line up and enter. When used in the main view
1729 * this will always show the diff of the current commit in the
1732 * Move cursor one line down and enter.
1734 * Move curser one page up.
1736 * Move cursor one page down.
1738 * Jump to first line.
1740 * Jump to last line.
1742 { KEY_UP
, REQ_MOVE_UP
},
1743 { KEY_DOWN
, REQ_MOVE_DOWN
},
1744 { 'k', REQ_MOVE_UP_ENTER
},
1745 { 'j', REQ_MOVE_DOWN_ENTER
},
1746 { KEY_HOME
, REQ_MOVE_FIRST_LINE
},
1747 { KEY_END
, REQ_MOVE_LAST_LINE
},
1748 { KEY_NPAGE
, REQ_MOVE_PAGE_DOWN
},
1749 { KEY_PPAGE
, REQ_MOVE_PAGE_UP
},
1755 * Scroll view one line up.
1757 * Scroll view one line down.
1759 * Scroll view one page up.
1761 * Scroll view one page down.
1763 { KEY_IC
, REQ_SCROLL_LINE_UP
},
1764 { KEY_DC
, REQ_SCROLL_LINE_DOWN
},
1765 { 'w', REQ_SCROLL_PAGE_UP
},
1766 { 's', REQ_SCROLL_PAGE_DOWN
},
1776 * Stop all background loading. This can be useful if you use
1777 * tig(1) in a repository with a long history without limiting
1782 * Toggle line numbers on/off.
1784 * Open prompt. This allows you to specify what git command
1790 { 'z', REQ_STOP_LOADING
},
1791 { 'v', REQ_SHOW_VERSION
},
1792 { 'r', REQ_SCREEN_REDRAW
},
1793 { 'n', REQ_TOGGLE_LINE_NUMBERS
},
1794 { ':', REQ_PROMPT
},
1796 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
1797 { ERR
, REQ_SCREEN_UPDATE
},
1799 /* Use the ncurses SIGWINCH handler. */
1800 { KEY_RESIZE
, REQ_SCREEN_RESIZE
},
1804 get_request(int key
)
1808 for (i
= 0; i
< ARRAY_SIZE(keymap
); i
++)
1809 if (keymap
[i
].alias
== key
)
1810 return keymap
[i
].request
;
1812 return (enum request
) key
;
1820 /* Whether or not the curses interface has been initialized. */
1821 bool cursed
= FALSE
;
1823 /* The status window is used for polling keystrokes. */
1824 static WINDOW
*status_win
;
1826 /* Update status and title window. */
1828 report(const char *msg
, ...)
1830 static bool empty
= TRUE
;
1831 struct view
*view
= display
[current_view
];
1833 if (!empty
|| *msg
) {
1836 va_start(args
, msg
);
1839 wmove(status_win
, 0, 0);
1841 vwprintw(status_win
, msg
, args
);
1846 wrefresh(status_win
);
1851 update_view_title(view
);
1853 /* Move the cursor to the right-most column of the cursor line.
1855 * XXX: This could turn out to be a bit expensive, but it ensures that
1856 * the cursor does not jump around. */
1858 wmove(view
->win
, view
->lineno
- view
->offset
, view
->width
- 1);
1859 wrefresh(view
->win
);
1863 /* Controls when nodelay should be in effect when polling user input. */
1865 set_nonblocking_input(bool loading
)
1867 static unsigned int loading_views
;
1869 if ((loading
== FALSE
&& loading_views
-- == 1) ||
1870 (loading
== TRUE
&& loading_views
++ == 0))
1871 nodelay(status_win
, loading
);
1879 /* Initialize the curses library */
1880 if (isatty(STDIN_FILENO
)) {
1881 cursed
= !!initscr();
1883 /* Leave stdin and stdout alone when acting as a pager. */
1884 FILE *io
= fopen("/dev/tty", "r+");
1886 cursed
= !!newterm(NULL
, io
, io
);
1890 die("Failed to initialize curses");
1892 nonl(); /* Tell curses not to do NL->CR/NL on output */
1893 cbreak(); /* Take input chars one at a time, no wait for \n */
1894 noecho(); /* Don't echo input */
1895 leaveok(stdscr
, TRUE
);
1900 getmaxyx(stdscr
, y
, x
);
1901 status_win
= newwin(1, 0, y
- 1, 0);
1903 die("Failed to create status window");
1905 /* Enable keyboard mapping */
1906 keypad(status_win
, TRUE
);
1907 wbkgdset(status_win
, get_line_attr(LINE_STATUS
));
1912 * Repository references
1915 static struct ref
*refs
;
1918 static struct ref
**
1921 struct ref
**id_refs
= NULL
;
1922 size_t id_refs_size
= 0;
1925 for (i
= 0; i
< refs_size
; i
++) {
1928 if (strcmp(id
, refs
[i
].id
))
1931 tmp
= realloc(id_refs
, (id_refs_size
+ 1) * sizeof(*id_refs
));
1939 if (id_refs_size
> 0)
1940 id_refs
[id_refs_size
- 1]->next
= 1;
1941 id_refs
[id_refs_size
] = &refs
[i
];
1943 /* XXX: The properties of the commit chains ensures that we can
1944 * safely modify the shared ref. The repo references will
1945 * always be similar for the same id. */
1946 id_refs
[id_refs_size
]->next
= 0;
1956 char *cmd_env
= getenv("TIG_LS_REMOTE");
1957 char *cmd
= cmd_env
&& *cmd_env ? cmd_env
: TIG_LS_REMOTE
;
1958 FILE *pipe
= popen(cmd
, "r");
1959 char buffer
[BUFSIZ
];
1965 while ((line
= fgets(buffer
, sizeof(buffer
), pipe
))) {
1966 char *name
= strchr(line
, '\t');
1970 bool tag_commit
= FALSE
;
1976 namelen
= strlen(name
) - 1;
1978 /* Commits referenced by tags has "^{}" appended. */
1979 if (name
[namelen
- 1] == '}') {
1980 while (namelen
> 0 && name
[namelen
] != '^')
1987 if (!strncmp(name
, "refs/tags/", STRING_SIZE("refs/tags/"))) {
1990 name
+= STRING_SIZE("refs/tags/");
1993 } else if (!strncmp(name
, "refs/heads/", STRING_SIZE("refs/heads/"))) {
1994 name
+= STRING_SIZE("refs/heads/");
1996 } else if (!strcmp(name
, "HEAD")) {
2000 refs
= realloc(refs
, sizeof(*refs
) * (refs_size
+ 1));
2004 ref
= &refs
[refs_size
++];
2006 ref
->name
= strdup(name
);
2010 string_copy(ref
->id
, line
);
2028 /* XXX: Restore tty modes and let the OS cleanup the rest! */
2034 static void die(const char *err
, ...)
2040 va_start(args
, err
);
2041 fputs("tig: ", stderr
);
2042 vfprintf(stderr
, err
, args
);
2043 fputs("\n", stderr
);
2050 main(int argc
, char *argv
[])
2053 enum request request
;
2056 signal(SIGINT
, quit
);
2058 if (!parse_options(argc
, argv
))
2061 if (load_refs() == ERR
)
2062 die("Failed to load refs.");
2064 for (i
= 0; i
< ARRAY_SIZE(views
) && (view
= &views
[i
]); i
++)
2065 view
->cmd_env
= getenv(view
->cmd_env
);
2067 request
= opt_request
;
2071 while (view_driver(display
[current_view
], request
)) {
2075 foreach_view (view
, i
)
2078 /* Refresh, accept single keystroke of input */
2079 key
= wgetch(status_win
);
2080 request
= get_request(key
);
2082 /* Some low-level request handling. This keeps access to
2083 * status_win restricted. */
2087 /* Temporarily switch to line-oriented and echoed
2092 if (wgetnstr(status_win
, opt_cmd
+ 4, sizeof(opt_cmd
) - 4) == OK
) {
2093 memcpy(opt_cmd
, "git ", 4);
2094 opt_request
= REQ_VIEW_PAGER
;
2103 case REQ_SCREEN_RESIZE
:
2107 getmaxyx(stdscr
, height
, width
);
2109 /* Resize the status view and let the view driver take
2110 * care of resizing the displayed views. */
2111 wresize(status_win
, 1, width
);
2112 mvwin(status_win
, height
- 1, 0);
2113 wrefresh(status_win
);
2127 * Specifying revisions
2128 * --------------------
2129 * This section describes various ways to specify what revisions to
2130 * display or otherwise limit the view to.
2132 * Limit by date or number
2133 * ~~~~~~~~~~~~~~~~~~~~~~~
2134 * To speed up interaction with git, you can limit the amount of commits
2135 * to show both for the log and main view. Either limit by date using
2136 * e.g. `--since=1.month` or limit by the number of commits using `-n400`.
2138 * NOTE: You can tune the interaction with git by making use of the options
2139 * explained in this section.
2141 * NOTE: tig(1) does not itself parse the revision options
2142 * described in this section.
2146 * Alternatively, commits can be limited to a specific range, such as
2147 * "all commits between 'tag-1.0' and 'tag-2.0'". For example:
2149 * $ tig log tag-1.0..tag-2.0
2151 * This way of commit limiting makes it trivial to only browse the commits
2152 * which haven't been pushed to a remote branch. Assuming 'origin' is your
2153 * upstream remote branch, using:
2155 * $ tig log origin..HEAD
2157 * will list what will be pushed to the remote branch. Optionally, the ending
2158 * 'HEAD' can be left out since it is implied.
2160 * Limiting by reachability
2161 * ~~~~~~~~~~~~~~~~~~~~~~~~
2162 * Git interprets the range specifier "tag-1.0..tag-2.0" as
2163 * "all commits reachable from 'tag-2.0' but not from 'tag-1.0'".
2164 * If you prefer to specify which commit to preview in this way use the
2167 * $ tig log tag-2.0 ^tag-1.0
2169 * You can think of '^' as a negator. Using this alternate syntax, it is
2170 * possible to furthur prune commits by specifying multiple negators.
2174 * Known bugs and problems:
2176 * - If the screen width is very small the main view can draw
2177 * outside the current view causing bad wrapping. Same goes
2178 * for title and status windows.
2182 * Features that should be explored.
2190 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
2192 * This program is free software; you can redistribute it and/or modify
2193 * it under the terms of the GNU General Public License as published by
2194 * the Free Software Foundation; either version 2 of the License, or
2195 * (at your option) any later version.
2200 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
2201 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
2202 * gitk(1): git repository browser written using tcl/tk,
2203 * gitview(1): git repository browser written using python/gtk.