Rearrange things in the start of the viewer
[tig] / tig.c
CommitLineData
4a2909a7
JF
1/* Copyright (c) 2006 Jonas Fonseca <fonseca@diku.dk>
2 * See license info at the bottom. */
b801d8b2
JF
3/**
4 * TIG(1)
5 * ======
6 *
7 * NAME
8 * ----
9 * tig - text-mode interface for git
10 *
11 * SYNOPSIS
12 * --------
13 * [verse]
03a93dbb
JF
14 * tig [options]
15 * tig [options] [--] [git log options]
b76c2afc
JF
16 * tig [options] log [git log options]
17 * tig [options] diff [git diff options]
8855ada4
JF
18 * tig [options] show [git show options]
19 * tig [options] < [git command output]
b801d8b2
JF
20 *
21 * DESCRIPTION
22 * -----------
6706b2ba
JF
23 * Browse changes in a git repository. Additionally, tig(1) can also act
24 * as a pager for output of various git commands.
25 *
57bdf034 26 * When browsing repositories, tig(1) uses the underlying git commands
468876c9
JF
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.
6706b2ba 29 *
468876c9
JF
30 * Using tig(1) as a pager, it will display input from stdin and try
31 * to colorize it.
b801d8b2
JF
32 **/
33
b76c2afc 34#ifndef VERSION
8c11094f 35#define VERSION "tig-0.3"
b76c2afc
JF
36#endif
37
8855ada4
JF
38#ifndef DEBUG
39#define NDEBUG
40#endif
41
22f66b0a 42#include <assert.h>
4c6fabc2 43#include <errno.h>
22f66b0a
JF
44#include <ctype.h>
45#include <signal.h>
b801d8b2 46#include <stdarg.h>
b801d8b2 47#include <stdio.h>
22f66b0a 48#include <stdlib.h>
b801d8b2 49#include <string.h>
6908bdbd 50#include <unistd.h>
b76c2afc 51#include <time.h>
b801d8b2
JF
52
53#include <curses.h>
b801d8b2
JF
54
55static void die(const char *err, ...);
56static void report(const char *msg, ...);
1ba2ae4b 57static void set_nonblocking_input(bool loading);
6b161b31
JF
58
59#define ABS(x) ((x) >= 0 ? (x) : -(x))
60#define MIN(x, y) ((x) < (y) ? (x) : (y))
61
62#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
63#define STRING_SIZE(x) (sizeof(x) - 1)
b76c2afc 64
2e8488b4
JF
65#define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
66#define SIZEOF_CMD 1024 /* Size of command buffer. */
b801d8b2 67
82e78006
JF
68/* This color name can be used to refer to the default term colors. */
69#define COLOR_DEFAULT (-1)
78c70acd 70
468876c9
JF
71#define TIG_HELP "(d)iff, (l)og, (m)ain, (q)uit, (h)elp, (Enter) show diff"
72
82e78006 73/* The format and size of the date column in the main view. */
4c6fabc2 74#define DATE_FORMAT "%Y-%m-%d %H:%M"
6b161b31 75#define DATE_COLS STRING_SIZE("2006-04-29 14:21 ")
4c6fabc2 76
a28bcc22 77/* The default interval between line numbers. */
4a2909a7 78#define NUMBER_INTERVAL 1
82e78006 79
6706b2ba
JF
80#define TABSIZE 8
81
a28bcc22
JF
82#define SCALE_SPLIT_VIEW(height) ((height) * 2 / 3)
83
8855ada4 84/* Some ascii-shorthands fitted into the ncurses namespace. */
a28bcc22
JF
85#define KEY_TAB '\t'
86#define KEY_RETURN '\r'
4a2909a7
JF
87#define KEY_ESC 27
88
6706b2ba 89
a28bcc22 90/* User action requests. */
4a2909a7 91enum request {
a28bcc22
JF
92 /* Offset all requests to avoid conflicts with ncurses getch values. */
93 REQ_OFFSET = KEY_MAX + 1,
94
4a2909a7 95 /* XXX: Keep the view request first and in sync with views[]. */
2e8488b4 96 REQ_VIEW_MAIN,
4a2909a7
JF
97 REQ_VIEW_DIFF,
98 REQ_VIEW_LOG,
2e8488b4 99 REQ_VIEW_HELP,
6908bdbd 100 REQ_VIEW_PAGER,
4a2909a7 101
6b161b31 102 REQ_ENTER,
03a93dbb
JF
103 REQ_QUIT,
104 REQ_PROMPT,
4a2909a7 105 REQ_SCREEN_REDRAW,
fac7db6c 106 REQ_SCREEN_RESIZE,
4a2909a7 107 REQ_SCREEN_UPDATE,
03a93dbb
JF
108 REQ_SHOW_VERSION,
109 REQ_STOP_LOADING,
4a2909a7 110 REQ_TOGGLE_LINE_NUMBERS,
03a93dbb 111 REQ_VIEW_NEXT,
4a2909a7
JF
112
113 REQ_MOVE_UP,
6706b2ba 114 REQ_MOVE_UP_ENTER,
4a2909a7 115 REQ_MOVE_DOWN,
6706b2ba 116 REQ_MOVE_DOWN_ENTER,
4a2909a7
JF
117 REQ_MOVE_PAGE_UP,
118 REQ_MOVE_PAGE_DOWN,
119 REQ_MOVE_FIRST_LINE,
120 REQ_MOVE_LAST_LINE,
121
122 REQ_SCROLL_LINE_UP,
123 REQ_SCROLL_LINE_DOWN,
124 REQ_SCROLL_PAGE_UP,
125 REQ_SCROLL_PAGE_DOWN,
126};
127
c34d9c9f 128struct ref {
468876c9
JF
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? */
c34d9c9f
JF
133};
134
4c6fabc2 135struct commit {
a28bcc22 136 char id[41]; /* SHA1 ID. */
6b161b31 137 char title[75]; /* The first line of the commit message. */
a28bcc22
JF
138 char author[75]; /* The author of the commit. */
139 struct tm time; /* Date from the author ident. */
c34d9c9f 140 struct ref **refs; /* Repository references; tags & branch heads. */
4c6fabc2
JF
141};
142
6706b2ba 143
03a93dbb
JF
144/*
145 * String helpers
146 */
78c70acd 147
82e78006 148static inline void
4685845e 149string_ncopy(char *dst, const char *src, int dstlen)
82e78006
JF
150{
151 strncpy(dst, src, dstlen - 1);
152 dst[dstlen - 1] = 0;
03a93dbb 153
82e78006
JF
154}
155
156/* Shorthand for safely copying into a fixed buffer. */
157#define string_copy(dst, src) \
158 string_ncopy(dst, src, sizeof(dst))
159
6706b2ba 160
03a93dbb
JF
161/* Shell quoting
162 *
163 * NOTE: The following is a slightly modified copy of the git project's shell
164 * quoting routines found in the quote.c file.
165 *
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
169 *
170 * E.g.
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'
176 */
177
178static size_t
179sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
180{
181 char c;
182
a9274b95 183#define BUFPUT(x) do { if (bufsize < SIZEOF_CMD) buf[bufsize++] = (x); } while (0)
03a93dbb
JF
184
185 BUFPUT('\'');
186 while ((c = *src++)) {
187 if (c == '\'' || c == '!') {
188 BUFPUT('\'');
189 BUFPUT('\\');
190 BUFPUT(c);
191 BUFPUT('\'');
192 } else {
193 BUFPUT(c);
194 }
195 }
196 BUFPUT('\'');
197
198 return bufsize;
199}
200
82e78006 201
b76c2afc
JF
202/**
203 * OPTIONS
204 * -------
205 **/
206
6706b2ba
JF
207/* Option and state variables. */
208static bool opt_line_number = FALSE;
2e8488b4 209static int opt_num_interval = NUMBER_INTERVAL;
6706b2ba 210static int opt_tab_size = TABSIZE;
6b161b31 211static enum request opt_request = REQ_VIEW_MAIN;
03a93dbb 212static char opt_cmd[SIZEOF_CMD] = "";
6908bdbd 213static FILE *opt_pipe = NULL;
b76c2afc 214
b76c2afc 215/* Returns the index of log or diff command or -1 to exit. */
8855ada4 216static bool
b76c2afc
JF
217parse_options(int argc, char *argv[])
218{
219 int i;
220
221 for (i = 1; i < argc; i++) {
222 char *opt = argv[i];
223
224 /**
b76c2afc 225 * -l::
1ba2ae4b 226 * Start up in log view using the internal log command.
b76c2afc 227 **/
6b161b31 228 if (!strcmp(opt, "-l")) {
4a2909a7 229 opt_request = REQ_VIEW_LOG;
6b161b31
JF
230 continue;
231 }
b76c2afc
JF
232
233 /**
234 * -d::
1ba2ae4b 235 * Start up in diff view using the internal diff command.
b76c2afc 236 **/
6b161b31 237 if (!strcmp(opt, "-d")) {
4a2909a7 238 opt_request = REQ_VIEW_DIFF;
6b161b31
JF
239 continue;
240 }
b76c2afc
JF
241
242 /**
2e8488b4 243 * -n[INTERVAL], --line-number[=INTERVAL]::
b76c2afc 244 * Prefix line numbers in log and diff view.
2e8488b4 245 * Optionally, with interval different than each line.
b76c2afc 246 **/
6b161b31 247 if (!strncmp(opt, "-n", 2) ||
8855ada4 248 !strncmp(opt, "--line-number", 13)) {
2e8488b4
JF
249 char *num = opt;
250
251 if (opt[1] == 'n') {
252 num = opt + 2;
253
254 } else if (opt[STRING_SIZE("--line-number")] == '=') {
255 num = opt + STRING_SIZE("--line-number=");
256 }
257
258 if (isdigit(*num))
259 opt_num_interval = atoi(num);
260
6b161b31
JF
261 opt_line_number = TRUE;
262 continue;
263 }
b76c2afc
JF
264
265 /**
6706b2ba
JF
266 * -t[NSPACES], --tab-size[=NSPACES]::
267 * Set the number of spaces tabs should be expanded to.
268 **/
269 if (!strncmp(opt, "-t", 2) ||
270 !strncmp(opt, "--tab-size", 10)) {
271 char *num = opt;
272
273 if (opt[1] == 't') {
274 num = opt + 2;
275
276 } else if (opt[STRING_SIZE("--tab-size")] == '=') {
277 num = opt + STRING_SIZE("--tab-size=");
278 }
279
280 if (isdigit(*num))
281 opt_tab_size = MIN(atoi(num), TABSIZE);
282 continue;
283 }
284
285 /**
b76c2afc
JF
286 * -v, --version::
287 * Show version and exit.
288 **/
6b161b31 289 if (!strcmp(opt, "-v") ||
8855ada4 290 !strcmp(opt, "--version")) {
b76c2afc 291 printf("tig version %s\n", VERSION);
8855ada4 292 return FALSE;
6b161b31 293 }
b76c2afc
JF
294
295 /**
03a93dbb 296 * \--::
0721c53a 297 * End of tig(1) options. Useful when specifying command
d44f34bf 298 * options for the main view. Example:
03a93dbb 299 *
889cb462 300 * $ tig -- --since=1.month
b76c2afc 301 **/
6908bdbd
JF
302 if (!strcmp(opt, "--")) {
303 i++;
304 break;
305 }
03a93dbb 306
8855ada4 307 /**
d44f34bf 308 * log [git log options]::
8855ada4
JF
309 * Open log view using the given git log options.
310 *
d44f34bf 311 * diff [git diff options]::
8855ada4
JF
312 * Open diff view using the given git diff options.
313 *
d44f34bf 314 * show [git show options]::
8855ada4
JF
315 * Open diff view using the given git show options.
316 **/
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;
322 break;
323 }
324
d44f34bf 325 /**
57bdf034 326 * [git log options]::
d44f34bf
JF
327 * tig(1) will stop the option parsing when the first
328 * command line parameter not starting with "-" is
57bdf034
JF
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:
889cb462 332 *
03a93dbb 333 * $ tig tag-1.0..HEAD
d44f34bf 334 **/
03a93dbb 335 if (opt[0] && opt[0] != '-')
6908bdbd 336 break;
6b161b31
JF
337
338 die("unknown command '%s'", opt);
b76c2afc
JF
339 }
340
6908bdbd 341 if (!isatty(STDIN_FILENO)) {
8855ada4
JF
342 /**
343 * Pager mode
344 * ~~~~~~~~~~
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.
348 *
349 * Example on how to colorize the output of git-show(1):
350 *
351 * $ git show | tig
352 **/
6908bdbd
JF
353 opt_request = REQ_VIEW_PAGER;
354 opt_pipe = stdin;
355
356 } else if (i < argc) {
357 size_t buf_size;
358
8855ada4
JF
359 /**
360 * Git command options
361 * ~~~~~~~~~~~~~~~~~~~
8855ada4
JF
362 * All git command options specified on the command line will
363 * be passed to the given command and all will be shell quoted
468876c9 364 * before they are passed to the shell.
8855ada4 365 *
468876c9
JF
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.
8855ada4
JF
369 *
370 * Example on how to open the log view and show both author and
371 * committer information:
372 *
373 * $ tig log --pretty=fuller
468876c9 374 *
0721c53a
JF
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.
8855ada4
JF
379 **/
380
6908bdbd 381 if (opt_request == REQ_VIEW_MAIN)
8855ada4
JF
382 /* XXX: This is vulnerable to the user overriding
383 * options required for the main view parser. */
6908bdbd
JF
384 string_copy(opt_cmd, "git log --stat --pretty=raw");
385 else
386 string_copy(opt_cmd, "git");
387 buf_size = strlen(opt_cmd);
388
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++]);
392 }
393
394 if (buf_size >= sizeof(opt_cmd))
395 die("command too long");
396
397 opt_cmd[buf_size] = 0;
398
399 }
400
8855ada4 401 return TRUE;
b76c2afc
JF
402}
403
404
405/*
406 * Line-oriented content detection.
6b161b31
JF
407 */
408
2e8488b4 409#define LINE_INFO \
6b161b31
JF
410/* Line type String to match Foreground Background Attributes
411 * --------- --------------- ---------- ---------- ---------- */ \
a28bcc22
JF
412/* Diff markup */ \
413LINE(DIFF, "diff --git ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
8855ada4 414LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
a28bcc22
JF
415LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
416LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
417LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
3af8774e
JF
418LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
419LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
a28bcc22
JF
420LINE(DIFF_COPY, "copy ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
421LINE(DIFF_RENAME, "rename ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
422LINE(DIFF_SIM, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
423LINE(DIFF_DISSIM, "dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
424/* Pretty print commit header */ \
6908bdbd 425LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
8855ada4 426LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
6908bdbd
JF
427LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
428LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
8855ada4
JF
429LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
430LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
a28bcc22
JF
431/* Raw commit header */ \
432LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
433LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
434LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
8855ada4 435LINE(AUTHOR, "author ", COLOR_CYAN, COLOR_DEFAULT, 0), \
a28bcc22
JF
436LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
437/* Misc */ \
438LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
439LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
440/* UI colors */ \
441LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
442LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
443LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
6b161b31
JF
444LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
445LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
a28bcc22
JF
446LINE(MAIN_DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
447LINE(MAIN_AUTHOR, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
448LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
c34d9c9f
JF
449LINE(MAIN_DELIM, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
450LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
451LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD),
2e8488b4 452
78c70acd 453enum line_type {
2e8488b4
JF
454#define LINE(type, line, fg, bg, attr) \
455 LINE_##type
456 LINE_INFO
457#undef LINE
78c70acd
JF
458};
459
460struct line_info {
4685845e 461 const char *line; /* The start of line to match. */
2e8488b4
JF
462 int linelen; /* Size of string to match. */
463 int fg, bg, attr; /* Color and text attributes for the lines. */
78c70acd
JF
464};
465
2e8488b4 466static struct line_info line_info[] = {
78c70acd 467#define LINE(type, line, fg, bg, attr) \
a28bcc22 468 { (line), STRING_SIZE(line), (fg), (bg), (attr) }
2e8488b4
JF
469 LINE_INFO
470#undef LINE
78c70acd
JF
471};
472
2e8488b4
JF
473static enum line_type
474get_line_type(char *line)
78c70acd
JF
475{
476 int linelen = strlen(line);
a28bcc22 477 enum line_type type;
78c70acd 478
a28bcc22 479 for (type = 0; type < ARRAY_SIZE(line_info); type++)
2e8488b4 480 /* Case insensitive search matches Signed-off-by lines better. */
a28bcc22
JF
481 if (linelen >= line_info[type].linelen &&
482 !strncasecmp(line_info[type].line, line, line_info[type].linelen))
483 return type;
78c70acd 484
2e8488b4 485 return LINE_DEFAULT;
78c70acd
JF
486}
487
2e8488b4 488static inline int
78c70acd
JF
489get_line_attr(enum line_type type)
490{
2e8488b4
JF
491 assert(type < ARRAY_SIZE(line_info));
492 return COLOR_PAIR(type) | line_info[type].attr;
78c70acd
JF
493}
494
495static void
496init_colors(void)
497{
82e78006
JF
498 int default_bg = COLOR_BLACK;
499 int default_fg = COLOR_WHITE;
a28bcc22 500 enum line_type type;
78c70acd
JF
501
502 start_color();
503
504 if (use_default_colors() != ERR) {
82e78006
JF
505 default_bg = -1;
506 default_fg = -1;
78c70acd
JF
507 }
508
a28bcc22
JF
509 for (type = 0; type < ARRAY_SIZE(line_info); type++) {
510 struct line_info *info = &line_info[type];
82e78006
JF
511 int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
512 int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
78c70acd 513
a28bcc22 514 init_pair(type, fg, bg);
78c70acd
JF
515 }
516}
517
518
1ba2ae4b
JF
519/**
520 * ENVIRONMENT VARIABLES
521 * ---------------------
c34d9c9f
JF
522 * Several options related to the interface with git can be configured
523 * via environment options.
524 *
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 ']':
529 *
468876c9 530 * 2006-03-26 19:42 Petr Baudis | [cogito-0.17.1] Cogito 0.17.1
c34d9c9f
JF
531 *
532 * If you want to filter out certain directories under `.git/refs/`, say
533 * `tmp` you can do it by setting the following variable:
534 *
6706b2ba 535 * $ TIG_LS_REMOTE="git ls-remote . | sed /\/tmp\//d" tig
c34d9c9f
JF
536 *
537 * Or set the variable permanently in your environment.
538 *
539 * TIG_LS_REMOTE::
6706b2ba
JF
540 * Set command for retrieving all repository references. The command
541 * should output data in the same format as git-ls-remote(1).
c34d9c9f
JF
542 **/
543
544#define TIG_LS_REMOTE \
1b4b0bd9 545 "git ls-remote . 2>/dev/null"
c34d9c9f
JF
546
547/**
0721c53a 548 * [[view-commands]]
c34d9c9f
JF
549 * View commands
550 * ~~~~~~~~~~~~~
1ba2ae4b 551 * It is possible to alter which commands are used for the different views.
6706b2ba
JF
552 * If for example you prefer commits in the main view to be sorted by date
553 * and only show 500 commits, use:
1ba2ae4b
JF
554 *
555 * $ TIG_MAIN_CMD="git log --date-order -n500 --pretty=raw %s" tig
556 *
557 * Or set the variable permanently in your environment.
558 *
559 * Notice, how `%s` is used to specify the commit reference. There can
560 * be a maximum of 5 `%s` ref specifications.
561 *
562 * TIG_DIFF_CMD::
563 * The command used for the diff view. By default, git show is used
564 * as a backend.
565 *
566 * TIG_LOG_CMD::
6706b2ba
JF
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.
1ba2ae4b
JF
570 *
571 * TIG_MAIN_CMD::
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
574 * read that format.
575 **/
576
577#define TIG_DIFF_CMD \
578 "git show --patch-with-stat --find-copies-harder -B -C %s"
579
580#define TIG_LOG_CMD \
581 "git log --cc --stat -n100 %s"
582
583#define TIG_MAIN_CMD \
584 "git log --topo-order --stat --pretty=raw %s"
585
0721c53a 586/* ... silently ignore that the following are also exported. */
1ba2ae4b
JF
587
588#define TIG_HELP_CMD \
1b4b0bd9 589 "man tig 2>/dev/null"
1ba2ae4b
JF
590
591#define TIG_PAGER_CMD \
592 ""
593
c2124ccd 594
468876c9
JF
595/**
596 * The viewer
597 * ----------
c2124ccd
JF
598 * The display consists of a status window on the last line of the screen and
599 * one or more views. The default is to only show one view at the time but it
600 * is possible to split both the main and log view to also show the commit
601 * diff.
468876c9 602 *
c2124ccd
JF
603 * If you are in the log view and press 'Enter' when the current line is a
604 * commit line, such as:
468876c9 605 *
c2124ccd 606 * commit 4d55caff4cc89335192f3e566004b4ceef572521
468876c9 607 *
c2124ccd
JF
608 * You will split the view so that the log view is displayed in the top window
609 * and the diff view in the bottom window. You can switch between the two
610 * views by pressing 'Tab'. To maximize the log view again, simply press 'l'.
611 **/
612
613struct view;
614
615/* The display array of active views and the index of the current view. */
616static struct view *display[2];
617static unsigned int current_view;
618
619#define foreach_view(view, i) \
620 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
621
622
623/**
624 * Current head and commit ID
625 * ~~~~~~~~~~~~~~~~~~~~~~~~~~
626 * The viewer keeps track of both what head and commit ID you are currently
627 * viewing. The commit ID will follow the cursor line and change everytime time
628 * you highlight a different commit. Whenever you reopen the diff view it
629 * will be reloaded, if the commit ID changed.
468876c9 630 *
c2124ccd
JF
631 * The head ID is used when opening the main and log view to indicate from
632 * what revision to show history.
468876c9 633 **/
b801d8b2 634
c2124ccd
JF
635static char ref_commit[SIZEOF_REF] = "HEAD";
636static char ref_head[SIZEOF_REF] = "HEAD";
637
638
b801d8b2 639struct view {
03a93dbb 640 const char *name; /* View name */
4685845e
TH
641 const char *cmd_fmt; /* Default command line format */
642 const char *cmd_env; /* Command line set via environment */
643 const char *id; /* Points to either of ref_{head,commit} */
03a93dbb 644 size_t objsize; /* Size of objects in the line index */
6b161b31
JF
645
646 struct view_ops {
6734f6b9
JF
647 /* What type of content being displayed. Used in the
648 * title bar. */
4685845e 649 const char *type;
8855ada4 650 /* Draw one line; @lineno must be < view->height. */
6b161b31 651 bool (*draw)(struct view *view, unsigned int lineno);
8855ada4 652 /* Read one line; updates view->line. */
6b161b31 653 bool (*read)(struct view *view, char *line);
8855ada4 654 /* Depending on view, change display based on current line. */
6b161b31
JF
655 bool (*enter)(struct view *view);
656 } *ops;
22f66b0a 657
03a93dbb 658 char cmd[SIZEOF_CMD]; /* Command buffer */
49f2b43f
JF
659 char ref[SIZEOF_REF]; /* Hovered commit reference */
660 char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */
2e8488b4 661
8855ada4
JF
662 int height, width; /* The width and height of the main window */
663 WINDOW *win; /* The main window */
664 WINDOW *title; /* The title window living below the main window */
b801d8b2
JF
665
666 /* Navigation */
667 unsigned long offset; /* Offset of the window top */
668 unsigned long lineno; /* Current line number */
669
670 /* Buffering */
671 unsigned long lines; /* Total number of lines */
8855ada4
JF
672 void **line; /* Line index; each line contains user data */
673 unsigned int digits; /* Number of digits in the lines member. */
b801d8b2
JF
674
675 /* Loading */
676 FILE *pipe;
2e8488b4 677 time_t start_time;
b801d8b2
JF
678};
679
6b161b31
JF
680static struct view_ops pager_ops;
681static struct view_ops main_ops;
a28bcc22 682
1ba2ae4b
JF
683#define VIEW_STR(name, cmd, env, ref, objsize, ops) \
684 { name, cmd, #env, ref, objsize, ops }
685
686#define VIEW_(id, name, ops, ref, objsize) \
687 VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, objsize, ops)
688
c2124ccd
JF
689/**
690 * Views
691 * ~~~~~
692 * tig(1) presents various 'views' of a repository. Each view is based on output
693 * from an external command, most often 'git log', 'git diff', or 'git show'.
694 *
695 * The main view::
696 * Is the default view, and it shows a one line summary of each commit
697 * in the chosen list of revisions. The summary includes commit date,
698 * author, and the first line of the log message. Additionally, any
699 * repository references, such as tags, will be shown.
700 *
701 * The log view::
702 * Presents a more rich view of the revision log showing the whole log
703 * message and the diffstat.
704 *
705 * The diff view::
706 * Shows either the diff of the current working tree, that is, what
707 * has changed since the last commit, or the commit diff complete
708 * with log message, diffstat and diff.
709 *
710 * The pager view::
711 * Is used for displaying both input from stdin and output from git
712 * commands entered in the internal prompt.
713 *
714 * The help view::
715 * Displays the information from the tig(1) man page. For the help view
716 * to work you need to have the tig(1) man page installed.
717 **/
718
b801d8b2 719static struct view views[] = {
1ba2ae4b
JF
720 VIEW_(MAIN, "main", &main_ops, ref_head, sizeof(struct commit)),
721 VIEW_(DIFF, "diff", &pager_ops, ref_commit, sizeof(char)),
722 VIEW_(LOG, "log", &pager_ops, ref_head, sizeof(char)),
c2124ccd 723 VIEW_(HELP, "help", &pager_ops, "static", sizeof(char)),
1ba2ae4b 724 VIEW_(PAGER, "pager", &pager_ops, "static", sizeof(char)),
b801d8b2
JF
725};
726
a28bcc22
JF
727#define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
728
4c6fabc2 729
b801d8b2 730static void
82e78006 731redraw_view_from(struct view *view, int lineno)
b801d8b2 732{
82e78006 733 assert(0 <= lineno && lineno < view->height);
b801d8b2 734
82e78006 735 for (; lineno < view->height; lineno++) {
6b161b31 736 if (!view->ops->draw(view, lineno))
fd85fef1 737 break;
b801d8b2
JF
738 }
739
740 redrawwin(view->win);
741 wrefresh(view->win);
742}
743
b76c2afc 744static void
82e78006
JF
745redraw_view(struct view *view)
746{
747 wclear(view->win);
748 redraw_view_from(view, 0);
749}
750
c2124ccd
JF
751
752/**
753 * Title windows
754 * ~~~~~~~~~~~~~
755 * Each view has a title window which shows the name of the view, current
756 * commit ID if available, and where the view is positioned:
757 *
758 * [main] c622eefaa485995320bc743431bae0d497b1d875 - commit 1 of 61 (1%)
759 *
760 * By default, the title of the current view is highlighted using bold font.
761 **/
762
6b161b31 763static void
81030ec8
JF
764update_view_title(struct view *view)
765{
766 if (view == display[current_view])
767 wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS));
768 else
769 wbkgdset(view->title, get_line_attr(LINE_TITLE_BLUR));
770
771 werase(view->title);
772 wmove(view->title, 0, 0);
773
81030ec8
JF
774 if (*view->ref)
775 wprintw(view->title, "[%s] %s", view->name, view->ref);
776 else
777 wprintw(view->title, "[%s]", view->name);
778
779 if (view->lines) {
780 wprintw(view->title, " - %s %d of %d (%d%%)",
781 view->ops->type,
782 view->lineno + 1,
783 view->lines,
784 (view->lineno + 1) * 100 / view->lines);
785 }
786
787 wrefresh(view->title);
788}
789
790static void
6b161b31 791resize_display(void)
b76c2afc 792{
03a93dbb 793 int offset, i;
6b161b31
JF
794 struct view *base = display[0];
795 struct view *view = display[1] ? display[1] : display[0];
b76c2afc 796
6b161b31 797 /* Setup window dimensions */
b76c2afc 798
03a93dbb 799 getmaxyx(stdscr, base->height, base->width);
b76c2afc 800
6b161b31 801 /* Make room for the status window. */
03a93dbb 802 base->height -= 1;
6b161b31
JF
803
804 if (view != base) {
03a93dbb
JF
805 /* Horizontal split. */
806 view->width = base->width;
6b161b31
JF
807 view->height = SCALE_SPLIT_VIEW(base->height);
808 base->height -= view->height;
809
810 /* Make room for the title bar. */
811 view->height -= 1;
812 }
813
814 /* Make room for the title bar. */
815 base->height -= 1;
816
817 offset = 0;
818
819 foreach_view (view, i) {
4d55caff
JF
820 /* Keep the height of all view->win windows one larger than is
821 * required so that the cursor can wrap-around on the last line
822 * without scrolling the window. */
b76c2afc 823 if (!view->win) {
6706b2ba 824 view->win = newwin(view->height + 1, 0, offset, 0);
6b161b31
JF
825 if (!view->win)
826 die("Failed to create %s view", view->name);
827
828 scrollok(view->win, TRUE);
829
830 view->title = newwin(1, 0, offset + view->height, 0);
831 if (!view->title)
832 die("Failed to create title window");
833
834 } else {
6706b2ba 835 wresize(view->win, view->height + 1, view->width);
6b161b31
JF
836 mvwin(view->win, offset, 0);
837 mvwin(view->title, offset + view->height, 0);
838 wrefresh(view->win);
a28bcc22 839 }
a28bcc22 840
6b161b31 841 offset += view->height + 1;
b76c2afc 842 }
6b161b31 843}
b76c2afc 844
6b161b31 845static void
20bb5e18
JF
846redraw_display(void)
847{
848 struct view *view;
849 int i;
850
851 foreach_view (view, i) {
852 redraw_view(view);
853 update_view_title(view);
854 }
855}
856
857
2e8488b4
JF
858/*
859 * Navigation
860 */
861
4a2909a7 862/* Scrolling backend */
b801d8b2 863static void
4a2909a7 864do_scroll_view(struct view *view, int lines)
b801d8b2 865{
fd85fef1
JF
866 /* The rendering expects the new offset. */
867 view->offset += lines;
868
869 assert(0 <= view->offset && view->offset < view->lines);
870 assert(lines);
b801d8b2 871
82e78006 872 /* Redraw the whole screen if scrolling is pointless. */
4c6fabc2 873 if (view->height < ABS(lines)) {
b76c2afc
JF
874 redraw_view(view);
875
876 } else {
22f66b0a 877 int line = lines > 0 ? view->height - lines : 0;
82e78006 878 int end = line + ABS(lines);
fd85fef1
JF
879
880 wscrl(view->win, lines);
881
22f66b0a 882 for (; line < end; line++) {
6b161b31 883 if (!view->ops->draw(view, line))
fd85fef1
JF
884 break;
885 }
886 }
887
888 /* Move current line into the view. */
889 if (view->lineno < view->offset) {
890 view->lineno = view->offset;
6b161b31 891 view->ops->draw(view, 0);
fd85fef1
JF
892
893 } else if (view->lineno >= view->offset + view->height) {
6706b2ba
JF
894 if (view->lineno == view->offset + view->height) {
895 /* Clear the hidden line so it doesn't show if the view
896 * is scrolled up. */
897 wmove(view->win, view->height, 0);
898 wclrtoeol(view->win);
899 }
fd85fef1 900 view->lineno = view->offset + view->height - 1;
6b161b31 901 view->ops->draw(view, view->lineno - view->offset);
fd85fef1
JF
902 }
903
4c6fabc2 904 assert(view->offset <= view->lineno && view->lineno < view->lines);
fd85fef1
JF
905
906 redrawwin(view->win);
907 wrefresh(view->win);
9d3f5834 908 report("");
fd85fef1 909}
78c70acd 910
4a2909a7 911/* Scroll frontend */
fd85fef1 912static void
6b161b31 913scroll_view(struct view *view, enum request request)
fd85fef1
JF
914{
915 int lines = 1;
b801d8b2
JF
916
917 switch (request) {
4a2909a7 918 case REQ_SCROLL_PAGE_DOWN:
fd85fef1 919 lines = view->height;
4a2909a7 920 case REQ_SCROLL_LINE_DOWN:
b801d8b2 921 if (view->offset + lines > view->lines)
bde3653a 922 lines = view->lines - view->offset;
b801d8b2 923
fd85fef1 924 if (lines == 0 || view->offset + view->height >= view->lines) {
eb98559e 925 report("Cannot scroll beyond the last line");
b801d8b2
JF
926 return;
927 }
928 break;
929
4a2909a7 930 case REQ_SCROLL_PAGE_UP:
fd85fef1 931 lines = view->height;
4a2909a7 932 case REQ_SCROLL_LINE_UP:
b801d8b2
JF
933 if (lines > view->offset)
934 lines = view->offset;
935
936 if (lines == 0) {
eb98559e 937 report("Cannot scroll beyond the first line");
b801d8b2
JF
938 return;
939 }
940
fd85fef1 941 lines = -lines;
b801d8b2 942 break;
03a93dbb 943
6b161b31
JF
944 default:
945 die("request %d not handled in switch", request);
b801d8b2
JF
946 }
947
4a2909a7 948 do_scroll_view(view, lines);
fd85fef1 949}
b801d8b2 950
4a2909a7 951/* Cursor moving */
fd85fef1 952static void
6b161b31 953move_view(struct view *view, enum request request)
fd85fef1
JF
954{
955 int steps;
b801d8b2 956
fd85fef1 957 switch (request) {
4a2909a7 958 case REQ_MOVE_FIRST_LINE:
78c70acd
JF
959 steps = -view->lineno;
960 break;
961
4a2909a7 962 case REQ_MOVE_LAST_LINE:
78c70acd
JF
963 steps = view->lines - view->lineno - 1;
964 break;
965
4a2909a7 966 case REQ_MOVE_PAGE_UP:
78c70acd
JF
967 steps = view->height > view->lineno
968 ? -view->lineno : -view->height;
969 break;
970
4a2909a7 971 case REQ_MOVE_PAGE_DOWN:
78c70acd
JF
972 steps = view->lineno + view->height >= view->lines
973 ? view->lines - view->lineno - 1 : view->height;
974 break;
975
4a2909a7 976 case REQ_MOVE_UP:
6706b2ba 977 case REQ_MOVE_UP_ENTER:
fd85fef1
JF
978 steps = -1;
979 break;
b801d8b2 980
4a2909a7 981 case REQ_MOVE_DOWN:
6706b2ba 982 case REQ_MOVE_DOWN_ENTER:
fd85fef1
JF
983 steps = 1;
984 break;
6b161b31
JF
985
986 default:
987 die("request %d not handled in switch", request);
78c70acd 988 }
b801d8b2 989
4c6fabc2 990 if (steps <= 0 && view->lineno == 0) {
eb98559e 991 report("Cannot move beyond the first line");
78c70acd 992 return;
b801d8b2 993
6908bdbd 994 } else if (steps >= 0 && view->lineno + 1 >= view->lines) {
eb98559e 995 report("Cannot move beyond the last line");
78c70acd 996 return;
fd85fef1
JF
997 }
998
4c6fabc2 999 /* Move the current line */
fd85fef1 1000 view->lineno += steps;
4c6fabc2
JF
1001 assert(0 <= view->lineno && view->lineno < view->lines);
1002
1003 /* Repaint the old "current" line if we be scrolling */
2e8488b4
JF
1004 if (ABS(steps) < view->height) {
1005 int prev_lineno = view->lineno - steps - view->offset;
1006
1007 wmove(view->win, prev_lineno, 0);
1008 wclrtoeol(view->win);
03a93dbb 1009 view->ops->draw(view, prev_lineno);
2e8488b4 1010 }
fd85fef1 1011
4c6fabc2 1012 /* Check whether the view needs to be scrolled */
fd85fef1
JF
1013 if (view->lineno < view->offset ||
1014 view->lineno >= view->offset + view->height) {
1015 if (steps < 0 && -steps > view->offset) {
1016 steps = -view->offset;
b76c2afc
JF
1017
1018 } else if (steps > 0) {
1019 if (view->lineno == view->lines - 1 &&
1020 view->lines > view->height) {
1021 steps = view->lines - view->offset - 1;
1022 if (steps >= view->height)
1023 steps -= view->height - 1;
1024 }
b801d8b2 1025 }
78c70acd 1026
4a2909a7 1027 do_scroll_view(view, steps);
fd85fef1 1028 return;
b801d8b2
JF
1029 }
1030
4c6fabc2 1031 /* Draw the current line */
6b161b31 1032 view->ops->draw(view, view->lineno - view->offset);
fd85fef1 1033
b801d8b2
JF
1034 redrawwin(view->win);
1035 wrefresh(view->win);
9d3f5834 1036 report("");
b801d8b2
JF
1037}
1038
b801d8b2 1039
2e8488b4
JF
1040/*
1041 * Incremental updating
1042 */
b801d8b2 1043
03a93dbb 1044static bool
b801d8b2
JF
1045begin_update(struct view *view)
1046{
4685845e 1047 const char *id = view->id;
fd85fef1 1048
03a93dbb
JF
1049 if (opt_cmd[0]) {
1050 string_copy(view->cmd, opt_cmd);
1051 opt_cmd[0] = 0;
8855ada4
JF
1052 /* When running random commands, the view ref could have become
1053 * invalid so clear it. */
1054 view->ref[0] = 0;
03a93dbb 1055 } else {
4685845e 1056 const char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt;
1ba2ae4b
JF
1057
1058 if (snprintf(view->cmd, sizeof(view->cmd), format,
1059 id, id, id, id, id) >= sizeof(view->cmd))
03a93dbb
JF
1060 return FALSE;
1061 }
b801d8b2 1062
6908bdbd
JF
1063 /* Special case for the pager view. */
1064 if (opt_pipe) {
1065 view->pipe = opt_pipe;
1066 opt_pipe = NULL;
1067 } else {
1068 view->pipe = popen(view->cmd, "r");
1069 }
1070
2e8488b4
JF
1071 if (!view->pipe)
1072 return FALSE;
b801d8b2 1073
6b161b31 1074 set_nonblocking_input(TRUE);
b801d8b2
JF
1075
1076 view->offset = 0;
1077 view->lines = 0;
1078 view->lineno = 0;
49f2b43f 1079 string_copy(view->vid, id);
b801d8b2 1080
2e8488b4
JF
1081 if (view->line) {
1082 int i;
1083
1084 for (i = 0; i < view->lines; i++)
1085 if (view->line[i])
1086 free(view->line[i]);
1087
1088 free(view->line);
1089 view->line = NULL;
1090 }
1091
1092 view->start_time = time(NULL);
1093
b801d8b2
JF
1094 return TRUE;
1095}
1096
1097static void
1098end_update(struct view *view)
1099{
03a93dbb
JF
1100 if (!view->pipe)
1101 return;
6b161b31 1102 set_nonblocking_input(FALSE);
80ce96ea
JF
1103 if (view->pipe == stdin)
1104 fclose(view->pipe);
1105 else
1106 pclose(view->pipe);
2e8488b4 1107 view->pipe = NULL;
b801d8b2
JF
1108}
1109
03a93dbb 1110static bool
b801d8b2
JF
1111update_view(struct view *view)
1112{
1113 char buffer[BUFSIZ];
1114 char *line;
22f66b0a 1115 void **tmp;
82e78006
JF
1116 /* The number of lines to read. If too low it will cause too much
1117 * redrawing (and possible flickering), if too high responsiveness
1118 * will suffer. */
8855ada4 1119 unsigned long lines = view->height;
82e78006 1120 int redraw_from = -1;
b801d8b2
JF
1121
1122 if (!view->pipe)
1123 return TRUE;
1124
82e78006
JF
1125 /* Only redraw if lines are visible. */
1126 if (view->offset + view->height >= view->lines)
1127 redraw_from = view->lines - view->offset;
b801d8b2
JF
1128
1129 tmp = realloc(view->line, sizeof(*view->line) * (view->lines + lines));
1130 if (!tmp)
1131 goto alloc_error;
1132
1133 view->line = tmp;
1134
1135 while ((line = fgets(buffer, sizeof(buffer), view->pipe))) {
c34d9c9f 1136 int linelen = strlen(line);
b801d8b2 1137
b801d8b2
JF
1138 if (linelen)
1139 line[linelen - 1] = 0;
1140
6b161b31 1141 if (!view->ops->read(view, line))
b801d8b2 1142 goto alloc_error;
fd85fef1
JF
1143
1144 if (lines-- == 1)
1145 break;
b801d8b2
JF
1146 }
1147
8855ada4
JF
1148 {
1149 int digits;
1150
1151 lines = view->lines;
1152 for (digits = 0; lines; digits++)
1153 lines /= 10;
1154
1155 /* Keep the displayed view in sync with line number scaling. */
1156 if (digits != view->digits) {
1157 view->digits = digits;
1158 redraw_from = 0;
1159 }
1160 }
1161
82e78006
JF
1162 if (redraw_from >= 0) {
1163 /* If this is an incremental update, redraw the previous line
a28bcc22
JF
1164 * since for commits some members could have changed when
1165 * loading the main view. */
82e78006
JF
1166 if (redraw_from > 0)
1167 redraw_from--;
1168
1169 /* Incrementally draw avoids flickering. */
1170 redraw_view_from(view, redraw_from);
4c6fabc2 1171 }
b801d8b2 1172
eb98559e
JF
1173 /* Update the title _after_ the redraw so that if the redraw picks up a
1174 * commit reference in view->ref it'll be available here. */
1175 update_view_title(view);
1176
b801d8b2 1177 if (ferror(view->pipe)) {
03a93dbb 1178 report("Failed to read: %s", strerror(errno));
b801d8b2
JF
1179 goto end;
1180
1181 } else if (feof(view->pipe)) {
2e8488b4
JF
1182 time_t secs = time(NULL) - view->start_time;
1183
a28bcc22 1184 if (view == VIEW(REQ_VIEW_HELP)) {
4685845e 1185 const char *msg = TIG_HELP;
468876c9
JF
1186
1187 if (view->lines == 0) {
1188 /* Slightly ugly, but abusing view->ref keeps
1189 * the error message. */
1190 string_copy(view->ref, "No help available");
1191 msg = "The tig(1) manpage is not installed";
1192 }
1193
1194 report("%s", msg);
2e8488b4
JF
1195 goto end;
1196 }
1197
1198 report("Loaded %d lines in %ld second%s", view->lines, secs,
1199 secs == 1 ? "" : "s");
b801d8b2
JF
1200 goto end;
1201 }
1202
1203 return TRUE;
1204
1205alloc_error:
2e8488b4 1206 report("Allocation failure");
b801d8b2
JF
1207
1208end:
1209 end_update(view);
1210 return FALSE;
1211}
1212
49f2b43f
JF
1213enum open_flags {
1214 OPEN_DEFAULT = 0, /* Use default view switching. */
1215 OPEN_SPLIT = 1, /* Split current view. */
1216 OPEN_BACKGROUNDED = 2, /* Backgrounded. */
1217 OPEN_RELOAD = 4, /* Reload view even if it is the current. */
1218};
1219
6b161b31 1220static void
49f2b43f 1221open_view(struct view *prev, enum request request, enum open_flags flags)
b801d8b2 1222{
49f2b43f
JF
1223 bool backgrounded = !!(flags & OPEN_BACKGROUNDED);
1224 bool split = !!(flags & OPEN_SPLIT);
1225 bool reload = !!(flags & OPEN_RELOAD);
a28bcc22 1226 struct view *view = VIEW(request);
b801d8b2 1227 struct view *displayed;
6b161b31
JF
1228 int nviews;
1229
03a93dbb 1230 /* Cycle between displayed views and count the views. */
6b161b31 1231 foreach_view (displayed, nviews) {
03a93dbb
JF
1232 if (prev != view &&
1233 view == displayed &&
8855ada4 1234 !strcmp(view->vid, prev->vid)) {
6b161b31
JF
1235 current_view = nviews;
1236 /* Blur out the title of the previous view. */
1237 update_view_title(prev);
6734f6b9 1238 report("");
6b161b31 1239 return;
a28bcc22 1240 }
6b161b31 1241 }
b801d8b2 1242
49f2b43f 1243 if (view == prev && nviews == 1 && !reload) {
6b161b31
JF
1244 report("Already in %s view", view->name);
1245 return;
1246 }
b801d8b2 1247
8855ada4 1248 if ((reload || strcmp(view->vid, view->id)) &&
03a93dbb 1249 !begin_update(view)) {
6b161b31
JF
1250 report("Failed to load %s view", view->name);
1251 return;
1252 }
a28bcc22 1253
6b161b31
JF
1254 if (split) {
1255 display[current_view + 1] = view;
1256 if (!backgrounded)
a28bcc22 1257 current_view++;
6b161b31
JF
1258 } else {
1259 /* Maximize the current view. */
1260 memset(display, 0, sizeof(display));
1261 current_view = 0;
1262 display[current_view] = view;
a28bcc22 1263 }
b801d8b2 1264
6b161b31 1265 resize_display();
b801d8b2 1266
a8891802 1267 if (split && prev->lineno - prev->offset >= prev->height) {
03a93dbb 1268 /* Take the title line into account. */
eb98559e 1269 int lines = prev->lineno - prev->offset - prev->height + 1;
03a93dbb
JF
1270
1271 /* Scroll the view that was split if the current line is
1272 * outside the new limited view. */
1273 do_scroll_view(prev, lines);
1274 }
1275
6b161b31
JF
1276 if (prev && view != prev) {
1277 /* "Blur" the previous view. */
6706b2ba
JF
1278 if (!backgrounded)
1279 update_view_title(prev);
6b161b31
JF
1280
1281 /* Continue loading split views in the background. */
03a93dbb 1282 if (!split)
6b161b31 1283 end_update(prev);
b801d8b2
JF
1284 }
1285
03a93dbb
JF
1286 if (view->pipe) {
1287 /* Clear the old view and let the incremental updating refill
1288 * the screen. */
1289 wclear(view->win);
1290 report("Loading...");
1291 } else {
1292 redraw_view(view);
468876c9
JF
1293 if (view == VIEW(REQ_VIEW_HELP))
1294 report("%s", TIG_HELP);
1295 else
1296 report("");
03a93dbb 1297 }
6706b2ba
JF
1298
1299 /* If the view is backgrounded the above calls to report()
1300 * won't redraw the view title. */
1301 if (backgrounded)
1302 update_view_title(view);
b801d8b2
JF
1303}
1304
1305
6b161b31
JF
1306/*
1307 * User request switch noodle
1308 */
1309
b801d8b2 1310static int
6b161b31 1311view_driver(struct view *view, enum request request)
b801d8b2 1312{
b801d8b2
JF
1313 int i;
1314
1315 switch (request) {
4a2909a7
JF
1316 case REQ_MOVE_UP:
1317 case REQ_MOVE_DOWN:
1318 case REQ_MOVE_PAGE_UP:
1319 case REQ_MOVE_PAGE_DOWN:
1320 case REQ_MOVE_FIRST_LINE:
1321 case REQ_MOVE_LAST_LINE:
a28bcc22 1322 move_view(view, request);
fd85fef1
JF
1323 break;
1324
4a2909a7
JF
1325 case REQ_SCROLL_LINE_DOWN:
1326 case REQ_SCROLL_LINE_UP:
1327 case REQ_SCROLL_PAGE_DOWN:
1328 case REQ_SCROLL_PAGE_UP:
a28bcc22 1329 scroll_view(view, request);
b801d8b2
JF
1330 break;
1331
4a2909a7 1332 case REQ_VIEW_MAIN:
4a2909a7 1333 case REQ_VIEW_DIFF:
2e8488b4
JF
1334 case REQ_VIEW_LOG:
1335 case REQ_VIEW_HELP:
6908bdbd 1336 case REQ_VIEW_PAGER:
49f2b43f 1337 open_view(view, request, OPEN_DEFAULT);
b801d8b2
JF
1338 break;
1339
6706b2ba
JF
1340 case REQ_MOVE_UP_ENTER:
1341 case REQ_MOVE_DOWN_ENTER:
1342 move_view(view, request);
1343 /* Fall-through */
1344
6b161b31 1345 case REQ_ENTER:
6908bdbd
JF
1346 if (!view->lines) {
1347 report("Nothing to enter");
1348 break;
1349 }
6b161b31
JF
1350 return view->ops->enter(view);
1351
03a93dbb
JF
1352 case REQ_VIEW_NEXT:
1353 {
1354 int nviews = display[1] ? 2 : 1;
1355 int next_view = (current_view + 1) % nviews;
1356
1357 if (next_view == current_view) {
1358 report("Only one view is displayed");
1359 break;
1360 }
1361
1362 current_view = next_view;
1363 /* Blur out the title of the previous view. */
1364 update_view_title(view);
6734f6b9 1365 report("");
03a93dbb
JF
1366 break;
1367 }
4a2909a7 1368 case REQ_TOGGLE_LINE_NUMBERS:
b76c2afc 1369 opt_line_number = !opt_line_number;
20bb5e18 1370 redraw_display();
b801d8b2
JF
1371 break;
1372
03a93dbb 1373 case REQ_PROMPT:
8855ada4 1374 /* Always reload^Wrerun commands from the prompt. */
49f2b43f 1375 open_view(view, opt_request, OPEN_RELOAD);
03a93dbb
JF
1376 break;
1377
4a2909a7 1378 case REQ_STOP_LOADING:
03a93dbb 1379 foreach_view (view, i) {
2e8488b4 1380 if (view->pipe)
6706b2ba 1381 report("Stopped loaded the %s view", view->name),
03a93dbb
JF
1382 end_update(view);
1383 }
b801d8b2
JF
1384 break;
1385
4a2909a7 1386 case REQ_SHOW_VERSION:
2e8488b4 1387 report("Version: %s", VERSION);
b801d8b2
JF
1388 return TRUE;
1389
fac7db6c
JF
1390 case REQ_SCREEN_RESIZE:
1391 resize_display();
1392 /* Fall-through */
4a2909a7 1393 case REQ_SCREEN_REDRAW:
20bb5e18 1394 redraw_display();
4a2909a7
JF
1395 break;
1396
1397 case REQ_SCREEN_UPDATE:
b801d8b2
JF
1398 doupdate();
1399 return TRUE;
1400
1401 case REQ_QUIT:
1402 return FALSE;
1403
1404 default:
2e8488b4 1405 /* An unknown key will show most commonly used commands. */
468876c9 1406 report("Unknown key, press 'h' for help");
b801d8b2
JF
1407 return TRUE;
1408 }
1409
1410 return TRUE;
1411}
1412
1413
1414/*
6b161b31 1415 * View backend handlers
b801d8b2
JF
1416 */
1417
6b161b31 1418static bool
22f66b0a 1419pager_draw(struct view *view, unsigned int lineno)
b801d8b2 1420{
78c70acd 1421 enum line_type type;
b801d8b2 1422 char *line;
4c6fabc2 1423 int linelen;
78c70acd 1424 int attr;
b801d8b2 1425
fd85fef1
JF
1426 if (view->offset + lineno >= view->lines)
1427 return FALSE;
1428
b801d8b2 1429 line = view->line[view->offset + lineno];
78c70acd 1430 type = get_line_type(line);
b801d8b2 1431
6706b2ba
JF
1432 wmove(view->win, lineno, 0);
1433
fd85fef1 1434 if (view->offset + lineno == view->lineno) {
8855ada4 1435 if (type == LINE_COMMIT) {
03a93dbb
JF
1436 string_copy(view->ref, line + 7);
1437 string_copy(ref_commit, view->ref);
1438 }
8855ada4 1439
78c70acd 1440 type = LINE_CURSOR;
6706b2ba 1441 wchgat(view->win, -1, 0, type, NULL);
fd85fef1
JF
1442 }
1443
78c70acd 1444 attr = get_line_attr(type);
b801d8b2 1445 wattrset(view->win, attr);
b76c2afc 1446
4c6fabc2 1447 linelen = strlen(line);
4c6fabc2 1448
6706b2ba
JF
1449 if (opt_line_number || opt_tab_size < TABSIZE) {
1450 static char spaces[] = " ";
1451 int col_offset = 0, col = 0;
1452
1453 if (opt_line_number) {
1454 unsigned long real_lineno = view->offset + lineno + 1;
82e78006 1455
6706b2ba
JF
1456 if (real_lineno == 1 ||
1457 (real_lineno % opt_num_interval) == 0) {
1458 wprintw(view->win, "%.*d", view->digits, real_lineno);
8855ada4 1459
6706b2ba
JF
1460 } else {
1461 waddnstr(view->win, spaces,
1462 MIN(view->digits, STRING_SIZE(spaces)));
1463 }
1464 waddstr(view->win, ": ");
1465 col_offset = view->digits + 2;
1466 }
8855ada4 1467
6706b2ba
JF
1468 while (line && col_offset + col < view->width) {
1469 int cols_max = view->width - col_offset - col;
1470 char *text = line;
1471 int cols;
4c6fabc2 1472
b76c2afc 1473 if (*line == '\t') {
6706b2ba 1474 assert(sizeof(spaces) > TABSIZE);
b76c2afc 1475 line++;
6706b2ba
JF
1476 text = spaces;
1477 cols = opt_tab_size - (col % opt_tab_size);
82e78006 1478
b76c2afc 1479 } else {
6706b2ba
JF
1480 line = strchr(line, '\t');
1481 cols = line ? line - text : strlen(text);
b76c2afc 1482 }
6706b2ba
JF
1483
1484 waddnstr(view->win, text, MIN(cols, cols_max));
1485 col += cols;
b76c2afc 1486 }
b76c2afc
JF
1487
1488 } else {
6706b2ba 1489 int col = 0, pos = 0;
b801d8b2 1490
6706b2ba
JF
1491 for (; pos < linelen && col < view->width; pos++, col++)
1492 if (line[pos] == '\t')
1493 col += TABSIZE - (col % TABSIZE) - 1;
1494
1495 waddnstr(view->win, line, pos);
1496 }
2e8488b4 1497
b801d8b2
JF
1498 return TRUE;
1499}
1500
6b161b31 1501static bool
22f66b0a
JF
1502pager_read(struct view *view, char *line)
1503{
6706b2ba
JF
1504 /* Compress empty lines in the help view. */
1505 if (view == VIEW(REQ_VIEW_HELP) &&
1506 !*line &&
1507 view->lines &&
1508 !*((char *) view->line[view->lines - 1]))
1509 return TRUE;
1510
22f66b0a
JF
1511 view->line[view->lines] = strdup(line);
1512 if (!view->line[view->lines])
1513 return FALSE;
1514
1515 view->lines++;
1516 return TRUE;
1517}
1518
6b161b31
JF
1519static bool
1520pager_enter(struct view *view)
1521{
1522 char *line = view->line[view->lineno];
1523
1524 if (get_line_type(line) == LINE_COMMIT) {
6706b2ba
JF
1525 if (view == VIEW(REQ_VIEW_LOG))
1526 open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT | OPEN_BACKGROUNDED);
1527 else
1528 open_view(view, REQ_VIEW_DIFF, OPEN_DEFAULT);
6b161b31
JF
1529 }
1530
1531 return TRUE;
1532}
1533
6b161b31 1534static struct view_ops pager_ops = {
6734f6b9 1535 "line",
6b161b31
JF
1536 pager_draw,
1537 pager_read,
1538 pager_enter,
1539};
1540
80ce96ea 1541
c34d9c9f
JF
1542static struct ref **get_refs(char *id);
1543
6b161b31 1544static bool
22f66b0a
JF
1545main_draw(struct view *view, unsigned int lineno)
1546{
2e8488b4 1547 char buf[DATE_COLS + 1];
22f66b0a 1548 struct commit *commit;
78c70acd 1549 enum line_type type;
6706b2ba 1550 int col = 0;
b76c2afc 1551 size_t timelen;
22f66b0a
JF
1552
1553 if (view->offset + lineno >= view->lines)
1554 return FALSE;
1555
1556 commit = view->line[view->offset + lineno];
4c6fabc2
JF
1557 if (!*commit->author)
1558 return FALSE;
22f66b0a 1559
6706b2ba
JF
1560 wmove(view->win, lineno, col);
1561
22f66b0a 1562 if (view->offset + lineno == view->lineno) {
03a93dbb 1563 string_copy(view->ref, commit->id);
49f2b43f 1564 string_copy(ref_commit, view->ref);
78c70acd 1565 type = LINE_CURSOR;
6706b2ba
JF
1566 wattrset(view->win, get_line_attr(type));
1567 wchgat(view->win, -1, 0, type, NULL);
1568
78c70acd 1569 } else {
b76c2afc 1570 type = LINE_MAIN_COMMIT;
6706b2ba 1571 wattrset(view->win, get_line_attr(LINE_MAIN_DATE));
b76c2afc
JF
1572 }
1573
4c6fabc2 1574 timelen = strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time);
b76c2afc 1575 waddnstr(view->win, buf, timelen);
4c6fabc2 1576 waddstr(view->win, " ");
b76c2afc 1577
6706b2ba
JF
1578 col += DATE_COLS;
1579 wmove(view->win, lineno, col);
1580 if (type != LINE_CURSOR)
1581 wattrset(view->win, get_line_attr(LINE_MAIN_AUTHOR));
b76c2afc
JF
1582
1583 if (strlen(commit->author) > 19) {
1584 waddnstr(view->win, commit->author, 18);
6706b2ba
JF
1585 if (type != LINE_CURSOR)
1586 wattrset(view->win, get_line_attr(LINE_MAIN_DELIM));
b76c2afc
JF
1587 waddch(view->win, '~');
1588 } else {
1589 waddstr(view->win, commit->author);
22f66b0a
JF
1590 }
1591
6706b2ba
JF
1592 col += 20;
1593 if (type != LINE_CURSOR)
1594 wattrset(view->win, A_NORMAL);
1595
1596 mvwaddch(view->win, lineno, col, ACS_LTEE);
1597 wmove(view->win, lineno, col + 2);
1598 col += 2;
c34d9c9f
JF
1599
1600 if (commit->refs) {
1601 size_t i = 0;
1602
1603 do {
6706b2ba
JF
1604 if (type == LINE_CURSOR)
1605 ;
1606 else if (commit->refs[i]->tag)
c34d9c9f
JF
1607 wattrset(view->win, get_line_attr(LINE_MAIN_TAG));
1608 else
1609 wattrset(view->win, get_line_attr(LINE_MAIN_REF));
1610 waddstr(view->win, "[");
1611 waddstr(view->win, commit->refs[i]->name);
1612 waddstr(view->win, "]");
6706b2ba
JF
1613 if (type != LINE_CURSOR)
1614 wattrset(view->win, A_NORMAL);
c34d9c9f 1615 waddstr(view->win, " ");
6706b2ba 1616 col += strlen(commit->refs[i]->name) + STRING_SIZE("[] ");
c34d9c9f
JF
1617 } while (commit->refs[i++]->next);
1618 }
1619
6706b2ba
JF
1620 if (type != LINE_CURSOR)
1621 wattrset(view->win, get_line_attr(type));
1622
1623 {
1624 int titlelen = strlen(commit->title);
1625
1626 if (col + titlelen > view->width)
1627 titlelen = view->width - col;
1628
1629 waddnstr(view->win, commit->title, titlelen);
1630 }
22f66b0a
JF
1631
1632 return TRUE;
1633}
1634
4c6fabc2 1635/* Reads git log --pretty=raw output and parses it into the commit struct. */
6b161b31 1636static bool
22f66b0a
JF
1637main_read(struct view *view, char *line)
1638{
78c70acd
JF
1639 enum line_type type = get_line_type(line);
1640 struct commit *commit;
22f66b0a 1641
78c70acd
JF
1642 switch (type) {
1643 case LINE_COMMIT:
22f66b0a
JF
1644 commit = calloc(1, sizeof(struct commit));
1645 if (!commit)
1646 return FALSE;
1647
4c6fabc2 1648 line += STRING_SIZE("commit ");
b76c2afc 1649
22f66b0a 1650 view->line[view->lines++] = commit;
82e78006 1651 string_copy(commit->id, line);
c34d9c9f 1652 commit->refs = get_refs(commit->id);
78c70acd 1653 break;
22f66b0a 1654
8855ada4 1655 case LINE_AUTHOR:
b76c2afc 1656 {
4c6fabc2 1657 char *ident = line + STRING_SIZE("author ");
b76c2afc
JF
1658 char *end = strchr(ident, '<');
1659
1660 if (end) {
1661 for (; end > ident && isspace(end[-1]); end--) ;
1662 *end = 0;
1663 }
1664
1665 commit = view->line[view->lines - 1];
82e78006 1666 string_copy(commit->author, ident);
b76c2afc 1667
4c6fabc2 1668 /* Parse epoch and timezone */
b76c2afc
JF
1669 if (end) {
1670 char *secs = strchr(end + 1, '>');
1671 char *zone;
1672 time_t time;
1673
1674 if (!secs || secs[1] != ' ')
1675 break;
1676
1677 secs += 2;
1678 time = (time_t) atol(secs);
1679 zone = strchr(secs, ' ');
4c6fabc2 1680 if (zone && strlen(zone) == STRING_SIZE(" +0700")) {
b76c2afc
JF
1681 long tz;
1682
1683 zone++;
1684 tz = ('0' - zone[1]) * 60 * 60 * 10;
1685 tz += ('0' - zone[2]) * 60 * 60;
1686 tz += ('0' - zone[3]) * 60;
1687 tz += ('0' - zone[4]) * 60;
1688
1689 if (zone[0] == '-')
1690 tz = -tz;
1691
1692 time -= tz;
1693 }
1694 gmtime_r(&time, &commit->time);
1695 }
1696 break;
1697 }
78c70acd 1698 default:
2e8488b4
JF
1699 /* We should only ever end up here if there has already been a
1700 * commit line, however, be safe. */
1701 if (view->lines == 0)
1702 break;
1703
1704 /* Fill in the commit title if it has not already been set. */
78c70acd 1705 commit = view->line[view->lines - 1];
2e8488b4
JF
1706 if (commit->title[0])
1707 break;
1708
1709 /* Require titles to start with a non-space character at the
1710 * offset used by git log. */
eb98559e
JF
1711 /* FIXME: More gracefull handling of titles; append "..." to
1712 * shortened titles, etc. */
2e8488b4 1713 if (strncmp(line, " ", 4) ||
eb98559e 1714 isspace(line[4]))
82e78006
JF
1715 break;
1716
1717 string_copy(commit->title, line + 4);
22f66b0a
JF
1718 }
1719
1720 return TRUE;
1721}
1722
6b161b31
JF
1723static bool
1724main_enter(struct view *view)
b801d8b2 1725{
49f2b43f 1726 open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT | OPEN_BACKGROUNDED);
6b161b31 1727 return TRUE;
b801d8b2
JF
1728}
1729
6b161b31 1730static struct view_ops main_ops = {
6734f6b9 1731 "commit",
6b161b31
JF
1732 main_draw,
1733 main_read,
1734 main_enter,
1735};
2e8488b4 1736
c34d9c9f 1737
468876c9
JF
1738/**
1739 * KEYS
1740 * ----
1741 * Below the default key bindings are shown.
1742 **/
1743
1744struct keymap {
1745 int alias;
1746 int request;
1747};
1748
3a91b75e 1749static struct keymap keymap[] = {
468876c9
JF
1750 /**
1751 * View switching
1752 * ~~~~~~~~~~~~~~
1753 * m::
1754 * Switch to main view.
1755 * d::
1756 * Switch to diff view.
1757 * l::
1758 * Switch to log view.
1759 * p::
1760 * Switch to pager view.
1761 * h::
1762 * Show man page.
1763 * Return::
57bdf034 1764 * If on a commit line show the commit diff. Additionally, if in
468876c9
JF
1765 * main or log view this will split the view. To open the commit
1766 * diff in full size view either use 'd' or press Return twice.
1767 * Tab::
1768 * Switch to next view.
1769 **/
1770 { 'm', REQ_VIEW_MAIN },
1771 { 'd', REQ_VIEW_DIFF },
1772 { 'l', REQ_VIEW_LOG },
1773 { 'p', REQ_VIEW_PAGER },
1774 { 'h', REQ_VIEW_HELP },
1775
1776 { KEY_TAB, REQ_VIEW_NEXT },
1777 { KEY_RETURN, REQ_ENTER },
1778
1779 /**
1780 * Cursor navigation
1781 * ~~~~~~~~~~~~~~~~~
1782 * Up::
57bdf034 1783 * Move cursor one line up.
468876c9
JF
1784 * Down::
1785 * Move cursor one line down.
1786 * k::
57bdf034 1787 * Move cursor one line up and enter. When used in the main view
468876c9
JF
1788 * this will always show the diff of the current commit in the
1789 * split diff view.
1790 * j::
1791 * Move cursor one line down and enter.
1792 * PgUp::
c622eefa 1793 * b::
57bdf034 1794 * Move cursor one page up.
468876c9 1795 * PgDown::
c622eefa 1796 * Space::
468876c9
JF
1797 * Move cursor one page down.
1798 * Home::
1799 * Jump to first line.
1800 * End::
1801 * Jump to last line.
1802 **/
1803 { KEY_UP, REQ_MOVE_UP },
1804 { KEY_DOWN, REQ_MOVE_DOWN },
1805 { 'k', REQ_MOVE_UP_ENTER },
1806 { 'j', REQ_MOVE_DOWN_ENTER },
1807 { KEY_HOME, REQ_MOVE_FIRST_LINE },
1808 { KEY_END, REQ_MOVE_LAST_LINE },
1809 { KEY_NPAGE, REQ_MOVE_PAGE_DOWN },
c622eefa 1810 { ' ', REQ_MOVE_PAGE_DOWN },
468876c9 1811 { KEY_PPAGE, REQ_MOVE_PAGE_UP },
c622eefa 1812 { 'b', REQ_MOVE_PAGE_UP },
468876c9
JF
1813
1814 /**
1815 * Scrolling
1816 * ~~~~~~~~~
1817 * Insert::
1818 * Scroll view one line up.
1819 * Delete::
1820 * Scroll view one line down.
1821 * w::
1822 * Scroll view one page up.
1823 * s::
1824 * Scroll view one page down.
1825 **/
1826 { KEY_IC, REQ_SCROLL_LINE_UP },
1827 { KEY_DC, REQ_SCROLL_LINE_DOWN },
1828 { 'w', REQ_SCROLL_PAGE_UP },
1829 { 's', REQ_SCROLL_PAGE_DOWN },
1830
1831 /**
1832 * Misc
1833 * ~~~~
1834 * q::
1835 * Quit
1836 * r::
1837 * Redraw screen.
1838 * z::
1839 * Stop all background loading. This can be useful if you use
1840 * tig(1) in a repository with a long history without limiting
0721c53a 1841 * the revision log.
468876c9
JF
1842 * v::
1843 * Show version.
1844 * n::
1845 * Toggle line numbers on/off.
1846 * ':'::
1847 * Open prompt. This allows you to specify what git command
1848 * to run. Example:
1849 *
1850 * :log -p
1851 **/
1852 { 'q', REQ_QUIT },
1853 { 'z', REQ_STOP_LOADING },
1854 { 'v', REQ_SHOW_VERSION },
1855 { 'r', REQ_SCREEN_REDRAW },
1856 { 'n', REQ_TOGGLE_LINE_NUMBERS },
1857 { ':', REQ_PROMPT },
1858
1859 /* wgetch() with nodelay() enabled returns ERR when there's no input. */
1860 { ERR, REQ_SCREEN_UPDATE },
1861
1862 /* Use the ncurses SIGWINCH handler. */
1863 { KEY_RESIZE, REQ_SCREEN_RESIZE },
1864};
1865
1866static enum request
1867get_request(int key)
1868{
1869 int i;
1870
1871 for (i = 0; i < ARRAY_SIZE(keymap); i++)
1872 if (keymap[i].alias == key)
1873 return keymap[i].request;
1874
1875 return (enum request) key;
1876}
1877
1878
6b161b31
JF
1879/*
1880 * Status management
1881 */
2e8488b4 1882
8855ada4
JF
1883/* Whether or not the curses interface has been initialized. */
1884bool cursed = FALSE;
1885
6b161b31
JF
1886/* The status window is used for polling keystrokes. */
1887static WINDOW *status_win;
4a2909a7 1888
2e8488b4 1889/* Update status and title window. */
4a2909a7
JF
1890static void
1891report(const char *msg, ...)
1892{
6706b2ba
JF
1893 static bool empty = TRUE;
1894 struct view *view = display[current_view];
b76c2afc 1895
6706b2ba
JF
1896 if (!empty || *msg) {
1897 va_list args;
4a2909a7 1898
6706b2ba 1899 va_start(args, msg);
4b76734f 1900
6706b2ba
JF
1901 werase(status_win);
1902 wmove(status_win, 0, 0);
1903 if (*msg) {
1904 vwprintw(status_win, msg, args);
1905 empty = FALSE;
1906 } else {
1907 empty = TRUE;
1908 }
1909 wrefresh(status_win);
b801d8b2 1910
6706b2ba
JF
1911 va_end(args);
1912 }
1913
1914 update_view_title(view);
1915
1916 /* Move the cursor to the right-most column of the cursor line.
1917 *
1918 * XXX: This could turn out to be a bit expensive, but it ensures that
1919 * the cursor does not jump around. */
1920 if (view->lines) {
1921 wmove(view->win, view->lineno - view->offset, view->width - 1);
1922 wrefresh(view->win);
1923 }
b801d8b2
JF
1924}
1925
6b161b31
JF
1926/* Controls when nodelay should be in effect when polling user input. */
1927static void
1ba2ae4b 1928set_nonblocking_input(bool loading)
b801d8b2 1929{
6706b2ba 1930 static unsigned int loading_views;
b801d8b2 1931
6706b2ba
JF
1932 if ((loading == FALSE && loading_views-- == 1) ||
1933 (loading == TRUE && loading_views++ == 0))
1ba2ae4b 1934 nodelay(status_win, loading);
6b161b31
JF
1935}
1936
1937static void
1938init_display(void)
1939{
1940 int x, y;
b76c2afc 1941
6908bdbd
JF
1942 /* Initialize the curses library */
1943 if (isatty(STDIN_FILENO)) {
8855ada4 1944 cursed = !!initscr();
6908bdbd
JF
1945 } else {
1946 /* Leave stdin and stdout alone when acting as a pager. */
1947 FILE *io = fopen("/dev/tty", "r+");
1948
8855ada4 1949 cursed = !!newterm(NULL, io, io);
6908bdbd
JF
1950 }
1951
8855ada4
JF
1952 if (!cursed)
1953 die("Failed to initialize curses");
1954
2e8488b4
JF
1955 nonl(); /* Tell curses not to do NL->CR/NL on output */
1956 cbreak(); /* Take input chars one at a time, no wait for \n */
1957 noecho(); /* Don't echo input */
b801d8b2 1958 leaveok(stdscr, TRUE);
b801d8b2
JF
1959
1960 if (has_colors())
1961 init_colors();
1962
1963 getmaxyx(stdscr, y, x);
1964 status_win = newwin(1, 0, y - 1, 0);
1965 if (!status_win)
1966 die("Failed to create status window");
1967
1968 /* Enable keyboard mapping */
1969 keypad(status_win, TRUE);
78c70acd 1970 wbkgdset(status_win, get_line_attr(LINE_STATUS));
6b161b31
JF
1971}
1972
c34d9c9f
JF
1973
1974/*
1975 * Repository references
1976 */
1977
1978static struct ref *refs;
3a91b75e 1979static size_t refs_size;
c34d9c9f
JF
1980
1981static struct ref **
1982get_refs(char *id)
1983{
1984 struct ref **id_refs = NULL;
1985 size_t id_refs_size = 0;
1986 size_t i;
1987
1988 for (i = 0; i < refs_size; i++) {
1989 struct ref **tmp;
1990
1991 if (strcmp(id, refs[i].id))
1992 continue;
1993
1994 tmp = realloc(id_refs, (id_refs_size + 1) * sizeof(*id_refs));
1995 if (!tmp) {
1996 if (id_refs)
1997 free(id_refs);
1998 return NULL;
1999 }
2000
2001 id_refs = tmp;
3af8774e 2002 if (id_refs_size > 0)
c34d9c9f 2003 id_refs[id_refs_size - 1]->next = 1;
3af8774e
JF
2004 id_refs[id_refs_size] = &refs[i];
2005
2006 /* XXX: The properties of the commit chains ensures that we can
2007 * safely modify the shared ref. The repo references will
2008 * always be similar for the same id. */
2009 id_refs[id_refs_size]->next = 0;
2010 id_refs_size++;
c34d9c9f
JF
2011 }
2012
2013 return id_refs;
2014}
2015
2016static int
2017load_refs(void)
2018{
4685845e
TH
2019 const char *cmd_env = getenv("TIG_LS_REMOTE");
2020 const char *cmd = cmd_env && *cmd_env ? cmd_env : TIG_LS_REMOTE;
c34d9c9f
JF
2021 FILE *pipe = popen(cmd, "r");
2022 char buffer[BUFSIZ];
2023 char *line;
2024
2025 if (!pipe)
2026 return ERR;
2027
2028 while ((line = fgets(buffer, sizeof(buffer), pipe))) {
2029 char *name = strchr(line, '\t');
2030 struct ref *ref;
2031 int namelen;
2032 bool tag = FALSE;
6734f6b9 2033 bool tag_commit = FALSE;
c34d9c9f
JF
2034
2035 if (!name)
2036 continue;
2037
2038 *name++ = 0;
2039 namelen = strlen(name) - 1;
6706b2ba
JF
2040
2041 /* Commits referenced by tags has "^{}" appended. */
c34d9c9f
JF
2042 if (name[namelen - 1] == '}') {
2043 while (namelen > 0 && name[namelen] != '^')
2044 namelen--;
6734f6b9
JF
2045 if (namelen > 0)
2046 tag_commit = TRUE;
c34d9c9f
JF
2047 }
2048 name[namelen] = 0;
2049
2050 if (!strncmp(name, "refs/tags/", STRING_SIZE("refs/tags/"))) {
6734f6b9
JF
2051 if (!tag_commit)
2052 continue;
c34d9c9f
JF
2053 name += STRING_SIZE("refs/tags/");
2054 tag = TRUE;
3af8774e
JF
2055
2056 } else if (!strncmp(name, "refs/heads/", STRING_SIZE("refs/heads/"))) {
2057 name += STRING_SIZE("refs/heads/");
2058
2059 } else if (!strcmp(name, "HEAD")) {
2060 continue;
c34d9c9f
JF
2061 }
2062
2063 refs = realloc(refs, sizeof(*refs) * (refs_size + 1));
2064 if (!refs)
2065 return ERR;
2066
2067 ref = &refs[refs_size++];
2068 ref->tag = tag;
2069 ref->name = strdup(name);
2070 if (!ref->name)
2071 return ERR;
2072
2073 string_copy(ref->id, line);
2074 }
2075
2076 if (ferror(pipe))
2077 return ERR;
2078
2079 pclose(pipe);
2080
1b4b0bd9
JF
2081 if (refs_size == 0)
2082 die("Not a git repository");
2083
c34d9c9f
JF
2084 return OK;
2085}
2086
6b161b31
JF
2087/*
2088 * Main
2089 */
2090
b5c9e67f
TH
2091#if __GNUC__ >= 3
2092#define __NORETURN __attribute__((__noreturn__))
2093#else
2094#define __NORETURN
2095#endif
2096
2097static void __NORETURN
6b161b31
JF
2098quit(int sig)
2099{
8855ada4
JF
2100 /* XXX: Restore tty modes and let the OS cleanup the rest! */
2101 if (cursed)
2102 endwin();
6b161b31
JF
2103 exit(0);
2104}
2105
c6704a4e
JF
2106static void __NORETURN
2107die(const char *err, ...)
6b161b31
JF
2108{
2109 va_list args;
2110
2111 endwin();
2112
2113 va_start(args, err);
2114 fputs("tig: ", stderr);
2115 vfprintf(stderr, err, args);
2116 fputs("\n", stderr);
2117 va_end(args);
2118
2119 exit(1);
2120}
2121
2122int
2123main(int argc, char *argv[])
2124{
1ba2ae4b 2125 struct view *view;
6b161b31 2126 enum request request;
1ba2ae4b 2127 size_t i;
6b161b31
JF
2128
2129 signal(SIGINT, quit);
2130
8855ada4 2131 if (!parse_options(argc, argv))
6b161b31
JF
2132 return 0;
2133
c34d9c9f
JF
2134 if (load_refs() == ERR)
2135 die("Failed to load refs.");
2136
1ba2ae4b
JF
2137 for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++)
2138 view->cmd_env = getenv(view->cmd_env);
2139
6b161b31
JF
2140 request = opt_request;
2141
2142 init_display();
b801d8b2
JF
2143
2144 while (view_driver(display[current_view], request)) {
6b161b31 2145 int key;
b801d8b2
JF
2146 int i;
2147
6b161b31
JF
2148 foreach_view (view, i)
2149 update_view(view);
b801d8b2
JF
2150
2151 /* Refresh, accept single keystroke of input */
6b161b31
JF
2152 key = wgetch(status_win);
2153 request = get_request(key);
03a93dbb 2154
6706b2ba 2155 /* Some low-level request handling. This keeps access to
fac7db6c
JF
2156 * status_win restricted. */
2157 switch (request) {
2158 case REQ_PROMPT:
6908bdbd
JF
2159 report(":");
2160 /* Temporarily switch to line-oriented and echoed
2161 * input. */
03a93dbb
JF
2162 nocbreak();
2163 echo();
49f2b43f
JF
2164
2165 if (wgetnstr(status_win, opt_cmd + 4, sizeof(opt_cmd) - 4) == OK) {
2166 memcpy(opt_cmd, "git ", 4);
2167 opt_request = REQ_VIEW_PAGER;
2168 } else {
2169 request = ERR;
2170 }
2171
6908bdbd
JF
2172 noecho();
2173 cbreak();
fac7db6c
JF
2174 break;
2175
2176 case REQ_SCREEN_RESIZE:
2177 {
2178 int height, width;
2179
2180 getmaxyx(stdscr, height, width);
2181
2182 /* Resize the status view and let the view driver take
2183 * care of resizing the displayed views. */
2184 wresize(status_win, 1, width);
2185 mvwin(status_win, height - 1, 0);
2186 wrefresh(status_win);
2187 break;
2188 }
2189 default:
2190 break;
03a93dbb 2191 }
b801d8b2
JF
2192 }
2193
2194 quit(0);
2195
2196 return 0;
2197}
2198
2199/**
0721c53a 2200 * [[refspec]]
3a11b38f
JF
2201 * Revision specification
2202 * ----------------------
0721c53a 2203 * This section describes various ways to specify what revisions to display
3a11b38f
JF
2204 * or otherwise limit the view to. tig(1) does not itself parse the described
2205 * revision options so refer to the relevant git man pages for futher
2206 * information. Relevant man pages besides git-log(1) are git-diff(1) and
2207 * git-rev-list(1).
468876c9 2208 *
3a11b38f
JF
2209 * You can tune the interaction with git by making use of the options
2210 * explained in this section. For example, by configuring the environment
2211 * variables described in the <<view-commands, "View commands">> section.
2212 *
2213 * Limit by path name
2214 * ~~~~~~~~~~~~~~~~~~
ab46037d
JF
2215 * If you are interested only in those revisions that made changes to a
2216 * specific file (or even several files) list the files like this:
2217 *
c760e6ba 2218 * $ tig log Makefile README
ab46037d
JF
2219 *
2220 * To avoid ambiguity with repository references such as tag name, be sure
2221 * to separate file names from other git options using "\--". So if you
2222 * have a file named 'master' it will clash with the reference named
2223 * 'master', and thus you will have to use:
2224 *
1b4b0bd9 2225 * $ tig log -- master
ab46037d
JF
2226 *
2227 * NOTE: For the main view, avoiding ambiguity will in some cases require
2228 * you to specify two "\--" options. The first will make tig(1) stop
2229 * option processing and the latter will be passed to git log.
2230 *
468876c9
JF
2231 * Limit by date or number
2232 * ~~~~~~~~~~~~~~~~~~~~~~~
2233 * To speed up interaction with git, you can limit the amount of commits
2234 * to show both for the log and main view. Either limit by date using
2235 * e.g. `--since=1.month` or limit by the number of commits using `-n400`.
2236 *
3a11b38f
JF
2237 * If you are only interested in changed that happened between two dates
2238 * you can use:
2239 *
c760e6ba 2240 * $ tig -- --after="May 5th" --before="2006-05-16 15:44"
468876c9 2241 *
c760e6ba
JF
2242 * NOTE: If you want to avoid having to quote dates containing spaces you
2243 * can use "." instead, e.g. `--after=May.5th`.
3a11b38f
JF
2244 *
2245 * Limiting by commit ranges
2246 * ~~~~~~~~~~~~~~~~~~~~~~~~~
468876c9
JF
2247 * Alternatively, commits can be limited to a specific range, such as
2248 * "all commits between 'tag-1.0' and 'tag-2.0'". For example:
2249 *
2250 * $ tig log tag-1.0..tag-2.0
2251 *
2252 * This way of commit limiting makes it trivial to only browse the commits
2253 * which haven't been pushed to a remote branch. Assuming 'origin' is your
2254 * upstream remote branch, using:
2255 *
2256 * $ tig log origin..HEAD
2257 *
2258 * will list what will be pushed to the remote branch. Optionally, the ending
2259 * 'HEAD' can be left out since it is implied.
2260 *
2261 * Limiting by reachability
2262 * ~~~~~~~~~~~~~~~~~~~~~~~~
2263 * Git interprets the range specifier "tag-1.0..tag-2.0" as
2264 * "all commits reachable from 'tag-2.0' but not from 'tag-1.0'".
3a11b38f
JF
2265 * Where reachability refers to what commits are ancestors (or part of the
2266 * history) of the branch or tagged revision in question.
2267 *
468876c9
JF
2268 * If you prefer to specify which commit to preview in this way use the
2269 * following:
2270 *
2271 * $ tig log tag-2.0 ^tag-1.0
2272 *
57bdf034
JF
2273 * You can think of '^' as a negation operator. Using this alternate syntax,
2274 * it is possible to further prune commits by specifying multiple branch
2275 * cut offs.
468876c9 2276 *
3a11b38f
JF
2277 * Combining revisions specification
2278 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2279 * Revisions options can to some degree be combined, which makes it possible
2280 * to say "show at most 20 commits from within the last month that changed
2281 * files under the Documentation/ directory."
2282 *
2283 * $ tig -- --since=1.month -n20 -- Documentation/
2284 *
2285 * Examining all repository references
2286 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2287 * In some cases, it can be useful to query changes across all references
2288 * in a repository. An example is to ask "did any line of development in
2289 * this repository change a particular file within the last week". This
2290 * can be accomplished using:
2291 *
2292 * $ tig -- --all --since=1.week -- Makefile
2293 *
6706b2ba
JF
2294 * BUGS
2295 * ----
2296 * Known bugs and problems:
2297 *
2298 * - If the screen width is very small the main view can draw
468876c9
JF
2299 * outside the current view causing bad wrapping. Same goes
2300 * for title and status windows.
6706b2ba 2301 *
4c6fabc2
JF
2302 * TODO
2303 * ----
2304 * Features that should be explored.
2305 *
fac7db6c 2306 * - Searching.
4c6fabc2
JF
2307 *
2308 * - Locale support.
2309 *
b801d8b2
JF
2310 * COPYRIGHT
2311 * ---------
4a2909a7 2312 * Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2006
b801d8b2
JF
2313 *
2314 * This program is free software; you can redistribute it and/or modify
2315 * it under the terms of the GNU General Public License as published by
2316 * the Free Software Foundation; either version 2 of the License, or
2317 * (at your option) any later version.
2318 *
2319 * SEE ALSO
2320 * --------
4c6fabc2 2321 * [verse]
b801d8b2
JF
2322 * link:http://www.kernel.org/pub/software/scm/git/docs/[git(7)],
2323 * link:http://www.kernel.org/pub/software/scm/cogito/docs/[cogito(7)]
4c6fabc2 2324 * gitk(1): git repository browser written using tcl/tk,
c6704a4e 2325 * qgit(1): git repository browser written using c++/Qt,
4c6fabc2 2326 * gitview(1): git repository browser written using python/gtk.
b801d8b2 2327 **/