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
);
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
, const 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 command
298 * options for the main view. Example:
300 * $ tig -- --since=1.month
302 if (!strcmp(opt
, "--")) {
308 * log [git log options]::
309 * Open log view using the given git log options.
311 * diff [git diff options]::
312 * Open diff view using the given git diff options.
314 * show [git show 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
;
326 * [git log options]::
327 * tig(1) will stop the option parsing when the first
328 * command line parameter not starting with "-" is
329 * encountered. All options including this one will be
330 * passed to git log when loading the main view.
331 * This makes it possible to say:
333 * $ tig tag-1.0..HEAD
335 if (opt
[0] && opt
[0] != '-')
338 die("unknown command '%s'", opt
);
341 if (!isatty(STDIN_FILENO
)) {
345 * If stdin is a pipe, any log or diff options will be ignored and the
346 * pager view will be opened loading data from stdin. The pager mode
347 * can be used for colorizing output from various git commands.
349 * Example on how to colorize the output of git-show(1):
353 opt_request
= REQ_VIEW_PAGER
;
356 } else if (i
< argc
) {
360 * Git command options
361 * ~~~~~~~~~~~~~~~~~~~
362 * All git command options specified on the command line will
363 * be passed to the given command and all will be shell quoted
364 * before they are passed to the shell.
366 * NOTE: If you specify options for the main view, you should
367 * not use the `--pretty` option as this option will be set
368 * automatically to the format expected by the main view.
370 * Example on how to open the log view and show both author and
371 * committer information:
373 * $ tig log --pretty=fuller
375 * See the <<refspec, "Specifying revisions">> section below
376 * for an introduction to revision options supported by the git
377 * commands. For details on specific git command options, refer
378 * to the man page of the command in question.
381 if (opt_request
== REQ_VIEW_MAIN
)
382 /* XXX: This is vulnerable to the user overriding
383 * options required for the main view parser. */
384 string_copy(opt_cmd
, "git log --stat --pretty=raw");
386 string_copy(opt_cmd
, "git");
387 buf_size
= strlen(opt_cmd
);
389 while (buf_size
< sizeof(opt_cmd
) && i
< argc
) {
390 opt_cmd
[buf_size
++] = ' ';
391 buf_size
= sq_quote(opt_cmd
, buf_size
, argv
[i
++]);
394 if (buf_size
>= sizeof(opt_cmd
))
395 die("command too long");
397 opt_cmd
[buf_size
] = 0;
406 * Line-oriented content detection.
410 /* Line type String to match Foreground Background Attributes
411 * --------- --------------- ---------- ---------- ---------- */ \
413 LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
414 LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
415 LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
416 LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
417 LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
418 LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
419 LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
420 LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
421 LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
422 LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
423 LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
424 /* Pretty print commit header */ \
425 LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
426 LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
427 LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
428 LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
429 LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
430 LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
431 /* Raw commit header */ \
432 LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
433 LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
434 LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
435 LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
436 LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
438 LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
439 LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
441 LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
442 LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
443 LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
444 LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
445 LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
446 LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
447 LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
448 LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
449 LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
450 LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
451 LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD),
454 #define LINE(type, line, fg, bg, attr) \
461 const char *line
; /* The start of line to match. */
462 int linelen
; /* Size of string to match. */
463 int fg
, bg
, attr
; /* Color and text attributes for the lines. */
466 static struct line_info line_info
[] = {
467 #define LINE(type, line, fg, bg, attr) \
468 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
473 static enum line_type
474 get_line_type(char *line
)
476 int linelen
= strlen(line
);
479 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++)
480 /* Case insensitive search matches Signed-off-by lines better. */
481 if (linelen
>= line_info
[type
].linelen
&&
482 !strncasecmp(line_info
[type
].line
, line
, line_info
[type
].linelen
))
489 get_line_attr(enum line_type type
)
491 assert(type
< ARRAY_SIZE(line_info
));
492 return COLOR_PAIR(type
) | line_info
[type
].attr
;
498 int default_bg
= COLOR_BLACK
;
499 int default_fg
= COLOR_WHITE
;
504 if (use_default_colors() != ERR
) {
509 for (type
= 0; type
< ARRAY_SIZE(line_info
); type
++) {
510 struct line_info
*info
= &line_info
[type
];
511 int bg
= info
->bg
== COLOR_DEFAULT ? default_bg
: info
->bg
;
512 int fg
= info
->fg
== COLOR_DEFAULT ? default_fg
: info
->fg
;
514 init_pair(type
, fg
, bg
);
520 * ENVIRONMENT VARIABLES
521 * ---------------------
522 * Several options related to the interface with git can be configured
523 * via environment options.
525 * Repository references
526 * ~~~~~~~~~~~~~~~~~~~~~
527 * Commits that are referenced by tags and branch heads will be marked
528 * by the reference name surrounded by '[' and ']':
530 * 2006-03-26 19:42 Petr Baudis | [cogito-0.17.1] Cogito 0.17.1
532 * If you want to filter out certain directories under `.git/refs/`, say
533 * `tmp` you can do it by setting the following variable:
535 * $ TIG_LS_REMOTE="git ls-remote . | sed /\/tmp\//d" tig
537 * Or set the variable permanently in your environment.
540 * Set command for retrieving all repository references. The command
541 * should output data in the same format as git-ls-remote(1).
544 #define TIG_LS_REMOTE \
545 "git ls-remote . 2>/dev/null"
551 * It is possible to alter which commands are used for the different views.
552 * If for example you prefer commits in the main view to be sorted by date
553 * and only show 500 commits, use:
555 * $ TIG_MAIN_CMD="git log --date-order -n500 --pretty=raw %s" tig
557 * Or set the variable permanently in your environment.
559 * Notice, how `%s` is used to specify the commit reference. There can
560 * be a maximum of 5 `%s` ref specifications.
563 * The command used for the diff view. By default, git show is used
567 * The command used for the log view. If you prefer to have both
568 * author and committer shown in the log view be sure to pass
569 * `--pretty=fuller` to git log.
572 * The command used for the main view. Note, you must always specify
573 * the option: `--pretty=raw` since the main view parser expects to
577 #define TIG_DIFF_CMD \
578 "git show --patch-with-stat --find-copies-harder -B -C %s"
580 #define TIG_LOG_CMD \
581 "git log --cc --stat -n100 %s"
583 #define TIG_MAIN_CMD \
584 "git log --topo-order --stat --pretty=raw %s"
586 /* ... silently ignore that the following are also exported. */
588 #define TIG_HELP_CMD \
589 "man tig 2>/dev/null"
591 #define TIG_PAGER_CMD \
598 * tig(1) presents various 'views' of a repository. Each view is based on output
599 * from an external command, most often 'git log', 'git diff', or 'git show'.
602 * Is the default view, and it shows a one line summary of each commit
603 * in the chosen list of revisions. The summary includes commit date,
604 * author, and the first line of the log message. Additionally, any
605 * repository references, such as tags, will be shown.
608 * Presents a more rich view of the revision log showing the whole log
609 * message and the diffstat.
612 * Shows either the diff of the current working tree, that is, what
613 * has changed since the last commit, or the commit diff complete
614 * with log message, diffstat and diff.
617 * Is used for displaying both input from stdin and output from git
618 * commands entered in the internal prompt.
621 * Displays the information from the tig(1) man page. For the help view
622 * to work you need to have the tig(1) man page installed.
626 const char *name
; /* View name */
627 const char *cmd_fmt
; /* Default command line format */
628 const char *cmd_env
; /* Command line set via environment */
629 const char *id
; /* Points to either of ref_{head,commit} */
630 size_t objsize
; /* Size of objects in the line index */
633 /* What type of content being displayed. Used in the
636 /* Draw one line; @lineno must be < view->height. */
637 bool (*draw
)(struct view
*view
, unsigned int lineno
);
638 /* Read one line; updates view->line. */
639 bool (*read
)(struct view
*view
, char *line
);
640 /* Depending on view, change display based on current line. */
641 bool (*enter
)(struct view
*view
);
644 char cmd
[SIZEOF_CMD
]; /* Command buffer */
645 char ref
[SIZEOF_REF
]; /* Hovered commit reference */
646 char vid
[SIZEOF_REF
]; /* View ID. Set to id member when updating. */
648 int height
, width
; /* The width and height of the main window */
649 WINDOW
*win
; /* The main window */
650 WINDOW
*title
; /* The title window living below the main window */
653 unsigned long offset
; /* Offset of the window top */
654 unsigned long lineno
; /* Current line number */
657 unsigned long lines
; /* Total number of lines */
658 void **line
; /* Line index; each line contains user data */
659 unsigned int digits
; /* Number of digits in the lines member. */
666 static struct view_ops pager_ops
;
667 static struct view_ops main_ops
;
669 static char ref_head
[SIZEOF_REF
] = "HEAD";
670 static char ref_commit
[SIZEOF_REF
] = "HEAD";
672 #define VIEW_STR(name, cmd, env, ref, objsize, ops) \
673 { name, cmd, #env, ref, objsize, ops }
675 #define VIEW_(id, name, ops, ref, objsize) \
676 VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, objsize, ops)
678 static struct view views
[] = {
679 VIEW_(MAIN
, "main", &main_ops
, ref_head
, sizeof(struct commit
)),
680 VIEW_(DIFF
, "diff", &pager_ops
, ref_commit
, sizeof(char)),
681 VIEW_(LOG
, "log", &pager_ops
, ref_head
, sizeof(char)),
682 VIEW_(HELP
, "help", &pager_ops
, ref_head
, sizeof(char)),
683 VIEW_(PAGER
, "pager", &pager_ops
, "static", sizeof(char)),
686 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
688 /* The display array of active views and the index of the current view. */
689 static struct view
*display
[2];
690 static unsigned int current_view
;
692 #define foreach_view(view, i) \
693 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
697 redraw_view_from(struct view
*view
, int lineno
)
699 assert(0 <= lineno
&& lineno
< view
->height
);
701 for (; lineno
< view
->height
; lineno
++) {
702 if (!view
->ops
->draw(view
, lineno
))
706 redrawwin(view
->win
);
711 redraw_view(struct view
*view
)
714 redraw_view_from(view
, 0);
721 struct view
*base
= display
[0];
722 struct view
*view
= display
[1] ? display
[1] : display
[0];
724 /* Setup window dimensions */
726 getmaxyx(stdscr
, base
->height
, base
->width
);
728 /* Make room for the status window. */
732 /* Horizontal split. */
733 view
->width
= base
->width
;
734 view
->height
= SCALE_SPLIT_VIEW(base
->height
);
735 base
->height
-= view
->height
;
737 /* Make room for the title bar. */
741 /* Make room for the title bar. */
746 foreach_view (view
, i
) {
747 /* Keep the size of the all view windows one lager than is
748 * required. This makes current line management easier when the
749 * cursor will go outside the window. */
751 view
->win
= newwin(view
->height
+ 1, 0, offset
, 0);
753 die("Failed to create %s view", view
->name
);
755 scrollok(view
->win
, TRUE
);
757 view
->title
= newwin(1, 0, offset
+ view
->height
, 0);
759 die("Failed to create title window");
762 wresize(view
->win
, view
->height
+ 1, view
->width
);
763 mvwin(view
->win
, offset
, 0);
764 mvwin(view
->title
, offset
+ view
->height
, 0);
768 offset
+= view
->height
+ 1;
773 update_view_title(struct view
*view
)
775 if (view
== display
[current_view
])
776 wbkgdset(view
->title
, get_line_attr(LINE_TITLE_FOCUS
));
778 wbkgdset(view
->title
, get_line_attr(LINE_TITLE_BLUR
));
781 wmove(view
->title
, 0, 0);
783 /* [main] ref: 334b506... - commit 6 of 4383 (0%) */
786 wprintw(view
->title
, "[%s] %s", view
->name
, view
->ref
);
788 wprintw(view
->title
, "[%s]", view
->name
);
791 wprintw(view
->title
, " - %s %d of %d (%d%%)",
795 (view
->lineno
+ 1) * 100 / view
->lines
);
798 wrefresh(view
->title
);
805 /* Scrolling backend */
807 do_scroll_view(struct view
*view
, int lines
)
809 /* The rendering expects the new offset. */
810 view
->offset
+= lines
;
812 assert(0 <= view
->offset
&& view
->offset
< view
->lines
);
815 /* Redraw the whole screen if scrolling is pointless. */
816 if (view
->height
< ABS(lines
)) {
820 int line
= lines
> 0 ? view
->height
- lines
: 0;
821 int end
= line
+ ABS(lines
);
823 wscrl(view
->win
, lines
);
825 for (; line
< end
; line
++) {
826 if (!view
->ops
->draw(view
, line
))
831 /* Move current line into the view. */
832 if (view
->lineno
< view
->offset
) {
833 view
->lineno
= view
->offset
;
834 view
->ops
->draw(view
, 0);
836 } else if (view
->lineno
>= view
->offset
+ view
->height
) {
837 if (view
->lineno
== view
->offset
+ view
->height
) {
838 /* Clear the hidden line so it doesn't show if the view
840 wmove(view
->win
, view
->height
, 0);
841 wclrtoeol(view
->win
);
843 view
->lineno
= view
->offset
+ view
->height
- 1;
844 view
->ops
->draw(view
, view
->lineno
- view
->offset
);
847 assert(view
->offset
<= view
->lineno
&& view
->lineno
< view
->lines
);
849 redrawwin(view
->win
);
854 /* Scroll frontend */
856 scroll_view(struct view
*view
, enum request request
)
861 case REQ_SCROLL_PAGE_DOWN
:
862 lines
= view
->height
;
863 case REQ_SCROLL_LINE_DOWN
:
864 if (view
->offset
+ lines
> view
->lines
)
865 lines
= view
->lines
- view
->offset
;
867 if (lines
== 0 || view
->offset
+ view
->height
>= view
->lines
) {
868 report("Cannot scroll beyond the last line");
873 case REQ_SCROLL_PAGE_UP
:
874 lines
= view
->height
;
875 case REQ_SCROLL_LINE_UP
:
876 if (lines
> view
->offset
)
877 lines
= view
->offset
;
880 report("Cannot scroll beyond the first line");
888 die("request %d not handled in switch", request
);
891 do_scroll_view(view
, lines
);
896 move_view(struct view
*view
, enum request request
)
901 case REQ_MOVE_FIRST_LINE
:
902 steps
= -view
->lineno
;
905 case REQ_MOVE_LAST_LINE
:
906 steps
= view
->lines
- view
->lineno
- 1;
909 case REQ_MOVE_PAGE_UP
:
910 steps
= view
->height
> view
->lineno
911 ?
-view
->lineno
: -view
->height
;
914 case REQ_MOVE_PAGE_DOWN
:
915 steps
= view
->lineno
+ view
->height
>= view
->lines
916 ? view
->lines
- view
->lineno
- 1 : view
->height
;
920 case REQ_MOVE_UP_ENTER
:
925 case REQ_MOVE_DOWN_ENTER
:
930 die("request %d not handled in switch", request
);
933 if (steps
<= 0 && view
->lineno
== 0) {
934 report("Cannot move beyond the first line");
937 } else if (steps
>= 0 && view
->lineno
+ 1 >= view
->lines
) {
938 report("Cannot move beyond the last line");
942 /* Move the current line */
943 view
->lineno
+= steps
;
944 assert(0 <= view
->lineno
&& view
->lineno
< view
->lines
);
946 /* Repaint the old "current" line if we be scrolling */
947 if (ABS(steps
) < view
->height
) {
948 int prev_lineno
= view
->lineno
- steps
- view
->offset
;
950 wmove(view
->win
, prev_lineno
, 0);
951 wclrtoeol(view
->win
);
952 view
->ops
->draw(view
, prev_lineno
);
955 /* Check whether the view needs to be scrolled */
956 if (view
->lineno
< view
->offset
||
957 view
->lineno
>= view
->offset
+ view
->height
) {
958 if (steps
< 0 && -steps
> view
->offset
) {
959 steps
= -view
->offset
;
961 } else if (steps
> 0) {
962 if (view
->lineno
== view
->lines
- 1 &&
963 view
->lines
> view
->height
) {
964 steps
= view
->lines
- view
->offset
- 1;
965 if (steps
>= view
->height
)
966 steps
-= view
->height
- 1;
970 do_scroll_view(view
, steps
);
974 /* Draw the current line */
975 view
->ops
->draw(view
, view
->lineno
- view
->offset
);
977 redrawwin(view
->win
);
984 * Incremental updating
988 begin_update(struct view
*view
)
990 const char *id
= view
->id
;
993 string_copy(view
->cmd
, opt_cmd
);
995 /* When running random commands, the view ref could have become
996 * invalid so clear it. */
999 const char *format
= view
->cmd_env ? view
->cmd_env
: view
->cmd_fmt
;
1001 if (snprintf(view
->cmd
, sizeof(view
->cmd
), format
,
1002 id
, id
, id
, id
, id
) >= sizeof(view
->cmd
))
1006 /* Special case for the pager view. */
1008 view
->pipe
= opt_pipe
;
1011 view
->pipe
= popen(view
->cmd
, "r");
1017 set_nonblocking_input(TRUE
);
1022 string_copy(view
->vid
, id
);
1027 for (i
= 0; i
< view
->lines
; i
++)
1029 free(view
->line
[i
]);
1035 view
->start_time
= time(NULL
);
1041 end_update(struct view
*view
)
1045 set_nonblocking_input(FALSE
);
1046 if (view
->pipe
== stdin
)
1054 update_view(struct view
*view
)
1056 char buffer
[BUFSIZ
];
1059 /* The number of lines to read. If too low it will cause too much
1060 * redrawing (and possible flickering), if too high responsiveness
1062 unsigned long lines
= view
->height
;
1063 int redraw_from
= -1;
1068 /* Only redraw if lines are visible. */
1069 if (view
->offset
+ view
->height
>= view
->lines
)
1070 redraw_from
= view
->lines
- view
->offset
;
1072 tmp
= realloc(view
->line
, sizeof(*view
->line
) * (view
->lines
+ lines
));
1078 while ((line
= fgets(buffer
, sizeof(buffer
), view
->pipe
))) {
1079 int linelen
= strlen(line
);
1082 line
[linelen
- 1] = 0;
1084 if (!view
->ops
->read(view
, line
))
1094 lines
= view
->lines
;
1095 for (digits
= 0; lines
; digits
++)
1098 /* Keep the displayed view in sync with line number scaling. */
1099 if (digits
!= view
->digits
) {
1100 view
->digits
= digits
;
1105 if (redraw_from
>= 0) {
1106 /* If this is an incremental update, redraw the previous line
1107 * since for commits some members could have changed when
1108 * loading the main view. */
1109 if (redraw_from
> 0)
1112 /* Incrementally draw avoids flickering. */
1113 redraw_view_from(view
, redraw_from
);
1116 /* Update the title _after_ the redraw so that if the redraw picks up a
1117 * commit reference in view->ref it'll be available here. */
1118 update_view_title(view
);
1120 if (ferror(view
->pipe
)) {
1121 report("Failed to read: %s", strerror(errno
));
1124 } else if (feof(view
->pipe
)) {
1125 time_t secs
= time(NULL
) - view
->start_time
;
1127 if (view
== VIEW(REQ_VIEW_HELP
)) {
1128 const char *msg
= TIG_HELP
;
1130 if (view
->lines
== 0) {
1131 /* Slightly ugly, but abusing view->ref keeps
1132 * the error message. */
1133 string_copy(view
->ref
, "No help available");
1134 msg
= "The tig(1) manpage is not installed";
1141 report("Loaded %d lines in %ld second%s", view
->lines
, secs
,
1142 secs
== 1 ?
"" : "s");
1149 report("Allocation failure");
1157 OPEN_DEFAULT
= 0, /* Use default view switching. */
1158 OPEN_SPLIT
= 1, /* Split current view. */
1159 OPEN_BACKGROUNDED
= 2, /* Backgrounded. */
1160 OPEN_RELOAD
= 4, /* Reload view even if it is the current. */
1164 open_view(struct view
*prev
, enum request request
, enum open_flags flags
)
1166 bool backgrounded
= !!(flags
& OPEN_BACKGROUNDED
);
1167 bool split
= !!(flags
& OPEN_SPLIT
);
1168 bool reload
= !!(flags
& OPEN_RELOAD
);
1169 struct view
*view
= VIEW(request
);
1170 struct view
*displayed
;
1173 /* Cycle between displayed views and count the views. */
1174 foreach_view (displayed
, nviews
) {
1176 view
== displayed
&&
1177 !strcmp(view
->vid
, prev
->vid
)) {
1178 current_view
= nviews
;
1179 /* Blur out the title of the previous view. */
1180 update_view_title(prev
);
1186 if (view
== prev
&& nviews
== 1 && !reload
) {
1187 report("Already in %s view", view
->name
);
1191 if ((reload
|| strcmp(view
->vid
, view
->id
)) &&
1192 !begin_update(view
)) {
1193 report("Failed to load %s view", view
->name
);
1198 display
[current_view
+ 1] = view
;
1202 /* Maximize the current view. */
1203 memset(display
, 0, sizeof(display
));
1205 display
[current_view
] = view
;
1210 if (split
&& prev
->lineno
- prev
->offset
>= prev
->height
) {
1211 /* Take the title line into account. */
1212 int lines
= prev
->lineno
- prev
->offset
- prev
->height
+ 1;
1214 /* Scroll the view that was split if the current line is
1215 * outside the new limited view. */
1216 do_scroll_view(prev
, lines
);
1219 if (prev
&& view
!= prev
) {
1220 /* "Blur" the previous view. */
1222 update_view_title(prev
);
1224 /* Continue loading split views in the background. */
1230 /* Clear the old view and let the incremental updating refill
1233 report("Loading...");
1236 if (view
== VIEW(REQ_VIEW_HELP
))
1237 report("%s", TIG_HELP
);
1242 /* If the view is backgrounded the above calls to report()
1243 * won't redraw the view title. */
1245 update_view_title(view
);
1250 * User request switch noodle
1254 view_driver(struct view
*view
, enum request request
)
1261 case REQ_MOVE_PAGE_UP
:
1262 case REQ_MOVE_PAGE_DOWN
:
1263 case REQ_MOVE_FIRST_LINE
:
1264 case REQ_MOVE_LAST_LINE
:
1265 move_view(view
, request
);
1268 case REQ_SCROLL_LINE_DOWN
:
1269 case REQ_SCROLL_LINE_UP
:
1270 case REQ_SCROLL_PAGE_DOWN
:
1271 case REQ_SCROLL_PAGE_UP
:
1272 scroll_view(view
, request
);
1279 case REQ_VIEW_PAGER
:
1280 open_view(view
, request
, OPEN_DEFAULT
);
1283 case REQ_MOVE_UP_ENTER
:
1284 case REQ_MOVE_DOWN_ENTER
:
1285 move_view(view
, request
);
1290 report("Nothing to enter");
1293 return view
->ops
->enter(view
);
1297 int nviews
= display
[1] ?
2 : 1;
1298 int next_view
= (current_view
+ 1) % nviews
;
1300 if (next_view
== current_view
) {
1301 report("Only one view is displayed");
1305 current_view
= next_view
;
1306 /* Blur out the title of the previous view. */
1307 update_view_title(view
);
1311 case REQ_TOGGLE_LINE_NUMBERS
:
1312 opt_line_number
= !opt_line_number
;
1314 update_view_title(view
);
1318 /* Always reload^Wrerun commands from the prompt. */
1319 open_view(view
, opt_request
, OPEN_RELOAD
);
1322 case REQ_STOP_LOADING
:
1323 foreach_view (view
, i
) {
1325 report("Stopped loaded the %s view", view
->name
),
1330 case REQ_SHOW_VERSION
:
1331 report("Version: %s", VERSION
);
1334 case REQ_SCREEN_RESIZE
:
1337 case REQ_SCREEN_REDRAW
:
1338 foreach_view (view
, i
) {
1340 update_view_title(view
);
1344 case REQ_SCREEN_UPDATE
:
1352 /* An unknown key will show most commonly used commands. */
1353 report("Unknown key, press 'h' for help");
1362 * View backend handlers
1366 pager_draw(struct view
*view
, unsigned int lineno
)
1368 enum line_type type
;
1373 if (view
->offset
+ lineno
>= view
->lines
)
1376 line
= view
->line
[view
->offset
+ lineno
];
1377 type
= get_line_type(line
);
1379 wmove(view
->win
, lineno
, 0);
1381 if (view
->offset
+ lineno
== view
->lineno
) {
1382 if (type
== LINE_COMMIT
) {
1383 string_copy(view
->ref
, line
+ 7);
1384 string_copy(ref_commit
, view
->ref
);
1388 wchgat(view
->win
, -1, 0, type
, NULL
);
1391 attr
= get_line_attr(type
);
1392 wattrset(view
->win
, attr
);
1394 linelen
= strlen(line
);
1396 if (opt_line_number
|| opt_tab_size
< TABSIZE
) {
1397 static char spaces
[] = " ";
1398 int col_offset
= 0, col
= 0;
1400 if (opt_line_number
) {
1401 unsigned long real_lineno
= view
->offset
+ lineno
+ 1;
1403 if (real_lineno
== 1 ||
1404 (real_lineno
% opt_num_interval
) == 0) {
1405 wprintw(view
->win
, "%.*d", view
->digits
, real_lineno
);
1408 waddnstr(view
->win
, spaces
,
1409 MIN(view
->digits
, STRING_SIZE(spaces
)));
1411 waddstr(view
->win
, ": ");
1412 col_offset
= view
->digits
+ 2;
1415 while (line
&& col_offset
+ col
< view
->width
) {
1416 int cols_max
= view
->width
- col_offset
- col
;
1420 if (*line
== '\t') {
1421 assert(sizeof(spaces
) > TABSIZE
);
1424 cols
= opt_tab_size
- (col
% opt_tab_size
);
1427 line
= strchr(line
, '\t');
1428 cols
= line ? line
- text
: strlen(text
);
1431 waddnstr(view
->win
, text
, MIN(cols
, cols_max
));
1436 int col
= 0, pos
= 0;
1438 for (; pos
< linelen
&& col
< view
->width
; pos
++, col
++)
1439 if (line
[pos
] == '\t')
1440 col
+= TABSIZE
- (col
% TABSIZE
) - 1;
1442 waddnstr(view
->win
, line
, pos
);
1449 pager_read(struct view
*view
, char *line
)
1451 /* Compress empty lines in the help view. */
1452 if (view
== VIEW(REQ_VIEW_HELP
) &&
1455 !*((char *) view
->line
[view
->lines
- 1]))
1458 view
->line
[view
->lines
] = strdup(line
);
1459 if (!view
->line
[view
->lines
])
1467 pager_enter(struct view
*view
)
1469 char *line
= view
->line
[view
->lineno
];
1471 if (get_line_type(line
) == LINE_COMMIT
) {
1472 if (view
== VIEW(REQ_VIEW_LOG
))
1473 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
| OPEN_BACKGROUNDED
);
1475 open_view(view
, REQ_VIEW_DIFF
, OPEN_DEFAULT
);
1481 static struct view_ops pager_ops
= {
1489 static struct ref
**get_refs(char *id
);
1492 main_draw(struct view
*view
, unsigned int lineno
)
1494 char buf
[DATE_COLS
+ 1];
1495 struct commit
*commit
;
1496 enum line_type type
;
1500 if (view
->offset
+ lineno
>= view
->lines
)
1503 commit
= view
->line
[view
->offset
+ lineno
];
1504 if (!*commit
->author
)
1507 wmove(view
->win
, lineno
, col
);
1509 if (view
->offset
+ lineno
== view
->lineno
) {
1510 string_copy(view
->ref
, commit
->id
);
1511 string_copy(ref_commit
, view
->ref
);
1513 wattrset(view
->win
, get_line_attr(type
));
1514 wchgat(view
->win
, -1, 0, type
, NULL
);
1517 type
= LINE_MAIN_COMMIT
;
1518 wattrset(view
->win
, get_line_attr(LINE_MAIN_DATE
));
1521 timelen
= strftime(buf
, sizeof(buf
), DATE_FORMAT
, &commit
->time
);
1522 waddnstr(view
->win
, buf
, timelen
);
1523 waddstr(view
->win
, " ");
1526 wmove(view
->win
, lineno
, col
);
1527 if (type
!= LINE_CURSOR
)
1528 wattrset(view
->win
, get_line_attr(LINE_MAIN_AUTHOR
));
1530 if (strlen(commit
->author
) > 19) {
1531 waddnstr(view
->win
, commit
->author
, 18);
1532 if (type
!= LINE_CURSOR
)
1533 wattrset(view
->win
, get_line_attr(LINE_MAIN_DELIM
));
1534 waddch(view
->win
, '~');
1536 waddstr(view
->win
, commit
->author
);
1540 if (type
!= LINE_CURSOR
)
1541 wattrset(view
->win
, A_NORMAL
);
1543 mvwaddch(view
->win
, lineno
, col
, ACS_LTEE
);
1544 wmove(view
->win
, lineno
, col
+ 2);
1551 if (type
== LINE_CURSOR
)
1553 else if (commit
->refs
[i
]->tag
)
1554 wattrset(view
->win
, get_line_attr(LINE_MAIN_TAG
));
1556 wattrset(view
->win
, get_line_attr(LINE_MAIN_REF
));
1557 waddstr(view
->win
, "[");
1558 waddstr(view
->win
, commit
->refs
[i
]->name
);
1559 waddstr(view
->win
, "]");
1560 if (type
!= LINE_CURSOR
)
1561 wattrset(view
->win
, A_NORMAL
);
1562 waddstr(view
->win
, " ");
1563 col
+= strlen(commit
->refs
[i
]->name
) + STRING_SIZE("[] ");
1564 } while (commit
->refs
[i
++]->next
);
1567 if (type
!= LINE_CURSOR
)
1568 wattrset(view
->win
, get_line_attr(type
));
1571 int titlelen
= strlen(commit
->title
);
1573 if (col
+ titlelen
> view
->width
)
1574 titlelen
= view
->width
- col
;
1576 waddnstr(view
->win
, commit
->title
, titlelen
);
1582 /* Reads git log --pretty=raw output and parses it into the commit struct. */
1584 main_read(struct view
*view
, char *line
)
1586 enum line_type type
= get_line_type(line
);
1587 struct commit
*commit
;
1591 commit
= calloc(1, sizeof(struct commit
));
1595 line
+= STRING_SIZE("commit ");
1597 view
->line
[view
->lines
++] = commit
;
1598 string_copy(commit
->id
, line
);
1599 commit
->refs
= get_refs(commit
->id
);
1604 char *ident
= line
+ STRING_SIZE("author ");
1605 char *end
= strchr(ident
, '<');
1608 for (; end
> ident
&& isspace(end
[-1]); end
--) ;
1612 commit
= view
->line
[view
->lines
- 1];
1613 string_copy(commit
->author
, ident
);
1615 /* Parse epoch and timezone */
1617 char *secs
= strchr(end
+ 1, '>');
1621 if (!secs
|| secs
[1] != ' ')
1625 time
= (time_t) atol(secs
);
1626 zone
= strchr(secs
, ' ');
1627 if (zone
&& strlen(zone
) == STRING_SIZE(" +0700")) {
1631 tz
= ('0' - zone
[1]) * 60 * 60 * 10;
1632 tz
+= ('0' - zone
[2]) * 60 * 60;
1633 tz
+= ('0' - zone
[3]) * 60;
1634 tz
+= ('0' - zone
[4]) * 60;
1641 gmtime_r(&time
, &commit
->time
);
1646 /* We should only ever end up here if there has already been a
1647 * commit line, however, be safe. */
1648 if (view
->lines
== 0)
1651 /* Fill in the commit title if it has not already been set. */
1652 commit
= view
->line
[view
->lines
- 1];
1653 if (commit
->title
[0])
1656 /* Require titles to start with a non-space character at the
1657 * offset used by git log. */
1658 /* FIXME: More gracefull handling of titles; append "..." to
1659 * shortened titles, etc. */
1660 if (strncmp(line
, " ", 4) ||
1664 string_copy(commit
->title
, line
+ 4);
1671 main_enter(struct view
*view
)
1673 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
| OPEN_BACKGROUNDED
);
1677 static struct view_ops main_ops
= {
1688 * Below the default key bindings are shown.
1696 static struct keymap keymap
[] = {
1701 * Switch to main view.
1703 * Switch to diff view.
1705 * Switch to log view.
1707 * Switch to pager view.
1711 * If on a commit line show the commit diff. Additionally, if in
1712 * main or log view this will split the view. To open the commit
1713 * diff in full size view either use 'd' or press Return twice.
1715 * Switch to next view.
1717 { 'm', REQ_VIEW_MAIN
},
1718 { 'd', REQ_VIEW_DIFF
},
1719 { 'l', REQ_VIEW_LOG
},
1720 { 'p', REQ_VIEW_PAGER
},
1721 { 'h', REQ_VIEW_HELP
},
1723 { KEY_TAB
, REQ_VIEW_NEXT
},
1724 { KEY_RETURN
, REQ_ENTER
},
1730 * Move cursor one line up.
1732 * Move cursor one line down.
1734 * Move cursor one line up and enter. When used in the main view
1735 * this will always show the diff of the current commit in the
1738 * Move cursor one line down and enter.
1740 * Move cursor one page up.
1742 * Move cursor one page down.
1744 * Jump to first line.
1746 * Jump to last line.
1748 { KEY_UP
, REQ_MOVE_UP
},
1749 { KEY_DOWN
, REQ_MOVE_DOWN
},
1750 { 'k', REQ_MOVE_UP_ENTER
},
1751 { 'j', REQ_MOVE_DOWN_ENTER
},
1752 { KEY_HOME
, REQ_MOVE_FIRST_LINE
},
1753 { KEY_END
, REQ_MOVE_LAST_LINE
},
1754 { KEY_NPAGE
, REQ_MOVE_PAGE_DOWN
},
1755 { KEY_PPAGE
, REQ_MOVE_PAGE_UP
},
1761 * Scroll view one line up.
1763 * Scroll view one line down.
1765 * Scroll view one page up.
1767 * Scroll view one page down.
1769 { KEY_IC
, REQ_SCROLL_LINE_UP
},
1770 { KEY_DC
, REQ_SCROLL_LINE_DOWN
},
1771 { 'w', REQ_SCROLL_PAGE_UP
},
1772 { 's', REQ_SCROLL_PAGE_DOWN
},
1782 * Stop all background loading. This can be useful if you use
1783 * tig(1) in a repository with a long history without limiting
1788 * Toggle line numbers on/off.
1790 * Open prompt. This allows you to specify what git command
1796 { 'z', REQ_STOP_LOADING
},
1797 { 'v', REQ_SHOW_VERSION
},
1798 { 'r', REQ_SCREEN_REDRAW
},
1799 { 'n', REQ_TOGGLE_LINE_NUMBERS
},
1800 { ':', REQ_PROMPT
},
1802 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
1803 { ERR
, REQ_SCREEN_UPDATE
},
1805 /* Use the ncurses SIGWINCH handler. */
1806 { KEY_RESIZE
, REQ_SCREEN_RESIZE
},
1810 get_request(int key
)
1814 for (i
= 0; i
< ARRAY_SIZE(keymap
); i
++)
1815 if (keymap
[i
].alias
== key
)
1816 return keymap
[i
].request
;
1818 return (enum request
) key
;
1826 /* Whether or not the curses interface has been initialized. */
1827 bool cursed
= FALSE
;
1829 /* The status window is used for polling keystrokes. */
1830 static WINDOW
*status_win
;
1832 /* Update status and title window. */
1834 report(const char *msg
, ...)
1836 static bool empty
= TRUE
;
1837 struct view
*view
= display
[current_view
];
1839 if (!empty
|| *msg
) {
1842 va_start(args
, msg
);
1845 wmove(status_win
, 0, 0);
1847 vwprintw(status_win
, msg
, args
);
1852 wrefresh(status_win
);
1857 update_view_title(view
);
1859 /* Move the cursor to the right-most column of the cursor line.
1861 * XXX: This could turn out to be a bit expensive, but it ensures that
1862 * the cursor does not jump around. */
1864 wmove(view
->win
, view
->lineno
- view
->offset
, view
->width
- 1);
1865 wrefresh(view
->win
);
1869 /* Controls when nodelay should be in effect when polling user input. */
1871 set_nonblocking_input(bool loading
)
1873 static unsigned int loading_views
;
1875 if ((loading
== FALSE
&& loading_views
-- == 1) ||
1876 (loading
== TRUE
&& loading_views
++ == 0))
1877 nodelay(status_win
, loading
);
1885 /* Initialize the curses library */
1886 if (isatty(STDIN_FILENO
)) {
1887 cursed
= !!initscr();
1889 /* Leave stdin and stdout alone when acting as a pager. */
1890 FILE *io
= fopen("/dev/tty", "r+");
1892 cursed
= !!newterm(NULL
, io
, io
);
1896 die("Failed to initialize curses");
1898 nonl(); /* Tell curses not to do NL->CR/NL on output */
1899 cbreak(); /* Take input chars one at a time, no wait for \n */
1900 noecho(); /* Don't echo input */
1901 leaveok(stdscr
, TRUE
);
1906 getmaxyx(stdscr
, y
, x
);
1907 status_win
= newwin(1, 0, y
- 1, 0);
1909 die("Failed to create status window");
1911 /* Enable keyboard mapping */
1912 keypad(status_win
, TRUE
);
1913 wbkgdset(status_win
, get_line_attr(LINE_STATUS
));
1918 * Repository references
1921 static struct ref
*refs
;
1922 static size_t refs_size
;
1924 static struct ref
**
1927 struct ref
**id_refs
= NULL
;
1928 size_t id_refs_size
= 0;
1931 for (i
= 0; i
< refs_size
; i
++) {
1934 if (strcmp(id
, refs
[i
].id
))
1937 tmp
= realloc(id_refs
, (id_refs_size
+ 1) * sizeof(*id_refs
));
1945 if (id_refs_size
> 0)
1946 id_refs
[id_refs_size
- 1]->next
= 1;
1947 id_refs
[id_refs_size
] = &refs
[i
];
1949 /* XXX: The properties of the commit chains ensures that we can
1950 * safely modify the shared ref. The repo references will
1951 * always be similar for the same id. */
1952 id_refs
[id_refs_size
]->next
= 0;
1962 const char *cmd_env
= getenv("TIG_LS_REMOTE");
1963 const char *cmd
= cmd_env
&& *cmd_env ? cmd_env
: TIG_LS_REMOTE
;
1964 FILE *pipe
= popen(cmd
, "r");
1965 char buffer
[BUFSIZ
];
1971 while ((line
= fgets(buffer
, sizeof(buffer
), pipe
))) {
1972 char *name
= strchr(line
, '\t');
1976 bool tag_commit
= FALSE
;
1982 namelen
= strlen(name
) - 1;
1984 /* Commits referenced by tags has "^{}" appended. */
1985 if (name
[namelen
- 1] == '}') {
1986 while (namelen
> 0 && name
[namelen
] != '^')
1993 if (!strncmp(name
, "refs/tags/", STRING_SIZE("refs/tags/"))) {
1996 name
+= STRING_SIZE("refs/tags/");
1999 } else if (!strncmp(name
, "refs/heads/", STRING_SIZE("refs/heads/"))) {
2000 name
+= STRING_SIZE("refs/heads/");
2002 } else if (!strcmp(name
, "HEAD")) {
2006 refs
= realloc(refs
, sizeof(*refs
) * (refs_size
+ 1));
2010 ref
= &refs
[refs_size
++];
2012 ref
->name
= strdup(name
);
2016 string_copy(ref
->id
, line
);
2025 die("Not a git repository");
2035 #define __NORETURN __attribute__((__noreturn__))
2040 static void __NORETURN
2043 /* XXX: Restore tty modes and let the OS cleanup the rest! */
2049 static void __NORETURN
2050 die(const char *err
, ...)
2056 va_start(args
, err
);
2057 fputs("tig: ", stderr
);
2058 vfprintf(stderr
, err
, args
);
2059 fputs("\n", stderr
);
2066 main(int argc
, char *argv
[])
2069 enum request request
;
2072 signal(SIGINT
, quit
);
2074 if (!parse_options(argc
, argv
))
2077 if (load_refs() == ERR
)
2078 die("Failed to load refs.");
2080 for (i
= 0; i
< ARRAY_SIZE(views
) && (view
= &views
[i
]); i
++)
2081 view
->cmd_env
= getenv(view
->cmd_env
);
2083 request
= opt_request
;
2087 while (view_driver(display
[current_view
], request
)) {
2091 foreach_view (view
, i
)
2094 /* Refresh, accept single keystroke of input */
2095 key
= wgetch(status_win
);
2096 request
= get_request(key
);
2098 /* Some low-level request handling. This keeps access to
2099 * status_win restricted. */
2103 /* Temporarily switch to line-oriented and echoed
2108 if (wgetnstr(status_win
, opt_cmd
+ 4, sizeof(opt_cmd
) - 4) == OK
) {
2109 memcpy(opt_cmd
, "git ", 4);
2110 opt_request
= REQ_VIEW_PAGER
;
2119 case REQ_SCREEN_RESIZE
:
2123 getmaxyx(stdscr
, height
, width
);
2125 /* Resize the status view and let the view driver take
2126 * care of resizing the displayed views. */
2127 wresize(status_win
, 1, width
);
2128 mvwin(status_win
, height
- 1, 0);
2129 wrefresh(status_win
);
2144 * Revision specification
2145 * ----------------------
2146 * This section describes various ways to specify what revisions to display
2147 * or otherwise limit the view to. tig(1) does not itself parse the described
2148 * revision options so refer to the relevant git man pages for futher
2149 * information. Relevant man pages besides git-log(1) are git-diff(1) and
2152 * You can tune the interaction with git by making use of the options
2153 * explained in this section. For example, by configuring the environment
2154 * variables described in the <<view-commands, "View commands">> section.
2156 * Limit by path name
2157 * ~~~~~~~~~~~~~~~~~~
2158 * If you are interested only in those revisions that made changes to a
2159 * specific file (or even several files) list the files like this:
2161 * $ tig log Makefile
2163 * To avoid ambiguity with repository references such as tag name, be sure
2164 * to separate file names from other git options using "\--". So if you
2165 * have a file named 'master' it will clash with the reference named
2166 * 'master', and thus you will have to use:
2168 * $ tig log -- master
2170 * NOTE: For the main view, avoiding ambiguity will in some cases require
2171 * you to specify two "\--" options. The first will make tig(1) stop
2172 * option processing and the latter will be passed to git log.
2174 * Limit by date or number
2175 * ~~~~~~~~~~~~~~~~~~~~~~~
2176 * To speed up interaction with git, you can limit the amount of commits
2177 * to show both for the log and main view. Either limit by date using
2178 * e.g. `--since=1.month` or limit by the number of commits using `-n400`.
2180 * If you are only interested in changed that happened between two dates
2183 * $ tig -- --after=May.5th --before=2006-05-16.15:44
2185 * NOTE: The dot (".") is used as a separator instead of a space to avoid
2186 * having to quote the option value. If you prefer use `--after="May 5th"`
2187 * instead of `--after="May 5th"`.
2189 * Limiting by commit ranges
2190 * ~~~~~~~~~~~~~~~~~~~~~~~~~
2191 * Alternatively, commits can be limited to a specific range, such as
2192 * "all commits between 'tag-1.0' and 'tag-2.0'". For example:
2194 * $ tig log tag-1.0..tag-2.0
2196 * This way of commit limiting makes it trivial to only browse the commits
2197 * which haven't been pushed to a remote branch. Assuming 'origin' is your
2198 * upstream remote branch, using:
2200 * $ tig log origin..HEAD
2202 * will list what will be pushed to the remote branch. Optionally, the ending
2203 * 'HEAD' can be left out since it is implied.
2205 * Limiting by reachability
2206 * ~~~~~~~~~~~~~~~~~~~~~~~~
2207 * Git interprets the range specifier "tag-1.0..tag-2.0" as
2208 * "all commits reachable from 'tag-2.0' but not from 'tag-1.0'".
2209 * Where reachability refers to what commits are ancestors (or part of the
2210 * history) of the branch or tagged revision in question.
2212 * If you prefer to specify which commit to preview in this way use the
2215 * $ tig log tag-2.0 ^tag-1.0
2217 * You can think of '^' as a negation operator. Using this alternate syntax,
2218 * it is possible to further prune commits by specifying multiple branch
2221 * Combining revisions specification
2222 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2223 * Revisions options can to some degree be combined, which makes it possible
2224 * to say "show at most 20 commits from within the last month that changed
2225 * files under the Documentation/ directory."
2227 * $ tig -- --since=1.month -n20 -- Documentation/
2229 * Examining all repository references
2230 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2231 * In some cases, it can be useful to query changes across all references
2232 * in a repository. An example is to ask "did any line of development in
2233 * this repository change a particular file within the last week". This
2234 * can be accomplished using:
2236 * $ tig -- --all --since=1.week -- Makefile
2240 * Known bugs and problems:
2242 * - If the screen width is very small the main view can draw
2243 * outside the current view causing bad wrapping. Same goes
2244 * for title and status windows.
2248 * Features that should be explored.
2256 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
2258 * This program is free software; you can redistribute it and/or modify
2259 * it under the terms of the GNU General Public License as published by
2260 * the Free Software Foundation; either version 2 of the License, or
2261 * (at your option) any later version.
2266 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
2267 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
2268 * gitk(1): git repository browser written using tcl/tk,
2269 * qgit(1): git repository browser written using c++/Qt,
2270 * gitview(1): git repository browser written using python/gtk.